From 9a3eea6535aff610faa9db0a1fa311c4594ea88b Mon Sep 17 00:00:00 2001 From: William Chu Date: Sat, 17 Aug 2024 15:42:09 +1000 Subject: [PATCH] fix(autocomplete): do not insert extra quote when completing between two quotes --- src/components/LuceneQueryEditor.tsx | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/components/LuceneQueryEditor.tsx b/src/components/LuceneQueryEditor.tsx index 85cfd6d..99e3518 100644 --- a/src/components/LuceneQueryEditor.tsx +++ b/src/components/LuceneQueryEditor.tsx @@ -2,16 +2,15 @@ import React, { useRef, useCallback } from "react"; import { css } from "@emotion/css"; -import CodeMirror, { ReactCodeMirrorRef, keymap } from '@uiw/react-codemirror'; +import CodeMirror, { ReactCodeMirrorRef, keymap} from '@uiw/react-codemirror'; import {linter, Diagnostic, lintGutter} from "@codemirror/lint" -import {autocompletion, CompletionContext} from "@codemirror/autocomplete" +import {autocompletion, CompletionContext, CompletionResult} from "@codemirror/autocomplete" import { LuceneQuery } from "@/utils/lucene"; - export type LuceneQueryEditorProps = { placeholder?: string, value: string, - autocompleter: (word: string) => any, + autocompleter: (word: string) => CompletionResult, onChange: (query: string) => void onSubmit: (query: string) => void } @@ -39,10 +38,10 @@ export function LuceneQueryEditor(props: LuceneQueryEditorProps){ let suggestions; let word = context.matchBefore(/\S*/); if (!word){ return null } - suggestions = await autocompleter(word?.text); + suggestions = await autocompleter(word?.text); if (suggestions && suggestions.options.length > 0 ) { // Fixes autocompletion inserting an extra quote when the cursor is before a quote - const cursorIsBeforeQuote = context.state.doc.toString().slice(context.pos, context.pos + 1) === '"'; + const cursorIsBeforeQuote = /^\s*"/.test(context.state.doc.toString().slice(context.pos)); if (cursorIsBeforeQuote) { suggestions.options = suggestions.options.map(o => ({...o, apply: `${o.label.replace(/"$/g, '')}`})); }