@@ -57,10 +57,13 @@ local DEFAULT_SETTINGS = {
57
57
58
58
--- @class grapple.scope_definition
59
59
--- @field name string
60
- --- @field force ? boolean
61
60
--- @field desc ? string
61
+ --- @field force ? boolean
62
62
--- @field fallback ? string name of scope to fall back on
63
63
--- @field cache ? grapple.cache.options | boolean
64
+ --- @field priority ? integer
65
+ --- @field hidden ? boolean
66
+ --- @field delete ? boolean
64
67
--- @field resolver grapple.scope_resolver
65
68
66
69
--- Default scopes provided by Grapple
@@ -483,32 +486,68 @@ end
483
486
--- @return grapple.scope_definition[]
484
487
--- @diagnostic disable-next-line : assign-type-mismatch
485
488
function Settings :scopes ()
486
- -- HACK: Define the order so that fallbacks are defined first
487
- local default_order = {
488
- " global" ,
489
- " cwd" ,
490
- " git" ,
491
- " git_branch" ,
492
- " lsp" ,
493
- }
494
-
489
+ --- @type grapple.scope_definition[]
495
490
local scopes = {}
496
491
497
- for _ , name in ipairs (default_order ) do
498
- local definition = self .inner .default_scopes [name ]
492
+ -- Lookup table of whether a scope is used as a fallback
493
+ --- @type table<string , boolean>
494
+ local fallback_lookup = {}
495
+
496
+ -- Add default scopes
497
+ for name , definition in pairs (self .inner .default_scopes ) do
499
498
if definition == false then
500
- table.insert (scopes , { name = name , delete = true })
501
- elseif type (definition ) == " table" then
502
- table.insert (scopes , self .inner .default_scopes [name ])
503
- else
504
- error (string.format (" invalid default scope: %s" , vim .inspect (definition )))
499
+ definition = { delete = true }
505
500
end
501
+
502
+ definition = vim .tbl_extend (" keep" , definition , {
503
+ name = name ,
504
+ desc = " " ,
505
+ })
506
+
507
+ assert (type (definition .name ) == " string" )
508
+
509
+ table.insert (scopes , definition )
506
510
end
507
511
508
- for _ , definition in ipairs (self .inner .scopes ) do
512
+ -- Add user-defined scopes
513
+ for name , definition in pairs (self .inner .scopes ) do
514
+ definition = vim .tbl_extend (" keep" , definition , {
515
+ name = name ,
516
+ desc = " " ,
517
+ })
518
+
519
+ assert (type (definition .name ) == " string" )
520
+
521
+ if definition .fallback then
522
+ fallback_lookup [definition .fallback ] = true
523
+ end
524
+
509
525
table.insert (scopes , definition )
510
526
end
511
527
528
+ -- Prioritize scope loading
529
+ for _ , scope in ipairs (scopes ) do
530
+ if scope .priority then
531
+ -- Skip. Already given an explicit priority
532
+ elseif not scope .fallback then
533
+ scope .priority = 1000
534
+ elseif fallback_lookup [scope .name ] then
535
+ scope .priority = 100
536
+ else
537
+ scope .priority = 1
538
+ end
539
+ end
540
+
541
+ local function by_priority (scope_a , scope_b )
542
+ if scope_a .priority == scope_b .priority then
543
+ return string.lower (scope_a .name ) < string.lower (scope_b .name )
544
+ else
545
+ return scope_a .priority > scope_b .priority
546
+ end
547
+ end
548
+
549
+ table.sort (scopes , by_priority )
550
+
512
551
return scopes
513
552
end
514
553
0 commit comments