-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: path highlight and autocompletion (#88)
feat: path highlight and autocompletion - [x] path highlight - [x] path keywords autocompletion obsolete now. Refs: #57 --------- Signed-off-by: seven <zilisheng1996@gmail.com>
- Loading branch information
Showing
7 changed files
with
234 additions
and
194 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as monaco from 'monaco-editor'; | ||
import { keywords } from './lexerRules.ts'; | ||
|
||
const provideMethodCompletionItems = (lineContent: string) => { | ||
const methods = new Map<RegExp, string>([ | ||
[/^ge?t?$/gi, 'GET '], | ||
[/^put?$/gi, 'PUT '], | ||
[/^pos?t?$/gi, 'POST '], | ||
[/^de?l?e?t?e?$/gi, 'DELETE '], | ||
]); | ||
const matchedMethodKey = Array.from(methods.keys()).find(regex => regex.test(lineContent)); | ||
console.log('matchedMethodKey', { matchedMethodKey, lineContent }); | ||
if (!matchedMethodKey) { | ||
return null; | ||
} | ||
|
||
const method = methods.get(matchedMethodKey); | ||
|
||
return { | ||
suggestions: [ | ||
{ | ||
label: method, | ||
kind: monaco.languages.CompletionItemKind.Constant, | ||
insertText: method, | ||
}, | ||
], | ||
}; | ||
}; | ||
|
||
const provideKeywordCompletionItems = (lineContent: string) => { | ||
const word = lineContent.split(/[ /]+/).pop() || ''; | ||
const suggestions = keywords | ||
.filter(keyword => { | ||
return keyword.startsWith(word); | ||
}) | ||
.map(keyword => ({ | ||
label: keyword, | ||
kind: monaco.languages.CompletionItemKind.Keyword, | ||
insertText: keyword, | ||
})); | ||
return { suggestions }; | ||
}; | ||
|
||
export const searchCompletionProvider = ( | ||
model: monaco.editor.ITextModel, | ||
position: monaco.Position, | ||
) => { | ||
const textUntilPosition = model.getValueInRange({ | ||
startLineNumber: position.lineNumber, | ||
startColumn: 1, | ||
endLineNumber: position.lineNumber, | ||
endColumn: position.column, | ||
}); | ||
|
||
const methodCompletions = provideMethodCompletionItems(textUntilPosition); | ||
if (methodCompletions) { | ||
return methodCompletions; | ||
} | ||
const keywordCompletions = provideKeywordCompletionItems(textUntilPosition); | ||
|
||
if (keywordCompletions) { | ||
return keywordCompletions; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.