-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdefaults.lua
More file actions
456 lines (435 loc) · 13.7 KB
/
defaults.lua
File metadata and controls
456 lines (435 loc) · 13.7 KB
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
---@alias Project.Telescope.ActionNames
---|'browse_project_files'
---|'change_working_directory'
---|'delete_project'
---|'find_project_files'
---|'help_mappings'
---|'recent_project_files'
---|'search_in_project_files'
local MODSTR = 'project.config.defaults'
local WARN = vim.log.levels.WARN
local in_list = vim.tbl_contains
local Util = require('project.utils.util')
---The options available for in `require('project').setup()`.
--- ---
---@class Project.Config.Options
local DEFAULTS = {
---Table containing all the LSP-adjacent options.
--- ---
---@class Project.Config.LSP
lsp = {
---If `true` then LSP-based method detection
---will take precedence over traditional pattern matching.
---
---See |project-nvim.pattern-matching| for more info.
--- ---
---Default: `true`
--- ---
enabled = true, ---@type boolean
---Table of lsp clients to ignore by name,
---e.g. `{ 'efm', ... }`.
---
---If you have `nvim-lspconfig` installed **see** `:h lspconfig-all`
---for a list of servers.
--- ---
---Default: `{}`
--- ---
ignore = {}, ---@type string[]
---If `true` then LSP-based method detection
---will not be compared with pattern-matching-based detection.
---
---**WARNING: USE AT YOUR OWN DISCRETION!**
--- ---
---Default: `false`
--- ---
no_fallback = false, ---@type boolean
---Sets whether to use Pattern Matching rules to the LSP client.
---
---If `false` the Pattern Matching will only apply
---to normal pattern matching.
---
---If `true` the `patterns` setting will also filter
---your LSP's `root_dir`, assuming there is one
---and `lsp.enabled` is set to `true`.
--- ---
---Default: `false`
--- ---
use_pattern_matching = false, ---@type boolean
},
---If `true` your root directory won't be changed automatically,
---so you have the option to manually do so
---using the `:ProjectRoot` command.
--- ---
---Default: `false`
--- ---
manual_mode = false, ---@type boolean
---All the patterns used to detect the project's root directory.
---
---See `:h project.nvim-pattern-matching`.
--- ---
---Default: `{ '.git', '.github', '_darcs', '.hg', '.bzr', '.svn', 'Pipfile', ... }`
--- ---
patterns = { ---@type string[]
'.git',
'.github',
'_darcs',
'.hg',
'.bzr',
'.svn',
'Pipfile',
'pyproject.toml',
'.pre-commit-config.yaml',
'.pre-commit-config.yml',
'.csproj',
'.sln',
'.nvim.lua',
},
---Hook to run before attaching to a new project.
---
---It recieves `target_dir` and, optionally,
---the `method` used to change directory.
---
---CREDITS: @danilevy1212
--- ---
---Default: `function(target_dir, method) end`
--- ---
---@param target_dir string
---@param method string
before_attach = function(target_dir, method) end, ---@diagnostic disable-line:unused-local
---Hook to run after attaching to a new project.
---**_This only runs if the directory changes successfully._**
---
---It recieves `dir` and, optionally,
---the `method` used to change directory.
---
---CREDITS: @danilevy1212
--- ---
---Default: `function(dir, method) end`
--- ---
---@param dir string
---@param method string
on_attach = function(dir, method) end, ---@diagnostic disable-line:unused-local
---Determines whether a project will be added
---if its project root is owned by a different user.
---
---If `true`, it will add a project to the history even if its root
---is not owned by the current nvim `UID` **(UNIX only)**.
--- ---
---Default: `false`
--- ---
allow_different_owners = false, ---@type boolean
---If enabled, set `vim.o.autochdir` to `true`.
---
---This is disabled by default because the plugin implicitly disables `autochdir`.
--- ---
---Default: `false`
--- ---
enable_autochdir = false, ---@type boolean
---Make hidden files visible when using any picker.
--- ---
---Default: `false`
--- ---
show_hidden = false, ---@type boolean
---Don't calculate root dir on specific directories,
---e.g. `{ '~/.cargo/*', ... }`.
---
---For more info see `:h project-nvim.pattern-matching`.
--- ---
---Default: `{}`
--- ---
exclude_dirs = {}, ---@type string[]
---If `false`, you'll get a _notification_ every time
---`project.nvim` changes directory.
---
---This is useful for debugging, or for players that
---enjoy verbose operations.
--- ---
---Default: `true`
--- ---
silent_chdir = true, ---@type boolean
---Determines the scope for changing the directory.
---
---Valid options are:
--- - `'global'`: All your nvim `cwd` will sync to your current buffer's project
--- - `'tab'`: _Per-tab_ `cwd` sync to the current buffer's project
--- - `'win'`: _Per-window_ `cwd` sync to the current buffer's project
--- ---
---Default: `'global'`
--- ---
scope_chdir = 'global', ---@type 'global'|'tab'|'win'
---Determines in what filetypes/buftypes the plugin won't execute.
---It's a table with two fields:
---
--- - `ft`: A string array of filetypes to exclude
--- - `bt`: A string array of buftypes to exclude
---
---CREDITS TO [@Zeioth](https://github.com/Zeioth)!:
---[`Zeioth/project.nvim`](https://github.com/Zeioth/project.nvim/commit/95f56b8454f3285b819340d7d769e67242d59b53)
--- ---
---The default value for this one can be found in the project's `README.md`.
--- ---
---@class Project.Config.DisableOn
---@field ft? string[]
---@field bt? string[]
disable_on = {
ft = {
'',
'NvimTree',
'TelescopePrompt',
'TelescopeResults',
'alpha',
'checkhealth',
'lazy',
'log',
'ministarter',
'neo-tree',
'notify',
'nvim-pack',
'packer',
'qf',
},
bt = { 'help', 'nofile', 'nowrite', 'terminal' },
},
---The path where `project.nvim` will store the project history directory,
---containing the project history in it.
---
---For more info, run `:lua vim.print(require('project').get_history_paths())`
--- ---
---Default: `vim.fn.stdpath('data')`
--- ---
datapath = vim.fn.stdpath('data'), ---@type string
---The history size. (by `@acristoffers`)
---
---This will indicate how many entries will be
---written to the history file.
---Set to `0` for no limit.
--- ---
---Default: `100`
--- ---
historysize = 100, ---@type integer
---Table of options used for `fzf-lua` integration
--- ---
---@class Project.Config.FzfLua
fzf_lua = {
---Determines whether the `fzf-lua` integration is enabled.
---
---If `fzf-lua` is not installed, this won't make a difference.
--- ---
---Default: `false`
--- ---
enabled = false, ---@type boolean
},
---Options for logging utility.
--- ---
---@class Project.Config.Logging
log = {
---If `true`, it enables logging in the same directory in which your
---history file is stored.
--- ---
---Default: `false`
--- ---
enabled = false, ---@type boolean
---The maximum logfile size (in megabytes).
--- ---
---Default: `1.1`
--- ---
max_size = 1.1, ---@type number
---Path in which the log file will be saved.
--- ---
---Default: `vim.fn.stdpath('state')`
--- ---
logpath = vim.fn.stdpath('state'), ---@type string
},
---Table of options used for the telescope picker.
--- ---
---@class Project.Config.Telescope
telescope = {
---Determines whether the newest projects come first in the
---telescope picker (`'newest'`), or the oldest (`'oldest'`).
--- ---
---Default: `'newest'`
--- ---
sort = 'newest', ---@type 'newest'|'oldest'
---If you have `telescope-file-browser.nvim` installed, you can enable this
---so that the Telescope picker uses it instead of the `find_files` builtin.
---
---If `true`, use `telescope-file-browser.nvim` instead of builtins.
---In case it is not available, it'll fall back to `find_files`.
--- ---
---Default: `false`
--- ---
prefer_file_browser = false, ---@type boolean
---Set this to `true` if you don't want the file picker to appear
---after you've selected a project.
---
---CREDITS: [UNKNOWN](https://github.com/ahmedkhalf/project.nvim/issues/157#issuecomment-2226419783)
--- ---
---Default: `false`
--- ---
disable_file_picker = false, ---@type boolean
---Table of mappings for the Telescope picker.
---
---Only supports Normal and Insert modes.
--- ---
---Default: check the README
--- ---
---@class Project.Telescope.Mappings
---Normal mode mappings.
--- ---
---@field i? table<string, Project.Telescope.ActionNames>
---Insert mode mappings.
--- ---
---@field n? table<string, Project.Telescope.ActionNames>
mappings = {
n = {
b = 'browse_project_files',
d = 'delete_project',
f = 'find_project_files',
r = 'recent_project_files',
s = 'search_in_project_files',
w = 'change_working_directory',
},
i = {
['<C-b>'] = 'browse_project_files',
['<C-d>'] = 'delete_project',
['<C-f>'] = 'find_project_files',
['<C-r>'] = 'recent_project_files',
['<C-s>'] = 'search_in_project_files',
['<C-w>'] = 'change_working_directory',
},
},
},
}
---Checks the `historysize` option.
---
---If the option is not valid, a warning will be raised and
---the value will revert back to the default.
--- ---
function DEFAULTS:verify_histsize()
Util.validate({ historysize = { self.historysize, { 'number', 'nil' }, true } })
if not self.historysize or type(self.historysize) ~= 'number' then
self.historysize = DEFAULTS.historysize
end
if self.historysize >= 0 or self.historysize == math.floor(self.historysize) then
return
end
vim.notify('`historysize` option invalid. Reverting to default option.', WARN)
self.historysize = DEFAULTS.historysize
end
---Checks the `scope_chdir` option.
---
---If the option is not valid, a warning will be raised and
---the value will revert back to the default.
--- ---
function DEFAULTS:verify_scope_chdir()
Util.validate({ scope_chdir = { self.scope_chdir, { 'string', 'nil' }, true } })
if self.scope_chdir and in_list({ 'global', 'tab', 'win' }, self.scope_chdir) then
return
end
vim.notify(
('`scope_chdir` option invalid (`%s`). Reverting to default option.'):format(self.scope_chdir),
WARN
)
self.scope_chdir = DEFAULTS.scope_chdir
end
function DEFAULTS:verify_datapath()
if not (self.datapath and require('project.utils.util').dir_exists(self.datapath)) then
vim.notify(('Invalid datapath `%s`, reverting to default.'):format(self.datapath), WARN)
self.datapath = DEFAULTS.datapath
end
end
---@return { [1]: 'pattern' }|{ [1]: 'lsp', [2]: 'pattern' } methods
function DEFAULTS:gen_methods()
self:verify_lsp()
local methods = { 'pattern' } ---@type { [1]: 'pattern' }|{ [1]: 'lsp', [2]: 'pattern' }
if self.lsp.enabled then
table.insert(methods, 1, 'lsp')
end
return setmetatable(methods, {
__index = methods,
__newindex = function(_, _, _)
vim.notify('Detection methods are immutable!', vim.log.levels.ERROR)
end,
})
end
function DEFAULTS:verify_logging()
local Path = require('project.utils.path')
local log = self.log
if not log or type(log) ~= 'table' then
self.log = vim.deepcopy(DEFAULTS.log)
end
if self.logging ~= nil and type(self.logging) == 'boolean' then
self.log.enabled = self.logging
self.logging = nil
vim.notify(('`options.logging` is deprecated, use `options.log.enabled`!'):format(MODSTR), WARN)
end
if not (Util.is_type('string', log.logpath) and Path.exists(log.logpath)) then
self.log.logpath = DEFAULTS.log.logpath
end
if not (Util.is_type('number', log.max_size) and log.max_size > 0) then
self.log.max_size = DEFAULTS.log.max_size
end
end
function DEFAULTS:expand_excluded()
if not self.exclude_dirs or type(self.exclude_dirs) ~= 'table' then
self.exclude_dirs = {}
end
if vim.tbl_isempty(self.exclude_dirs) then
return
end
for i, v in ipairs(self.exclude_dirs) do
self.exclude_dirs[i] = Util.rstrip('\\', Util.rstrip('/', vim.fn.fnamemodify(v, ':p')))
end
end
function DEFAULTS:verify_lsp()
self.lsp = self.lsp and vim.tbl_deep_extend('keep', self.lsp, DEFAULTS.lsp) or DEFAULTS.lsp
if self.use_lsp ~= nil then
vim.notify('`use_lsp` is deprecated! Use `lsp.enabled` instead.', WARN)
self.lsp.enabled = self.use_lsp
self.use_lsp = nil
end
if self.allow_patterns_for_lsp ~= nil then
vim.notify(
'`allow_patterns_for_lsp` is deprecated! Use `lsp.use_pattern_matching` instead.',
WARN
)
self.lsp.use_pattern_matching = self.allow_patterns_for_lsp
self.allow_patterns_for_lsp = nil
end
if self.ignore_lsp and type(self.ignore_lsp) == 'table' then
vim.notify('`ignore_lsp` is deprecated! Use `lsp.ignore` instead.', WARN)
self.lsp.ignore = vim.deepcopy(self.ignore_lsp)
self.ignore_lsp = nil
end
end
---Verify config integrity.
--- ---
function DEFAULTS:verify()
self:verify_datapath()
self:verify_lsp()
self:verify_histsize()
self:verify_scope_chdir()
self:verify_logging()
if not self.detection_methods then
return
end
vim.schedule(function()
vim.notify(
'(project.nvim): `detection_methods` has been deprecated!\nUse `lsp.enabled` instead.',
WARN
)
end)
end
---@param opts Project.Config.Options
---@return Project.Config.Options
---@overload fun(): Project.Config.Options
function DEFAULTS:new(opts)
Util.validate({ opts = { opts, { 'table', 'nil' }, true } })
---@type Project.Config.Options
local obj = setmetatable(vim.tbl_deep_extend('keep', opts or {}, DEFAULTS), {
__index = DEFAULTS,
})
return obj
end
return DEFAULTS
-- vim: set ts=2 sts=2 sw=2 et ai si sta: