-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsettings.lua
514 lines (436 loc) · 16.5 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
---@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 milliseconds) 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 force? boolean
---@field desc? string
---@field fallback? string name of scope to fall back on
---@field cache? grapple.cache.options | boolean
---@field resolver grapple.scope_resolver
---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 TagActions = require("grapple.tag_actions")
local App = require("grapple.app")
local app = App.get()
-- 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 ScopeActions = require("grapple.scope_actions")
local App = require("grapple.app")
local app = App.get()
-- 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" })
-- 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 ContainerActions = require("grapple.container_actions")
local App = require("grapple.app")
local app = App.get()
-- 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
-- Toggle
window:map("n", "<s-cr>", function()
window:perform_retain(ContainerActions.toggle_all)
end, { desc = "Toggle show all" })
-- Unload
window:map("n", "x", function()
local entry = window:current_entry()
local id = entry.data.id
window:perform_retain(ContainerActions.unload, { id = id })
end)
-- 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" })
-- 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_pos = "center",
title = "Grapple",
-- Custom: adds padding around window title
title_padding = " ",
},
---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
---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[]
function Settings:scopes()
-- HACK: Define the order so that fallbacks are defined first
local default_order = {
"global",
"cwd",
"git",
"git_branch",
"lsp",
}
local scopes = {}
for _, name in ipairs(default_order) do
local definition = self.inner.default_scopes[name]
if definition == false then
table.insert(scopes, { name = name, delete = true })
elseif type(definition) == "table" then
table.insert(scopes, self.inner.default_scopes[name])
else
error(string.format("invalid default scope: %s", vim.inspect(definition)))
end
end
for _, definition in ipairs(self.inner.scopes) do
table.insert(scopes, definition)
end
return scopes
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
return Settings