JoosepAlviste / nvim-ts-context-commentstring

Neovim treesitter plugin for setting the commentstring based on the cursor location in a file.
MIT License
1.16k stars 34 forks source link

Can't make it work with Vue, Comment.nvim and lazy.nvim #62

Closed n10v closed 1 year ago

n10v commented 1 year ago

Hey @JoosepAlviste! Thanks for this great plugin, but I can't make work with Vue. When I enter gcc, I always get <!-- --> comment regardless of location of my cursor:

Screenshot 2023-04-16 at 16 18 21

My init.lua config:

-- Set <space> as the leader key
-- See `:help mapleader`
--  NOTE: Must happen before plugins are required (otherwise wrong leader will be used)
vim.g.mapleader = ' '
vim.g.maplocalleader = ' '

-- Install package manager
--    https://github.com/folke/lazy.nvim
--    `:help lazy.nvim.txt` for more info
local lazypath = vim.fn.stdpath 'data' .. '/lazy/lazy.nvim'
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system {
    'git',
    'clone',
    '--filter=blob:none',
    'https://github.com/folke/lazy.nvim.git',
    '--branch=stable', -- latest stable release
    lazypath,
  }
end
vim.opt.rtp:prepend(lazypath)

-- NOTE: Here is where you install your plugins.
--  You can configure plugins using the `config` key.
--
--  You can also configure plugins after the setup call,
--    as they will be available in your neovim runtime.
require('lazy').setup({
  { -- Theme inspired by Atom
    'navarasu/onedark.nvim',
    cond = vim.g.vscode == nil,
    priority = 1000,
    config = function()
      vim.cmd.colorscheme 'onedark'
    end,
  },

  { -- Highlight, edit, and navigate code
    'nvim-treesitter/nvim-treesitter',
    dependencies = {
      'nvim-treesitter/nvim-treesitter-textobjects',
    },
    build = ":TSUpdate",
  },

  -- "gc" to comment visual regions/lines
  {
    'numToStr/Comment.nvim',
    opts = function() return { pre_hook = require('ts_context_commentstring.integrations.comment_nvim').create_pre_hook() } end,
    dependencies = { 'JoosepAlviste/nvim-ts-context-commentstring' },
  },

  -- NOTE: Next Step on Your Neovim Journey: Add/Configure additional "plugins" for kickstart
  --       These are some example plugins that I've included in the kickstart repository.
  --       Uncomment any of the lines below to enable them.
  -- require 'kickstart.plugins.autoformat',
  -- require 'kickstart.plugins.debug',
  -- NOTE: The import below automatically adds your own plugins, configuration, etc from `lua/custom/plugins/*.lua`
  --    You can use this folder to prevent any conflicts with this init.lua if you're interested in keeping
  --    up-to-date with whatever is in the kickstart repo.
  --
  --    For additional information see: https://github.com/folke/lazy.nvim#-structuring-your-plugins
  --
  --    An additional note is that if you only copied in the `init.lua`, you can just comment this line
  --    to get rid of the warning telling you that there are not plugins in `lua/custom/plugins/`.
  { import = 'custom.plugins' },
}, {})

-- [[ Setting options ]]
-- See `:help vim.o`

vim.o.breakindent = true -- Enable break indent
vim.o.clipboard = 'unnamedplus' -- Use the OS clipboard by default
vim.o.encoding = 'utf-8'
vim.o.ff = 'unix' -- Unix end of file
vim.o.hlsearch = true -- Set highlight on search
vim.o.ignorecase = true -- Search case insensitive...
vim.o.smartcase = true  -- ... but only when it does not begin with upper case
vim.o.incsearch = true -- Shows the match while typing
vim.o.noswapfile = true -- No swp files
vim.o.shiftwidth = 2 -- Number of spaces inserted for indentation
vim.o.softtabstop = 2 -- Number of columns that will be added when you hit Tab in insert mode
vim.o.splitright = true -- Puts new vsplit windows to the right of the current
vim.o.splitbelow = true -- Puts new split windows to the bottom of the current
vim.o.undofile = true -- Save undo history

if vim.g.vscode == nil then
  vim.o.autoindent = true
  vim.o.expandtab = true -- Insert space characters whenever the tab key is pressed
  vim.o.mouse = 'a' -- Enable mouse mode
  vim.o.number = true -- Make line numbers default
  vim.o.relativenumber = true
  vim.o.tabstop = 2 -- Number of columns a tab counts for
  vim.o.termguicolors = true
end

-- Decrease update time
vim.o.updatetime = 250
vim.o.timeout = true
vim.o.timeoutlen = 300

-- Delete all trailing whitespaces on save
if vim.g.vscode == nil then
  vim.o.showbreak = "\\ \\ \\"
  vim.api.nvim_create_autocmd("BufWritePre", {
      pattern = { "*" },
      command = ":%s/\\s\\+$//e",
  })
end

-- [[ Basic Keymaps ]]

-- Keymaps for better default experience
-- See `:help vim.keymap.set()`
vim.keymap.set({ 'n', 'v' }, '<Space>', '<Nop>', { silent = true })

-- Remap for dealing with word wrap
vim.keymap.set('n', 'k', "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true })
vim.keymap.set('n', 'j', "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })

-- [[ Highlight on yank ]]
-- See `:help vim.highlight.on_yank()`
local highlight_group = vim.api.nvim_create_augroup('YankHighlight', { clear = true })
vim.api.nvim_create_autocmd('TextYankPost', {
  callback = function()
    vim.highlight.on_yank()
  end,
  group = highlight_group,
  pattern = '*',
})

-- [[ Configure Treesitter ]]
-- See `:help nvim-treesitter`
require'nvim-treesitter.configs'.setup {
  context_commentstring = {
    enable = true,
    enable_autocmd = false,
  },

  -- Add languages to be installed here that you want installed for treesitter
  ensure_installed = { 'go', 'lua',  'tsx', 'typescript', 'vimdoc', 'vue', 'vim' },
}

NVIM v0.9.0

Would be great if you can help me 🙏

n10v commented 1 year ago

Just found the issue thanks to your comment here: https://github.com/JoosepAlviste/nvim-ts-context-commentstring/issues/18#issuecomment-886105815

I decreased updatetime to 100 and it started to work 🎉

n10v commented 1 year ago

Maybe we should mention it more explicitly in README? It could save somebody else couple of hours 😅

n10v commented 1 year ago

It worked with Vue files with script lang=ts, but it didn't work for script lang=js and style lang=scss. Adding 'scss' and 'javascript' to ensure_installed fixed this issue:

require'nvim-treesitter.configs'.setup {
  context_commentstring = {
    enable = true,
    enable_autocmd = false,
  },

  -- Add languages to be installed here that you want installed for treesitter
  ensure_installed = { 'go', 'lua',  'tsx', 'javascript', 'scss', 'typescript', 'vimdoc', 'vue', 'vim' },
}

Would be great to mentioning in README too 🙏

JoosepAlviste commented 1 year ago

Hey! Thanks for the ideas!

I've been meaning to take another look at the docs, trying to make the readme simpler and providing the more detailed configuration information in the Vim help docs. I also wanted to move the integrations section to the wiki, and to have an issue template with the minimal config to make it easier for users to report bugs.

Anyways, this encourages me to get started on these improvements, so thanks!

JoosepAlviste commented 1 year ago

I set up a PR with some improvements: https://github.com/JoosepAlviste/nvim-ts-context-commentstring/pull/63

Could you take a look at it from the perspective of someone setting up the plugin from scratch (mainly the changes in the README)? 🙏