0

Target: Trying to write lua function that will return true if current cursor position in code-block in markdown, false if not.

Current Status: I have following code, that (based on tree-sitter) captures the current node, checks its type, and then checks the parent node, and so on.

local ts_utils   = require("nvim-treesitter.ts_utils")
In_md_fence = function()
    local node = ts_utils.get_node_at_cursor()
    while node do
        if node:type() == "fenced_code_block" then
            print('its fenced block')
            return true
        end
        print(node:type())
        node = node:parent()
    end

    print('finish')
    return false
end

Problem: when I run :lua In_md_fence() at range(1) in following markdown file:

# hello

```python
import numpy as np
for i in range(1):
    print(i)
```

asdf as
dfadsfkjasdlfjasdl;kfjasldfjaskl
askjjfalsdkjfaksdlf

enter image description here

I got these printed:

integer
argument_list
call
for_statement
module
finish
Press ENTER or type command to continue
  • My expectation was it should have print integer, argument_list, call, for_statement, ?module?, code_fence_content, fenced_code_block

Question:

  • Why didn't fenced_code_block didn't show up?
  • Where did I messed up?
  • How can I fix it?
  • I only get expected output, if cursor was at ```python or ```, not if it was actually inside of the block.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.