Improved autocompletion!

This commit is contained in:
Gregory Schier
2023-03-02 11:14:51 -08:00
parent 59967374c5
commit 957739ba5e
8 changed files with 89 additions and 64 deletions

View File

@@ -0,0 +1,19 @@
import type { CompletionContext } from '@codemirror/autocomplete';
const options = [
{ label: 'http://', type: 'constant' },
{ label: 'https://', type: 'constant' },
];
const MIN_MATCH = 1;
export function completions(context: CompletionContext) {
const toMatch = context.matchBefore(/^[\w:/]*/);
if (toMatch === null) return null;
const matchedMinimumLength = toMatch.to - toMatch.from >= MIN_MATCH;
if (!matchedMinimumLength && !context.explicit) return null;
const optionsWithoutExactMatches = options.filter((o) => o.label !== toMatch.text);
return { from: toMatch.from, options: optionsWithoutExactMatches };
}

View File

@@ -1,5 +1,6 @@
import { completeFromList } from '@codemirror/autocomplete';
import { LanguageSupport, LRLanguage } from '@codemirror/language';
import { completions } from './completion';
import { parser } from './url';
const urlLanguage = LRLanguage.define({
@@ -7,13 +8,8 @@ const urlLanguage = LRLanguage.define({
languageData: {},
});
const exampleCompletion = urlLanguage.data.of({
autocomplete: completeFromList([
{ label: 'http://', type: 'constant' },
{ label: 'https://', type: 'constant' },
]),
});
const completion = urlLanguage.data.of({ autocomplete: completions });
export function url() {
return new LanguageSupport(urlLanguage, [exampleCompletion]);
return new LanguageSupport(urlLanguage, [completion]);
}