Skip to content

Detects the language using treesitter and adds a comment block

License

Notifications You must be signed in to change notification settings

s1n7ax/nvim-comment-frame

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nvim-comment-frame

Basically when you give it some text it creates a comment frame like below.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~                   nvim-comment-frame                   ~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Features

  • Language detection
  • Wrap lines
  • Auto indent

Language detection

  • Detects the current language using treesitter and changes the comment string based on the language.

JavaScript

//--------------------------------------------------------//
//       JavaScript, often abbreviated as JS, is a        //
//       programming language that conforms to the        //
//                ECMAScript specification                //
//--------------------------------------------------------//
var fs = require('fs');
var path = require('path');

var BUFFER = bufferFile('../public/data.png');

function bufferFile(relPath) {
    return fs.readFileSync(path.join(__dirname, relPath));
}

Bash

#----------------------------------------------------------#
#    Bash is a Unix shell and command language written     #
#                       by Brian Fox                       #
#----------------------------------------------------------#
for i in $(seq 1 10);
do
    echo $i
done

Lua

--********************************************************--
--    Lua is a lightweight, high-level, multi-paradigm    --
--      programming language designed primarily for       --
--              embedded use in applications              --
--********************************************************--
function String.trim(str)
    return str:gsub('^%s+', ''):gsub('%s+$', '')
end

Wrap lines

CSS

/*------------------------------------------------------------------*/
/*         Cascading Style Sheets is a style sheet language         */
/*        used for describing the presentation of a document        */
/*        written in a markup language such as HTML. CSS is         */
/*         a cornerstone technology of the World Wide Web,          */
/*                  alongside HTML and JavaScript.                  */
/*------------------------------------------------------------------*/
body, h1 {
    font-family: arial,sans-serif;
    font-size: 14px;
}

Auto indent

  • auto_indent is set to true by default. When this option is on, plugin will use treesitter to get the indentation of the current line and indent the comment
  • Based on the expandtab option, plugin will use tab or space for indentation
  • You can turn off this globally or just for a language by auto_indent = false
detachstack(Client *c)
{
    Client **tc, *t;

    for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
    *tc = c->snext;
    //--------------------------------------------------------//
    //      C is a general-purpose, procedural computer       //
    //       programming language supporting structured       //
    //        programming, lexical variable scope, and        //
    //          recursion, with a static type system          //
    //--------------------------------------------------------//

    if (c == c->mon->sel) {
        for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
        c->mon->sel = t;
    }
}

Install

Packer

  • Add the plugin to the configuration
use { 
    's1n7ax/nvim-comment-frame',
    requires = {
        { 'nvim-treesitter' }
    },
    config = function()
        require('nvim-comment-frame').setup()
    end
}
  • Install the plugin
:PackerInstall
  • Compile the packages
:PackerCompile

Keymaps

Following are the default keymaps.

  • <leader>cf - Single line comment
  • <leader>cm - Multiline comment

Adding custom keymaps

require('nvim-comment-frame').setup({
    keymap = '<leader>cc',
    --       ^^^^^^^^^^^ change this to what ever you want

    multiline_keymap = '<leader>C',
    --                 ^^^^^^^^^^^ change this to what ever you want
})

Adding keymaps manually

Lua

vim.api.nvim_set_keymap('n', '<leader>cc', ":lua require('nvim-comment-frame').add_comment()<CR>", {})
--                           ^^^^^^^^^^^ change this to what ever you want

vim.api.nvim_set_keymap('n', '<leader>C', ":lua require('nvim-comment-frame').add_multiline_comment()<CR>", {})
--                           ^^^^^^^^^^^ change this to what ever you want

Vimscript

nmap <leader>cc :lua require('nvim-comment-frame').add_comment()<CR>
"   ^^^^^^^^^^^ change this to what ever you want

nmap <leader>C :lua require('nvim-comment-frame').add_multiline_comment()<CR>
"   ^^^^^^^^^^^ change this to what ever you want

Configurations

Common configurations

Except disable_default_keymap and keymap, all the other properties can be overridden for individual language.

Following are the general configurations with default values.

require('nvim-comment-frame').setup({

    -- if true, <leader>cf keymap will be disabled
    disable_default_keymap = false,

    -- adds custom keymap
    keymap = '<leader>cc',
    multiline_keymap = '<leader>C',

    -- start the comment with this string
    start_str = '//',

    -- end the comment line with this string
    end_str = '//',

    -- fill the comment frame border with this character
    fill_char = '-',

    -- width of the comment frame
    frame_width = 70,

    -- wrap the line after 'n' characters
    line_wrap_len = 50,

    -- automatically indent the comment frame based on the line
    auto_indent = true,

    -- add comment above the current line
    add_comment_above = true,

    -- configurations for individual language goes here
    languages = {
    }
})

Language specific configurations

require('nvim-comment-frame').setup({
    languages = {
        -- configuration for Lua programming language
        -- @NOTE global configuration will be overridden by language level
        -- configuration if provided
        lua = {
            -- start the comment with this string
            start_str = '--[[',

            -- end the comment line with this string
            end_str = ']]--',

            -- fill the comment frame border with this character
            fill_char = '*',

            -- width of the comment frame
            frame_width = 100,

            -- wrap the line after 'n' characters
            line_wrap_len = 70,

            -- automatically indent the comment frame based on the line
            auto_indent = false,

            -- add comment above the current line
            add_comment_above = false,
        },
    }
})

About

Detects the language using treesitter and adds a comment block

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages