| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | local function get_commentstring(ref_position) |
| | local buf_cs = vim.bo.commentstring |
| |
|
| | local ts_parser = vim.treesitter.get_parser(0, '', { error = false }) |
| | if not ts_parser then |
| | return buf_cs |
| | end |
| |
|
| | |
| | |
| | local row, col = ref_position[1] - 1, ref_position[2] |
| | local ref_range = { row, col, row, col + 1 } |
| |
|
| | |
| | |
| | local caps = vim.treesitter.get_captures_at_pos(0, row, col) |
| | for i = #caps, 1, -1 do |
| | local id, metadata = caps[i].id, caps[i].metadata |
| | local md_cms = metadata['bo.commentstring'] or metadata[id] and metadata[id]['bo.commentstring'] |
| |
|
| | if md_cms then |
| | return md_cms |
| | end |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | |
| | local ts_cs, res_level = nil, 0 |
| |
|
| | |
| | local function traverse(lang_tree, level) |
| | if not lang_tree:contains(ref_range) then |
| | return |
| | end |
| |
|
| | local lang = lang_tree:lang() |
| | local filetypes = vim.treesitter.language.get_filetypes(lang) |
| | for _, ft in ipairs(filetypes) do |
| | local cur_cs = vim.filetype.get_option(ft, 'commentstring') |
| | if cur_cs ~= '' and level > res_level then |
| | ts_cs = cur_cs |
| | end |
| | end |
| |
|
| | for _, child_lang_tree in pairs(lang_tree:children()) do |
| | traverse(child_lang_tree, level + 1) |
| | end |
| | end |
| | traverse(ts_parser, 1) |
| |
|
| | return ts_cs or buf_cs |
| | end |
| |
|
| | |
| | |
| | |
| | local function get_comment_parts(ref_position) |
| | local cs = get_commentstring(ref_position) |
| |
|
| | if cs == nil or cs == '' then |
| | vim.api.nvim_echo({ { "Option 'commentstring' is empty.", 'WarningMsg' } }, true, {}) |
| | return { left = '', right = '' } |
| | end |
| |
|
| | if not (type(cs) == 'string' and cs:find('%%s') ~= nil) then |
| | error(vim.inspect(cs) .. " is not a valid 'commentstring'.") |
| | end |
| |
|
| | |
| | local left, right = cs:match('^(.-)%%s(.-)$') |
| | return { left = left, right = right } |
| | end |
| |
|
| | |
| | |
| | |
| | local function make_comment_check(parts) |
| | local l_esc, r_esc = vim.pesc(parts.left), vim.pesc(parts.right) |
| |
|
| | |
| | |
| | local regex = '^%s-' .. vim.trim(l_esc) .. '.*' .. vim.trim(r_esc) .. '%s-$' |
| |
|
| | return function(line) |
| | return line:find(regex) ~= nil |
| | end |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | |
| | local function get_lines_info(lines, parts) |
| | local comment_check = make_comment_check(parts) |
| |
|
| | local is_commented = true |
| | local indent_width = math.huge |
| | |
| | local indent |
| |
|
| | for _, l in ipairs(lines) do |
| | |
| | local _, indent_width_cur, indent_cur = l:find('^(%s*)') |
| |
|
| | |
| | if indent_width_cur < l:len() then |
| | |
| | |
| | if indent_width_cur < indent_width then |
| | |
| | indent_width, indent = indent_width_cur, indent_cur |
| | end |
| |
|
| | |
| | if is_commented then |
| | is_commented = comment_check(l) |
| | end |
| | end |
| | end |
| |
|
| | |
| | return indent or '', is_commented |
| | end |
| |
|
| | |
| | |
| | |
| | local function is_blank(x) |
| | return x:find('^%s*$') ~= nil |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | local function make_comment_function(parts, indent) |
| | local prefix, nonindent_start, suffix = indent .. parts.left, indent:len() + 1, parts.right |
| | local blank_comment = indent .. vim.trim(parts.left) .. vim.trim(parts.right) |
| |
|
| | return function(line) |
| | if is_blank(line) then |
| | return blank_comment |
| | end |
| | return prefix .. line:sub(nonindent_start) .. suffix |
| | end |
| | end |
| |
|
| | |
| | |
| | |
| | local function make_uncomment_function(parts) |
| | local l_esc, r_esc = vim.pesc(parts.left), vim.pesc(parts.right) |
| | local regex = '^(%s*)' .. l_esc .. '(.*)' .. r_esc .. '(%s-)$' |
| | local regex_trimmed = '^(%s*)' .. vim.trim(l_esc) .. '(.*)' .. vim.trim(r_esc) .. '(%s-)$' |
| |
|
| | return function(line) |
| | |
| | local indent, new_line, trail = line:match(regex) |
| | if new_line == nil then |
| | indent, new_line, trail = line:match(regex_trimmed) |
| | end |
| |
|
| | |
| | if new_line == nil then |
| | return line |
| | end |
| |
|
| | |
| | if is_blank(new_line) then |
| | indent, trail = '', '' |
| | end |
| |
|
| | return indent .. new_line .. trail |
| | end |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | local function toggle_lines(line_start, line_end, ref_position) |
| | ref_position = ref_position or { line_start, 0 } |
| | local parts = get_comment_parts(ref_position) |
| | local lines = vim.api.nvim_buf_get_lines(0, line_start - 1, line_end, false) |
| | local indent, is_comment = get_lines_info(lines, parts) |
| |
|
| | local f = is_comment and make_uncomment_function(parts) or make_comment_function(parts, indent) |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | vim._with({ lockmarks = true }, function() |
| | vim.api.nvim_buf_set_lines(0, line_start - 1, line_end, false, vim.tbl_map(f, lines)) |
| | end) |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | |
| | local function operator(mode) |
| | |
| | |
| | if mode == nil then |
| | vim.o.operatorfunc = "v:lua.require'vim._comment'.operator" |
| | return 'g@' |
| | end |
| |
|
| | |
| | local mark_from, mark_to = "'[", "']" |
| | local lnum_from, col_from = vim.fn.line(mark_from), vim.fn.col(mark_from) |
| | local lnum_to, col_to = vim.fn.line(mark_to), vim.fn.col(mark_to) |
| |
|
| | |
| | if (lnum_from > lnum_to) or (lnum_from == lnum_to and col_from > col_to) then |
| | return |
| | end |
| |
|
| | |
| | |
| | |
| | toggle_lines(lnum_from, lnum_to, vim.api.nvim_win_get_cursor(0)) |
| | return '' |
| | end |
| |
|
| | |
| | local function textobject() |
| | local lnum_cur = vim.fn.line('.') |
| | local parts = get_comment_parts({ lnum_cur, vim.fn.col('.') }) |
| | local comment_check = make_comment_check(parts) |
| |
|
| | if not comment_check(vim.fn.getline(lnum_cur)) then |
| | return |
| | end |
| |
|
| | |
| | local lnum_from = lnum_cur |
| | while (lnum_from >= 2) and comment_check(vim.fn.getline(lnum_from - 1)) do |
| | lnum_from = lnum_from - 1 |
| | end |
| |
|
| | local lnum_to = lnum_cur |
| | local n_lines = vim.api.nvim_buf_line_count(0) |
| | while (lnum_to <= n_lines - 1) and comment_check(vim.fn.getline(lnum_to + 1)) do |
| | lnum_to = lnum_to + 1 |
| | end |
| |
|
| | |
| | vim.cmd('normal! ' .. lnum_from .. 'GV' .. lnum_to .. 'G') |
| | end |
| |
|
| | return { operator = operator, textobject = textobject, toggle_lines = toggle_lines } |
| |
|