Skip to content

feat: Improve syntax highlighting support for template languages, such as Tera #13549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ package.helix-tui.opt-level = 2
package.helix-term.opt-level = 2

[workspace.dependencies]
tree-house = { version = "0.1.0", default-features = false }
tree-house = { git = "https://github.com/nik-rev/tree-house.git", rev = "c5b3f53dbffce5cf8dc42fb5597deddeaa5c9d0a", default-features = false }
nucleo = "0.5.0"
slotmap = "1.0.7"
thiserror = "2.0"
Expand Down
13 changes: 9 additions & 4 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,13 @@ pub struct Syntax {
const PARSE_TIMEOUT: Duration = Duration::from_millis(500); // half a second is pretty generous

impl Syntax {
pub fn new(source: RopeSlice, language: Language, loader: &Loader) -> Result<Self, Error> {
let inner = tree_house::Syntax::new(source, language, PARSE_TIMEOUT, loader)?;
pub fn new(
source: RopeSlice,
language: Language,
loader: &Loader,
path: Option<std::path::PathBuf>,
) -> Result<Self, Error> {
let inner = tree_house::Syntax::new(source, language, PARSE_TIMEOUT, loader, path)?;
Ok(Self { inner })
}

Expand Down Expand Up @@ -981,7 +986,7 @@ mod test {
let grammar = LOADER.get_config(language).unwrap().grammar;
let query = Query::new(grammar, query_str, |_, _| Ok(())).unwrap();
let textobject = TextObjectQuery::new(query);
let syntax = Syntax::new(source.slice(..), language, &LOADER).unwrap();
let syntax = Syntax::new(source.slice(..), language, &LOADER, None).unwrap();

let root = syntax.tree().root_node();
let test = |capture, range| {
Expand Down Expand Up @@ -1070,7 +1075,7 @@ mod test {
) {
let source = Rope::from_str(source);
let language = LOADER.language_for_name(language_name).unwrap();
let syntax = Syntax::new(source.slice(..), language, &LOADER).unwrap();
let syntax = Syntax::new(source.slice(..), language, &LOADER, None).unwrap();

let root = syntax
.tree()
Expand Down
2 changes: 1 addition & 1 deletion helix-core/tests/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn test_treesitter_indent(
let language_config = loader.language(language).config();
let indent_style = IndentStyle::from_str(&language_config.indent.as_ref().unwrap().unit);
let text = doc.slice(..);
let syntax = Syntax::new(text, language, &loader).unwrap();
let syntax = Syntax::new(text, language, &loader, None).unwrap();
let indent_query = loader.indent_query(language).unwrap();

for i in 0..doc.len_lines() {
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn highlighted_code_block<'a>(
let ropeslice = RopeSlice::from(text);
let Some(syntax) = loader
.language_for_match(RopeSlice::from(language))
.and_then(|lang| Syntax::new(ropeslice, lang, loader).ok())
.and_then(|lang| Syntax::new(ropeslice, lang, loader, None).ok())
else {
return styled_multiline_text(text, code_style);
};
Expand Down
3 changes: 2 additions & 1 deletion helix-term/src/ui/picker/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> AsyncHook
let text = doc.text().clone();

tokio::task::spawn_blocking(move || {
let syntax = match helix_core::Syntax::new(text.slice(..), language, &loader) {
let syntax = match helix_core::Syntax::new(text.slice(..), language, &loader, None)
{
Ok(syntax) => syntax,
Err(err) => {
log::info!("highlighting picker preview failed: {err}");
Expand Down
25 changes: 15 additions & 10 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1291,16 +1291,21 @@ impl Document {
) {
self.language = language_config;
self.syntax = self.language.as_ref().and_then(|config| {
Syntax::new(self.text.slice(..), config.language(), loader)
.map_err(|err| {
// `NoRootConfig` means that there was an issue loading the language/syntax
// config for the root language of the document. An error must have already
// been logged by `LanguageData::syntax_config`.
if err != syntax::HighlighterError::NoRootConfig {
log::warn!("Error building syntax for '{}': {err}", self.display_name());
}
})
.ok()
Syntax::new(
self.text.slice(..),
config.language(),
loader,
self.path().cloned(),
)
.map_err(|err| {
// `NoRootConfig` means that there was an issue loading the language/syntax
// config for the root language of the document. An error must have already
// been logged by `LanguageData::syntax_config`.
if err != syntax::HighlighterError::NoRootConfig {
log::warn!("Error building syntax for '{}': {err}", self.display_name());
}
})
.ok()
});
}

Expand Down
4 changes: 4 additions & 0 deletions runtime/queries/tera/injections.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
(#set! injection.language "yaml")
(#set! injection.combined)
)

((content) @injection.content
(#set! injection.language "<use-2nd-filename-extension>")
(#set! injection.combined))