local M = {} M.exec = function(cmd) return io.popen(cmd):read'*l' end local function find_sub(s, replacement, pattern, start, special) local x1, x2 = s:find(pattern, start, special) if x1 then return s:sub(1, x1-1) .. replacement .. s:sub(x2+1), x1, x2 end return s end -- Only works with prefix comments. local function get_line_comment() local cms = vim.bo.commentstring -- This whole pesc dance is a bummer. local pattern = '^(%s*)' .. (find_sub(vim.pesc((find_sub(cms, '\0', '%s', 1, true))), '(%s*).*', '\0', 1, true)) local pre_ws, inner_ws = vim.api.nvim_get_current_line():match(pattern) if pre_ws then return pre_ws .. cms:format('') .. inner_ws end return '' end M.comment = function(str) -- add space local prefix = vim.bo.commentstring:format(''):gsub('%S$', '%0 ') local comment = get_line_comment() if comment ~= '' then prefix = '' end return prefix .. str end return M