Skip to content

Commit

Permalink
Merge pull request #16 from personalizedrefrigerator/pr/fix-cm6-searc…
Browse files Browse the repository at this point in the history
…h-bug

CodeMirror 6 editor: Fix searches can't cotain spaces, don't appear immediately after typing `@@`
  • Loading branch information
roman-r-m committed Mar 2, 2024
2 parents a6ef4ee + 80b4d08 commit 97ab5db
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/contentScript/codeMirror6Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ export default function codeMirror6Plugin(pluginContext: PluginContext, CodeMirr
const { EditorSelection } = require('@codemirror/state') as typeof CodeMirrorStateType;

const completeMarkdown = async (completionContext: CompletionContext): Promise<CompletionResult> => {
const prefix = completionContext.matchBefore(/[@][@]\w+/);
// Start completions on @@, match any characters that aren't in "()[]{};>,\n"
const prefix = completionContext.matchBefore(/[@][@][^()\[\]{};:>,\n]*/);
if (!prefix || (prefix.from === prefix.to && !completionContext.explicit)) {
return null;
}

const searchText = prefix.text.substring(2); // Remove @@s
const response = await pluginContext.postMessage({
command: 'getNotes',
prefix: prefix.text,
prefix: searchText,
});

const createApplyCompletionFn = (noteTitle: string, noteId: string) => {
Expand Down Expand Up @@ -63,7 +65,7 @@ export default function codeMirror6Plugin(pluginContext: PluginContext, CodeMirr
}

const addNewNoteCompletion = (todo: boolean) => {
const title = prefix.text.substring(2);
const title = searchText;
const description = todo ? 'New Task' : 'New Note';
completions.push({
label: description,
Expand All @@ -81,7 +83,7 @@ export default function codeMirror6Plugin(pluginContext: PluginContext, CodeMirr
});
};

if (response.allowNewNotes) {
if (response.allowNewNotes && searchText.length > 0) {
addNewNoteCompletion(true);
addNewNoteCompletion(false);
}
Expand Down

0 comments on commit 97ab5db

Please sign in to comment.