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

get_node_text() behaves differently between buffer and string sources on EOL char #28677

Open
cshuaimin opened this issue May 9, 2024 · 0 comments · May be fixed by #28841
Open

get_node_text() behaves differently between buffer and string sources on EOL char #28677

cshuaimin opened this issue May 9, 2024 · 0 comments · May be fixed by #28841
Labels
bug issues reporting wrong behavior complexity:low Low-risk, self-contained. Do NOT ask "can I work on this", just read CONTRIBUTING.md treesitter
Milestone

Comments

@cshuaimin
Copy link

cshuaimin commented May 9, 2024

Problem

If a treesitter node range's end_col is 0, vim.treesitter.get_node_text() will not return the trailing new line char for buffer source.

Steps to reproduce

test.lua:

local ts = vim.treesitter

local query = ts.query.parse("python", "(string_content) @s")

local function dbg(root, source)
  for _, nodes in query:iter_matches(root, source, 0, -1) do
    local node = nodes[#nodes]
    print("Range =", vim.inspect({ node:range() }))
    print("Text = |" .. ts.get_node_text(node, source) .. "|")
  end
end

local source = [[
"""test
"""
]]

print("====== string")
local root = ts.get_string_parser(source, "python"):parse()[1]:root()
dbg(root, source)

print("====== buffer")
local buf = vim.api.nvim_create_buf(true, true)
vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\n", { plain = true }))
root = ts.get_parser(buf, "python"):parse()[1]:root()
dbg(root, buf)
> nvim -l test.lua 
====== string
Range = { 0, 3, 1, 0 }
Text = |test
|
====== buffer
Range = { 0, 3, 1, 0 }
Text = |test|

Node ranges are same, but the result of get_node_text() is different.

Expected behavior

get_node_text() on buffers returns the trailing newline char, or does not return newline char on string sources, to make it consistent.

Neovim version (nvim -v)

NVIM v0.9.5 & NVIM v0.10.0-dev-d3fa88b

Vim (not Nvim) behaves the same?

no

Operating system/version

Linux

Terminal name/version

kitty 0.34.1

$TERM environment variable

xterm-kitty

Installation

nixpkgs

@cshuaimin cshuaimin added the bug issues reporting wrong behavior label May 9, 2024
@justinmk justinmk added this to the backlog milestone May 11, 2024
@justinmk justinmk added the complexity:low Low-risk, self-contained. Do NOT ask "can I work on this", just read CONTRIBUTING.md label May 11, 2024
@neovim neovim deleted a comment from NathanChase22 May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug issues reporting wrong behavior complexity:low Low-risk, self-contained. Do NOT ask "can I work on this", just read CONTRIBUTING.md treesitter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants