numToStr / Comment.nvim

:brain: :muscle: // Smart and powerful comment plugin for neovim. Supports treesitter, dot repeat, left-right/up-down motions, hooks, and more
MIT License
4k stars 159 forks source link

Not working with lua embed in init.vim #161

Closed ritwikvd closed 2 years ago

ritwikvd commented 2 years ago
lua << EOF
require'nvim-treesitter.configs'.setup {
  context_commentstring = {
    enable = true
  }
}

require'Comment'.setup{
pre_hook = function(ctx)
        -- Only calculate commentstring for tsx filetypes
        if vim.bo.filetype == 'typescriptreact' then
            local U = require('Comment.utils')

            -- Determine 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
}

Added the above config after installing comment and JoosepAlviste/nvim-ts-context-commentstring. jsx is working fine in .tsx files but trying to comment inside lua << EOF {...} EOF is giving me the vimscript commentstring " and not lua's --. I tried with the cursor at multiple different locations, no difference.

Is there some additional configuration involved; I was of the opinion that embedded lua worked out of the box and only jsx/tsx was problematic and fixed with the above pre-hook.

Entire init.vim can be viewed here if required: https://github.com/ritwikvd/dotfiles/blob/main/init.vim

numToStr commented 2 years ago

Yes, this should work without any problems.

Just to be sure, Can you check whether the Lua heredoc inside vim is actually recognised as lua?

ritwikvd commented 2 years ago

@numToStr I'm sorry, how do I do that?

numToStr commented 2 years ago

I'm sorry, how do I do that?

@ritwikvd You can use the TSPlaygroundToggle command from https://github.com/nvim-treesitter/playground after that, press I in the syntax-tree buffer

And also, I've already checked and I think Lua heredoc is not supported by the viml parser https://github.com/vigoux/tree-sitter-viml

ritwikvd commented 2 years ago

Oh your post reminded me that I didn't have the vim parser installed in the first place. It's working fine after I got that. Maybe that could be added as a note somewhere in the readme for future users who might be novices like me :)

thanks a ton for the very quick responses and for making this great plugin!