Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improved autocompletion #135

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build_and_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
npm install
npm run build
mage -v
docker-compose up --build --force-recreate
docker compose up --build --force-recreate
19 changes: 12 additions & 7 deletions src/components/LuceneQueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -39,8 +38,14 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to only check that last character?
I don't understand the slice with context.pos. Can you explain exactly what we want to check here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to only check that last character?
I've modified it to check for all characters following the cursor for a double quote instead of just the immediate character.

"quickwit-indexing/src/actors/indexing_pipeline.rs"

Some examples of autocompletion where ↑ is the current cursor position.:

  1. Example of completing immediately behind a double quote: events.event_attributes.code.filepath:"quickwit-indexing/src/actors/↑"
  • The returned completion would omit the final quote "quickwit-indexing/src/actors/indexing_pipeline.rs.
  • Applying the completion would give us: events.event_attributes.code.filepath:"quickwit-indexing/src/actors/indexing_pipeline.rs"
  • The current behaviour would instead give us: events.event_attributes.code.filepath:"quickwit-indexing/src/actors/indexing_pipeline.rs""
  1. Example of completing without quotes: events.event_attributes.code.filepath:↑
  • The returned completion would be surrounded by quotes "quickwit-indexing/src/actors/indexing_pipeline.rs".
  • Applying the completion would give us: events.event_attributes.code.filepath:"quickwit-indexing/src/actors/indexing_pipeline.rs"
  1. Example of completing with whitespace before double quotes: events.event_attributes.code.filepath:"quickwit-indexing/src/actors/↑ "
  • The returned completion would omit the final quote "quickwit-indexing/src/actors/indexing_pipeline.rs.
  • Applying the completion would give us: events.event_attributes.code.filepath:"quickwit-indexing/src/actors/indexing_pipeline.rs " which isn't ideal but is better than an extra double quote

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, '')}`}));
}

return {
from: word.from + suggestions.from,
options: suggestions.options
Expand All @@ -55,11 +60,11 @@ export function LuceneQueryEditor(props: LuceneQueryEditorProps){
activateOnTyping: false,
})

return (<CodeMirror
return (<CodeMirror
ref={editorRef}
className={css`height:100%`} // XXX : need to set height for both wrapper elements
height="100%"
theme={'dark'}
theme={'dark'}
placeholder={props.placeholder}
value={props.value}
onChange={props.onChange}
Expand Down
17 changes: 9 additions & 8 deletions src/datasource/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import {
TimeRange,
} from '@grafana/data';
import { BucketAggregation, DataLinkConfig, ElasticsearchQuery, TermsQuery, FieldCapabilitiesResponse } from '@/types';
import {
DataSourceWithBackend,
getTemplateSrv,
import {
DataSourceWithBackend,
getTemplateSrv,
TemplateSrv } from '@grafana/runtime';
import { QuickwitOptions } from 'quickwit';
import { getDataQuery } from 'QueryBuilder/elastic';
Expand All @@ -36,7 +36,7 @@ import { getQueryResponseProcessor } from 'datasource/processResponse';
import { SECOND } from 'utils/time';
import { GConstructor } from 'utils/mixins';
import { LuceneQuery } from '@/utils/lucene';
import { uidMaker } from "@/utils/uid"
import { uidMaker } from "@/utils/uid"
import { DefaultsConfigOverrides } from 'store/defaults/conf';

export type BaseQuickwitDataSourceConstructor = GConstructor<BaseQuickwitDataSource>
Expand Down Expand Up @@ -199,7 +199,7 @@ export class BaseQuickwitDataSource
.map(field_capability => {
return {
text: field_capability.field_name,
value: fieldTypeMap[field_capability.type],
value: fieldTypeMap[field_capability.type],
}
});
const uniquefieldCapabilities = fieldCapabilities.filter((field_capability, index, self) =>
Expand All @@ -223,9 +223,10 @@ export class BaseQuickwitDataSource
/**
* Get tag values for adhoc filters
*/
getTagValues(options: any) {
const terms = this.getTerms({ field: options.key }, options.timeRange)
return lastValueFrom(terms, {defaultValue:[]});
getTagValues(options: { key: string, fieldValue: string, timeRange: TimeRange }) {
const query = `${options.key}:${options.fieldValue}*`
const terms = this.getTerms({ field: options.key, query }, options.timeRange)
return lastValueFrom(terms, { defaultValue: [] });
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/datasource/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export function useDatasourceFields(datasource: BaseQuickwitDataSource, range: T

const wordIsField = word.match(/([^:\s]+):"?([^"\s]*)"?/);
if (wordIsField?.length) {
const [_match, fieldName, _fieldValue] = wordIsField;
const candidateValues = await datasource.getTagValues({ key: fieldName, timeRange: range });
const [_match, fieldName, fieldValue] = wordIsField;
const candidateValues = await datasource.getTagValues({ key: fieldName, timeRange: range, fieldValue});
suggestions.from = fieldName.length + 1; // Replace only the value part
suggestions.options = candidateValues.map(v => ({
type: 'text',
Expand Down