rstudio / rstudio

RStudio is an integrated development environment (IDE) for R
https://posit.co/products/open-source/rstudio/
Other
4.6k stars 1.08k forks source link

Add ability to create custom syntax highlighting for specific file types #14595

Open jmrothen opened 2 months ago

jmrothen commented 2 months ago

RStudio currently supports syntax highlighting for a number of languages (R, Python, etc), and this syntax highlighting is triggered on the file-type level (For example, opening a .py file will trigger python syntax highlighting).

However, I believe there is not currently a method for users/package-developers to add new file-specific highlighting for languages not already supported in native RStudio.

Rather than having the RStudio development team add this in for every language, it would be better to provide an avenue for package creators to build and distribute packages that contain the corresponding code that enables the highlighting for different files.

Specific use case: I'd like to be able to open an F# file in RStudio (.fs, .fsx), and it to trigger the syntax highlighting for that language.

Proposed Solution: I imagine this could be done by allowing custom grammar files to be integrated into RStudio, or perhaps this can be done in some way by allowing file-type specific rstheme mapping. This would allow me to create a package that imports the corresponding grammar/theme file, attach it to the file-type, and display syntax highlighting accordingly to those files.

Thanks for taking the time to read this, and as always, thank you for developing the best IDE.

ronblum commented 2 months ago

@jmrothen Thank you for the thorough enhancement request! I'll mark this for review.

kevinushey commented 2 months ago

For what it's worth, other than the grammars explicitly maintained / supported by RStudio, we normally take the existing grammars from the upstream Ace editor.

It does look like Ace provided highlight rules for the F# language, as in:

https://github.com/ajaxorg/ace/blob/master/src/mode/fsharp_highlight_rules.js

so we likely just need to add the associated boilerplate to make sure that RStudio recognizes and uses F# highlight rules for those files.

(I realize this doesn't answer the underlying question of "can we make it possible for users to do this?", but at least it would be straightforward for us in this case.)

jmrothen commented 2 months ago

That makes sense. Perhaps instead there could be an option/ability for users to import additional grammars into the editor?

Also just for additional context, I was originally picturing a system where one could import a grammar file (for example, the files used by ionide) by placing a json or .rstheme file into an RStudio directory somewhere. For example, a package may come with a raw-grammar file in the data-raw folder, which could then be imported using a function in that package or something of the sort.

However it seems that it may make more sense to approach this from the Ace side of things.