| 1 |
use std::borrow::Cow; |
| 2 |
use std::cell::RefCell; |
| 3 |
use std::collections::HashSet; |
| 4 |
use std::fs; |
| 5 |
use std::path::{Path, PathBuf}; |
| 6 |
|
| 7 |
use indexmap::{IndexMap, IndexSet}; |
| 8 |
use once_cell::sync::Lazy; |
| 9 |
use once_cell::unsync::OnceCell; |
| 10 |
use skidder::Repo; |
| 11 |
use tree_sitter::{Grammar, InputEdit, Point}; |
| 12 |
|
| 13 |
use crate::config::{LanguageConfig, LanguageLoader}; |
| 14 |
use crate::fixtures::{check_highlighter_fixture, check_injection_fixture}; |
| 15 |
use crate::highlighter::{Highlight, HighlightEvent, Highlighter}; |
| 16 |
use crate::injections_query::InjectionLanguageMarker; |
| 17 |
use crate::{Language, Layer, Syntax}; |
| 18 |
|
| 19 |
const PARSE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(1); |
| 20 |
|
| 21 |
static GRAMMARS: Lazy<Vec<PathBuf>> = Lazy::new(|| { |
| 22 |
let skidder_config = skidder_config(); |
| 23 |
skidder::fetch(&skidder_config, false).unwrap(); |
| 24 |
skidder::build_all_grammars(&skidder_config, false, None).unwrap(); |
| 25 |
let grammars = skidder::list_grammars(&skidder_config).unwrap(); |
| 26 |
assert!(!grammars.is_empty()); |
| 27 |
grammars |
| 28 |
}); |
| 29 |
|
| 30 |
fn skidder_config() -> skidder::Config { |
| 31 |
skidder::Config { |
| 32 |
repos: vec![Repo::Local { |
| 33 |
|
| 34 |
path: Path::new("../test-grammars").canonicalize().unwrap(), |
| 35 |
}], |
| 36 |
index: PathBuf::new(), |
| 37 |
verbose: true, |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
#[derive(Debug, Clone, Default)] |
| 42 |
struct Overwrites { |
| 43 |
highlights: Option<String>, |
| 44 |
locals: Option<String>, |
| 45 |
injections: Option<String>, |
| 46 |
} |
| 47 |
|
| 48 |
fn get_grammar(lang_name: &str, overwrites: &Overwrites) -> LanguageConfig { |
| 49 |
let skidder_config = skidder_config(); |
| 50 |
let grammar_dir = skidder_config.grammar_dir(lang_name).unwrap(); |
| 51 |
let parser_path = skidder::build_grammar(&skidder_config, lang_name, false).unwrap(); |
| 52 |
let grammar = unsafe { Grammar::new(lang_name, &parser_path).unwrap() }; |
| 53 |
let highlights_query_path = grammar_dir.join("highlights.scm"); |
| 54 |
let injections_query_path = grammar_dir.join("injections.scm"); |
| 55 |
if !injections_query_path.exists() { |
| 56 |
println!("\x1b[36mskipping loading of injections for {lang_name:?} since {injections_query_path:?} does not exist\x1b[0m"); |
| 57 |
} |
| 58 |
let locals_query_path = grammar_dir.join("locals.scm"); |
| 59 |
if !locals_query_path.exists() { |
| 60 |
println!("\x1b[36mskipping loading of locals for {lang_name:?} since {locals_query_path:?} does not exist\x1b[0m"); |
| 61 |
} |
| 62 |
LanguageConfig::new( |
| 63 |
grammar, |
| 64 |
&overwrites.highlights.clone().unwrap_or_else(|| { |
| 65 |
fs::read_to_string(&highlights_query_path) |
| 66 |
.map_err(|err| { |
| 67 |
format!( |
| 68 |
"failed to read highlights in {}: {err}", |
| 69 |
highlights_query_path.display() |
| 70 |
) |
| 71 |
}) |
| 72 |
.unwrap() |
| 73 |
}), |
| 74 |
&overwrites |
| 75 |
.injections |
| 76 |
.clone() |
| 77 |
.unwrap_or_else(|| fs::read_to_string(&injections_query_path).unwrap_or_default()), |
| 78 |
&overwrites |
| 79 |
.locals |
| 80 |
.clone() |
| 81 |
.unwrap_or_else(|| fs::read_to_string(&locals_query_path).unwrap_or_default()), |
| 82 |
) |
| 83 |
.unwrap() |
| 84 |
} |
| 85 |
|
| 86 |
#[derive(Debug)] |
| 87 |
struct TestLanguageLoader { |
| 88 |
languages: IndexMap<String, Language>, |
| 89 |
lang_config: Box<[OnceCell<LanguageConfig>]>, |
| 90 |
overwrites: Box<[Overwrites]>, |
| 91 |
test_theme: RefCell<IndexSet<String>>, |
| 92 |
|
| 93 |
highlight_filter: Option<HashSet<String>>, |
| 94 |
} |
| 95 |
|
| 96 |
impl TestLanguageLoader { |
| 97 |
fn new() -> Self { |
| 98 |
let grammars = &GRAMMARS; |
| 99 |
|
| 100 |
Self { |
| 101 |
lang_config: (0..grammars.len()).map(|_| OnceCell::new()).collect(), |
| 102 |
overwrites: vec![Overwrites::default(); grammars.len()].into_boxed_slice(), |
| 103 |
test_theme: RefCell::default(), |
| 104 |
highlight_filter: None, |
| 105 |
languages: grammars |
| 106 |
.iter() |
| 107 |
.enumerate() |
| 108 |
.map(|(i, grammar)| { |
| 109 |
( |
| 110 |
grammar.file_name().unwrap().to_str().unwrap().to_owned(), |
| 111 |
Language::new(i as u32), |
| 112 |
) |
| 113 |
}) |
| 114 |
.collect(), |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
fn get(&self, name: &str) -> Language { |
| 119 |
self.languages[name] |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
#[allow(dead_code)] |
| 124 |
fn overwrite_injections(&mut self, lang: &str, content: String) { |
| 125 |
let lang = self.get(lang); |
| 126 |
self.overwrites[lang.idx()].injections = Some(content); |
| 127 |
self.lang_config[lang.idx()] = OnceCell::new(); |
| 128 |
} |
| 129 |
|
| 130 |
fn overwrite_highlights(&mut self, lang: &str, content: String) { |
| 131 |
let lang = self.get(lang); |
| 132 |
self.overwrites[lang.idx()].highlights = Some(content); |
| 133 |
self.lang_config[lang.idx()] = OnceCell::new(); |
| 134 |
} |
| 135 |
|
| 136 |
fn shadow_injections(&mut self, lang: &str, content: &str) { |
| 137 |
let lang = self.get(lang); |
| 138 |
let skidder_config = skidder_config(); |
| 139 |
let grammar = self.languages.get_index(lang.idx()).unwrap().0; |
| 140 |
let grammar_dir = skidder_config.grammar_dir(grammar).unwrap(); |
| 141 |
let mut injections = |
| 142 |
fs::read_to_string(grammar_dir.join("injections.scm")).unwrap_or_default(); |
| 143 |
injections.push('\n'); |
| 144 |
injections.push_str(content); |
| 145 |
self.overwrites[lang.idx()].injections = Some(injections); |
| 146 |
self.lang_config[lang.idx()] = OnceCell::new(); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
fn set_highlight_filter(&mut self, scopes: impl IntoIterator<Item = impl Into<String>>) { |
| 152 |
self.highlight_filter = Some(scopes.into_iter().map(Into::into).collect()); |
| 153 |
} |
| 154 |
|
| 155 |
fn shadow_highlights(&mut self, lang: &str, content: &str) { |
| 156 |
let lang = self.get(lang); |
| 157 |
let skidder_config = skidder_config(); |
| 158 |
let grammar = self.languages.get_index(lang.idx()).unwrap().0; |
| 159 |
let grammar_dir = skidder_config.grammar_dir(grammar).unwrap(); |
| 160 |
let mut highlights = fs::read_to_string(grammar_dir.join("highlights.scm")).unwrap(); |
| 161 |
highlights.push('\n'); |
| 162 |
highlights.push_str(content); |
| 163 |
self.overwrites[lang.idx()].highlights = Some(highlights); |
| 164 |
self.lang_config[lang.idx()] = OnceCell::new(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
impl LanguageLoader for TestLanguageLoader { |
| 169 |
fn language_for_marker(&self, marker: InjectionLanguageMarker) -> Option<Language> { |
| 170 |
match marker { |
| 171 |
InjectionLanguageMarker::Name(name) => self.languages.get(name).copied(), |
| 172 |
InjectionLanguageMarker::Match(text) => { |
| 173 |
let name: Cow<str> = text.into(); |
| 174 |
self.languages.get(name.as_ref()).copied() |
| 175 |
} |
| 176 |
_ => unimplemented!(), |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
fn get_config(&self, lang: Language) -> Option<&LanguageConfig> { |
| 181 |
let config = self.lang_config[lang.idx()].get_or_init(|| { |
| 182 |
let config = get_grammar( |
| 183 |
self.languages.get_index(lang.idx()).unwrap().0, |
| 184 |
&self.overwrites[lang.idx()], |
| 185 |
); |
| 186 |
let mut theme = self.test_theme.borrow_mut(); |
| 187 |
config.configure(|scope| { |
| 188 |
if let Some(ref allowed) = self.highlight_filter { |
| 189 |
if !allowed.contains(scope) { |
| 190 |
return None; |
| 191 |
} |
| 192 |
} |
| 193 |
Some(Highlight::new(theme.insert_full(scope.to_owned()).0 as u32)) |
| 194 |
}); |
| 195 |
config |
| 196 |
}); |
| 197 |
Some(config) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
fn lang_for_path(path: &Path, loader: &TestLanguageLoader) -> Language { |
| 202 |
match path |
| 203 |
.extension() |
| 204 |
.and_then(|it| it.to_str()) |
| 205 |
.unwrap_or_default() |
| 206 |
{ |
| 207 |
"rs" => loader.get("rust"), |
| 208 |
"html" => loader.get("html"), |
| 209 |
"css" => loader.get("css"), |
| 210 |
"erl" => loader.get("erlang"), |
| 211 |
"md" => loader.get("markdown"), |
| 212 |
extension => panic!("unknown file type .{extension}"), |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
fn highlight_fixture(loader: &TestLanguageLoader, fixture: impl AsRef<Path>) { |
| 217 |
let path = Path::new("../fixtures").join(fixture); |
| 218 |
let lang = lang_for_path(&path, loader); |
| 219 |
check_highlighter_fixture( |
| 220 |
path, |
| 221 |
"// ", |
| 222 |
lang, |
| 223 |
loader, |
| 224 |
|highlight| loader.test_theme.borrow()[highlight.idx()].clone(), |
| 225 |
|_| .., |
| 226 |
) |
| 227 |
} |
| 228 |
|
| 229 |
fn injection_fixture(loader: &TestLanguageLoader, fixture: impl AsRef<Path>) { |
| 230 |
let path = Path::new("../fixtures").join(fixture); |
| 231 |
let lang = lang_for_path(&path, loader); |
| 232 |
check_injection_fixture( |
| 233 |
path, |
| 234 |
"// ", |
| 235 |
lang, |
| 236 |
loader, |
| 237 |
|lang| loader.languages.get_index(lang.idx()).unwrap().0.clone(), |
| 238 |
|_| .., |
| 239 |
) |
| 240 |
} |
| 241 |
|
| 242 |
#[test] |
| 243 |
fn highlight() { |
| 244 |
let loader = TestLanguageLoader::new(); |
| 245 |
highlight_fixture(&loader, "highlighter/hello_world.rs"); |
| 246 |
} |
| 247 |
|
| 248 |
#[test] |
| 249 |
fn layers() { |
| 250 |
let loader = TestLanguageLoader::new(); |
| 251 |
|
| 252 |
let input = "/// Says hello. |
| 253 |
/// |
| 254 |
/// this is *markdown-inline* markdown |
| 255 |
/// |
| 256 |
/// # Example |
| 257 |
/// |
| 258 |
/// ```rust |
| 259 |
/// fn add(left: usize, right: usize) -> usize { |
| 260 |
/// left + right |
| 261 |
/// } |
| 262 |
/// ``` |
| 263 |
pub fn hello() {}"; |
| 264 |
|
| 265 |
let syntax = Syntax::new(input.into(), loader.get("rust"), PARSE_TIMEOUT, &loader).unwrap(); |
| 266 |
|
| 267 |
let assert_injection = |snippet: &str, expected: &[&str]| { |
| 268 |
assert!(!expected.is_empty(), "all layers have at least 1 injection"); |
| 269 |
|
| 270 |
let layer_lang_name = |layer: Layer| { |
| 271 |
loader |
| 272 |
.languages |
| 273 |
.get_index(syntax.layer(layer).language.idx()) |
| 274 |
.unwrap() |
| 275 |
.0 |
| 276 |
.clone() |
| 277 |
}; |
| 278 |
|
| 279 |
let snippet_start = input.find(snippet).unwrap() as u32; |
| 280 |
let snippet_end = snippet_start + snippet.len() as u32; |
| 281 |
|
| 282 |
let layers = syntax |
| 283 |
.layers_for_byte_range(snippet_start, snippet_end) |
| 284 |
.map(layer_lang_name) |
| 285 |
.collect::<Vec<_>>(); |
| 286 |
|
| 287 |
assert_eq!(&layers, expected, r#"snippet: "{snippet}""#); |
| 288 |
|
| 289 |
let layer = syntax.layer_for_byte_range(snippet_start, snippet_end); |
| 290 |
assert_eq!( |
| 291 |
&layer_lang_name(layer), |
| 292 |
expected.last().unwrap(), |
| 293 |
"last layer is the smallest fully encompassing layer" |
| 294 |
); |
| 295 |
}; |
| 296 |
|
| 297 |
|
| 298 |
assert_injection("fn add(left: usize, ri", &["rust", "markdown", "rust"]); |
| 299 |
|
| 300 |
|
| 301 |
assert_injection("# Example", &["rust", "markdown"]); |
| 302 |
|
| 303 |
|
| 304 |
assert_injection("pub fn hello() {}", &["rust"]); |
| 305 |
|
| 306 |
|
| 307 |
assert_injection("markdown-inline", &["rust", "markdown", "markdown-inline"]); |
| 308 |
} |
| 309 |
|
| 310 |
#[test] |
| 311 |
fn layers_for_byte_range_injection_boundary() { |
| 312 |
let mut loader = TestLanguageLoader::new(); |
| 313 |
|
| 314 |
loader.overwrite_injections( |
| 315 |
"rust", |
| 316 |
r#"((block_comment) @injection.content |
| 317 |
(#set! injection.language "markdown") |
| 318 |
(#set! injection.include-children))"# |
| 319 |
.to_string(), |
| 320 |
); |
| 321 |
|
| 322 |
|
| 323 |
let input = "/* first *//* second */"; |
| 324 |
let syntax = Syntax::new(input.into(), loader.get("rust"), PARSE_TIMEOUT, &loader).unwrap(); |
| 325 |
|
| 326 |
let second_start = input.find("/* second */").unwrap() as u32; |
| 327 |
|
| 328 |
let root = syntax.layer(syntax.root()); |
| 329 |
|
| 330 |
let first_inj_layer = root.injection_at_byte_idx(second_start - 1).unwrap().layer; |
| 331 |
let second_inj_layer = root.injection_at_byte_idx(second_start).unwrap().layer; |
| 332 |
|
| 333 |
let layers: Vec<_> = syntax |
| 334 |
.layers_for_byte_range(second_start, second_start) |
| 335 |
.collect(); |
| 336 |
|
| 337 |
assert!(layers.contains(&second_inj_layer)); |
| 338 |
assert!(!layers.contains(&first_inj_layer)); |
| 339 |
} |
| 340 |
|
| 341 |
#[test] |
| 342 |
fn highlight_overlaps_with_injection() { |
| 343 |
let loader = TestLanguageLoader::new(); |
| 344 |
|
| 345 |
|
| 346 |
highlight_fixture(&loader, "highlighter/comment.html"); |
| 347 |
} |
| 348 |
|
| 349 |
#[test] |
| 350 |
fn rust_parameter_locals() { |
| 351 |
let loader = TestLanguageLoader::new(); |
| 352 |
highlight_fixture(&loader, "highlighter/rust_parameter_locals.rs"); |
| 353 |
} |
| 354 |
|
| 355 |
#[test] |
| 356 |
fn codefence_rust_doc_comments() { |
| 357 |
let loader = TestLanguageLoader::new(); |
| 358 |
highlight_fixture(&loader, "highlighter/codefence_rust_doc_comments.md"); |
| 359 |
} |
| 360 |
|
| 361 |
#[test] |
| 362 |
fn parameters_within_injections_within_injections() { |
| 363 |
let loader = TestLanguageLoader::new(); |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
highlight_fixture(&loader, "highlighter/injectionception.rs"); |
| 368 |
injection_fixture(&loader, "injections/injectionception.rs"); |
| 369 |
} |
| 370 |
|
| 371 |
#[test] |
| 372 |
fn html_in_edoc_in_erlang() { |
| 373 |
let loader = TestLanguageLoader::new(); |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
highlight_fixture(&loader, "highlighter/html_in_edoc_in_erlang.erl"); |
| 378 |
injection_fixture(&loader, "injections/html_in_edoc_in_erlang.erl"); |
| 379 |
} |
| 380 |
|
| 381 |
#[test] |
| 382 |
fn non_local_pattern() { |
| 383 |
let mut loader = TestLanguageLoader::new(); |
| 384 |
|
| 385 |
loader.shadow_highlights( |
| 386 |
"rust", |
| 387 |
r#" |
| 388 |
((identifier) @variable.builtin |
| 389 |
(#eq? @variable.builtin "this") |
| 390 |
(#is-not? local)) |
| 391 |
"#, |
| 392 |
); |
| 393 |
highlight_fixture(&loader, "highlighter/non_local.rs"); |
| 394 |
} |
| 395 |
|
| 396 |
#[test] |
| 397 |
fn reference_highlight_starts_after_definition_ends() { |
| 398 |
let loader = TestLanguageLoader::new(); |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
highlight_fixture( |
| 403 |
&loader, |
| 404 |
"highlighter/reference_highlight_starts_after_definition_ends.rs", |
| 405 |
); |
| 406 |
} |
| 407 |
|
| 408 |
#[test] |
| 409 |
fn combined_injection() { |
| 410 |
let mut loader = TestLanguageLoader::new(); |
| 411 |
loader.shadow_injections( |
| 412 |
"rust", |
| 413 |
r#" |
| 414 |
((doc_comment) @injection.content |
| 415 |
(#set! injection.language "markdown") |
| 416 |
(#set! injection.combined))"#, |
| 417 |
); |
| 418 |
highlight_fixture(&loader, "highlighter/rust_doc_comment.rs"); |
| 419 |
} |
| 420 |
|
| 421 |
#[test] |
| 422 |
fn injection_in_child() { |
| 423 |
let mut loader = TestLanguageLoader::new(); |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
loader.shadow_injections( |
| 428 |
"rust", |
| 429 |
r#" |
| 430 |
([(line_comment) (block_comment)] @injection.content |
| 431 |
(#set! injection.language "comment")) |
| 432 |
|
| 433 |
([(line_comment (doc_comment) @injection.content) (block_comment (doc_comment) @injection.content)] |
| 434 |
(#set! injection.language "markdown") |
| 435 |
(#set! injection.combined)) |
| 436 |
"#, |
| 437 |
); |
| 438 |
highlight_fixture(&loader, "highlighter/rust_doc_comment.rs"); |
| 439 |
injection_fixture(&loader, "injections/rust_doc_comment.rs"); |
| 440 |
} |
| 441 |
|
| 442 |
#[test] |
| 443 |
fn injection_precedence() { |
| 444 |
let mut loader = TestLanguageLoader::new(); |
| 445 |
loader.shadow_injections( |
| 446 |
"rust", |
| 447 |
r#" |
| 448 |
([(line_comment) (block_comment)] @injection.content |
| 449 |
(#set! injection.language "comment") |
| 450 |
(#set! injection.include-children)) |
| 451 |
|
| 452 |
([(line_comment (doc_comment) @injection.content) (block_comment (doc_comment) @injection.content)] |
| 453 |
(#set! injection.language "markdown") |
| 454 |
(#set! injection.combined)) |
| 455 |
"#, |
| 456 |
); |
| 457 |
highlight_fixture(&loader, "highlighter/rust_doc_comment.rs"); |
| 458 |
|
| 459 |
loader.shadow_injections( |
| 460 |
"rust", |
| 461 |
r#" |
| 462 |
([(line_comment (doc_comment) @injection.content) (block_comment (doc_comment) @injection.content)] |
| 463 |
(#set! injection.language "markdown") |
| 464 |
(#set! injection.combined)) |
| 465 |
|
| 466 |
([(line_comment) (block_comment)] @injection.content |
| 467 |
(#set! injection.language "comment") |
| 468 |
(#set! injection.include-children)) |
| 469 |
"#, |
| 470 |
); |
| 471 |
highlight_fixture(&loader, "highlighter/rust_no_doc_comment.rs"); |
| 472 |
injection_fixture(&loader, "injections/rust_no_doc_comment.rs"); |
| 473 |
|
| 474 |
loader.shadow_injections( |
| 475 |
"rust", |
| 476 |
r#" |
| 477 |
((macro_invocation |
| 478 |
macro: |
| 479 |
[ |
| 480 |
(scoped_identifier |
| 481 |
name: (_) @_macro_name) |
| 482 |
(identifier) @_macro_name |
| 483 |
] |
| 484 |
(token_tree . (_) . (_) . (string_literal) @injection.content)) |
| 485 |
(#any-of? @_macro_name |
| 486 |
; std |
| 487 |
"some_macro") |
| 488 |
(#set! injection.language "rust") |
| 489 |
(#set! injection.include-children)) |
| 490 |
"#, |
| 491 |
); |
| 492 |
injection_fixture(&loader, "injections/overlapping_injection.rs"); |
| 493 |
} |
| 494 |
|
| 495 |
#[test] |
| 496 |
fn rust_nested_vec() { |
| 497 |
let loader = TestLanguageLoader::new(); |
| 498 |
highlight_fixture(&loader, "highlighter/nested_vec.rs"); |
| 499 |
injection_fixture(&loader, "injections/nested_vec.rs"); |
| 500 |
} |
| 501 |
|
| 502 |
#[test] |
| 503 |
fn edit_remove_and_add_injection_layer() { |
| 504 |
let loader = TestLanguageLoader::new(); |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
let before_text = "<!---->\n``\n<!---->"; |
| 510 |
let after_text = "<!---->\n```\n<!---->"; |
| 511 |
let edit = InputEdit { |
| 512 |
start_byte: 10, |
| 513 |
old_end_byte: 10, |
| 514 |
new_end_byte: 11, |
| 515 |
start_point: Point::ZERO, |
| 516 |
old_end_point: Point::ZERO, |
| 517 |
new_end_point: Point::ZERO, |
| 518 |
}; |
| 519 |
let mut syntax = Syntax::new( |
| 520 |
before_text.into(), |
| 521 |
loader.get("markdown"), |
| 522 |
PARSE_TIMEOUT, |
| 523 |
&loader, |
| 524 |
) |
| 525 |
.unwrap(); |
| 526 |
|
| 527 |
syntax |
| 528 |
.update(after_text.into(), PARSE_TIMEOUT, &[edit], &loader) |
| 529 |
.unwrap(); |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
let edit = InputEdit { |
| 534 |
start_byte: 10, |
| 535 |
old_end_byte: 11, |
| 536 |
new_end_byte: 10, |
| 537 |
start_point: Point::ZERO, |
| 538 |
old_end_point: Point::ZERO, |
| 539 |
new_end_point: Point::ZERO, |
| 540 |
}; |
| 541 |
let mut syntax = Syntax::new( |
| 542 |
after_text.into(), |
| 543 |
loader.get("markdown"), |
| 544 |
PARSE_TIMEOUT, |
| 545 |
&loader, |
| 546 |
) |
| 547 |
.unwrap(); |
| 548 |
|
| 549 |
syntax |
| 550 |
.update(before_text.into(), PARSE_TIMEOUT, &[edit], &loader) |
| 551 |
.unwrap(); |
| 552 |
} |
| 553 |
|
| 554 |
#[test] |
| 555 |
fn markdown_bold_highlight() { |
| 556 |
let loader = TestLanguageLoader::new(); |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
highlight_fixture(&loader, "highlighter/markdown_bold.md"); |
| 561 |
} |
| 562 |
|
| 563 |
#[test] |
| 564 |
fn css_parent_child_highlight_precedence() { |
| 565 |
let mut loader = TestLanguageLoader::new(); |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
loader.overwrite_highlights( |
| 571 |
"css", |
| 572 |
r##" |
| 573 |
((property_name) @variable |
| 574 |
(#match? @variable "^--")) |
| 575 |
|
| 576 |
"#" @punctuation |
| 577 |
((color_value) "#") @string.special |
| 578 |
(color_value) @string.special |
| 579 |
|
| 580 |
[";" ":"] @punctuation.delimiter |
| 581 |
"## |
| 582 |
.to_string(), |
| 583 |
); |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
highlight_fixture(&loader, "highlighter/parent_child_highlight_precedence.css"); |
| 593 |
} |
| 594 |
|
| 595 |
#[test] |
| 596 |
fn edoc_code_combined_injection() { |
| 597 |
let loader = TestLanguageLoader::new(); |
| 598 |
|
| 599 |
highlight_fixture(&loader, "highlighter/edoc_code_combined_injection.erl"); |
| 600 |
injection_fixture(&loader, "injections/edoc_code_combined_injection.erl"); |
| 601 |
} |
| 602 |
|
| 603 |
#[test] |
| 604 |
fn edoc_code_combined_injection_in_markdown() { |
| 605 |
let loader = TestLanguageLoader::new(); |
| 606 |
|
| 607 |
highlight_fixture( |
| 608 |
&loader, |
| 609 |
"highlighter/edoc_code_combined_injection_in_markdown.md", |
| 610 |
); |
| 611 |
} |
| 612 |
|
| 613 |
#[test] |
| 614 |
fn combined_injection_no_invalid_ranges() { |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
let mut loader = TestLanguageLoader::new(); |
| 621 |
loader.shadow_injections( |
| 622 |
"rust", |
| 623 |
r#" |
| 624 |
((doc_comment) @injection.content |
| 625 |
(#set! injection.language "markdown") |
| 626 |
(#set! injection.combined))"#, |
| 627 |
); |
| 628 |
let input = "/** |
| 629 |
* foo |
| 630 |
*/ |
| 631 |
|
| 632 |
impl Foo { |
| 633 |
/** |
| 634 |
* foo |
| 635 |
*/ |
| 636 |
|
| 637 |
/// foo |
| 638 |
}"; |
| 639 |
let result = Syntax::new(input.into(), loader.get("rust"), PARSE_TIMEOUT, &loader); |
| 640 |
assert!( |
| 641 |
result.is_ok(), |
| 642 |
"parsing should succeed without Error::InvalidRanges, got: {:?}", |
| 643 |
result.err() |
| 644 |
); |
| 645 |
} |
| 646 |
|
| 647 |
#[test] |
| 648 |
fn highlighter_no_panic_overlapping_captures_missing_theme() { |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
let mut loader = TestLanguageLoader::new(); |
| 656 |
loader.overwrite_highlights( |
| 657 |
"python", |
| 658 |
r#" |
| 659 |
(attribute attribute: (identifier) @variable.other.member) |
| 660 |
((identifier) @type (#match? @type "^[A-Z]")) |
| 661 |
(decorator) @function |
| 662 |
((identifier) @constant (#match? @constant "^_*[A-Z][A-Z\d_]*$")) |
| 663 |
"# |
| 664 |
.to_string(), |
| 665 |
); |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
loader.set_highlight_filter(["function", "variable.other.member"]); |
| 670 |
|
| 671 |
let source = "@a.B"; |
| 672 |
let syntax = Syntax::new(source.into(), loader.get("python"), PARSE_TIMEOUT, &loader) |
| 673 |
.expect("parse should succeed"); |
| 674 |
let mut highlighter = Highlighter::new(&syntax, source.into(), &loader, 0..); |
| 675 |
while highlighter.next_event_offset() != u32::MAX { |
| 676 |
highlighter.advance(); |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
#[test] |
| 681 |
fn highlighter_ancestor_highlight_not_replaced_by_child_capture() { |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
let mut loader = TestLanguageLoader::new(); |
| 693 |
loader.overwrite_highlights( |
| 694 |
"python", |
| 695 |
r#" |
| 696 |
(decorator) @function |
| 697 |
(attribute attribute: (identifier) @variable.other.member) |
| 698 |
((identifier) @type (#match? @type "^[A-Z]")) |
| 699 |
((identifier) @constant (#match? @constant "^[A-Z]")) |
| 700 |
"# |
| 701 |
.to_string(), |
| 702 |
); |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
loader.set_highlight_filter(["function", "variable.other.member", "constant"]); |
| 708 |
|
| 709 |
let source = "@a.B"; |
| 710 |
let syntax = Syntax::new(source.into(), loader.get("python"), PARSE_TIMEOUT, &loader) |
| 711 |
.expect("parse should succeed"); |
| 712 |
let mut highlighter = Highlighter::new(&syntax, source.into(), &loader, 0..); |
| 713 |
|
| 714 |
|
| 715 |
let offset_of_b = source.find('B').unwrap() as u32; |
| 716 |
while highlighter.next_event_offset() < offset_of_b { |
| 717 |
highlighter.advance(); |
| 718 |
} |
| 719 |
assert_eq!(highlighter.next_event_offset(), offset_of_b); |
| 720 |
|
| 721 |
let (event, pushed) = highlighter.advance(); |
| 722 |
assert_eq!(event, HighlightEvent::Push); |
| 723 |
|
| 724 |
assert_eq!( |
| 725 |
pushed.len(), |
| 726 |
1, |
| 727 |
"expected exactly one new highlight pushed for `B`" |
| 728 |
); |
| 729 |
d. |
| 730 |
assert_eq!( |
| 731 |
highlighter.active_highlights().len(), |
| 732 |
2, |
| 733 |
"both the decorator's @function and `B`'s @constant should be active" |
| 734 |
); |
| 735 |
} |