Socket
Socket
Sign inDemoInstall

tree-sitter-rstml

Package Overview
Dependencies
48
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    tree-sitter-rstml

Rust + html grammar for the tree-sitter parser library.


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

tree-sitter-rstml

GitHub License GitHub last commit (branch) GitHub Tag NPM Version Crates.io Version docs.rs

Rust + html grammar for the tree-sitter parser library.

Rust web frameworks, like Leptos, rely on JSX-style templates embedded inside Rust code using the rstml library. This project enables the parsing of those templates for various purposes, such as syntax highlighting in text editors.

Usage

Since rstml isn't a supposed to be a standalone language, there are two grammars defined for convenience:

rstmlrust_with_rstml
LanguageThis grammar only parses the rstml template without requiring it to be wrapped in a view! macro invocation.This grammar parses an entire rust source file as normal but will parse any view! macro invocations as a rstml template.
Intended useThis is intended to be injected into the tree-sitter-rust grammar. This approach provides the most flexibility by allowing the user to configure what should be interpreted as an rstml macro.In cases where tree-sitter injection is unsupported, this grammar is the best option. The macro invocation behaviour cannot be configured by the user.
Example valid code
<div>Hello, world</div>
view! {
    <div>Hello, world</div>
}
Parser locationrstml/srcrust_with_rstml/src
Rust binding usage
Show code
let code = "<div>Hello, world</div>";
let mut parser = tree_sitter::Parser::new();
parser.set_language(tree_sitter_rstml::language_rstml()).expect("Error loading rstml grammar");
let tree = parser.parse(code, None).unwrap();
Show code
let code = r#"
    view! {
        <div>hello, world</div>
    }
"#;
let mut parser = tree_sitter::Parser::new();
parser.set_language(tree_sitter_rstml::language_rust_with_rstml()).expect("Error loading rust_with_rstml grammar");
let tree = parser.parse(code, None).unwrap();
JavaScript binding usage
Show code
const Parser = require('tree-sitter')
const code = '<div>Hello, world</div>'
const parser = new Parser()
parser.setLanguage(require('tree-sitter-rstml').rstml)
const tree = parser.parse(code)
Show code
const Parser = require('tree-sitter')
const code = `
    view! {
        <div>Hello, world</div>
    }
`
const parser = new Parser()
parser.setLanguage(require('tree-sitter-rstml').rust_with_rstml)
const tree = parser.parse(code)

Editor support

Neovim

Neovim's tree-sitter integration supports syntax highlighting, indentation, and code folding.

Without rstml highlightingWith rstml highlighting
beforeafter

To use the Neovim support with nvim-treesitter, you should:

  • Ensure "nvim-treesitter/nvim-treesitter" is installed and configured correctly.
  • Install the "rayliwell/tree-sitter-rstml" plugin in your preferred package manager.
  • Ensure require("tree-sitter-rstml").setup() is ran after everytime nvim-treesitter is loaded.

Here's an example config using lazy.nvim:

require("lazy").setup({
    {
        "rayliwell/nvim-treesitter",
        build = ":TSUpdate",
        config = function ()
            local configs = require("nvim-treesitter.configs")

            configs.setup({
                ensure_installed = { "c", "lua", "vim", "vimdoc", "query", "rust" },
                sync_install = false,
                highlight = { enable = true },
                indent = { enable = true },
            })
        end
    },
    {
        "rayliwell/tree-sitter-rstml",
        dependencies = { "nvim-treesitter" },
        build = ":TSUpdate",
        config = function ()
    	   require("tree-sitter-rstml").setup()
        end
    },
})

Emacs

Emacs' (29.1+) tree-sitter integration supports syntax highlighting and indentation.

Before (rust-ts-mode)After (rstml-ts-mode)
beforeafter

Emacs support is provided by the rstml-ts-mode package. Installation instructions can be found on the project's GitHub.

Acknowledgements

This project extends and heavily relies upon the tree-sitter-rust grammar. It would not be possible without its contributors, as well as those who have contributed to the wider tree-sitter ecosystem.

Additionally, this project is based on the work of the rstml library. Originating as a fork of syn-rsx, whose creator, unfortunately, has passed away.

License

Licensed under the MIT License.

Copyright © 2024 Ryan Halliwell

Keywords

FAQs

Last updated on 06 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc