@@ -7,6 +7,7 @@ import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../../base/
7
7
import { TerminalCompletionModel } from '../../browser/terminalCompletionModel.js' ;
8
8
import { LineContext } from '../../../../../services/suggest/browser/simpleCompletionModel.js' ;
9
9
import { TerminalCompletionItem , TerminalCompletionItemKind , type ITerminalCompletion } from '../../browser/terminalCompletionItem.js' ;
10
+ import type { CompletionItemLabel } from '../../../../../services/suggest/browser/simpleCompletionItem.js' ;
10
11
11
12
function createItem ( options : Partial < ITerminalCompletion > ) : TerminalCompletionItem {
12
13
return new TerminalCompletionItem ( {
@@ -41,7 +42,7 @@ function createFolderItemsModel(...labels: string[]): TerminalCompletionModel {
41
42
) ;
42
43
}
43
44
44
- function assertItems ( model : TerminalCompletionModel , labels : string [ ] ) : void {
45
+ function assertItems ( model : TerminalCompletionModel , labels : ( string | CompletionItemLabel ) [ ] ) : void {
45
46
assert . deepStrictEqual ( model . items . map ( i => i . completion . label ) , labels ) ;
46
47
assert . strictEqual ( model . items . length , labels . length ) ; // sanity check
47
48
}
@@ -296,4 +297,83 @@ suite('TerminalCompletionModel', function () {
296
297
297
298
298
299
} ) ;
300
+
301
+ suite ( 'git branch priority sorting' , ( ) => {
302
+ test ( 'should prioritize main and master branches for git commands' , ( ) => {
303
+ const items = [
304
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'feature-branch' } ) ,
305
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'master' } ) ,
306
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'development' } ) ,
307
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'main' } )
308
+ ] ;
309
+ const model = new TerminalCompletionModel ( items , new LineContext ( 'git checkout ' , 0 ) ) ;
310
+ assertItems ( model , [ 'main' , 'master' , 'development' , 'feature-branch' ] ) ;
311
+ } ) ;
312
+
313
+ test ( 'should prioritize main and master branches for git switch command' , ( ) => {
314
+ const items = [
315
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'feature-branch' } ) ,
316
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'main' } ) ,
317
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'another-feature' } ) ,
318
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'master' } )
319
+ ] ;
320
+ const model = new TerminalCompletionModel ( items , new LineContext ( 'git switch ' , 0 ) ) ;
321
+ assertItems ( model , [ 'main' , 'master' , 'another-feature' , 'feature-branch' ] ) ;
322
+ } ) ;
323
+
324
+ test ( 'should not prioritize main and master for non-git commands' , ( ) => {
325
+ const items = [
326
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'feature-branch' } ) ,
327
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'master' } ) ,
328
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'main' } )
329
+ ] ;
330
+ const model = new TerminalCompletionModel ( items , new LineContext ( 'ls ' , 0 ) ) ;
331
+ assertItems ( model , [ 'feature-branch' , 'main' , 'master' ] ) ;
332
+ } ) ;
333
+
334
+ test ( 'should handle git commands with leading whitespace' , ( ) => {
335
+ const items = [
336
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'feature-branch' } ) ,
337
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'master' } ) ,
338
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'main' } )
339
+ ] ;
340
+ const model = new TerminalCompletionModel ( items , new LineContext ( ' git checkout ' , 0 ) ) ;
341
+ assertItems ( model , [ 'main' , 'master' , 'feature-branch' ] ) ;
342
+ } ) ;
343
+
344
+ test ( 'should work with complex label objects' , ( ) => {
345
+ const items = [
346
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : { label : 'feature-branch' , description : 'Feature branch' } } ) ,
347
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : { label : 'master' , description : 'Master branch' } } ) ,
348
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : { label : 'main' , description : 'Main branch' } } )
349
+ ] ;
350
+ const model = new TerminalCompletionModel ( items , new LineContext ( 'git checkout ' , 0 ) ) ;
351
+ assertItems ( model , [
352
+ { label : "main" , description : "Main branch" } ,
353
+ { label : "master" , description : "Master branch" } ,
354
+ { label : "feature-branch" , description : "Feature branch" } ,
355
+ ] ) ;
356
+ } ) ;
357
+
358
+ test ( 'should not prioritize branches with similar names' , ( ) => {
359
+ const items = [
360
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'mainline' } ) ,
361
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'masterpiece' } ) ,
362
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'main' } ) ,
363
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'master' } )
364
+ ] ;
365
+ const model = new TerminalCompletionModel ( items , new LineContext ( 'git checkout ' , 0 ) ) ;
366
+ assertItems ( model , [ 'main' , 'master' , 'mainline' , 'masterpiece' ] ) ;
367
+ } ) ;
368
+
369
+ test ( 'should prioritize for git branch -d' , ( ) => {
370
+ const items = [
371
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'main' } ) ,
372
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'master' } ) ,
373
+ createItem ( { kind : TerminalCompletionItemKind . Argument , label : 'dev' } )
374
+ ] ;
375
+ const model = new TerminalCompletionModel ( items , new LineContext ( 'git branch -d ' , 0 ) ) ;
376
+ assertItems ( model , [ 'main' , 'master' , 'dev' ] ) ;
377
+ } ) ;
378
+ } ) ;
299
379
} ) ;
0 commit comments