@@ -2,16 +2,15 @@ import React, { useRef, useCallback } from "react";
2
2
import { css } from "@emotion/css" ;
3
3
4
4
5
- import CodeMirror , { ReactCodeMirrorRef , keymap } from '@uiw/react-codemirror' ;
5
+ import CodeMirror , { ReactCodeMirrorRef , keymap } from '@uiw/react-codemirror' ;
6
6
import { linter , Diagnostic , lintGutter } from "@codemirror/lint"
7
- import { autocompletion , CompletionContext } from "@codemirror/autocomplete"
7
+ import { autocompletion , CompletionContext , CompletionResult } from "@codemirror/autocomplete"
8
8
import { LuceneQuery } from "@/utils/lucene" ;
9
9
10
-
11
10
export type LuceneQueryEditorProps = {
12
11
placeholder ?: string ,
13
12
value : string ,
14
- autocompleter : ( word : string ) => any ,
13
+ autocompleter : ( word : string ) => CompletionResult ,
15
14
onChange : ( query : string ) => void
16
15
onSubmit : ( query : string ) => void
17
16
}
@@ -41,6 +40,12 @@ export function LuceneQueryEditor(props: LuceneQueryEditorProps){
41
40
if ( ! word ) { return null }
42
41
suggestions = await autocompleter ( word ?. text ) ;
43
42
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
+
44
49
return {
45
50
from : word . from + suggestions . from ,
46
51
options : suggestions . options
@@ -55,11 +60,11 @@ export function LuceneQueryEditor(props: LuceneQueryEditorProps){
55
60
activateOnTyping : false ,
56
61
} )
57
62
58
- return ( < CodeMirror
63
+ return ( < CodeMirror
59
64
ref = { editorRef }
60
65
className = { css `height : 100% ` } // XXX : need to set height for both wrapper elements
61
66
height = "100%"
62
- theme = { 'dark' }
67
+ theme = { 'dark' }
63
68
placeholder = { props . placeholder }
64
69
value = { props . value }
65
70
onChange = { props . onChange }
0 commit comments