Skip to content

Commit 9843909

Browse files
CopilotTyriar
andcommitted
Implement git branch prioritization in compareCompletionsFn
- Revert changes to upstream/git.ts per feedback - Add git branch boosting logic to terminalCompletionModel.ts compareCompletionsFn - Boost main and master branches for git commands from terminal-suggest provider - Add comprehensive test suite for git branch priority sorting - Follows same pattern as existing LSP provider boost logic Co-authored-by: Tyriar <[email protected]>
1 parent 2e9cf53 commit 9843909

File tree

4 files changed

+86
-83
lines changed

4 files changed

+86
-83
lines changed

extensions/terminal-suggest/src/completions/upstream/git.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,11 @@ const postProcessBranches =
100100
name = name.slice(0, space);
101101
}
102102

103-
// Boost main and master branches to the top
104-
const isMainOrMaster = name === "main" || name === "master";
105-
const priority = isMainOrMaster ? 76 : 75;
106-
107103
return {
108104
name,
109105
description,
110106
icon: "fig://icon?type=git",
111-
priority,
107+
priority: 75,
112108
};
113109
})
114110
.filter((suggestion) => {

extensions/terminal-suggest/src/test/completions/upstream/git-branches.test.ts

Lines changed: 0 additions & 78 deletions
This file was deleted.

src/vs/workbench/contrib/terminalContrib/suggest/browser/terminalCompletionModel.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ const compareCompletionsFn = (leadingLineContent: string, a: TerminalCompletionI
3838
return 1;
3939
}
4040

41+
// Boost main and master branches for git commands
42+
const terminalSuggestProviderId = 'terminal-suggest';
43+
const isGitCommand = /^\s*git\b/.test(leadingLineContent);
44+
if (isGitCommand && a.completion.provider === terminalSuggestProviderId && b.completion.provider === terminalSuggestProviderId) {
45+
const aLabel = typeof a.completion.label === 'string' ? a.completion.label : a.completion.label.label;
46+
const bLabel = typeof b.completion.label === 'string' ? b.completion.label : b.completion.label.label;
47+
const aIsMainOrMaster = aLabel === 'main' || aLabel === 'master';
48+
const bIsMainOrMaster = bLabel === 'main' || bLabel === 'master';
49+
50+
if (aIsMainOrMaster && !bIsMainOrMaster) {
51+
return -1;
52+
}
53+
if (bIsMainOrMaster && !aIsMainOrMaster) {
54+
return 1;
55+
}
56+
}
57+
4158
// Sort by the score
4259
let score = b.score[0] - a.score[0];
4360
if (score !== 0) {

src/vs/workbench/contrib/terminalContrib/suggest/test/browser/terminalCompletionModel.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,72 @@ suite('TerminalCompletionModel', function () {
296296

297297

298298
});
299+
300+
suite('git branch priority sorting', () => {
301+
test('should prioritize main and master branches for git commands', () => {
302+
const items = [
303+
createItem({ label: 'feature-branch', provider: 'terminal-suggest' }),
304+
createItem({ label: 'master', provider: 'terminal-suggest' }),
305+
createItem({ label: 'development', provider: 'terminal-suggest' }),
306+
createItem({ label: 'main', provider: 'terminal-suggest' })
307+
];
308+
const model = new TerminalCompletionModel(items, new LineContext('git checkout ', 0));
309+
assertItems(model, ['main', 'master', 'development', 'feature-branch']);
310+
});
311+
312+
test('should prioritize main and master branches for git switch command', () => {
313+
const items = [
314+
createItem({ label: 'feature-branch', provider: 'terminal-suggest' }),
315+
createItem({ label: 'main', provider: 'terminal-suggest' }),
316+
createItem({ label: 'another-feature', provider: 'terminal-suggest' }),
317+
createItem({ label: 'master', provider: 'terminal-suggest' })
318+
];
319+
const model = new TerminalCompletionModel(items, new LineContext('git switch ', 0));
320+
assertItems(model, ['main', 'master', 'another-feature', 'feature-branch']);
321+
});
322+
323+
test('should not prioritize main and master for non-git commands', () => {
324+
const items = [
325+
createItem({ label: 'feature-branch', provider: 'terminal-suggest' }),
326+
createItem({ label: 'master', provider: 'terminal-suggest' }),
327+
createItem({ label: 'main', provider: 'terminal-suggest' })
328+
];
329+
const model = new TerminalCompletionModel(items, new LineContext('ls ', 0));
330+
assertItems(model, ['feature-branch', 'main', 'master']);
331+
});
332+
333+
test('should only apply to terminal-suggest provider', () => {
334+
const items = [
335+
createItem({ label: 'feature-branch', provider: 'other-provider' }),
336+
createItem({ label: 'master', provider: 'other-provider' }),
337+
createItem({ label: 'main', provider: 'other-provider' })
338+
];
339+
const model = new TerminalCompletionModel(items, new LineContext('git checkout ', 0));
340+
assertItems(model, ['feature-branch', 'main', 'master']);
341+
});
342+
343+
test('should handle git commands with leading whitespace', () => {
344+
const items = [
345+
createItem({ label: 'feature-branch', provider: 'terminal-suggest' }),
346+
createItem({ label: 'master', provider: 'terminal-suggest' }),
347+
createItem({ label: 'main', provider: 'terminal-suggest' })
348+
];
349+
const model = new TerminalCompletionModel(items, new LineContext(' git checkout ', 0));
350+
assertItems(model, ['main', 'master', 'feature-branch']);
351+
});
352+
353+
test('should work with complex label objects', () => {
354+
const items = [
355+
createItem({ label: { label: 'feature-branch', description: 'Feature branch' }, provider: 'terminal-suggest' }),
356+
createItem({ label: { label: 'master', description: 'Master branch' }, provider: 'terminal-suggest' }),
357+
createItem({ label: { label: 'main', description: 'Main branch' }, provider: 'terminal-suggest' })
358+
];
359+
const model = new TerminalCompletionModel(items, new LineContext('git checkout ', 0));
360+
assertItems(model, [
361+
{ label: 'main', description: 'Main branch' },
362+
{ label: 'master', description: 'Master branch' },
363+
{ label: 'feature-branch', description: 'Feature branch' }
364+
]);
365+
});
366+
});
299367
});

0 commit comments

Comments
 (0)