-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.lua
221 lines (199 loc) · 7.17 KB
/
init.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
local helper = require("commented.helper")
local opts = {
comment_padding = " ",
keybindings = { n = "<leader>c", v = "<leader>c", nl = "<leader>cc" },
set_keybindings = true,
prefer_block_comment = false, -- Set it to true to automatically use block comment when multiple lines are selected
inline_cms = {
hjson = { inline = "//%s" },
mysql = { inline = "#%s" },
},
block_cms = {
typescriptreact = { block = "/*%s*/" },
javascriptreact = { block = "/*%s*/" },
javascript = { block = "/*%s*/" },
typescript = { block = "/*%s*/" },
sql = { block = "/*%s*/" },
mysql = { block = "/*%s*/" },
lua = { block = "--[[%s--]]" },
teal = { block = "--[[%s--]]" },
rust = { block = "/*%s*/" },
kotlin = { block = "/*%s*/" },
java = { block = "/*%s*/" },
groovy = { block = "/*%s*/" },
go = { block = "/*%s*/" },
php = { block = "/*%s*/" },
c = { block = "/*%s*/", throw_away_block = "#if 0%s#endif" },
cpp = { block = "/*%s*/", throw_away_block = "#if 0%s#endif" },
cs = { block = "/*%s*/" },
fs = { block = "(*%s*)" },
julia = { block = "#=%s=#" },
hjson = { block = "/*%s*/" },
dhall = { block = "{-%s-}" },
lean = { block = "/-%s-/" },
wren = { block = "/*%s*/" },
pug = { unbuffered = "//-%s", block = "//-%s//" },
haml = { unbuffered = "-#" },
haxe = { block = "/**%s**/" },
rjson = { block = "/*%s*/" },
d = { block = "/*%s*/", alt_block = "/+%s+/" },
},
ex_mode_cmd = "Comment",
left_align_comment = false,
}
local leading_space = "^%s*"
local space_only = leading_space .. "$"
local function commenting_lines(lines, start_line, end_line, start_symbol, end_symbol)
local commented_lines = vim.tbl_map(function(line)
-- local pattern = opts.left_align_comment and leading_space or "([^%s])"
-- ([^%s*])
-- Make this a global option?
local pattern = "([^%s])"
local commented_line = line:gsub(pattern, start_symbol .. opts.comment_padding .. "%1", 1)
if end_symbol ~= "" then
commented_line = commented_line .. opts.comment_padding .. end_symbol
end
return commented_line
end, lines)
vim.api.nvim_buf_set_lines(0, start_line, end_line, false, commented_lines)
end
local function clear_lines_symbols(lines, target_symbols)
local index = 1
return vim.tbl_map(function(line)
if line:match(space_only) then
return line
end
local start_symbol, end_symbol = unpack(target_symbols[index])
local pattern = opts.left_align_comment and opts.comment_padding or "%s*"
local cleaned_line = line:gsub(start_symbol .. pattern, "", 1)
if end_symbol ~= "" then
cleaned_line = cleaned_line:gsub("%s*" .. end_symbol, "")
end
index = index + 1
return cleaned_line
end, lines)
end
local function uncommenting_lines(lines, start_line, end_line, uncomment_symbols)
vim.api.nvim_buf_set_lines(0, start_line, end_line, false, clear_lines_symbols(lines, uncomment_symbols))
end
local function has_matching_pattern(line, comment_patterns, uncomment_symbols)
local matched = false
for _, pattern in pairs(comment_patterns) do
local escaped_start_symbol, escaped_end_symbol = helper.escape_symbols(helper.get_comment_wrap(pattern))
local escaped_pattern = "^%s*" .. escaped_start_symbol .. ".*" .. escaped_end_symbol
if line:match(escaped_pattern) then
table.insert(uncomment_symbols, { escaped_start_symbol, escaped_end_symbol })
matched = true
break
end
end
return matched
end
local function toggle_inline_comment(lines, start_line, end_line, filetype)
local should_comment = false
local uncomment_symbols, filetype = {}, vim.o.filetype
local cms = vim.api.nvim_buf_get_option(0, "commentstring")
local alt_cms = opts.inline_cms[filetype] or {}
local comment_patterns = vim.tbl_extend("force", { cms = cms }, alt_cms or {})
local comment_start_symbol, comment_end_symbol = helper.get_comment_wrap(cms)
for _, line in ipairs(lines) do
if not line:match(space_only) then
local matched = has_matching_pattern(line, comment_patterns, uncomment_symbols)
if not matched then
should_comment = true
break
end
end
end
if should_comment then
commenting_lines(lines, start_line, end_line, comment_start_symbol, comment_end_symbol)
else
uncommenting_lines(lines, start_line, end_line, uncomment_symbols)
end
end
local function toggle_block_comment(lines, start_line, end_line, block_symbols, should_comment)
if should_comment then
lines[1] = lines[1]:gsub("([^%s]*)", block_symbols[1][1] .. opts.comment_padding .. "%1", 1)
lines[#lines] = lines[#lines]:gsub("%s*$", "%1" .. opts.comment_padding .. block_symbols[2][2], 1)
vim.api.nvim_buf_set_lines(0, start_line, end_line, false, lines)
else
lines[1], lines[#lines] = unpack(clear_lines_symbols({ lines[1], lines[#lines] }, block_symbols))
vim.api.nvim_buf_set_lines(0, start_line, end_line, false, lines)
end
end
local function has_symbol(prefix, suffix)
return function(str, symbol)
return str:match(prefix .. symbol .. suffix)
end
end
local has_start_symbol = has_symbol("^%s*", "")
local has_end_symbol = has_symbol("", "%s*$")
local function toggle_comment(mode, line1, line2)
local start_line, end_line = helper.get_lines(mode, line1, line2)
local lines = vim.api.nvim_buf_get_lines(0, start_line, end_line, false)
local is_block, should_comment = false, true
local block_symbols = nil
local filetype = vim.o.filetype
if opts.block_cms[filetype] then
if #lines > 1 then
local comment_patterns = opts.block_cms[filetype]
if not comment_patterns then
return
end
local first_line, last_line = lines[1], lines[#lines]
for _, pattern in pairs(comment_patterns) do
local start_symbol, end_symbol = helper.escape_symbols(helper.get_comment_wrap(pattern))
if
has_start_symbol(first_line, start_symbol)
and not has_end_symbol(first_line, end_symbol)
and not has_start_symbol(last_line, start_symbol)
and has_end_symbol(last_line, end_symbol)
then
block_symbols = { { start_symbol, "" }, { "", end_symbol } }
is_block, should_comment = true, false
break
end
end
if opts.prefer_block_comment then
-- Decide what block symbol to use
local start_symbol, end_symbol = helper.escape_symbols(
helper.get_comment_wrap(opts.block_cms[filetype].block)
)
block_symbols = { { start_symbol, "" }, { "", end_symbol } }
is_block = true
end
end
end
if is_block then
toggle_block_comment(lines, start_line, end_line, block_symbols, should_comment)
else
toggle_inline_comment(lines, start_line, end_line)
end
end
local function setup(user_opts)
opts = vim.tbl_deep_extend("force", opts, user_opts or {})
opts.inline_cms = vim.tbl_deep_extend("force", opts.inline_cms, opts.block_cms)
local supported_modes = { "n", "v" }
if opts.set_keybindings then
for _, mode in ipairs(supported_modes) do
vim.api.nvim_set_keymap(mode, opts.keybindings[mode], "Commented_n()", {
expr = true,
silent = true,
noremap = true,
})
end
vim.api.nvim_set_keymap(
"n",
opts.keybindings.nl,
"Commented_nl()",
{ expr = true, silent = true, noremap = true }
)
end
if opts.ex_mode_cmd then
vim.api.nvim_exec(
"command! -range " .. opts.ex_mode_cmd .. " lua require('commented').toggle_comment('c', <line1>, <line2>)",
true
)
end
end
return { setup = setup, toggle_comment = toggle_comment }