henriklovhaug / md-tui

Markdown renderer in the terminal
GNU Affero General Public License v3.0
208 stars 13 forks source link

Feature Request: Add support for luau syntax highlighting #143

Closed 0xJWLabs closed 4 months ago

0xJWLabs commented 4 months ago

Since there is a tree-sitter of luau now, can you add support for luau language since it kinda has different syntax highlight to lua? Thanks!

Here is the link to the tree sitter https://github.com/tree-sitter-grammars/tree-sitter-luau

henriklovhaug commented 4 months ago

The crate they are supplying, or someone else is building for them, is too old to work with md-tui. I would have to downgrade all other parsers. Link to luau crate

0xJWLabs commented 4 months ago

@henriklovhaug Did you mean like their github's repo is different than their crate yes?

or are we talking about tree-sitter version?

henriklovhaug commented 4 months ago

I use rust bindings I find on crates.io along with tree-sitter itself. Their API is breaking between versions, so the luau one I found is for tree-sitter 0.20 and all the other I use is for 0.21. They are not compatible. I may package both version of tree-sitter with md-tui, but I see that as a last resort.

0xJWLabs commented 4 months ago

Mind to add with my fork one? @henriklovhaug since the maintainer of that luau grammar isn't really active.

https://crates.io/crates/tree-sitter-luau-fork, I set tree-siter to ^0.21.0

henriklovhaug commented 4 months ago

I tried adding it, but it won't build. You can see it on the linked branch.

0xJWLabs commented 4 months ago

Can you try update it to 0.1.3, I try yours using cargo run, and it works @henriklovhaug

Btw this is the way I do it highlight/luau.rs

use tree_sitter_highlight::{HighlightConfiguration, HighlightEvent, Highlighter};
use tree_sitter_luau_fork::language;

use crate::highlight::HIGHLIGHT_NAMES;

pub fn highlight_luau(lines: &[u8]) -> Result<Vec<HighlightEvent>, String> {
    let mut highlither = Highlighter::new();
    let language = language();

    let mut luau_config =
        HighlightConfiguration::new(language, "luau", tree_sitter_luau_fork::HIGHLIGHTS_QUERY, "", "")
            .unwrap();

    luau_config.configure(&HIGHLIGHT_NAMES);

    let highlights: Result<Vec<HighlightEvent>, String> =
        if let Ok(lines) = highlither.highlight(&luau_config, lines, None, |_| None) {
            lines
                .collect::<Result<Vec<_>, _>>()
                .map_err(|e| e.to_string())
        } else {
            Err("Failed to highlight".to_string())
        };

    highlights
}

And just add that to mod.rs on highlight same like you