snippets: fix disambiguation of completions with identical prefixes#306608
snippets: fix disambiguation of completions with identical prefixes#306608xingsy97 wants to merge 1 commit intomicrosoft:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes snippet completion disambiguation when multiple snippet completions share the same prefix by comparing the underlying label string (CompletionItemLabel.label) instead of the label object reference, so duplicates can be detected and renamed.
Changes:
- Update duplicate-detection logic in
_disambiguateSnippets()to compareitem.label.labelstrings instead ofitem.labelobject identity.
Comments suppressed due to low confidence (1)
src/vs/workbench/contrib/snippets/browser/snippetCompletionProvider.ts:196
- Now that duplicate snippet prefixes are actually detected, the
if (to > i + 1)branch will run; however the subsequenti = to(next line) causes the outerforloop to skip the first non-duplicate entry after a run. This can miss disambiguation for a following duplicate group (e.g. labelsa,a,b,bwould never disambiguateb). Set the index so the next iteration starts atto(commonly by assigningi = to - 1before the loop increment), or refactor to awhileloop to avoid off-by-one issues.
for (; to < suggestions.length && item.label.label === suggestions[to].label.label; to++) {
suggestions[to].label.label = localize('snippetSuggest.longLabel', "{0}, {1}", suggestions[to].label.label, suggestions[to].snippet.name);
}
if (to > i + 1) {
suggestions[i].label.label = localize('snippetSuggest.longLabel', "{0}, {1}", suggestions[i].label.label, suggestions[i].snippet.name);
| for (; to < suggestions.length && item.label.label === suggestions[to].label.label; to++) { | ||
| suggestions[to].label.label = localize('snippetSuggest.longLabel', "{0}, {1}", suggestions[to].label.label, suggestions[to].snippet.name); |
There was a problem hiding this comment.
This change fixes duplicate-prefix detection, but there doesn't appear to be test coverage asserting that multiple snippets with the same prefix get disambiguated (e.g. label becomes ", "). Consider adding a unit test in the existing snippetsService test suite that registers two snippets with the same prefix and asserts the resulting completion labels are distinct.
Problem
SnippetCompletionProvider._disambiguateSnippets()sorts completions and then compares adjacent labels to detect duplicates. The comparison usesitem.label === suggestions[to].label, butlabelis aCompletionItemLabelobject ({ label: string, description: string }), not a string. Since eachSnippetCompletioncreates a fresh object in its constructor,===always returnsfalse(reference equality), and the disambiguation suffix is never appended.When multiple snippets share the same prefix (e.g. two 'for' snippets from different sources), they appear with identical labels in the completion list with no way to distinguish them.
Fix
Compare the
label.labelstring property instead of the label object reference.