Closed umlx5h closed 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.
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.
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).
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.
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.
FWIW, the built in Vim comment plugin puts in the spaces, so nvim's behavior here differs from upstream Vim.
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
})
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.
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 is more flexible in configuration, but requires modification of many filetype plugins.
Steps to reproduce
javascript
html
Expected behavior
javascript
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