terrortylor / nvim-comment

A comment toggler for Neovim, written in Lua
MIT License
479 stars 23 forks source link

Set marker_padding dynamically #36

Closed Markus00000 closed 2 years ago

Markus00000 commented 3 years ago

I’m using padded and unpadded comments. Having a way to create mappings for each would be useful. Ideas:

terrortylor commented 2 years ago

The config is exposed, so you can modify the marker_padding value, comment the line, then restore:

local function noPad()
  local comment_opts = require('nvim_comment').config
  print("marker_padding: ", comment_opts.marker_padding)
  local old = comment_opts.marker_padding
  comment_opts.marker_padding = false
  print("marker_padding: ", comment_opts.marker_padding)
  comment_opts.marker_padding = true
end
Markus00000 commented 2 years ago

Thank you!

I ended up creating a toggle function and mappings:

function _G.padding_toggle()
  require('nvim_comment').config.marker_padding = not require('nvim_comment').config.marker_padding
end

vim.api.nvim_set_keymap('n', '<Leader>k', ':lua padding_toggle(); vim.api.nvim_command("CommentToggle"); padding_toggle()<CR>', {noremap = true, silent = true})
vim.api.nvim_set_keymap('v', '<Leader>k', ':lua padding_toggle(); vim.api.nvim_command("\'<,\'>CommentToggle"); padding_toggle()<CR>', {noremap = true, silent = true})

(I wasn’t sure how to pass visual selection line numbers from mappings to Lua functions or how to get them directly in Lua.)