neovim / neovim

Vim-fork focused on extensibility and usability
https://neovim.io
Other
82.56k stars 5.65k forks source link

built-in commenting does not use whitespaces depending on 'commentstring' #28658

Closed umlx5h closed 5 months ago

umlx5h commented 5 months ago

Problem

The built-in comment function was added in #28176, but I have some concerns that I would like to report.

If there are no spaces in the 'commentstring', there will be no spaces in the comment.

This behavior of no whitespaces seems weird, since commenting in VSCode for example, always results in with whitespaces. Maybe almost anybody wants to use comment with whitespaces.

A easy examination of the default file type plugin shows that 61 filetypes do not use whitespaces. Users who want to include spaces need to modify the 'commentstring' of these 60 filetypes, which is very cumbersome.

$ grep -P 'set.* commentstring=' -R /usr/share/nvim/runtime/ftplugin | grep -v ' %s' | cut -d: -f 1 | sort | uniq | grep -oP '\w+(?=.vim)' | tr '\n' ' '
abaqus asm astro bitbake cgdbrc cobol csh cvsrc deb822sources debsources desktop dtd dtrace erlang eruby fennel fish fstab gdb groovy group hamster html javascript java jq jsonc json ld liquid lisp markdown mma modula2 modula3 nroff obse ocaml odin openvpn pascal passwd pdf perl php ps1 ps1xml qml racket raku rust scdoc scheme tcl text typescript ftplugin vim vroom wat xml 

Two other well-known plugins are listed below, both of them have the behavior of inserting spaces.

https://github.com/tpope/vim-commentary https://github.com/numToStr/Comment.nvim

It seems to me that either action is needed to resolve this issue.

  1. Put whitespaces to 'commentstring' in the all filetype plugin configs
  2. Change the behavior of built-in commenting

1 is more flexible in configuration, but requires modification of many filetype plugins.

Steps to reproduce

javascript

$ echo 'const foo = 1' | nvim --clean -c 'set filetype=javascript' -
> press gcc
//const foo = 1

html

$ echo '<html></html>' | nvim --clean -c 'set filetype=html' -
> press gcc
<!--<html></html>-->

Expected behavior

javascript

// const foo = 1

html

<!-- <html></html> -->

The following two plugins have this behavior. https://github.com/tpope/vim-commentary https://github.com/numToStr/Comment.nvim

Neovim version (nvim -v)

0.10.0 commit 0323ea8664fa85935b5b58ae251af0d5b986cbd9

Vim (not Nvim) behaves the same?

no

Operating system/version

ubuntu 22

Terminal name/version

wezterm

$TERM environment variable

tmux-256color

Installation

build from repo

clason commented 5 months ago

If there are no spaces in the 'commentstring', there will be no spaces in the comment.

This is intentional. Feel free to contact the filetype plugin maintainers to change this in Vim.

umlx5h commented 5 months ago

Okay, I'll use another plugin since it doesn't seem to solve the problem. I just don't think it is appropriate as the default behavior.

clason commented 5 months ago

It is exactly because it's default behavior that this is appropriate. It's supposed to be a minimal implementation leveraging existing builtin functionality, not being a VS Code kitchen sink. And, yes, using a more full-featured plugin because you need the additional functionality is perfectly fine (and also intended use).

umlx5h commented 5 months ago

It is exactly because it's default behavior that this is appropriate.

it may be appropriate in terms of implementation, but it is not appropriate when looking at just the function of commenting. When considered from the user's perspective.

VSCode is just an example of this. If you know an editor that does not use spaces, please let me know.

I understand that the filetype plugin is managed by another people and there is nothing you can do about it.

clason commented 5 months ago

When considered from the user's perspective.

That is not the only relevant perspective, though, and this was chosen as a deliberate compromise.

I understand that the filetype plugin is managed by another people and there is nothing you can do about it.

But you (who clearly care about this) can: contact the maintainer and ask them to add spaces. If they do, the changes will end up here and make you happy.

BoltsJ commented 4 months ago

FWIW, the built in Vim comment plugin puts in the spaces, so nvim's behavior here differs from upstream Vim.

mkalinski commented 4 months ago

In case someone finds this having the same issue: if you want to have spaces in commentstring for every filetype without having to contact maintainers for every odd ftplugin, it can be done quite easily with an autocommand:

vim.api.nvim_create_autocmd('FileType', {
    group = vim.api.nvim_create_augroup('commentstring_fix', {}),
    callback = function(ctx)
        local cms_value = vim.api.nvim_get_option_value(
            'commentstring',
            {buf = ctx.buf}
        )

        -- Note that this is the literal '%s'.
        -- All other '%s' in this function are patterns.
        local s_start, s_end = string.find(cms_value, '%s', 1, true)

        if s_start == nil then
            return
        end

        local s_before = s_start - 1
        local s_after = s_end + 1
        local s_prepend = ''
        local s_append = ''

        if s_start > 1
            and string.find(
                string.sub(cms_value, s_before, s_before),
                '%s'
            ) == nil
        then
            s_prepend = ' '
        end

        if s_end < #cms_value
            and string.find(
                string.sub(cms_value, s_after, s_after),
                '%s'
            ) == nil
        then
            s_append = ' '
        end

        if s_prepend ~= '' or s_append ~= '' then
            vim.api.nvim_set_option_value(
                'commentstring',
                table.concat{
                    string.sub(cms_value, 1, s_before),
                    s_prepend,
                    '%s',
                    s_append,
                    string.sub(cms_value, s_after)
                },
                {buf = ctx.buf}
            )
        end
    end
})