-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathLuceneQueryEditor.tsx
More file actions
93 lines (82 loc) · 2.53 KB
/
LuceneQueryEditor.tsx
File metadata and controls
93 lines (82 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import React, { useRef, useCallback } from "react";
import { css } from "@emotion/css";
import CodeMirror, { Prec, ReactCodeMirrorRef } from '@uiw/react-codemirror';
import { keymap } from '@codemirror/view';
import { linter, Diagnostic, lintGutter } from "@codemirror/lint"
import { autocompletion, CompletionContext, startCompletion } from "@codemirror/autocomplete"
import { LuceneQuery } from "@/utils/lucene";
export type LuceneQueryEditorProps = {
placeholder?: string,
value: string,
autocompleter: (word: string) => any,
onChange: (query: string) => void
onSubmit: (query: string) => void
}
export function LuceneQueryEditor(props: LuceneQueryEditorProps){
const editorRef = useRef<ReactCodeMirrorRef|null>(null)
const queryLinter = linter( view => {
let diagnostics: Diagnostic[] = [];
const query = LuceneQuery.parse(view.state.doc.toString())
const error = query.parseError
if (error) {
diagnostics.push({
severity: "error",
message: error.message,
from: view.state.doc.line(error.location.start.line).from + error.location.start.column -1,
to: view.state.doc.line(error.location.end.line).from + error.location.end.column -1,
}) ;
}
return diagnostics
})
const {autocompleter} = props;
const datasourceCompletions = useCallback(async (context: CompletionContext)=>{
let suggestions;
let word = context.matchBefore(/\S*/);
if (!word){ return null }
suggestions = await autocompleter(word?.text);
if (suggestions && suggestions.options.length > 0 ) {
return {
from: word.from + suggestions.from,
options: suggestions.options
}
}
return null
}, [autocompleter])
const autocomplete = autocompletion({
override: [datasourceCompletions],
activateOnTyping: false,
closeOnBlur: true,
maxRenderedOptions: 30,
})
const myKeymap = Prec.highest(keymap.of([
{
key: 'Shift-Enter',
run: (view) => {
props.onSubmit(view.state.doc.toString());
return true;
}
},
{
key: 'Ctrl-Enter',
run: (view) => {
return startCompletion(view);
}
},
]));
return (<CodeMirror
ref={editorRef}
className={css`height:100%`} // XXX : need to set height for both wrapper elements
height="100%"
theme={'dark'}
placeholder={props.placeholder}
value={props.value}
onChange={props.onChange}
indentWithTab={false}
extensions={[
queryLinter,
lintGutter(),
autocomplete,
myKeymap
]}
/>);
}