Skip to content

Commit

Permalink
feat: Added block comments
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasTavaresA committed Mar 19, 2023
1 parent 27e0b57 commit 6a00aba
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SingleComment.nvim

Super simple comment plugin that always uses single line comments
Comment using only single line comments.

## Contents

Expand All @@ -13,20 +13,22 @@ Super simple comment plugin that always uses single line comments

1. Supports
- counts like 5{comment} and dotrepeat
- commenting in front of the current line
- commenting in front of the current line, and start a comment on empty lines
- toggling a comment in front/top of the current line, [preview](#commentahead)
- block comments in case you really need them, and removing outermost block on cursor,
[preview](#blockcomment)

2. Simplest plugin of them all **~150 loc** in a single file
2. Simplest of them all **~270 loc** in a single file

3. Compatible with [nvim-ts-context-commentstring](https://github.com/JoosepAlviste/nvim-ts-context-commentstring), turns its results into single line comments

4. Single line comments avoid unexpected results when commenting:
- uncomments only when all the text selected is commented, avoiding confusion
when getting big blocks of code out of the way for debugging
- always comments at the most shallow comment avoiding weird indentation
issues
- always comments at the most shallow comment to make maintaining different levels of
commented code easier
- when you have block comments at the end of lines other plugins fail on
the simple task of getting this line out of the way, this plugin will **never** fail
the simple task of getting this line out of the way, this plugin should **never** fail

## Installation

Expand All @@ -48,7 +50,7 @@ use {

## Keybindings

You need to set those to use SingleComment.nvim
There is no keybindings by default.

Those are all the available functions:

Expand All @@ -61,6 +63,8 @@ vim.keymap.set("v", "gcc", require("SingleComment").Comment, {})
vim.keymap.set("n", "gca", require("SingleComment").ToggleCommentAhead, {})
-- comments ahead of the current line
vim.keymap.set("n", "gcA", require("SingleComment").CommentAhead, {})
-- comment a block, and removes the outermost block comment in normal mode
vim.keymap.set({ "n", "v" }, "gcb", require("SingleComment").BlockComment)
```

## Lazy load it
Expand Down Expand Up @@ -88,6 +92,7 @@ Those commands substitute all the above
vim.keymap.set("v", "gcc", require("SingleComment").Comment, {})
vim.keymap.set("n", "gca", require("SingleComment").ToggleCommentAhead, {})
vim.keymap.set("n", "gcA", require("SingleComment").CommentAhead, {})
vim.keymap.set({ "n", "v" }, "gcb", require("SingleComment").BlockComment)
end
}
```
Expand All @@ -107,10 +112,15 @@ Those commands substitute all the above
vim.keymap.set("v", "gcc", require("SingleComment").Comment, {})
vim.keymap.set("n", "gca", require("SingleComment").ToggleCommentAhead, {})
vim.keymap.set("n", "gcA", require("SingleComment").CommentAhead, {})
vim.keymap.set({ "n", "v" }, "gcb", require("SingleComment").BlockComment)
end,
},
```

## CommentAhead

[![asciicast](https://asciinema.org/a/NAIVgm9maDJ5QN2gfrehAaVyA.svg)](https://asciinema.org/a/NAIVgm9maDJ5QN2gfrehAaVyA)
[![asciicast](https://asciinema.org/a/jChjT4OZBaj7WsKWsAWiKsI3F.svg)](https://asciinema.org/a/jChjT4OZBaj7WsKWsAWiKsI3F)

## BlockComment

[![asciicast](https://asciinema.org/a/2VtZyh0Q3Nb5Eytwo0RSp5c2G.svg)](https://asciinema.org/a/2VtZyh0Q3Nb5Eytwo0RSp5c2G)
68 changes: 68 additions & 0 deletions lua/SingleComment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,72 @@ function M.Comment()
vim.api.nvim_win_set_cursor(winnr, { sr, col })
end

function M.BlockComment()
local bufnr = vim.api.nvim_get_current_buf()
local mode = vim.fn.mode()
local comment = GetComment("block")
local _, sr, sc, _ = unpack(vim.fn.getpos("."))
local _, er, ec, _ = unpack(vim.fn.getpos("v"))
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)

if mode == "v" or mode == "V" then
-- keep start/end in the right place in reverse selection
if sr > er then
sr, er = er, sr
sc, ec = ec, sc
end

-- get all the line in visual line mode
if mode == "V" then
sc, ec = 1, 999
end

if sr == er then
-- cursor in the same line
-- in case of reversed column
if sc > ec then
sc, ec = ec, sc
end

lines[sr] = lines[sr]:sub(1, sc - 1)
.. " "
.. comment[1]
.. lines[sr]:sub(sc, ec):gsub("^%s+", "")
.. comment[2]
.. (#lines[sr]:sub(ec + 1) > 0 and " " .. lines[er]:sub(ec + 1) or "")
else
-- cursor in separate lines
lines[sr] = lines[sr]:sub(1, sc - 1)
.. " "
.. comment[1]
.. lines[sr]:sub(sc):gsub("^%s+", "")

lines[er] = lines[er]:sub(1, ec)
.. comment[2]
.. (#lines[er]:sub(ec + 1) > 0 and " " .. lines[er]:sub(ec + 1) or "")
end

vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
vim.api.nvim_feedkeys("=", "n", false)
else
-- uncomment outermost comment
comment = { vim.pesc(comment[1]), vim.pesc(comment[2]) }

for i = sr, 1, -1 do
if lines[i]:find(comment[1]) then
lines[i] = lines[i]:gsub("%s?" .. comment[1], "")

for j = sr, #lines do
if lines[j]:find(comment[2]) then
lines[j] = lines[j]:gsub(comment[2] .. "%s?", "")

vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
return
end
end
end
end
end
end

return M
10 changes: 10 additions & 0 deletions tests/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,15 @@ <h1>comment/uncomment a single line</h1>
<!-- this should indent properly -->
</div>

<!-- Tests for Block comments -->
<!-- Test everything in both normal and reverse selection, and test uncommenting -->
<!-- Try visual line commenting -->
<!-- Try commenting on the same line -->
<!-- In multiline selections, Try commenting in a point before the starting position of the selection -->

<!-- (.) (.) (.) -->
<!-- (.) (.) (.) -->
<!-- (.) (.) -->

<!-- also test for line comments on `./tests/test.lua` -->
<!-- vim: set nofoldenable: -->
10 changes: 10 additions & 0 deletions tests/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,15 @@ function abc()
-- this should indent properly
end

--- Tests for Block comments
-- Test everything in both normal and reverse selection, and test uncommenting
-- Try visual line commenting
-- Try commenting on the same line
-- In multiline selections, Try commenting in a point before the starting position of the selection

-- (.) (.) (.)
-- (.) (.) (.)
-- (.) (.)

--- also test for block comments on `./tests/test.html`
-- vim: set nofoldenable:

0 comments on commit 6a00aba

Please sign in to comment.