nvim-treesitter / nvim-treesitter

Nvim Treesitter configurations and abstraction layer
Apache License 2.0
10.53k stars 884 forks source link

Inconsistent Commenting Behavior for Markdown Files #7183

Open yuukibarns opened 9 hours ago

yuukibarns commented 9 hours ago

Describe the bug

When opening a Markdown file in Neovim and attempting to comment a line using the gc command, the commenting behavior appears to be inconsistent and changeable. Specifically, the comment format alternates between the expected HTML comment (<!--%s-->) and a hash-style comment (# %s), even though the commentstring is consistently set to the default <!--%s-->.

https://github.com/user-attachments/assets/db5ca765-6bb0-44a5-83fe-49d5401acad4

Here is my minimal configuration:

local path = "~/nvim-treesitter"
vim.opt.rtp:prepend(path)

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

To Reproduce

  1. Open a Markdown file in Neovim.
  2. Type gc to comment a line, for example:
    This is a test
  3. Observe that the line is initially commented correctly as:
    <!--This is a test-->
  4. Quit Neovim and reopen the file.
  5. Repeat the gc command on the same line.
  6. Notice that the line is now commented as:
    # This is a test
  7. Repeat steps 4-6 multiple times, and observe that the commenting method alternates randomly between the two formats.

Expected behavior

The line should always be commented using the commentstring specified in Neovim, which is <!--%s--> for Markdown files. Therefore, the line should consistently be commented as:

<!--This is a test-->

Output of :checkhealth nvim-treesitter

==============================================================================
nvim-treesitter: require("nvim-treesitter.health").check()

Installation ~
- OK `tree-sitter` found 0.23.0 (parser generator, only needed for :TSInstallFromGrammar)
- OK `node` found v18.19.1 (only needed for :TSInstallFromGrammar)
- OK `git` executable found.
- OK `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
  Version: cc (Ubuntu 13.2.0-23ubuntu4) 13.2.0
- OK Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

OS Info:
{
  machine = "x86_64",
  release = "6.8.0-45-generic",
  sysname = "Linux",
  version = "#45-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 30 12:02:04 UTC 2024"
} ~

Parser/Features         H L F I J
  - c                   ✓ ✓ ✓ ✓ ✓
  - lua                 ✓ ✓ ✓ ✓ ✓
  - markdown            ✓ . ✓ ✓ ✓
  - markdown_inline     ✓ . . . ✓
  - query               ✓ ✓ ✓ ✓ ✓
  - vim                 ✓ ✓ ✓ . ✓
  - vimdoc              ✓ . . . ✓

  Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections
         +) multiple parsers found, only one will be used
         x) errors found in the query, try to run :TSUpdate {lang} ~

Output of nvim --version

NVIM v0.10.1
Build type: Release
LuaJIT 2.1.1713484068

Additional context

Comes from neovim/neovim#30522

clason commented 9 hours ago

Cannot reproduce it here, either. I expect you are testing with nvim --clean -u test.lua (if not, do that). What does :set ft? show in each instance? What does :InspectTree show (press I to toggle language display)?

clason commented 9 hours ago

Also, your comment string looks off: the HTML comment should include spaces. Can you please test the latest Nvim nightly?

yuukibarns commented 9 hours ago

use nvim --clean -u test.lua the weird commenting behavior persists... Now it stably comments like this:

# This is a test

:set ft shows filetype=markdown :InspectTree and press I show: test

yuukibarns commented 9 hours ago

:set commentstring shows commentstring=<!--%s-->

yuukibarns commented 9 hours ago

use nightly the only difference is the output of :set commentstring. it shows commentstring=<!-- %s --> now.

clason commented 9 hours ago

Sorry, I cannot reproduce this, and I have no explanation for what you observe.

yuukibarns commented 9 hours ago

I installed nvim-treesitter using lazy.nvim with an empty ensure_install list and then moved it to a different directory (~/nvim-treesitter/). I suspect that the issue might be related to lazy.nvim. Is there an alternative method to install nvim-treesitter neatly?

clason commented 9 hours ago

Just clone the directory and add it to your runtimepath, as in your minimal config (which is what I assumed you did). I strongly suspect that you're seeing -- for some reason I can't fathom -- a different mapping than the builtin commenting (which would use gcc for un/commenting a single line, mind you).

yuukibarns commented 8 hours ago

The same result... :confused:, this time i directly git clone the repo, but there is no difference... i find that without any further configuration, just include nvim-treesitter into path will cause problem.

yuukibarns commented 6 hours ago

Is there anyone who can reproduce it? Just include nvim-treesitter in path and try several times.

igorlfs commented 3 hours ago

Is there anyone who can reproduce it?

I'm also having this issue, though I haven't found a consistent way of reproducing

try several times

It happens fairly often with me, I'd say around half the time.

nvim version is the lastest commit from main, NVIM v0.11.0-dev-840+gf2fa4ca97e, and I'm on Linux

clason commented 3 hours ago

Needs details: installation, minimal config, exact reproduction steps. A simple "Me too" does not help in any way whatsoever.

clason commented 3 hours ago

And don't use main.