From b016d3fd9d8220969db67fcf3165411e90e59e46 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Wed, 8 Sep 2021 05:36:46 +0530 Subject: [PATCH] refactor: pass current token position to auto completers on codemirror --- helpers/editor/codemirror.ts | 11 ++++++++--- helpers/editor/completion/index.ts | 8 ++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/helpers/editor/codemirror.ts b/helpers/editor/codemirror.ts index 852d46598..3e9fe5d33 100644 --- a/helpers/editor/codemirror.ts +++ b/helpers/editor/codemirror.ts @@ -27,6 +27,7 @@ import "codemirror/addon/dialog/dialog" import { watch, onMounted, ref, Ref, useContext } from "@nuxtjs/composition-api" import { LinterDefinition } from "./linting/linter" import { Completer } from "./completion" +import { convertIndexToLineCh } from "./utils" type CodeMirrorOptions = { extendedEditorConfig: Omit @@ -88,10 +89,14 @@ export function useCodemirror( const pos = editor.getCursor() const text = editor.getValue() - const result = await options.completer!(text, pos) + const token = editor.getTokenAt(pos) - console.log("complete!") - console.log(result) + const result = await options.completer!(text, pos, { + start: convertIndexToLineCh(text, token.start), + end: convertIndexToLineCh(text, token.end), + }) + + if (!result) return null return { from: result.start, diff --git a/helpers/editor/completion/index.ts b/helpers/editor/completion/index.ts index f5f927b06..d23414446 100644 --- a/helpers/editor/completion/index.ts +++ b/helpers/editor/completion/index.ts @@ -29,5 +29,9 @@ export type Completer = ( /** * Position where the completer is fired */ - completePos: { line: number; ch: number } -) => Promise + completePos: { line: number; ch: number }, + completeTokenLocation: { + start: { line: number; ch: number } + end: { line: number; ch: number } + } +) => Promise