-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsettings.lua
579 lines (487 loc) · 18.6 KB
/
settings.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
---@class grapple.settings
local Settings = {}
---@class grapple.settings
local DEFAULT_SETTINGS = {
---Grapple save location
---@type string
---@diagnostic disable-next-line: param-type-mismatch
save_path = vim.fn.stdpath("data") .. "/" .. "grapple",
---Default scope to use when managing Grapple tags
---For more information, please see the Scopes section
---@type string
scope = "git",
---User-defined scopes or overrides
---For more information about scopes, please see the Scope API section
---@type grapple.scope_definition[]
scopes = {},
---Show icons next to tags in Grapple windows
---Requires "nvim-tree/nvim-web-devicons"
---@type boolean
icons = true,
---Highlight the current selection in Grapple windows
---Also, indicates when a tag path does not exist
---@type boolean
status = true,
---Position a tag's name should be shown in Grapple windows
---@type "start" | "end"
name_pos = "end",
---How a tag's path should be rendered in Grapple windows
--- "relative": show tag path relative to the scope's resolved path
--- "basename": show tag path basename and directory hint
---See: settings.styles
---@type "basename" | "relative"
style = "relative",
---A string of characters used for quick selecting in Grapple windows
---An empty string or false will disable quick select
---@type string | boolean
quick_select = "123456789",
---Default command to use when selecting a tag
---@type fun(path: string)
command = vim.cmd.edit,
---Time limit used for pruning unused scope (IDs). If a scope's save file
---modified time exceeds this limit, then it will be deleted when a prune
---requested. Can be an integer (in seconds) or a string time limit
---(e.g. "30d" or "2h" or "15m")
---@type integer | string
prune = "30d",
---@class grapple.scope_definition
---@field name string
---@field resolver grapple.scope_resolver
---@field desc? string
---@field force? boolean
---@field fallback? string name of scope to fall back on
---@field cache? grapple.cache.options | boolean
---@field priority? integer
---@field hidden? boolean hide only scopes which have this set
---@field shown? boolean show only scopes which have this set
---@field delete? boolean
---Default scopes provided by Grapple
---For more information about default scopes, please see the Scopes section
---Disable by setting scope to "false". For example, { lsp = false }
---@type table<string, grapple.scope_definition | boolean>
default_scopes = {
global = {
name = "global",
desc = "Global scope",
resolver = function()
return "global", vim.loop.cwd()
end,
},
static = {
name = "static",
desc = "Initial working directory",
cache = true,
resolver = function()
return vim.loop.cwd(), vim.loop.cwd()
end,
},
cwd = {
name = "cwd",
desc = "Current working directory",
cache = { event = "DirChanged" },
resolver = function()
return vim.loop.cwd(), vim.loop.cwd()
end,
},
git = {
name = "git",
desc = "Git root directory",
fallback = "cwd",
cache = {
event = { "BufEnter", "FocusGained" },
debounce = 1000, -- ms
},
resolver = function()
-- TODO: this will stop on submodules, needs fixing
local git_files = vim.fs.find(".git", { upward = true, stop = vim.loop.os_homedir() })
if #git_files == 0 then
return
end
local root = vim.fn.fnamemodify(git_files[1], ":h")
return root, root
end,
},
git_branch = {
name = "git_branch",
desc = "Git root directory and branch",
fallback = "cwd",
cache = {
event = { "BufEnter", "FocusGained" },
debounce = 1000, -- ms
},
resolver = function()
-- TODO: this will stop on submodules, needs fixing
local git_files = vim.fs.find(".git", { upward = true, stop = vim.loop.os_homedir() })
if #git_files == 0 then
return
end
local root = vim.fn.fnamemodify(git_files[1], ":h")
-- TODO: Don't use vim.system, it's a nvim-0.10 feature
-- TODO: Don't shell out, read the git head or something similar
local result = vim.fn.system({ "git", "symbolic-ref", "--short", "HEAD" })
local branch = vim.trim(string.gsub(result, "\n", ""))
local id = string.format("%s:%s", root, branch)
local path = root
return id, path
end,
},
lsp = {
name = "lsp",
desc = "LSP root directory",
fallback = "git",
cache = {
event = { "LspAttach", "LspDetach" },
debounce = 250, -- ms
},
resolver = function()
-- TODO: Don't use vim.lsp.get_clients, it's a nvim-0.10 feature
local clients = vim.lsp.get_active_clients({ bufnr = 0 })
if #clients == 0 then
return
end
local path = clients[1].root_dir
return path, path
end,
},
},
---User-defined tags title function for Grapple windows
---By default, uses the resolved scope's ID
---@type fun(scope: grapple.resolved_scope): string?
tag_title = function(scope)
local Path = require("grapple.path")
-- If the scope ID is something like "global"
if not Path.is_absolute(scope.id) then
return scope.id
end
return vim.fn.fnamemodify(scope.id, ":~")
end,
---Not user documented
---@type grapple.hook_fn
tag_hook = function(window)
local Grapple = require("grapple")
local TagActions = require("grapple.tag_actions")
local app = Grapple.app()
-- Select
window:map("n", "<cr>", function()
local cursor = window:cursor()
window:perform_close(TagActions.select, { index = cursor[1] })
end, { desc = "Select" })
-- Select (horizontal split)
window:map("n", "<c-s>", function()
local cursor = window:cursor()
window:perform_close(TagActions.select, { index = cursor[1], command = vim.cmd.split })
end, { desc = "Select (split)" })
-- Select (vertical split)
window:map("n", "|", function()
local cursor = window:cursor()
window:perform_close(TagActions.select, { index = cursor[1], command = vim.cmd.vsplit })
end, { desc = "Select (vsplit)" })
-- Quick select
for i, quick in ipairs(app.settings:quick_select()) do
window:map("n", string.format("%s", quick), function()
window:perform_close(TagActions.select, { index = i })
end, { desc = string.format("Quick select %d", i) })
end
-- Quickfix list
window:map("n", "<c-q>", function()
window:perform_close(TagActions.quickfix)
end, { desc = "Quickfix" })
-- Go "up" to scopes
window:map("n", "-", function()
window:perform_close(TagActions.open_scopes)
end, { desc = "Go to scopes" })
-- Rename
window:map("n", "R", function()
local entry = window:current_entry()
local path = entry.data.path
window:perform_retain(TagActions.rename, { path = path })
end, { desc = "Rename" })
-- Help
window:map("n", "?", function()
local WindowActions = require("grapple.window_actions")
window:perform_retain(WindowActions.help)
end, { desc = "Help" })
end,
---User-defined scopes title function for Grapple windows
---By default, renders "Grapple Scopes"
---@type fun(): string?
scope_title = function()
return "Grapple Scopes"
end,
---Not user documented
---@type grapple.hook_fn
scope_hook = function(window)
local Grapple = require("grapple")
local ScopeActions = require("grapple.scope_actions")
local app = Grapple.app()
-- Select
window:map("n", "<cr>", function()
local entry = window:current_entry()
local name = entry.data.name
window:perform_close(ScopeActions.open_tags, { name = name })
end, { desc = "Open scope" })
-- Quick select
for i, quick in ipairs(app.settings:quick_select()) do
window:map("n", string.format("%s", quick), function()
local entry, err = window:entry({ index = i })
if not entry then
---@diagnostic disable-next-line: param-type-mismatch
return vim.notify(err, vim.log.levels.ERROR)
end
local name = entry.data.name
window:perform_close(ScopeActions.open_tags, { name = name })
end, { desc = string.format("Quick open %d", i) })
end
-- Change
window:map("n", "<s-cr>", function()
local entry = window:current_entry()
local name = entry.data.name
window:perform_close(ScopeActions.change, { name = name })
end, { desc = "Change scope" })
-- Navigate "up" to loaded scopes
window:map("n", "-", function()
window:perform_close(ScopeActions.open_loaded)
end, { desc = "Go to loaded scopes" })
-- Toggle
window:map("n", "g.", function()
window:perform_retain(ScopeActions.toggle_all)
end, { desc = "Toggle show hidden" })
-- Help
window:map("n", "?", function()
local WindowActions = require("grapple.window_actions")
window:perform_retain(WindowActions.help)
end, { desc = "Help" })
end,
---User-defined loaded scopes title function for Grapple windows
---By default, renders "Grapple Loaded Scopes"
---@type fun(): string?
loaded_title = function()
return "Grapple Loaded Scopes"
end,
---Not user documented
---@type grapple.hook_fn
loaded_hook = function(window)
local Grapple = require("grapple")
local ContainerActions = require("grapple.container_actions")
local app = Grapple.app()
-- Select
window:map("n", "<cr>", function()
local entry = window:current_entry()
local id = entry.data.id
window:perform_close(ContainerActions.select, { id = id })
end, { desc = "Open tags" })
-- Quick select
for i, quick in ipairs(app.settings:quick_select()) do
window:map("n", string.format("%s", quick), function()
local entry, err = window:entry({ index = i })
if not entry then
---@diagnostic disable-next-line: param-type-mismatch
return vim.notify(err, vim.log.levels.ERROR)
end
local name = entry and entry.data.name
window:perform_close(ContainerActions.select, { name = name })
end, { desc = string.format("Quick select %d", i) })
end
-- Unload
window:map("n", "x", function()
local entry = window:current_entry()
local id = entry.data.id
window:perform_retain(ContainerActions.unload, { id = id })
end, { desc = "Unload scope" })
-- Reset
window:map("n", "X", function()
local entry = window:current_entry()
local id = entry.data.id
window:perform_retain(ContainerActions.reset, { id = id })
end, { desc = "Reset scope" })
-- Navigate "up" to scopes
window:map("n", "-", function()
window:perform_close(ContainerActions.open_scopes)
end, { desc = "Go to scopes" })
-- Toggle
window:map("n", "g.", function()
window:perform_retain(ContainerActions.toggle_all)
end, { desc = "Toggle show unloaded" })
-- Help
window:map("n", "?", function()
local WindowActions = require("grapple.window_actions")
window:perform_retain(WindowActions.help)
end, { desc = "Help" })
end,
---@alias grapple.content grapple.tag_content| grapple.scope_content| grapple.container_content
---@alias grapple.entity grapple.tag_content.entity | grapple.scope_content.entity | grapple.container_content.entity
---@alias grapple.style_fn fun(entity: grapple.entity, content: grapple.content): grapple.stylized
---@class grapple.stylized
---@field display string
---@field marks grapple.vim.mark[]
---Not user documented
---@type table<string, grapple.style_fn>
styles = {
relative = function(entity, content)
local Path = require("grapple.path")
---@type grapple.stylized
local line = {
display = assert(Path.fs_relative(content.scope.path, entity.tag.path)),
marks = {},
}
return line
end,
basename = function(entity, _)
local Path = require("grapple.path")
local parent_mark
if not entity.base_unique then
-- stylua: ignore
parent_mark = {
virt_text = { {
"."
.. Path.separator
.. Path.relative(Path.parent(entity.tag.path, 3), Path.parent(entity.tag.path, 1)),
"GrappleHint",
} },
virt_text_pos = "eol",
}
end
---@type grapple.stylized
local line = {
display = Path.base(entity.tag.path),
marks = { parent_mark },
}
return line
end,
},
---Additional window options for Grapple windows
---See :h nvim_open_win
---@type grapple.vim.win_opts
win_opts = {
-- Can be fractional
width = 80,
height = 12,
row = 0.5,
col = 0.5,
relative = "editor",
border = "single",
focusable = false,
style = "minimal",
title = "Grapple", -- fallback title for Grapple windows
title_pos = "center",
title_padding = " ", -- custom: adds padding around window title
-- footer = "", -- disable footer
footer_pos = "center",
},
---Values for which a buffer should be excluded from being tagged
exclusions = {
buftype = {
"nofile",
},
filetype = {
"grapple",
},
name = {
"",
},
},
---Not user documented
---Default statusline options
---@class grapple.statusline.options
statusline = {
icon = "",
inactive = " %s ",
active = "[%s]",
-- Mostly for lualine integration. Lualine will automatically prepend
-- the icon to the returned output
include_icon = true,
},
}
Settings.__index = function(tbl, key)
return Settings[key] or tbl.inner[key]
end
function Settings:new()
return setmetatable({
inner = vim.deepcopy(DEFAULT_SETTINGS),
}, self)
end
-- Update settings in-place
---@param opts? grapple.settings
function Settings:update(opts)
self.inner = vim.tbl_deep_extend("force", self.inner, opts or {})
end
---Override quick_select to ensure a string table is always returned
---@return string[]
---@diagnostic disable-next-line: assign-type-mismatch
function Settings:quick_select()
local Util = require("grapple.util")
if not self.inner.quick_select then
return {}
end
return vim.tbl_filter(Util.not_empty, vim.split(self.inner.quick_select, ""))
end
---Override scopes to combine both the default scopes and user-defined scopes
---@return grapple.scope_definition[]
---@diagnostic disable-next-line: assign-type-mismatch
function Settings:scopes()
---@type grapple.scope_definition[]
local scopes = {}
-- Lookup table of whether a scope is used as a fallback
---@type table<string, boolean>
local fallback_lookup = {}
-- Detect how a user is configuring scopes. There are two options:
-- exclude: scopes set the "hidden" field and are excluded by default
-- include: scopes set the "shown" field and are included by default
local using_shown = false
-- Add default scopes
for name, definition in pairs(self.inner.default_scopes) do
if definition == false then
definition = { delete = true }
end
definition = vim.tbl_extend("keep", definition, { name = name })
assert(type(definition.name) == "string")
if definition.shown then
using_shown = true
end
if definition.fallback then
fallback_lookup[definition.fallback] = true
end
table.insert(scopes, definition)
end
-- Add user-defined scopes
for name, definition in pairs(self.inner.scopes) do
definition = vim.tbl_extend("keep", definition, { name = name })
assert(type(definition.name) == "string")
if definition.shown then
using_shown = true
end
if definition.fallback then
fallback_lookup[definition.fallback] = true
end
table.insert(scopes, definition)
end
-- Prioritize scope loading
for _, scope in ipairs(scopes) do
if scope.priority then
-- Skip. Already given an explicit priority
elseif not scope.fallback then
scope.priority = 1000
elseif fallback_lookup[scope.name] then
scope.priority = 100
else
scope.priority = 1
end
end
-- Update to whitelist or blacklist scopes by default
if using_shown then
for _, scope in ipairs(scopes) do
scope.hidden = not scope.shown
scope.shown = nil
end
end
local function by_priority(scope_a, scope_b)
if scope_a.priority == scope_b.priority then
return string.lower(scope_a.name) < string.lower(scope_b.name)
else
return scope_a.priority > scope_b.priority
end
end
table.sort(scopes, by_priority)
return scopes
end
return Settings