Skip to content

Commit

Permalink
fix(autocomplete): do not insert extra quote when completing between …
Browse files Browse the repository at this point in the history
…two quotes
  • Loading branch information
uptickmetachu committed Aug 17, 2024
1 parent 49b0628 commit 6db8dc5
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 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 @@ -41,6 +40,12 @@ export function LuceneQueryEditor(props: LuceneQueryEditorProps){
if (!word){ return null }
suggestions = await autocompleter(word?.text);
if (suggestions && suggestions.options.length > 0 ) {
// Fixes autocompletion inserting an extra quote when the cursor is before a quote
const cursorIsBeforeQuote = context.state.doc.toString().slice(context.pos, context.pos + 1) === '"';
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

0 comments on commit 6db8dc5

Please sign in to comment.