Skip to content

Commit

Permalink
Add CommentOut/Uncomment commands
Browse files Browse the repository at this point in the history
  • Loading branch information
slarse committed Feb 28, 2024
1 parent a17eda4 commit fe1ab5b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions nvim/lua/slarse/comments.lua
@@ -0,0 +1,31 @@
local non_c_line_comments_by_filetype = {
lua = "--",
python = "#",
sql = "--",
}

local function comment_out(opts)
local line_comment = non_c_line_comments_by_filetype[vim.bo.filetype] or "//"
local start = math.min(opts.line1, opts.line2)
local finish = math.max(opts.line1, opts.line2)

vim.api.nvim_command(start .. "," .. finish .. "s:^:" .. line_comment .. ":")
vim.api.nvim_command("noh")
end

local function uncomment(opts)
local line_comment = non_c_line_comments_by_filetype[vim.bo.filetype] or "//"
local start = math.min(opts.line1, opts.line2)
local finish = math.max(opts.line1, opts.line2)

pcall(vim.api.nvim_command, start .. "," .. finish .. "s:^\\(\\s\\{-\\}\\)" .. line_comment .. ":\\1:")
vim.api.nvim_command("noh")
end

vim.api.nvim_create_user_command("CommentOut", comment_out, { range = true })
vim.keymap.set("v", "<leader>co", ":CommentOut<CR>")
vim.keymap.set("n", "<leader>co", ":CommentOut<CR>")

vim.api.nvim_create_user_command("Uncomment", uncomment, { range = true })
vim.keymap.set("v", "<leader>uc", ":Uncomment<CR>")
vim.keymap.set("n", "<leader>uc", ":Uncomment<CR>")
1 change: 1 addition & 0 deletions nvim/lua/slarse/init.lua
@@ -1,2 +1,3 @@
require('slarse.remap')
require('slarse.set')
require('slarse.comments')

0 comments on commit fe1ab5b

Please sign in to comment.