feat: implement base autocomplete implementation for codemirror along with preRequest autocompletion
This commit is contained in:
33
helpers/editor/completion/index.ts
Normal file
33
helpers/editor/completion/index.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
export type CompletionEntry = {
|
||||
text: string
|
||||
meta: string
|
||||
score: number
|
||||
}
|
||||
|
||||
export type CompleterResult = {
|
||||
/**
|
||||
* List of completions to display
|
||||
*/
|
||||
completions: CompletionEntry[]
|
||||
/**
|
||||
* Start of the completion position
|
||||
* (on completion the start..end region is replaced)
|
||||
*/
|
||||
start: { line: number; ch: number }
|
||||
/**
|
||||
* End of the completion position
|
||||
* (on completion the start..end region is replaced)
|
||||
*/
|
||||
end: { line: number; ch: number }
|
||||
}
|
||||
|
||||
export type Completer = (
|
||||
/**
|
||||
* The contents of the editor
|
||||
*/
|
||||
text: string,
|
||||
/**
|
||||
* Position where the completer is fired
|
||||
*/
|
||||
completePos: { line: number; ch: number }
|
||||
) => Promise<CompleterResult>
|
||||
30
helpers/editor/completion/preRequest.ts
Normal file
30
helpers/editor/completion/preRequest.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { convertIndexToLineCh } from "../utils"
|
||||
import { Completer, CompletionEntry } from "."
|
||||
import { getPreRequestScriptCompletions } from "~/helpers/tern"
|
||||
|
||||
const completer: Completer = async (text, completePos) => {
|
||||
const results = await getPreRequestScriptCompletions(
|
||||
text,
|
||||
completePos.line,
|
||||
completePos.ch
|
||||
)
|
||||
|
||||
const start = convertIndexToLineCh(text, results.start)
|
||||
const end = convertIndexToLineCh(text, results.end)
|
||||
|
||||
const completions = results.completions.map((completion: any, i: number) => {
|
||||
return <CompletionEntry>{
|
||||
text: completion.name,
|
||||
meta: completion.isKeyword ? "keyword" : completion.type,
|
||||
score: results.completions.length - i,
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
start,
|
||||
end,
|
||||
completions,
|
||||
}
|
||||
}
|
||||
|
||||
export default completer
|
||||
Reference in New Issue
Block a user