JoosepAlviste / nvim-ts-context-commentstring

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

Incorrent commentstring for inline markdown code blocks #83

Closed mortezadadgar closed 1 year ago

mortezadadgar commented 1 year ago

Minimal reproducible full config

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",
        lazypath,
    })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
    "JoosepAlviste/nvim-ts-context-commentstring",
    { "nvim-treesitter/nvim-treesitter", build = ":TSUpdate" },
    "tpope/vim-commentary",
})

require("nvim-treesitter.configs").setup({
    ensure_installed = {
        "markdown_inline",
        "markdown",
    },
    highlight = {
        enable = true,
    },
})

vim.opt.updatetime = 100

Description

Hey, commentstring inside markdown's code block is not recognized properly instead relays on default commentstring of markdown files image I see from documentation that markdown is not listed as supported so maybe that's the reason it's not working? =require("ts_context_commentstring.internal").calculate_commentstring() returns nil quickly debugging the source turns out nil is returned from this line: https://github.com/JoosepAlviste/nvim-ts-context-commentstring/blob/main/lua/ts_context_commentstring/internal.lua#L112

Steps to reproduce

  1. open a markdown file
  2. comment code blocks
  3. commented by <!-- -->

Expected behavior

to be commented by specified markdown code block

Actual behavior

commented by <!-- -->

Additional context

No response

Slotos commented 1 year ago

There's no default go configuration for this plugin. You can set it up by extending default configuration. See :h ts-context-commentstring-commentstring-configuration:

require('ts_context_commentstring').setup {
  languages = {
    go = { __default = '// %s', __multiline = '/* %s */' },
  },
} 

There's currently no way to provide this configuration without calling "require", but that should be a straightforward change.

A brief explanation. Commentstring is set when you open go file, because default golang ftplugin sets it up when buffer's filetype is being set. But with injections ftplugins are not executes, nor it would be trivial to describe expectations for such execution. As a result, your comment plugin works just fine in go files, but in markdown ts-context-commentstring plugin walks tresitter injection tree upwards until it finds a language that it has a configuration for.

mortezadadgar commented 1 year ago

@Slotos well then I have to config commentstring for every language that I use that's not a good solution Btw comment.mini without this plugin can inject markdown code blocks commentstring so that should be possible

Slotos commented 1 year ago

mini.comment uses:

      -- Using `vim.filetype.get_option()` for performance as it has caching
      local cur_cs = vim.filetype.get_option(ft, 'commentstring')

I'd been musing patching in an ftplugin fallback in, didn't know it was this straightforward - that one line can simplify quite a lot of code in ts-context-commentstring.

Anyways, this can be a fun mini-project, if you enjoy coding.

mortezadadgar commented 1 year ago

@Slotos thanks for the idea, see #86