add Comment pre_hook to calculate commentstring in jsx/tsx

This commit is contained in:
2022-02-17 16:32:13 +03:00
parent 06b7d36896
commit 1bb8d44efb
2 changed files with 26 additions and 6 deletions

View File

@@ -28,12 +28,7 @@ require("packer").startup(function(use)
use({ "folke/tokyonight.nvim" })
-- Make commenting code great
use({
"numToStr/Comment.nvim",
config = function()
require("Comment").setup()
end,
})
use({ "numToStr/Comment.nvim" })
-- Icons used by many plugins
use({ "kyazdani42/nvim-web-devicons" })
@@ -144,6 +139,7 @@ require("user.treesitter")
require("user.telescope")
require("user.git")
require("user.lsp")
require("user.comment")
require("user.snippets")
require("user.lualine")
require("user.remaps")

View File

@@ -0,0 +1,24 @@
require("Comment").setup({
pre_hook = function(ctx)
-- Only calculate commentstring for tsx filetypes
if vim.bo.filetype == "typescriptreact" or vim.bo.filetype == "javascriptreact" then
local U = require("Comment.utils")
-- Detemine whether to use linewise or blockwise commentstring
local type = ctx.ctype == U.ctype.line and "__default" or "__multiline"
-- Determine the location where to calculate commentstring from
local location = nil
if ctx.ctype == U.ctype.block then
location = require("ts_context_commentstring.utils").get_cursor_location()
elseif ctx.cmotion == U.cmotion.v or ctx.cmotion == U.cmotion.V then
location = require("ts_context_commentstring.utils").get_visual_start_location()
end
return require("ts_context_commentstring.internal").calculate_commentstring({
key = type,
location = location,
})
end
end,
})