Skip to content

Commit 6db8dc5

Browse files
committed
fix(autocomplete): do not insert extra quote when completing between two quotes
1 parent 49b0628 commit 6db8dc5

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/components/LuceneQueryEditor.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ import React, { useRef, useCallback } from "react";
22
import { css } from "@emotion/css";
33

44

5-
import CodeMirror, { ReactCodeMirrorRef, keymap } from '@uiw/react-codemirror';
5+
import CodeMirror, { ReactCodeMirrorRef, keymap} from '@uiw/react-codemirror';
66
import {linter, Diagnostic, lintGutter} from "@codemirror/lint"
7-
import {autocompletion, CompletionContext} from "@codemirror/autocomplete"
7+
import {autocompletion, CompletionContext, CompletionResult} from "@codemirror/autocomplete"
88
import { LuceneQuery } from "@/utils/lucene";
99

10-
1110
export type LuceneQueryEditorProps = {
1211
placeholder?: string,
1312
value: string,
14-
autocompleter: (word: string) => any,
13+
autocompleter: (word: string) => CompletionResult,
1514
onChange: (query: string) => void
1615
onSubmit: (query: string) => void
1716
}
@@ -41,6 +40,12 @@ export function LuceneQueryEditor(props: LuceneQueryEditorProps){
4140
if (!word){ return null }
4241
suggestions = await autocompleter(word?.text);
4342
if (suggestions && suggestions.options.length > 0 ) {
43+
// Fixes autocompletion inserting an extra quote when the cursor is before a quote
44+
const cursorIsBeforeQuote = context.state.doc.toString().slice(context.pos, context.pos + 1) === '"';
45+
if (cursorIsBeforeQuote) {
46+
suggestions.options = suggestions.options.map(o => ({...o, apply: `${o.label.replace(/"$/g, '')}`}));
47+
}
48+
4449
return {
4550
from: word.from + suggestions.from,
4651
options: suggestions.options
@@ -55,11 +60,11 @@ export function LuceneQueryEditor(props: LuceneQueryEditorProps){
5560
activateOnTyping: false,
5661
})
5762

58-
return (<CodeMirror
63+
return (<CodeMirror
5964
ref={editorRef}
6065
className={css`height:100%`} // XXX : need to set height for both wrapper elements
6166
height="100%"
62-
theme={'dark'}
67+
theme={'dark'}
6368
placeholder={props.placeholder}
6469
value={props.value}
6570
onChange={props.onChange}

0 commit comments

Comments
 (0)