Skip to content

Commit

Permalink
refactor(utils.lua): simplify language detection by using quarto modu…
Browse files Browse the repository at this point in the history
…le for code chunk parsing
  • Loading branch information
PMassicotte committed Mar 4, 2025
1 parent 508dc92 commit 2ff94f5
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions lua/r/utils.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local warn = require("r.log").warn
local quarto = require("r.quarto")
local uv = vim.uv

local M = {}
Expand Down Expand Up @@ -82,23 +83,8 @@ function M.get_lang()
-- No treesitter parser for rhelp
if vim.bo.filetype == "rhelp" then return get_rhelp_lang() end

local p
if vim.bo.filetype == "rmd" or vim.bo.filetype == "quarto" then
local cline = vim.api.nvim_get_current_line()
if cline:find("^```.*child *= *") then
return "chunk_child"
elseif cline:find("^```%{") then
return "chunk_header"
elseif cline:find("^```$") then
return "chunk_end"
end
p = vim.treesitter.get_parser(vim.api.nvim_get_current_buf(), "markdown")
else
p = vim.treesitter.get_parser()
end
local c = vim.api.nvim_win_get_cursor(0)
local lang = p:language_for_range({ c[1] - 1, c[2], c[1] - 1, c[2] }):lang()
return lang
local current_chunk = quarto.get_current_code_chunk(vim.api.nvim_get_current_buf())
return current_chunk.lang
end

--- Request the windows manager to focus a window.
Expand Down

0 comments on commit 2ff94f5

Please sign in to comment.