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

closes #353
  • Loading branch information
PMassicotte committed Mar 4, 2025
1 parent 508dc92 commit 04622c7
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 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 @@ -76,29 +77,18 @@ end
--- Get language at current cursor position
---@return string
function M.get_lang()
if vim.bo.filetype == "" then return "none" end
-- Treesitter for rnoweb always return "latex" or "rnoweb"
if vim.bo.filetype == "rnoweb" then return get_rnw_lang() end
-- 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 filetype = vim.bo.filetype
if filetype == "" then return "none" end
if filetype == "rnoweb" then return get_rnw_lang() end
if filetype == "rhelp" then return get_rhelp_lang() end

local current_chunk = quarto.get_current_code_chunk(vim.api.nvim_get_current_buf())
if current_chunk.lang then return current_chunk.lang end

local parser = vim.treesitter.get_parser(vim.api.nvim_get_current_buf(), "markdown")
if not parser then return "" end

return parser:language_for_range(vim.api.nvim_win_get_cursor(0)):lang()
end

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

0 comments on commit 04622c7

Please sign in to comment.