Coder Social home page Coder Social logo

nvim-comment-frame's Introduction

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,
        },
    }
})

nvim-comment-frame's People

Contributors

licf01 avatar n3wborn avatar s1n7ax avatar xfdj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

xfdj

nvim-comment-frame's Issues

for_each_child deprecated

The for_each_child is now deprecated (neovim/neovim#25118) in the latest Neovim and causes an error.

   Warn  06:15:45 PM notify.warn LanguageTree:for_each_child() is deprecated, use LanguageTree:children() instead. :help deprecated
This feature will be removed in Nvim version 0.11
   Warn  06:15:45 PM notify.warn stack traceback:
	...s/share/nvim/runtime/lua/vim/treesitter/languagetree.lua:461: in function 'for_each_child'
	.../lazy/nvim-comment-frame/lua/nvim-comment-frame/util.lua:218: in function 'get_curr_lang_stack_for_position'
	.../lazy/nvim-comment-frame/lua/nvim-comment-frame/init.lua:44: in function 'get_comment'
	.../lazy/nvim-comment-frame/lua/nvim-comment-frame/init.lua:82: in function 'add_comment'
	[string ":lua"]:1: in main chunk

root_parser:for_each_child(function(tree, _)

ref: neovim/neovim#25154

Feature request: Single-line separators

Possible feature to be able to add something like:
/* ------------------------- Comment ------------------------ */
and
/* ---------------------------------------------------------- */

start_str not properly set on lines starting and ending comment block ... observed in python and lua

  • Running Neovim 0.8.0 nightly build, LSP
  • my nvim-comment-frame config:
comment_frame.setup({
  disable_default_keymap = true,
  frame_width = 90,
  line_wrap_len = 79,
  languages = {
    python = {
      frame_width = 61,
      line_wrap_len = 52,
      start_str = '# test #',
    },
    lua = {
      start_str = '---test #'
    }
  }
})
  • start_str only seems to be applied to lines in the block, having actual comments, lines starting and ending comment block do not get value on start_str
  • On python: start_str is not properlly implemented, causing this linting warning as comment line should start with #<space>
  • In Lua, we get something similar: config above generates this comment block:
---test-#---------------------------------------------------------------------------------
---test #                         This is a test comment block                          --
---test-#---------------------------------------------------------------------------------

setup function not executed?

Great plugin!
I'm having a problem, I'd appreciate your direction troubleshooting it.

I'm trying to override your plugin's maps by first disabling built-in map. I'm using Packer to manage plugins so I'm trying this:

  use {
    's1n7ax/nvim-comment-frame',
    requires = {
      { 'nvim-treesitter' }
    },
    config = function() require('nvim-comment-frame').setup({
        disable_default_keymap = true,
      })
    end,
  }

... Yet, I still see the default map ... what I'm doing wrong? Configuration via setup() function seems simple ... please help and thanks again.

Some other data:

  • OS: macos
  • nvim:
:version
NVIM v0.8.0-dev-1117-g7bd4c8e8e
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compilation: /Applications/Xcode_13.2.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE
=1 -DNVIM_TS_HAS_SET_MATCH_LIMIT -DNVIM_TS_HAS_SET_ALLOCATOR -O2 -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99
-Wshadow -Wconversion -Wdouble-promotion -Wmissing-noreturn -Wmissing-format-attribute -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-
protector-strong -fno-common -fdiagnostics-color=always -DINCLUDE_GENERATED_DECLARATIONS -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DM
IN_LOG_LEVEL=3 -I/Users/runner/work/neovim/neovim/build/cmake.config -I/Users/runner/work/neovim/neovim/src -I/Users/runner/work/neovim/neovim/.d
eps/usr/include -I/Applications/Xcode_13.2.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include -I/Librar
y/Frameworks/Mono.framework/Headers -I/Users/runner/work/neovim/neovim/build/src/nvim/auto -I/Users/runner/work/neovim/neovim/build/include
Compiled by [email protected]

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/share/nvim"

feat: add tabs/spaces to align last chars to frame width

Hi ! Thanks for your work on this project. If find this really awesome to have so many plugins available in vim-world.

I was about to say that last characters should be aligned but I wonder if this is not just github markdown rendering problem...

Manually wrap line after detecting a special character

Wonderful project. The line wrap is really what was missing from other similar plugins.

It would be nice if we could also manually cut lines at a desired length instead of solely relying on line wrap after X characters. For example, the string

Hi, this is indeed a very long string in which i will put a\nnewline char. It is obviously more than 80 characters!

will render like this:

#------------------------------------------------------------------------------#
#  Hi, this is indeed a very long string in which i will put a\nnewline char.  #
#                   It is obviously more than 80 characters!                   #
#------------------------------------------------------------------------------#

But it could add a newline when it detects a \n, so it would render like that:

#------------------------------------------------------------------------------#
#          Hi, this is indeed a very long string in which i will put a         #
#            newline char. It is obviously more than 80 characters!            #
#------------------------------------------------------------------------------#

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.