Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Playground: check for nodes out of range (EOF) #47

Merged
merged 1 commit into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lua/nvim-treesitter-playground/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,13 @@ function M.highlight_node(bufnr)
end

local start_row, start_col, _ = node:start()
local last_row, last_col = utils.get_end_pos(bufnr)
-- Set the cursor to the last column
-- if the node starts at the EOF mark.
if start_row > last_row then
start_row = last_row
start_col = last_col
end

M.highlight_nodes(bufnr, { node })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't error for that type of nodes, so all good.


Expand Down
9 changes: 9 additions & 0 deletions lua/nvim-treesitter-playground/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ function M.node_contains(node, range)
return start_fits and end_fits
end

--- Returns a tuple with the position of the last line and last column (0-indexed).
function M.get_end_pos(bufnr)
local bufnr = bufnr or api.nvim_get_current_buf()
local last_row = api.nvim_buf_line_count(bufnr) - 1
local last_line = api.nvim_buf_get_lines(bufnr, last_row, last_row + 1, true)[1]
local last_col = #last_line
return last_row, last_col
end

return M