Skip to content

Commit

Permalink
refactor(numbers.lua, quarto.lua): generalize code chunk retrieval fu…
Browse files Browse the repository at this point in the history
…nction

Replace `get_r_chunks_from_quarto` with `get_code_chunk` to support
retrieving code chunks of any specified language, not just R.
  • Loading branch information
PMassicotte committed Feb 23, 2025
1 parent a32131a commit ccbfa12
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lua/r/format/numbers.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local inform = require("r.log").inform
local get_r_chunks_from_quarto = require("r.quarto").get_r_chunks_from_quarto
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 @@ -68,7 +68,7 @@ M.formatnum = function(bufnr)
if not root_node then return end

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

for _, r_chunk in ipairs(r_chunks_content) do
find_and_replace_float(r_chunk.content, bufnr, r_chunk.start_row, 0)
Expand Down
15 changes: 9 additions & 6 deletions lua/r/quarto.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ end
-- @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_r_chunks_from_quarto = function(root, bufnr, cursor_pos)
M.get_code_chunk = function(root, bufnr, cursor_pos, code_fence_language)
local query = vim.treesitter.query.parse(
"markdown",
[[
(fenced_code_block
(info_string (language) @lang (#eq? @lang "r"))
(code_fence_content) @content)
]]
string.format(
[[
(fenced_code_block
(info_string (language) @lang (#eq? @lang "%s"))
(code_fence_content) @content)
]],
code_fence_language
)
)

bufnr = bufnr or vim.api.nvim_get_current_buf()
Expand Down

0 comments on commit ccbfa12

Please sign in to comment.