Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incompatibility with md-headers, a plugin to navigate through the headings of .md .rmd. #353

Open
SoaresAlisson opened this issue Mar 1, 2025 · 22 comments

Comments

@SoaresAlisson
Copy link

Not an issue, but an incompatibility between packages. Must I maybe put it on "Discussions"?

I installed the https://github.com/AntonVanAssche/md-headers.nvim/wiki/Installation and loved the idea to easily navigate through the headers. Maybe in the future can I fork it and try to implement a chunk navigation, like in RStudio. But for now, my knowledges in Lua is very limited.
Well, after installing the plugin (I'm using Lazyvim) and enabling more formats ft = { "markdown", "rmd", "Rmd", "qmd" }, everything works fine until I call any function of the package, like MDHeaders or MDHeadersTelescope. After that, I cannot send code to terminal "not inside R or Python Chunk" and when I try to comment, it uses HTML comment <!-- everywhere, instead of #. Has anyone any clue on how to solve this problem? Or knows anybody an alternative, similar plugin? For me, the best solution would be to easily navigate through the names of the chunks.

Thanks!

@PMassicotte
Copy link
Collaborator

PMassicotte commented Mar 1, 2025

Maybe in the future can I fork it and try to implement a chunk navigation, like in RStudio.

This is there already

<LocalLeader>gn
<LocalLeader>gN

Does calling MDHeader changes the filetype?

@jalvesaq
Copy link
Member

jalvesaq commented Mar 1, 2025

I installed "md-headers.nvim" and can replicate the bug. Running :MDHeaders calls:

  local parser = vim.treesitter.get_parser(bufnr, "markdown", {})

which is the same command called by our lua/r/utils.lua. It, then, runs this query:

local md_query = vim.treesitter.query.parse(
  "markdown",
  [[
(atx_heading) @md_heading
]]
)

@PMassicotte
Copy link
Collaborator

Interesting. Not sure how this could potentially affect R.nvim. I will explore a bit.

@jalvesaq jalvesaq mentioned this issue Mar 3, 2025
@jalvesaq
Copy link
Member

jalvesaq commented Mar 3, 2025

@SoaresAlisson, could try the "new_get_lang" brach, please?

@SoaresAlisson
Copy link
Author

Wow!
I was expecting only some general comments on this issue, not a change proposal in the code of R.nvim, and not so fast. You guys are amazing!
Well, so I must do a git clone in ~projects, choose the branch and run nvim/lazyvim link, right?

@jalvesaq
Copy link
Member

jalvesaq commented Mar 3, 2025

Unfortunately, I realized that the bug was not fixed...

@PMassicotte
Copy link
Collaborator

I will do my best to explore this tomorrow.

@PMassicotte
Copy link
Collaborator

After calling : MDHeaders it does not work anymore like you said. Then, if you type :set filetype=quarto it works again. Looks like that the plugin is changing something.

@PMassicotte
Copy link
Collaborator

I think I have fixed the issue. @SoaresAlisson, @jalvesaq mind testing this branch?

https://github.com/R-nvim/R.nvim/tree/fix-wrong-filetype-md-header

PMassicotte added a commit that referenced this issue Mar 4, 2025
PMassicotte added a commit that referenced this issue Mar 4, 2025
@jalvesaq
Copy link
Member

jalvesaq commented Mar 4, 2025

It didn't work with this Quarto document:

First paragraph.

```{r}
x <- 1
y <- 2
```

Second paragraph.

```{r}
z <- 3
```

After running :MDHeaders, if I put the cursor over the line x <- 1 and send the line, the cursor jumps to z <- 3, not to y <- 2.

@PMassicotte
Copy link
Collaborator

PMassicotte commented Mar 4, 2025

I found the issue. It seems that the buffer becomes 1 based after calling :MDHeader.

Try with this document:

---
---

# H1

```{r}
library(tidyverse)
```

## H2

First paragraph.

```{r}
x <- 1
y <- 2
```

Second paragraph.

```{r}
z <- 3
```

With this branch, call sendline on x <- 1. It will print 14. Call :MDHeader, replace the cursor on that line, it will now print 15.

@PMassicotte
Copy link
Collaborator

PMassicotte commented Mar 4, 2025

I think this comes to this

R.nvim/lua/r/send.lua

Lines 121 to 132 in 493de62

if node and node:type() == "program" then node = node:child(0) end
while node do
local parent = node:parent()
if
parent
and (parent:type() == "program" or parent:type() == "braced_expression")
then
break
end
node = parent
end

With the node program not always accounting depending on ts version or something. I remember opening an issue on that.

neovim/neovim#31447

@PMassicotte
Copy link
Collaborator

PMassicotte commented Mar 4, 2025

    if node and node:type() == "program" then node = node:child(0) end

    vim.print("node: ", node:range())

Node range change after calling :MDHeader.

@PMassicotte
Copy link
Collaborator

 if node then vim.schedule(function() end) end

vim.print("node: ", node:type())

Before, the type is program then change to fenced_content.

@jalvesaq
Copy link
Member

jalvesaq commented Mar 4, 2025

Is it a bug in md-headers? Or in Neovim?

@PMassicotte
Copy link
Collaborator

Not sure, still scratching my head to understand what is going on.

@PMassicotte

This comment has been minimized.

@PMassicotte
Copy link
Collaborator

@PMassicotte
Copy link
Collaborator

If we comment out the html parsing it works

M.get_headings = function(bufnr)
    local md_headings = _query_md(bufnr) or {}
    -- local html_headings = _query_html(bufnr) or {}

    local headings = {}
    for _, h in ipairs(md_headings) do
        table.insert(headings, h)
    end

    -- for _, h in ipairs(html_headings) do
    --     table.insert(headings, h)
    -- end
    headings = _sort_headings(headings)

    return headings
end

@jalvesaq
Copy link
Member

jalvesaq commented Mar 4, 2025

So, at least for some tree-sitter operations, md-headers turns the buffer file type into HTML...

@PMassicotte
Copy link
Collaborator

yes, because doing :set filetype=quarto after :MDHeaders makes it work again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants