fix(editor): preserve contextual completion prefixes (#8323)

* fix(editor): preserve contextual completion prefixes

* fix(editor): preserve single-winner completion metadata in mergeResults
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-06 20:39:23 +08:00
1 джерело d1b03e837a
коміт 630992ca8f
2 files changed
+100 -6

No files matched your search

+91
Переглянути файл
@@ -22,6 +22,7 @@ import {
explicitCompletion,
makeCompletion,
matchRanges,
mergeResults,
completionValidFor,
pruneItems,
positionCompletionInfo,
@@ -34,6 +35,7 @@ import {
currentCompletions,
snippet,
startCompletion,
type CompletionResult,
type CompletionSource,
} from "@codemirror/autocomplete";
import { useProject } from "@/platform/project";
@@ -856,6 +858,95 @@ describe("applying completions", () => {
});
}
it.each(['return render(request, "shop/pro|", context)', '{% include "shop/pro|" %}'])(
"keeps the full path range ahead of word templates in %s",
async (doc) => {
const { view, pos } = setup(doc);
const word: CompletionSource = () => ({
from: pos - 3,
options: [{ label: "todo" }, { label: "date" }],
validFor: /^\w*$/,
});
const path: CompletionSource = () => ({
from: pos - "shop/pro".length,
options: [{ label: "shop/product_list.html", type: "file" }],
validFor: /^[^"'\n]*$/,
});
view.dispatch({
effects: StateEffect.appendConfig.of([
completionExtension(),
EditorState.languageData.of(() => [{ openideaCompletion: word }, { openideaCompletion: path }]),
]),
});
startCompletion(view);
await vi.waitFor(() =>
expect(currentCompletions(view.state).map((option) => option.label)).toEqual(["shop/product_list.html"]),
);
await new Promise((resolve) => setTimeout(resolve, 100));
expect(acceptCompletion(view)).toBe(true);
expect(view.state.doc.toString()).toBe(doc.replace("shop/pro|", "shop/product_list.html"));
},
);
it("preserves filter: false and extra metadata when a single longest-prefix source wins", () => {
const shorter: CompletionResult = {
from: 8,
options: [{ label: "todo" }],
validFor: /^\w*$/,
};
const validFor = () => true;
const getMatch = () => [0, 2];
const update = () => null;
const longest: CompletionResult = {
from: 5,
to: 10,
options: [{ label: "^4.19.0" }],
filter: false,
validFor,
getMatch,
update,
commitCharacters: ["."],
};
const merged = mergeResults([shorter, longest]);
expect(merged).toBe(longest);
expect(merged?.filter).toBe(false);
expect(merged?.validFor).toBe(validFor);
expect(merged?.getMatch).toBe(getMatch);
expect(merged?.update).toBe(update);
expect(merged?.commitCharacters).toEqual(["."]);
});
it("merges equal-prefix winners in registration order and drops shorter prefixes", () => {
const first: CompletionResult = {
from: 4,
to: 9,
options: [{ label: "first-a" }, { label: "first-b" }],
validFor: /^\w*$/,
};
const shorter: CompletionResult = {
from: 7,
options: [{ label: "shorter" }],
validFor: /^\w*$/,
};
const second: CompletionResult = {
from: 4,
to: 10,
options: [{ label: "second-a" }],
validFor: /^\w*$/,
};
const merged = mergeResults([first, shorter, second]);
expect(merged).toEqual({
from: 4,
to: 9,
options: [{ label: "first-a" }, { label: "first-b" }, { label: "second-a" }],
validFor: /^\w*$/,
});
const differentValidFor = mergeResults([first, { ...second, validFor: /^[\w-]*$/ }]);
expect(differentValidFor?.options.map((o) => o.label)).toEqual(["first-a", "first-b", "second-a"]);
expect(differentValidFor?.validFor).toBeUndefined();
});
it("applies additional edits (imports) in the same change", () => {
const { view, req } = setup("\nfoo.ba|");
const c = makeCompletion(
+9 -6
Переглянути файл
@@ -805,8 +805,8 @@ export function makeCompletion(item: CompletionItem, req: RequestContext): LspCo
/**
* Sources other features contribute via the `openideaCompletion` language-data
* key (live templates, postfix, SQL). Results starting where the first one
* does are merged into one list. Options may carry `icon` (an icon name, `null`
* key (live templates, postfix, SQL). Results matching the longest typed
* prefix are merged into one list. Options may carry `icon` (an icon name, `null`
* for none) and `typeText` (the right-aligned type column), like LSP items.
*/
const contributedSource: CompletionSource = async (ctx) => {
@@ -819,13 +819,16 @@ const contributedSource: CompletionSource = async (ctx) => {
export function mergeResults(results: readonly CompletionResult[]): CompletionResult | null {
const [first] = results;
if (!first || results.length === 1) return first ?? null;
const same = results.filter((r) => r.from === first.from);
// A contextual prefix such as a template path includes the word a generic source matches.
const longest = results.reduce((best, r) => (r.from < best.from ? r : best), first);
const same = results.filter((r) => r.from === longest.from);
if (same.length === 1) return longest;
const validFor = same.every(
(r) => r.validFor instanceof RegExp && r.validFor.source === (first.validFor as RegExp | undefined)?.source,
(r) => r.validFor instanceof RegExp && r.validFor.source === (longest.validFor as RegExp | undefined)?.source,
)
? first.validFor
? longest.validFor
: undefined;
return { from: first.from, to: first.to, options: same.flatMap((r) => r.options), validFor };
return { from: longest.from, to: longest.to, options: same.flatMap((r) => r.options), validFor };
}
/** Rendering extras of contributed (non-LSP) options. */