on master help sign in
style: fix lint of sumneko lua
commitd374efb7ec44267e9582ac3d82182b29683d20f8 authorzztrieuzz <trieu19@gmail.com> dateSat 30 Jul 2022, 11:45 (4 years ago) parent972a797
5 files changed+63−74
lua/nvim-autopairs.lua+19−28 lua/nvim-autopairs/_log.lua+8−8 lua/nvim-autopairs/fastwrap.lua+6−7 lua/nvim-autopairs/rule.lua+22−24 tests/test_utils.lua+8−7
lua/nvim-autopairs.lua+19−28
@@ -101,7 +101,7 @@ M.remove_rule = function(pair)
101101 vim.api.nvim_buf_del_keymap(0, 'i', r.key_map)
102102 end
103103 end
104− M.set_buf_rule(state_tbl)
104+ M.set_buf_rule(state_tbl, 0)
105105 end
106106 M.force_attach()
107107end
@@ -146,8 +146,7 @@ local function is_disable()
146146 return true
147147 end
148148
149− if
150− M.config.disable_in_macro
149+ if M.config.disable_in_macro
151150 and (vim.fn.reg_recording() ~= '' or vim.fn.reg_executing() ~= '')
152151 then
153152 return true
@@ -161,7 +160,7 @@ local function is_disable()
161160 -- should have a way to remove the mapping when vim.bo.filetype = ''
162161 -- now we only remove a rule
163162 -- the event FileType happen after BufEnter
164− M.set_buf_rule({})
163+ M.set_buf_rule({}, 0)
165164 return true
166165 end
167166 return false
@@ -190,8 +189,7 @@ M.on_attach = function(bufnr)
190189
191190 local rules = {}
192191 for _, rule in pairs(M.config.rules) do
193− if
194− utils.check_filetype(rule.filetypes, vim.bo.filetype)
192+ if utils.check_filetype(rule.filetypes, vim.bo.filetype)
195193 and utils.check_not_filetype(rule.not_filetypes, vim.bo.filetype)
196194 then
197195 table.insert(rules, rule)
@@ -327,7 +325,7 @@ M.on_attach = function(bufnr)
327325 "i",
328326 utils.key.c_h,
329327 string.format("v:lua.MPairs.autopairs_c_h(%d)", bufnr),
330− {expr = true, noremap = true}
328+ { expr = true, noremap = true }
331329 )
332330 end
333331
@@ -360,8 +358,7 @@ local autopairs_delete = function(bufnr, key)
360358 #rule.end_pair,
361359 rule.is_regex
362360 )
363− if
364− utils.compare(rule.start_pair, prev_char, rule.is_regex)
361+ if utils.compare(rule.start_pair, prev_char, rule.is_regex)
365362 and utils.compare(rule.end_pair, next_char, rule.is_regex)
366363 and rule:can_del({
367364 ts_node = M.state.ts_node,
@@ -438,8 +435,7 @@ M.autopairs_map = function(bufnr, char)
438435 -- log.debug("start_pair" .. rule.start_pair)
439436 -- log.debug('prev_char' .. prev_char)
440437 -- log.debug('next_char' .. next_char)
441− if
442− utils.compare(rule.end_pair, next_char, rule.is_regex)
438+ if utils.compare(rule.end_pair, next_char, rule.is_regex)
443439 and rule:can_move(cond_opt)
444440 then
445441 local end_pair = rule:get_end_pair(cond_opt)
@@ -447,8 +443,7 @@ M.autopairs_map = function(bufnr, char)
447443 return utils.esc(utils.repeat_key(utils.key.join_right, end_pair_length))
448444 end
449445
450− if
451− utils.compare(rule.start_pair, prev_char, rule.is_regex)
446+ if utils.compare(rule.start_pair, prev_char, rule.is_regex)
452447 and rule:can_pair(cond_opt)
453448 then
454449 local end_pair = rule:get_end_pair(cond_opt)
@@ -461,9 +456,9 @@ M.autopairs_map = function(bufnr, char)
461456 if end_pair:match('<.*>') then
462457 end_pair = utils.esc(end_pair)
463458 end
464− local result= char .. end_pair .. utils.esc(move_text)
459+ local result = char .. end_pair .. utils.esc(move_text)
465460 if rule.is_undo then
466− result = utils.esc(utils.key.undo_sequence) .. result..utils.esc(utils.key.undo_sequence)
461+ result = utils.esc(utils.key.undo_sequence) .. result .. utils.esc(utils.key.undo_sequence)
467462 end
468463 log.debug("key_map :" .. result)
469464 return result
@@ -512,8 +507,7 @@ M.autopairs_insert = function(bufnr, char)
512507 return false
513508 end
514509
515− if
516− utils.compare(rule.start_pair, prev_char, rule.is_regex)
510+ if utils.compare(rule.start_pair, prev_char, rule.is_regex)
517511 and rule:can_pair(cond_opt)
518512 then
519513 local end_pair = rule:get_end_pair(cond_opt)
@@ -564,28 +558,26 @@ M.autopairs_cr = function(bufnr)
564558 -- log.debug('prev_char' .. rule.start_pair)
565559 -- log.debug('prev_char' .. prev_char)
566560 -- log.debug('next_char' .. next_char)
567− if
568− rule.is_endwise
561+ if rule.is_endwise
569562 and utils.compare(rule.start_pair, prev_char, rule.is_regex)
570563 and rule:can_cr(cond_opt)
571564 then
572565 return utils.esc(
573566 end_pair
574− .. utils.repeat_key(utils.key.join_left, end_pair_length)
575− -- FIXME do i need to re indent twice #118
576− .. '<cr><esc>====O'
567+ .. utils.repeat_key(utils.key.join_left, end_pair_length)
568+ -- FIXME do i need to re indent twice #118
569+ .. '<cr><esc>====O'
577570 )
578571 end
579572
580573 cond_opt.check_endwise_ts = false
581574
582− if
583− utils.compare(rule.start_pair, prev_char, rule.is_regex)
575+ if utils.compare(rule.start_pair, prev_char, rule.is_regex)
584576 and utils.compare(rule.end_pair, next_char, rule.is_regex)
585577 and rule:can_cr(cond_opt)
586578 then
587579 log.debug('do_cr')
588− return utils.esc(rule:get_map_cr({rule = rule, line = line, color = col, bufnr = bufnr}))
580+ return utils.esc(rule:get_map_cr({ rule = rule, line = line, color = col, bufnr = bufnr }))
589581 end
590582 end
591583 end
@@ -598,8 +590,7 @@ M.autopairs_afterquote = function(line, key_char)
598590 line = line or utils.text_get_current_line(0)
599591 local _, col = utils.get_cursor()
600592 local prev_char, next_char = utils.text_cusor_line(line, col + 1, 1, 1, false)
601− if
602− utils.is_bracket(prev_char)
593+ if utils.is_bracket(prev_char)
603594 and utils.is_quote(next_char)
604595 and not utils.is_in_quotes(line, col, next_char)
605596 then
@@ -649,7 +640,7 @@ end
649640
650641M.map_cr = function()
651642 M.completion_confirm = function()
652− if vim.fn.pumvisible() ~= 0 then
643+ if vim.fn.pumvisible() ~= 0 then
653644 return M.esc("<cr>")
654645 else
655646 return M.autopairs_cr()
lua/nvim-autopairs/_log.lua+8−8
@@ -1,13 +1,13 @@
1+---@diagnostic disable: undefined-field
2+local empty = {
3+ debug = function(_) end,
4+ info = function(_) end,
5+ error = function(_) end,
6+}
17if _G.__is_log then
28 return require('plenary.log').new {
39 plugin = 'nvim-autopairs',
410 level = (_G.__is_log == true and 'debug') or 'warn',
5− }
6−else
7− return{
8− debug = function(_) end,
9− info = function(_) end,
10− error = function(_) end,
11−
12− }
11+ } or empty
1312end
13+return empty
lua/nvim-autopairs/fastwrap.lua+6−7
@@ -19,7 +19,7 @@ local config = {}
1919
2020M.setup = function(cfg)
2121 if config.chars == nil then
22− config = vim.tbl_extend('force', default_config, cfg or {})
22+ config = vim.tbl_extend('force', default_config, cfg or {}) or {}
2323 npairs.config.fast_wrap = config
2424 end
2525end
@@ -60,16 +60,14 @@ M.show = function(line)
6060 for i = col + 2, #line, 1 do
6161 local char = line:sub(i, i)
6262 local char2 = line:sub(i - 1, i)
63− if
64− string.match(char, config.pattern)
63+ if string.match(char, config.pattern)
6564 or (char == ' ' and string.match(char2, '%w'))
6665 then
6766 local key = config.keys:sub(index, index)
6867 index = index + 1
69− if
70− utils.is_quote(char)
68+ if utils.is_quote(char)
7169 or (
72− utils.is_close_bracket(char)
70+ utils.is_close_bracket(char)
7371 and utils.is_in_quotes(line, col, prev_char)
7472 )
7573 then
@@ -142,7 +140,8 @@ M.highlight_wrap = function(tbl_pos, row, col, end_col)
142140 M.ns_fast_wrap,
143141 config.highlight_grey,
144142 { row, col },
145− { row, end_col }
143+ { row, end_col },
144+ {}
146145 )
147146 end
148147 for _, pos in ipairs(tbl_pos) do
lua/nvim-autopairs/rule.lua+22−24
@@ -1,5 +1,4 @@
11local Cond = require('nvim-autopairs.conds')
2−local log = require('nvim-autopairs._log')
32
43--- @class Rule
54--- @field start_pair string
@@ -8,8 +7,8 @@ local log = require('nvim-autopairs._log')
87--- @field map_cr_func function dynamic change mapping_cr
98--- @field end_pair_length number change end_pair length for key map like <left>
109--- @field key_map string|nil equal nil mean it will skip on autopairs map
11−--- @field filetypes table
12−--- @field not_filetypes table
10+--- @field filetypes table|nil
11+--- @field not_filetypes table|nil
1312--- @field is_regex boolean use regex to compare
1413--- @field is_multibyte boolean
1514--- @field is_endwise boolean only use on end_wise
@@ -33,21 +32,21 @@ function Rule.new(...)
3332 end
3433 end
3534 opt = vim.tbl_extend('force', {
36− key_map = "",
37− start_pair = nil,
38− end_pair = nil,
39− end_pair_func = false,
40− filetypes = nil,
41− not_filetypes = nil,
42− move_cond = nil,
43− del_cond = {},
44− cr_cond = {},
45− pair_cond = {},
46− is_endwise = false,
47− is_regex = false,
48− is_multibyte = false,
35+ key_map = "",
36+ start_pair = nil,
37+ end_pair = nil,
38+ end_pair_func = false,
39+ filetypes = nil,
40+ not_filetypes = nil,
41+ move_cond = nil,
42+ del_cond = {},
43+ cr_cond = {},
44+ pair_cond = {},
45+ is_endwise = false,
46+ is_regex = false,
47+ is_multibyte = false,
4948 end_pair_length = nil,
50− }, opt)
49+ }, opt) or {}
5150
5251 ---@param rule Rule
5352 local function constructor(rule)
@@ -71,6 +70,7 @@ function Rule.new(...)
7170 end
7271 return rule
7372 end
73+
7474 local r = setmetatable(opt, { __index = Rule })
7575 return constructor(r)
7676end
@@ -113,7 +113,6 @@ function Rule:get_map_cr(opts)
113113 return '<c-g>u<cr><c-c>O'
114114end
115115
116−
117116function Rule:replace_map_cr(value)
118117 self.map_cr_func = value
119118 return self
@@ -147,29 +146,29 @@ function Rule:set_end_pair_length(length)
147146end
148147
149148function Rule:with_move(cond)
150− if self.move_cond == nil then self.move_cond = {}end
149+ if self.move_cond == nil then self.move_cond = {} end
151150 table.insert(self.move_cond, cond)
152151 return self
153152end
154153
155154function Rule:with_del(cond)
156− if self.del_cond == nil then self.del_cond = {}end
155+ if self.del_cond == nil then self.del_cond = {} end
157156 table.insert(self.del_cond, cond)
158157 return self
159158end
160159
161160function Rule:with_cr(cond)
162− if self.cr_cond == nil then self.cr_cond = {}end
161+ if self.cr_cond == nil then self.cr_cond = {} end
163162 table.insert(self.cr_cond, cond)
164163 return self
165164end
166165
167166---add condition to rule
168167---@param cond any
169−---@param pos number = 1. It have higher priority to another condition
168+---@param pos number|nil = 1. It have higher priority to another condition
170169---@return Rule
171170function Rule:with_pair(cond, pos)
172− if self.pair_cond == nil then self.pair_cond = {}end
171+ if self.pair_cond == nil then self.pair_cond = {} end
173172 if pos then
174173 table.insert(self.pair_cond, pos, cond)
175174 else
@@ -178,7 +177,6 @@ function Rule:with_pair(cond, pos)
178177 return self
179178end
180179
181−
182180function Rule:only_cr(cond)
183181 self.key_map = nil
184182 self.pair_cond = false
tests/test_utils.lua+8−7
@@ -27,6 +27,7 @@ utils.feed = function(text, num)
2727 result = result .. text
2828 end
2929 api.nvim_feedkeys(
30+ ---@diagnostic disable-next-line: param-type-mismatch
3031 api.nvim_replace_termcodes(result, true, false, true),
3132 'x',
3233 true
@@ -59,8 +60,7 @@ local compare_text = function(linenr, text_after, name, cursor_add, end_cursor)
5960 )
6061 for i = 1, #text_after, 1 do
6162 local t = string.gsub(text_after[i], '%|', '')
62− if
63− t
63+ if t
6464 and new_text[i]
6565 and t:gsub('%s+$', '') ~= new_text[i]:gsub('%s+$', '')
6666 then
@@ -92,7 +92,7 @@ end
9292
9393_G.Test_withfile = function(test_data, cb)
9494 for _, value in pairs(test_data) do
95− it('test ' .. value.name, function(done)
95+ it('test ' .. value.name, function(_)
9696 local text_before = {}
9797 value.linenr = value.linenr or 1
9898 local pos_before = {
@@ -103,11 +103,11 @@ _G.Test_withfile = function(test_data, cb)
103103 value.before = { value.before }
104104 end
105105 for index, text in pairs(value.before) do
106− local txt = string.gsub(text, '%|', '')
106+ local txt = string.gsub(tostring(text), '%|', '')
107107 table.insert(text_before, txt)
108− if string.match(text, '%|') then
109− if string.find(text, '%|') then
110− pos_before.colnr = string.find(text, '%|')
108+ if string.match(tostring(text), '%|') then
109+ if string.find(tostring(text), '%|') then
110+ pos_before.colnr = string.find(tostring(text), '%|')
111111 pos_before.linenr = value.linenr + index - 1
112112 end
113113 end
@@ -120,6 +120,7 @@ _G.Test_withfile = function(test_data, cb)
120120 if cb.before_each then
121121 cb.before_each(value)
122122 end
123+ ---@diagnostic disable-next-line: missing-parameter
123124 if vim.fn.filereadable(vim.fn.expand(value.filepath)) == 1 then
124125 vim.cmd(':e ' .. value.filepath)
125126 if value.filetype then