feat: implement base autocomplete implementation for codemirror along with preRequest autocompletion

This commit is contained in:
Andrew Bastin
2021-09-07 22:12:38 +05:30
parent a5197ee544
commit d4d3d96bbb
4 changed files with 105 additions and 0 deletions

View 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>

View 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