LucasTavaresA/SingleComment.nvim

github github
editing-supportcomment
stars 7
issues 1
subscribers 2
forks 1
CREATED

2023-01-08

UPDATED

2 months ago


SingleComment.nvim

Always single line, comment sensitive, indentation preserving commenting.

Turn on notifications in Breaking Changes if using this plugin.

Contents

Features

  1. Simple and stupid, just complain and i will change some lua tables around and we are ready to work

  2. Supports

    • counts like 5{comment} and dotrepeat
    • 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
    • block comments in case you really need them, and removing innermost block on cursor, preview
  3. Simplest of them all ~270 loc in a single file

  4. It uses nvim-ts-context-commentstring automatically if available them tries to turn all results into line comments

  5. 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 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 should never fail

Installation

packer.nvim:

use {
  "lucastavaresa/SingleComment.nvim",
}

lazy.nvim:

{
  "lucastavaresa/SingleComment.nvim",
}

Keybindings

There is no keybindings by default.

Those are all the available functions:

-- comments the current line, or a number of lines 5gcc
vim.keymap.set("n", "gcc", require("SingleComment").SingleComment, { expr = true })
-- comments the selected lines
vim.keymap.set("v", "gcc", require("SingleComment").Comment, {})
-- toggle a comment top/ahead of the current line
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 innermost block comment in normal mode
vim.keymap.set({ "n", "v" }, "gcb", require("SingleComment").BlockComment)

Utilities

GetComment()

Get a table with comment beginning and end

Improved by ts-context-commentstring but also works without it automatically, them gets single-lined/tweaked by this plugin custom tables

Also updates the commentstring

Useful for custom utility functions that need accurate comment detection

-- you can pass "block", and it will *try* to get block comments
local comment = require("SingleComment").GetComment()

Lazy load it

Those commands substitute all the above

  • with packer i did not test this, please inform me if it works :)
  use {
    "lucastavaresa/SingleComment.nvim",
    opt = true,
    keybindings = { { { "n", "v" }, "gcc" }, { "n", "gca" } },
    requires = {
      "nvim-treesitter/nvim-treesitter",
      "JoosepAlviste/nvim-ts-context-commentstring"
    },
    setup = function()
      vim.keymap.set(
        "n",
        "gcc",
        require("SingleComment").SingleComment,
        { expr = true }
      )
      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
  }
  • with lazy:
  {
    "lucastavaresa/SingleComment.nvim",
    lazy = true,
    dependencies = {
      "nvim-treesitter/nvim-treesitter",
      "JoosepAlviste/nvim-ts-context-commentstring"
    },
    init = function()
      vim.keymap.set("n", "gcc", require("SingleComment").SingleComment, { expr = true })
      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

BlockComment

asciicast

Other Comment plugins