Coder Social home page Coder Social logo

stefanterdell / tree-sitter-rstml Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rayliwell/tree-sitter-rstml

0.0 0.0 0.0 11.87 MB

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

Home Page: https://github.com/rayliwell/tree-sitter-rstml

License: MIT License

Shell 0.01% JavaScript 0.04% C++ 0.01% Scheme 0.01% Python 0.01% C 99.93% Rust 0.01%

tree-sitter-rstml's Introduction

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:

rstml rust_with_rstml
Language This 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 use This 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 location rstml/src rust_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 (experimental)

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

Without rstml highlighting With rstml highlighting
before after

To test the experimental Neovim support, nvim-treesitter users should:

  • Replace "nvim-treesitter/nvim-treesitter" with "rayliwell/nvim-treesitter" in your package manager's config.
  • Add "rstml" to ensure_installed in nvim-treesitter.configs.setup.
  • Run the update command of your preferred Neovim package manager and run :TSUpdate.

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 = { "rust", "rstml" },
            sync_install = false,
            highlight = { enable = true },
            indent = { enable = true },
        })
    end
}})

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

tree-sitter-rstml's People

Contributors

rayliwell avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.