-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.lua
More file actions
113 lines (102 loc) · 2.77 KB
/
config.lua
File metadata and controls
113 lines (102 loc) · 2.77 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
-- Represents the basic configuration for the individual languages and it
-- feature set.
local M = {}
---@type specto.Config
local defaults = {
exclude = {
filetypes = {
"",
"help",
},
},
languages = {
["*"] = {
filetypes = {},
file_patterns = {},
features = {},
},
javascript = {
filetypes = { "javascript", "typescript" },
file_patterns = { "__tests__/", "%.?test%.", "%.?spec%." },
features = {
only = {
flag = "only",
keywords = { "it", "describe", "test" },
prefix = false,
separator = ".",
},
skip = {
flag = "skip",
keywords = { "it", "describe", "test" },
prefix = false,
separator = ".",
},
todo = {
flag = "todo",
keywords = { "it", "describe", "test", "bench" },
prefix = false,
separator = ".",
},
},
},
ruby = {
file_patterns = { "%w_spec.rb$" },
features = {
skip = {
flag = "x",
keywords = { "context", "describe", "example", "it", "scenario", "specify", "test" },
prefix = true,
separator = "",
},
},
},
},
}
local options = defaults
local augroup_id
---Provides the configuration for a given filetype.
---@param filetype string
---@return specto.Language | nil
function M.get_config(filetype)
for lang, config in pairs(options.languages) do
if lang == filetype or vim.tbl_contains(config.filetypes or {}, filetype) then return config end
end
return nil
end
---Updates configuration for the current buffer's filetype.
function M.refresh()
local ft = vim.bo.filetype
if #ft == 0 or vim.tbl_contains(options.exclude.filetypes, ft) then return end
local config = M.get_config(ft)
if not config then return end
M.filetype_config = {
file_patterns = config.file_patterns or nil,
features = vim.tbl_deep_extend("force", options.languages["*"].features, config.features),
config,
}
end
---Evaluates the current buffer's filetype and refreshes the configuration.
function M.auto_detection()
M.refresh()
augroup_id = vim.api.nvim_create_augroup("SpectoSetup", { clear = true })
vim.api.nvim_create_autocmd({ "BufEnter" }, {
group = augroup_id,
desc = "Refresh Specto config on BufEnter",
callback = function() M.refresh() end,
})
end
---@return specto.Language | nil
M.filetype_config = nil
---Sets up the configuration for Specto.
---@param opts? specto.Config
function M.setup(opts)
opts = opts or {}
options = vim.tbl_deep_extend("force", {}, defaults, opts or {})
M.auto_detection()
end
return setmetatable(M, {
__index = function(_, key)
if not options then M.setup() end
return options[key]
end,
})