Skip to content

Commit bfbd75d

Browse files
committed
Add field values autocompletion
1 parent 720abee commit bfbd75d

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/LogContext/components/LogContextQueryEditor.tsx

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,31 @@ class QueryAutocompleter {
1515
constructor(datasource: QuickwitDataSource){
1616
this.datasource = datasource
1717
}
18-
getFields() {
19-
return this.datasource.getTagKeys()
18+
async getFields() {
19+
return await this.datasource.getTagKeys()
2020
}
21-
async getSuggestions(word: string): Promise<Completion[]>{
22-
let suggestions: Completion[] = [];
2321

22+
async getFieldValues(fieldName: string) {
23+
return this.datasource.getTagValues({key:fieldName})
24+
}
2425

26+
async getSuggestions(word: string): Promise<{from: number, options: Completion[]}>{
27+
let suggestions: {from: number, options: Completion[]} = {from: 0, options: []};
28+
29+
const wordIsField = word.match(/([\w\.]+):(\S*)/)
30+
if (wordIsField?.length) {
31+
const [_match, fieldName, fieldValue] = wordIsField;
32+
const candidateValues = await this.getFieldValues(fieldName)
33+
suggestions.from = fieldName.length+1
34+
suggestions.options = (candidateValues.filter(v=>v.text.startsWith(fieldValue))
35+
.map(v=>({ type:'text', label:v.text, })))
36+
} else {
2537
const candidateFields = await this.getFields()
26-
suggestions = (candidateFields
27-
.filter(f=>f.text.startsWith(word))
28-
.map(f=>({
29-
type: 'text',
30-
section: "Fields",
31-
label:f.text,
32-
detail:`${f.value}`,
33-
})));
34-
35-
return suggestions;
38+
suggestions.from = 0
39+
suggestions.options = (candidateFields.filter(f=>f.text.startsWith(word))
40+
.map(f=>({ type: 'text', label:f.text, detail:`${f.value}`, })))
41+
}
42+
return suggestions
3643
}
3744
}
3845

@@ -61,19 +68,15 @@ export function LogContextQueryEditor(){
6168
const autocomplete = autocompletion({
6269
override: [async (context: CompletionContext)=>{
6370
let word = context.matchBefore(/\S*/);
64-
6571
if (!word){ return null }
6672
const suggestions = await autocompleter.getSuggestions(word?.text);
67-
6873
return {
69-
from: word.from,
70-
options: suggestions
74+
from: word.from + suggestions.from,
75+
options: suggestions.options
7176
}
7277
}]
7378
})
7479

75-
76-
7780
return (<CodeMirror
7881
ref={editorRef}
7982
className={css`height:100%`} // XXX : need to set height for both wrapper elements

0 commit comments

Comments
 (0)