Skip to content

Commit

Permalink
New function for health.lua: check_lang()
Browse files Browse the repository at this point in the history
  • Loading branch information
jalvesaq committed Feb 16, 2025
1 parent 6d3000f commit 078d6bc
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 3 deletions.
7 changes: 4 additions & 3 deletions doc/R.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ If the plugin is active, pressing <LocalLeader>rf should start R.
Did you see warning messages but they disappeared before you have had time to
read them? Type the command |:messages| in Normal mode to see them again.

Run `:checkhealth r` to check if some dependencies are installed. For a more
complete diagnostic of `R.nvim` health, run the command `:RDebugInfo` which
will show information useful to fix some common issues.
Run `:checkhealth r` to check if some dependencies are installed and if
treesitter is working properly.

Run `:RDebugInfo` to see information useful to fix some common issues.

To see on the R Console messages received by `nvimcom`, put in your
`~/.Rprofile`:
Expand Down
103 changes: 103 additions & 0 deletions lua/r/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,107 @@ local function parser_installed(parser_name)
or pcall(vim.treesitter.query.get, parser_name, "highlights")
end

local check_buffer = function(ft, lines, langs)
-- Create a temporary buffer in a temporary window
local b = vim.api.nvim_create_buf(false, false)
vim.api.nvim_set_option_value("bufhidden", "wipe", { scope = "local" })
vim.api.nvim_set_option_value("number", false, { scope = "local" })
vim.api.nvim_set_option_value("swapfile", false, { scope = "local" })
vim.api.nvim_set_option_value("buftype", "nofile", { scope = "local" })
local w = vim.api.nvim_open_win(b, true, {
relative = "win",
row = 1,
col = 1,
width = 20,
height = 13,
hide = true,
})

vim.api.nvim_buf_set_lines(b, 0, -1, false, lines)
vim.api.nvim_set_option_value("filetype", ft, { buf = b })
vim.cmd("redraw")

for k, v in pairs(langs) do
vim.api.nvim_win_set_cursor(w, { v[2], v[3] })
langs[k][4] = require("r.utils").get_lang()
end

vim.api.nvim_win_close(w, true)
vim.api.nvim_buf_delete(b, { force = true })

return langs
end

local check_lang = function()
-- Fill the buffer with a Quarto document and check language detection
local lines = {
"---",
"title: Test",
"---",
"",
"Normal text.",
"",
"```{r}",
"x <- 1",
"```",
"",
"Normal text again.",
}

local qlangs = {
{ "yaml", 2, 0, "" },
{ "markdown", 4, 0, "" },
{ "markdown_inline", 5, 0, "" },
{ "chunk_header", 7, 0, "" },
{ "r", 8, 0, "" },
{ "chunk_end", 9, 0, "" },
}

qlangs = check_buffer("quarto", lines, qlangs)

lines = {
"\\documentclass{article}",
"\\begin{document}",
"",
"Normal text.",
"",
"<<example>>=",
"x <- 1",
"@",
"",
"Normal text again.",
"",
"\\end{document}",
}

local nlangs = {
{ "latex", 4, 0, "" },
{ "chunk_header", 6, 0, "" },
{ "r", 7, 0, "" },
{ "chunk_end", 8, 0, "" },
}

nlangs = check_buffer("rnoweb", lines, nlangs)

vim.health.start("Checking language detection in a Quarto document:")
for _, v in pairs(qlangs) do
if v[1] == v[4] then
vim.health.ok("Correctly detected: `" .. v[1] .. "`")
else
vim.health.error("Wrongly detected: `" .. v[1] .. "` vs `" .. v[4] .. "`")
end
end

vim.health.start("Checking language detection in an Rnoweb document:")
for _, v in pairs(nlangs) do
if v[1] == v[4] then
vim.health.ok("Correctly detected: `" .. v[1] .. "`")
else
vim.health.error("Wrongly detected: `" .. v[1] .. "` vs `" .. v[4] .. "`")
end
end
end

M.check = function()
vim.health.start("Checking applications and plugins:")

Expand Down Expand Up @@ -71,6 +172,8 @@ M.check = function()
vim.health.error("`" .. parser .. "` " .. "not found.")
end
end

check_lang()
end

return M

0 comments on commit 078d6bc

Please sign in to comment.