Skip to content

Support for RTypst#575

Draft
jalvesaq wants to merge 3 commits into
mainfrom
Rtypst
Draft

Support for RTypst#575
jalvesaq wants to merge 3 commits into
mainfrom
Rtypst

Conversation

@jalvesaq
Copy link
Copy Markdown
Member

No description provided.

@jalvesaq
Copy link
Copy Markdown
Member Author

Support for Typst was added to knitr on GitHub on Mar 22, 2026. It's not available in the last released version of knitr.

@jalvesaq
Copy link
Copy Markdown
Member Author

My approach in this pull request was to set the filetype of a .Rtyp file as typst and then enable R.nvim features for the Typst filetype. Would it be better to set a new filetype, Rtyp, and rename ftplugin/typst_rnvim.lua as ftplugin/rtyp_rnvim.lua?

Comment thread lua/r/chunk.lua Outdated
return chunks
end

--- Get code chunks from an RTypst (.Rtyp) document by scanning ```{r} and ``` markers.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not use

R.nvim/lua/r/chunk.lua

Lines 97 to 154 in 2d0cd15

local get_rmd_code_chunks = function(bufnr)
local root = require("r.utils").get_root_node()
if not root then return nil end
local query = vim.treesitter.query.parse(
"markdown",
[[
(fenced_code_block)
@fenced_code_block
]]
)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local code_chunks = {}
for id, node, _ in query:iter_captures(root, bufnr, 0, -1) do
local capture_name = query.captures[id]
if capture_name == "fenced_code_block" then
-- Loop through all children of the node and print their type and text
local lang
local info_string_params = {}
local comment_params = {}
local content_text = ""
local start_row, _, end_row, _ = node:range()
for child in node:iter_children() do
if child:type() == "info_string" then
local info_string = vim.treesitter.get_node_text(child, bufnr)
lang, info_string_params = M.parse_info_string_params(info_string)
end
if child:type() == "code_fence_content" then
content_text = vim.treesitter.get_node_text(child, bufnr)
-- Get the parameters specified in the code chunk with #|
comment_params =
M.parse_comment_params(vim.treesitter.get_node_text(node, bufnr))
end
end
-- Create the chunk object with the extracted information
local chunk = Chunk:new(
content_text,
start_row + 1,
end_row,
info_string_params,
comment_params,
lang,
node
)
table.insert(code_chunks, chunk)
end
end
return code_chunks
end

I think rtyps uses the same structure?

I think

local params_str = lines[i]:match("^```{r}$")

Will not capture params in r chunks, like {r echo=FALSE} or {python}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was easier for me to use regular expressions, but if you could rewrite the function using tree-sitter, it would be great.

@PMassicotte
Copy link
Copy Markdown
Collaborator

PMassicotte commented May 10, 2026

I think we could just do that in config.lua

vim.treesitter.language.register("markdown", { "quarto", "rmd", "rtyp" })

Then we could just reuse get_rmd_code_chunks + parse_info_string_params + parse_comment_params all. My preliminary understanding is that we already have all the tooling to parse rtyp code chunks.

What do you think?

@PMassicotte
Copy link
Copy Markdown
Collaborator

My approach in this pull request was to set the filetype of a .Rtyp file as typst and then enable R.nvim features for the Typst filetype. Would it be better to set a new filetype, Rtyp, and rename ftplugin/typst_rnvim.lua as ftplugin/rtyp_rnvim.lua?

Yes, I think this is the right approach.

@jalvesaq
Copy link
Copy Markdown
Member Author

vim.treesitter.language.register("markdown", { "quarto", "rmd", "rtyp" })

It would not work. Typst is very different from Markdown. It uses = instead of # for titles, and uses # as a prefix for functions in a similar way to LaTeX, which uses \.

@jalvesaq
Copy link
Copy Markdown
Member Author

Yes, I think this is the right approach.

This was my first thought. But then I realized that if the file is a Typst file, we can send the R code to R Console, but we will not be able to process the R code when rendering the PDF because knitr converts a .Rtyp file into a .typ file. If the file extension is already .typ, it would not work because it would overwrite itself.

@jalvesaq
Copy link
Copy Markdown
Member Author

I'll be able to continue working on this only at night...

@PMassicotte
Copy link
Copy Markdown
Collaborator

PMassicotte commented May 10, 2026

Peek 2026-05-10 08-07

This is what I got so far with TS.

If ok with you, I can push my changes.

@PMassicotte
Copy link
Copy Markdown
Collaborator

Note: r_ls should be activated for that filetype.

fix(ftplugin): use pcall for treesitter query set to handle errors

refactor(chunk.lua): remove custom RTypst code chunk parsing

feat(cursor.lua): add typst support for moving to next code chunk

fix(lsp/init.lua): correct iteration over attached_buffers

feat(maps.lua): add typst support for RSendChunk and related mappings

feat(send.lua): add typst support for sending lines and chunks
@PMassicotte
Copy link
Copy Markdown
Collaborator

PS. I have not tested knitting, I do not have the dev version of knitr installed.

@PMassicotte
Copy link
Copy Markdown
Collaborator

I will push this, let me know if it works for you, otherwise we can just delete the commit if anything :)

@PMassicotte
Copy link
Copy Markdown
Collaborator

PMassicotte commented May 10, 2026

I am currently using Markdown parser to get the chunks code, inline params and chunks parameters, The Markdown grammar is resilient enough to work with typst filetype.

I started to implement a typst specific TS extraction code, but we have to do some manually regex job to get the inline parameters in chunks like:

```{r, eval=TRUE}
plot(
  x,
  type = "b",
  main = "Random Normal Values",
  xlab = "Index",
  ylab = "Value"
)
```

What I have done if more a general refactor on how we get the code chunks based on the file type. At the moment, it works with just using Markdown parser, but maybe I could push the current refactor later in another PR.

@jalvesaq
Copy link
Copy Markdown
Member Author

If ok with you, I can push my changes.

Yes, it was OK. I think you have already pushed...

@jalvesaq
Copy link
Copy Markdown
Member Author

I'm getting this message when trying to send a line from a chunk of R code:

Not inside a supported code chunk.

@PMassicotte
Copy link
Copy Markdown
Collaborator

I'm getting this message when trying to send a line from a chunk of R code:

Not inside a supported code chunk.

Back to the office, I will have a look.

@jalvesaq
Copy link
Copy Markdown
Member Author

I've just found that the filetype typst does have the lang node if the block is:

```r
x <- rnorm(100)
```

But not if it is:

```{r}
x <- rnorm(100)
```

@PMassicotte
Copy link
Copy Markdown
Collaborator

I have some meetings this morning, I think I forgot to push a change yesterday. Will do that soon.

@jalvesaq
Copy link
Copy Markdown
Member Author

There is no hurry, and I will only have spare time next weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants