tree-sitter-grammars / tree-sitter-markdown

Markdown grammar for tree-sitter
MIT License
411 stars 52 forks source link

query: invalid node type for code_span #103

Closed mycf closed 1 year ago

mycf commented 1 year ago

i installed markdown_inline

vim.treesitter.query.parse("markdown", [[ (paragraph (inline  (code_span)  @codespan)) ]])

but get a error

E5108: Error executing lua ...-bc66b75/share/nvim/runtime/lua/vim/treesitter/query.lua:259: query: invalid node type at position 2 for language markdown
stack traceback:
    [C]: in function '_ts_parse_query'
    ...-bc66b75/share/nvim/runtime/lua/vim/treesitter/query.lua:259: in function 'parse'
    [string ":lua"]:1: in main chunk
mycf commented 1 year ago
---
title: Markdown page example
---

# Markdown page example
`fasdf`
kkkk
# fasdf 
## fasdf
> fiasdf
You don't need React to write simple standalone pages.

ddasdf

md file

playground

minus_metadata [0, 0] - [3, 0]
section [3, 0] - [4, 0]
section [4, 0] - [7, 0]
  atx_heading [4, 0] - [5, 0]
    atx_h1_marker [4, 0] - [4, 1]
    heading_content: inline [4, 1] - [4, 23]
  paragraph [5, 0] - [7, 0]
    inline [5, 0] - [6, 4]
      code_span [5, 0] - [5, 7]
        code_span_delimiter [5, 0] - [5, 1]
        code_span_delimiter [5, 6] - [5, 7]
section [7, 0] - [14, 0]
  atx_heading [7, 0] - [8, 0]
    atx_h1_marker [7, 0] - [7, 1]
    heading_content: inline [7, 1] - [7, 7]
  section [8, 0] - [14, 0]
    atx_heading [8, 0] - [9, 0]
      atx_h2_marker [8, 0] - [8, 2]
      heading_content: inline [8, 2] - [8, 8]
    block_quote [9, 0] - [11, 0]
      block_quote_marker [9, 0] - [9, 2]
      paragraph [9, 2] - [11, 0]
        inline [9, 2] - [10, 54]
    fenced_code_block [11, 0] - [14, 0]
      fenced_code_block_delimiter [11, 0] - [11, 3]
      block_continuation [12, 0] - [12, 0]
      code_fence_content [12, 0] - [13, 0]
        block_continuation [13, 0] - [13, 0]
      fenced_code_block_delimiter [13, 0] - [13, 3]
local namespace = vim.api.nvim_create_namespace "headlines_namespace"
local bufnr = vim.api.nvim_get_current_buf()
function get_node(node)
  if node == nil then
    return
  end
  for n, name in node:iter_children() do
    -- 打印当 前节点的类型和文本
    print(n:type(), type(n:range()), n:range())
    -- local start_row,start_col,end_row,end_col = n:range()
    if n:type() == '`' then
      local start_row, start_column, end_row, end_column =
          unpack(vim.tbl_extend("force", { n:range() }, {}))
      vim.api.nvim_buf_set_extmark(bufnr, namespace, start_row, start_column, {
        -- 设置#长度为高亮组长度
        end_col = end_column,
        -- 先随便用了一个高亮组
        hl_group = 'IncSearch'
      })
    end
    -- print(start_row)
    -- print(start_col)
    -- print(end_row)
    -- print(end_col)
    get_node(n)
  end
end

-- 获取当前缓冲区使用的语言的 Tree-sitter 解析器
local parser = vim.treesitter.get_parser(0)

-- 解析当前缓冲区的代码并获取语法树
local tree = parser:parse()
-- tree:for_each_tree(function(tstree, ltree)
--
-- end)
-- 遍历语法树中的节点
get_node(tree[1]:root())
minus_metadata number 0 0 3 0
section number 3 0 4 0
section number 4 0 7 0
atx_heading number 4 0 5 0
atx_h1_marker number 4 0 4 1
inline number 4 1 4 23
paragraph number 5 0 7 0
inline number 5 0 6 4
` number 5 0 5 1
` number 5 6 5 7
section number 7 0 14 0
atx_heading number 7 0 8 0
atx_h1_marker number 7 0 7 1
inline number 7 1 7 7
section number 8 0 14 0
atx_heading number 8 0 9 0
atx_h2_marker number 8 0 8 2
inline number 8 2 8 8
block_quote number 9 0 11 0
block_quote_marker number 9 0 9 2
paragraph number 9 2 11 0
inline number 9 2 10 54
' number 10 7 10 8
. number 10 53 10 54
fenced_code_block number 11 0 14 0
fenced_code_block_delimiter number 11 0 11 3
block_continuation number 12 0 12 0
code_fence_content number 12 0 13 0
block_continuation number 13 0 13 0
fenced_code_block_delimiter number 13 0 13 3

there no code_span

MDeiml commented 1 year ago

Were you able to resolve your problem? I guess the problem here is caused by the structure of the markdown parser. For reasons not important here the parser is split into two parts: The block and the inline parser. In tree sitter this is implemented, by "injecting" the inline parser into the inline nodes of the block syntax tree, similar to how a javascript parser would be injected into the <script> tags of a html parser.

Not sure how to access the injected trees in the neovim lua api though.

bugsbugsbux commented 1 year ago

please reopen, i have this error too! i installed a plugin dawsers/navigator.nvim and in markdown files it throws this error - except when i uninstall the markdown_inline ts-source...

MDeiml commented 1 year ago

@herrvonvoid what's the specific error and setup? As I said this is not really a problem with this grammar, but rather that it's a bit complex to use.

bugsbugsbux commented 1 year ago

@MDeiml i'm terribly sorry, i forgot to update this comment, there was a query mistake in the plugin, which is fixed now. kr!