Skip to content

Commit

Permalink
fix: empty metadata caused treesitter directives like offset to be ig…
Browse files Browse the repository at this point in the history
…nored (#80)
  • Loading branch information
rollincuberawhide authored Jan 15, 2024
1 parent cf29fc2 commit 0fd09ca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lua/otter/keeper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local M = {}

local fn = require("otter.tools.functions")
local extensions = require("otter.tools.extensions")
local treesitter_iterator = require("otter.tools.treesitter_iterator")
local api = vim.api
local ts = vim.treesitter

Expand Down Expand Up @@ -50,11 +51,10 @@ M.extract_code_chunks = function(main_nr, lang, exclude_eval_false, row_from, ro

local code_chunks = {}
local lang_capture = nil
for id, node, metadata in query:iter_captures(root, main_nr) do
for id, node, metadata in treesitter_iterator.iter_captures(root, main_nr, query) do
local name = query.captures[id]
local text
local was_stripped

lang_capture = determine_language(main_nr, name, node, metadata, lang_capture)
if
lang_capture
Expand Down Expand Up @@ -135,7 +135,7 @@ M.get_current_language_context = function(main_nr)
local tree = parser:parse()
local root = tree[1]:root()
local lang_capture = nil
for id, node, metadata in query:iter_captures(root, main_nr) do
for id, node, metadata in treesitter_iterator.iter_captures(root, main_nr, query) do
local name = query.captures[id]

lang_capture = determine_language(main_nr, name, node, metadata, lang_capture)
Expand Down Expand Up @@ -377,7 +377,7 @@ M.get_language_lines_around_cursor = function()
local tree = parser:parse()
local root = tree[1]:root()

for id, node, metadata in query:iter_captures(root, main_nr) do
for id, node, metadata in treesitter_iterator.iter_captures(root, main_nr, query) do
local name = query.captures[id]
if name == "content" then
if ts.is_in_node_range(node, row, col) then
Expand Down
31 changes: 31 additions & 0 deletions lua/otter/tools/treesitter_iterator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
M = {}

M.iter_captures = function (node, source, query)
if type(source) == "number" and source == 0 then
source = vim.api.nvim_get_current_buf()
end

local raw_iter = node:_rawquery(query.query, true, 0, -1)
local metadata = {}
local function iter(end_line)
local capture, captured_node, match = raw_iter()

if match ~= nil then
local active = query:match_preds(match, match.pattern, source)
match.active = active
if not active then
return iter(end_line) -- tail call: try next match
else
-- if it has an active match, reset the metadata.
-- then hopefully apply_directives can fill the metadata
metadata = {}
end
query:apply_directives(match, match.pattern, source, metadata)
end
return capture, captured_node, metadata
end
return iter
end


return M

0 comments on commit 0fd09ca

Please sign in to comment.