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 18, 2024
1 parent 70a45a1 commit 9a3eea6
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 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 @@ -39,10 +38,10 @@ 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
const cursorIsBeforeQuote = context.state.doc.toString().slice(context.pos, context.pos + 1) === '"';
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, '')}`}));
}
Expand Down

0 comments on commit 9a3eea6

Please sign in to comment.