Skip to content
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
10 changes: 9 additions & 1 deletion crates/syntax_tree/src/queries/highlight_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ fn convert_capture_name_to_color(name: &str, color_map: &ColorMap) -> Option<Col
_ => {}
}
match name.split('.').next() {
Some("keyword") => Some(color_map.keyword_color),
// Some bundled grammars (e.g. Lua, Kotlin, Scala) still use these legacy capture names for
// keywords instead of `keyword.*`.
Some("keyword" | "conditional" | "repeat" | "exception" | "include") => {
Some(color_map.keyword_color)
}
Some("function") => Some(color_map.function_color),
Some("string") => Some(color_map.string_color),
Some("type") => Some(color_map.type_color),
Expand Down Expand Up @@ -138,3 +142,7 @@ impl<'a> TextProvider<&'a [u8]> for TextBuffer<'a> {
)
}
}

#[cfg(test)]
#[path = "highlight_query_tests.rs"]
mod tests;
81 changes: 81 additions & 0 deletions crates/syntax_tree/src/queries/highlight_query_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use languages::language_by_name;
use warp_editor::content::buffer::Buffer;
use warp_editor::content::selection_model::BufferSelectionModel;
use warp_editor::content::text::IndentBehavior;
use warpui_core::App;

use super::*;
use crate::SyntaxTreeState;

/// Lua, Kotlin and Scala tag these keywords with legacy capture names rather than `keyword.*`.
#[test]
fn legacy_keyword_captures_use_keyword_color() {
App::test((), |mut app| async move {
let keyword_color = ColorU::white();
let other_color = ColorU::black();
let color_map = ColorMap {
keyword_color,
function_color: other_color,
string_color: other_color,
type_color: other_color,
number_color: other_color,
comment_color: other_color,
property_color: other_color,
tag_color: other_color,
};

for (language_name, source, keywords) in [
(
"lua",
"if x then\n while y do end\nend",
&["if", "while"][..],
),
(
"kotlin",
"fun f() {\n try { } catch (e: Exception) { }\n}",
&["try", "catch"][..],
),
("scala", "import a.b", &["import"][..]),
] {
let language = language_by_name(language_name).expect("language should be supported");

let buffer_handle =
app.add_model(|_| Buffer::new(Box::new(|_, _| IndentBehavior::Ignore)));
let selection = app.add_model(|_| BufferSelectionModel::new(buffer_handle.clone()));
buffer_handle.update(&mut app, |buffer, ctx| {
*buffer = Buffer::from_plain_text(
source,
None,
Box::new(|_, _| IndentBehavior::Ignore),
selection,
ctx,
);
});

let buffer_snapshot = buffer_handle.read(&app, |buffer, _| buffer.buffer_snapshot());
let tree = warpui_core::r#async::block_on(async {
SyntaxTreeState::parse_text(buffer_snapshot, None, &language).await
})
.expect("test buffer is small and should parse");

buffer_handle.read(&app, |buffer, _| {
let query = &language.highlight_query;
let highlights = HighlightQuery::new(query, color_map).get_highlighted_chunks(
CharOffset::zero()..buffer.max_charoffset(),
query,
buffer,
&tree,
);

for keyword in keywords {
let offset = source.find(keyword).expect("keyword should be in source");
assert_eq!(
highlights.get(&CharOffset::from(offset)),
Some(&keyword_color),
"`{keyword}` should be highlighted as a keyword in {language_name}"
);
}
});
}
});
}