-
Notifications
You must be signed in to change notification settings - Fork 4
/
helper.lua
130 lines (117 loc) · 3.06 KB
/
helper.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
-- Have a command that renames all feature but the one provided
-- Have a command that sets the correct name for all features
local M = {}
local FeaturesDir = vim.fn.expand('./features')
-- Example of a feature
-- {
-- name = "whatever",
-- filename = "whatever.feature",
-- }
M.get_features = function()
local features = {}
local loaded = {}
local bufs = vim.api.nvim_list_bufs()
vim.tbl_map(function(b)
local name = vim.api.nvim_buf_get_name(b)
if name:find('%.feature$') or name:find('%.feature.commented$') then
loaded[name] = b
end
end
,bufs)
for _, filename in ipairs(vim.fn.readdir('./features')) do
if filename:match('%.feature(.*)$') then
table.insert(features, {
name = filename:match('(.+)%.feature'),
filename = string.format('%s/%s', FeaturesDir, filename),
loaded = loaded[filename] or false,
buffer = loaded[filename]
})
end
end
return features
end
M.comment_features = function(features)
local count = 0
for _, feature in ipairs(features) do
if not feature.filename:find('%.commented$') then
if feature.loaded then
vim.api.nvim_buf_delete(feature.buffer, true)
end
count = count + 1
vim.fn.rename(feature.filename, feature.filename .. '.commented')
end
end
vim.notify(string.format('Commented %s features', count))
end
M.uncomment_features = function(features)
local count = 0
for _, feature in ipairs(features) do
if feature.filename:find('%.commented$') then
count = count + 1
local repl = feature.filename:gsub('.commented$', '')
vim.fn.rename(feature.filename, repl)
end
end
vim.notify(string.format('Uncommented %s features', count))
end
M.keep_one = function(name)
local features = M.get_features()
local found
for i, feature in ipairs(features) do
if feature.name == name then
found = i
break
end
end
if found then
M.uncomment_features({features[found]})
table.remove(features, found)
M.comment_features(features)
else
vim.notify(string.format('No feature named %s', name))
end
end
M.comment_one = function(name)
local features = M.get_features()
local found
for i, feature in ipairs(features) do
if feature.name == name then
found = i
break
end
end
if found then
M.comment_features({features[found]})
else
vim.notify(string.format('No feature named %s', name))
end
end
M.keep_all = function()
local features = M.get_features()
M.uncomment_features(features)
end
M.command = function(cargs)
local subcommand = cargs.fargs[1]
if cargs.bang then
return M.comment_one(subcommand)
end
if subcommand == 'all' then
return M.keep_all()
else
return M.keep_one(subcommand)
end
end
M.complete = function()
local features = M.get_features()
local completions = {}
for _, feature in ipairs(features) do
table.insert(completions, feature.name)
end
return completions
end
vim.api.nvim_create_user_command('Features', M.command, {
nargs = 1,
complete = M.complete,
force = true,
bang = true,
})