Skip to content

Commit

Permalink
refactor(numbers.lua, quarto.lua): simplify code chunk extraction by …
Browse files Browse the repository at this point in the history
…removing root node dependency and improve error handling
  • Loading branch information
PMassicotte committed Feb 23, 2025
1 parent ccbfa12 commit c17dd6f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
11 changes: 6 additions & 5 deletions lua/r/format/numbers.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local inform = require("r.log").inform
local get_code_chunk = require("r.quarto").get_code_chunk
local get_root_node = require("r.utils").get_root_node

local M = {}

Expand Down Expand Up @@ -64,11 +63,13 @@ M.formatnum = function(bufnr)
return
end

local root_node = get_root_node(bufnr)
if not root_node then return end

if filetype == "quarto" or filetype == "rmd" then
local r_chunks_content = get_code_chunk(root_node, bufnr, nil, "r")
local r_chunks_content = get_code_chunk(bufnr, "r")

if not r_chunks_content then
error("Failed to extract code chunks.")
return
end

for _, r_chunk in ipairs(r_chunks_content) do
find_and_replace_float(r_chunk.content, bufnr, r_chunk.start_row, 0)
Expand Down
22 changes: 13 additions & 9 deletions lua/r/quarto.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ M.command = function(what)
send_cmd(cmd)
end

-- Helper function to get R content/code block from Quarto document
-- @param root The root node of the parsed tree
-- @param bufnr The buffer number
-- @param cursor_pos The cursor position (optional)
-- @return A table containing R code blocks with their content, start row and end row
M.get_code_chunk = function(root, bufnr, cursor_pos, code_fence_language)
--- Find and replace floating point numbers in the given R content.
---@param bufnr integer The buffer number.
---@param lang string The language of the code chunk.
---@param row integer|nil The row number. If nil, all code chunks are returned.
---@return table|nil
M.get_code_chunk = function(bufnr, lang, row)
local root = require("r.utils").get_root_node()

if not root then return nil end

local query = vim.treesitter.query.parse(
"markdown",
string.format(
Expand All @@ -35,7 +39,7 @@ M.get_code_chunk = function(root, bufnr, cursor_pos, code_fence_language)
(info_string (language) @lang (#eq? @lang "%s"))
(code_fence_content) @content)
]],
code_fence_language
lang
)
)

Expand All @@ -45,13 +49,13 @@ M.get_code_chunk = function(root, bufnr, cursor_pos, code_fence_language)
for _, node, _ in query:iter_captures(root, bufnr, 0, -1) do
if node:type() == "code_fence_content" then
local start_row, _, end_row, _ = node:range()
if not cursor_pos or (cursor_pos >= start_row and cursor_pos <= end_row) then
if not row or (row >= start_row and row <= end_row) then
table.insert(r_contents, {
content = vim.treesitter.get_node_text(node, bufnr),
start_row = start_row,
end_row = end_row,
})
if cursor_pos then break end
if row then break end
end
end
end
Expand Down

0 comments on commit c17dd6f

Please sign in to comment.