.cargo/
crates/
brain/
diff/
files/
highlight/
benches/
src/
syntaxes/
tests/
syntaxes.rs
16.3 KiB
Cargo.toml
492 B
repo/
signals/
tui/
tui-core/
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/highlight/tests/syntaxes.rs
raw
| 1 | //! Tokenization tests for the built-in `syntaxes` grammars. |
| 2 | #![cfg(feature = "syntaxes")] |
| 3 | |
| 4 | use std::str::FromStr; |
| 5 | |
| 6 | use rustik_highlight::{Grammar, LineState, LineTokenizer, syntaxes}; |
| 7 | |
| 8 | #[test] |
| 9 | fn built_in_syntaxes_compile() { |
| 10 | for source in [ |
| 11 | syntaxes::C, |
| 12 | syntaxes::CSS, |
| 13 | syntaxes::ERB, |
| 14 | syntaxes::FISH, |
| 15 | syntaxes::GO, |
| 16 | syntaxes::HTML, |
| 17 | syntaxes::JAVASCRIPT, |
| 18 | syntaxes::LUA, |
| 19 | syntaxes::MAKE, |
| 20 | syntaxes::MARKDOWN, |
| 21 | syntaxes::RADIANCE, |
| 22 | syntaxes::RIL, |
| 23 | syntaxes::PYTHON, |
| 24 | syntaxes::RUBY, |
| 25 | syntaxes::RUST, |
| 26 | syntaxes::SHELL, |
| 27 | syntaxes::SQL, |
| 28 | syntaxes::TOML, |
| 29 | syntaxes::TYPESCRIPT, |
| 30 | syntaxes::TSX, |
| 31 | syntaxes::XML, |
| 32 | syntaxes::YAML, |
| 33 | ] { |
| 34 | Grammar::from_str(source).unwrap(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | #[test] |
| 39 | fn scripting_syntaxes_match_shebangs() { |
| 40 | let ruby = Grammar::from_str(syntaxes::RUBY).unwrap(); |
| 41 | let python = Grammar::from_str(syntaxes::PYTHON).unwrap(); |
| 42 | let shell = Grammar::from_str(syntaxes::SHELL).unwrap(); |
| 43 | let fish = Grammar::from_str(syntaxes::FISH).unwrap(); |
| 44 | |
| 45 | assert!(ruby.matches_head("#!/usr/bin/env ruby3.3")); |
| 46 | assert!(python.matches_head("#!/usr/bin/env python3.11")); |
| 47 | assert!(shell.matches_head("#!/bin/sh")); |
| 48 | assert!(fish.matches_head("#!/usr/bin/env fish")); |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn erb_embeds_html_and_ruby_scopes() { |
| 53 | let erb: rustik_highlight::RawGrammar = syntaxes::ERB.parse().unwrap(); |
| 54 | let html: rustik_highlight::RawGrammar = syntaxes::HTML.parse().unwrap(); |
| 55 | let ruby: rustik_highlight::RawGrammar = syntaxes::RUBY.parse().unwrap(); |
| 56 | let grammar = Grammar::compile_with(&erb, &[html, ruby]).unwrap(); |
| 57 | let mut state = LineState::default(); |
| 58 | let mut tokenizer = LineTokenizer::new(&grammar); |
| 59 | let spans = tokenizer.tokenize_line(&mut state, "<h1><% if ok %></h1>"); |
| 60 | let scopes = spans |
| 61 | .iter() |
| 62 | .filter_map(|span| grammar.scopes.get(span.scope.index())) |
| 63 | .map(String::as_str) |
| 64 | .collect::<Vec<_>>(); |
| 65 | |
| 66 | assert!(scopes.contains(&"entity.name.tag.html")); |
| 67 | assert!(scopes.contains(&"punctuation.section.embedded.begin.erb")); |
| 68 | assert!(scopes.contains(&"source.ruby")); |
| 69 | assert!(scopes.contains(&"keyword.control.ruby")); |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn erb_embeds_multiline_javascript_scopes() { |
| 74 | let erb: rustik_highlight::RawGrammar = syntaxes::ERB.parse().unwrap(); |
| 75 | let html: rustik_highlight::RawGrammar = syntaxes::HTML.parse().unwrap(); |
| 76 | let ruby: rustik_highlight::RawGrammar = syntaxes::RUBY.parse().unwrap(); |
| 77 | let css: rustik_highlight::RawGrammar = syntaxes::CSS.parse().unwrap(); |
| 78 | let javascript: rustik_highlight::RawGrammar = syntaxes::JAVASCRIPT.parse().unwrap(); |
| 79 | let grammar = Grammar::compile_with(&erb, &[html, ruby, css, javascript]).unwrap(); |
| 80 | let mut state = LineState::default(); |
| 81 | let mut tokenizer = LineTokenizer::new(&grammar); |
| 82 | let _ = tokenizer.tokenize_line(&mut state, "<script>"); |
| 83 | let spans = tokenizer.tokenize_line( |
| 84 | &mut state, |
| 85 | "document.addEventListener('DOMContentLoaded', function () {", |
| 86 | ); |
| 87 | let scopes = spans |
| 88 | .iter() |
| 89 | .filter_map(|span| grammar.scopes.get(span.scope.index())) |
| 90 | .map(String::as_str) |
| 91 | .collect::<Vec<_>>(); |
| 92 | |
| 93 | assert!(scopes.contains(&"string.quoted.single.js")); |
| 94 | assert!(scopes.contains(&"storage.type.function.js")); |
| 95 | } |
| 96 | |
| 97 | #[test] |
| 98 | fn html_embeds_javascript_scopes() { |
| 99 | let html: rustik_highlight::RawGrammar = syntaxes::HTML.parse().unwrap(); |
| 100 | let css: rustik_highlight::RawGrammar = syntaxes::CSS.parse().unwrap(); |
| 101 | let javascript: rustik_highlight::RawGrammar = syntaxes::JAVASCRIPT.parse().unwrap(); |
| 102 | let grammar = Grammar::compile_with(&html, &[css, javascript]).unwrap(); |
| 103 | let scopes = tokenize_scopes(&grammar, "<Script>if (ok) { return; }</Script>"); |
| 104 | |
| 105 | assert!(scopes.contains(&"source.js")); |
| 106 | assert!(scopes.contains(&"keyword.control.conditional.js")); |
| 107 | assert!(scopes.contains(&"keyword.control.flow.js")); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn javascript_highlights_function_call_arguments() { |
| 112 | let grammar = Grammar::from_str(syntaxes::JAVASCRIPT).unwrap(); |
| 113 | let line = "document.addEventListener('DOMContentLoaded', function () {"; |
| 114 | let mut state = LineState::default(); |
| 115 | let mut tokenizer = LineTokenizer::new(&grammar); |
| 116 | let spans = tokenizer.tokenize_line(&mut state, line); |
| 117 | let scopes = spans |
| 118 | .iter() |
| 119 | .filter_map(|span| grammar.scopes.get(span.scope.index())) |
| 120 | .map(String::as_str) |
| 121 | .collect::<Vec<_>>(); |
| 122 | |
| 123 | assert!(scopes.contains(&"string.quoted.single.js")); |
| 124 | assert!(scopes.contains(&"storage.type.function.js")); |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn typescript_highlights_comments_inside_interface_body() { |
| 129 | let grammar = Grammar::from_str(syntaxes::TYPESCRIPT).unwrap(); |
| 130 | let mut state = LineState::default(); |
| 131 | let mut tokenizer = LineTokenizer::new(&grammar); |
| 132 | let _ = tokenizer.tokenize_line(&mut state, "export interface OperationSpan {"); |
| 133 | let spans = tokenizer.tokenize_line(&mut state, " /** Semantic role for this group. */"); |
| 134 | let scopes = spans |
| 135 | .iter() |
| 136 | .filter_map(|span| grammar.scopes.get(span.scope.index())) |
| 137 | .map(String::as_str) |
| 138 | .collect::<Vec<_>>(); |
| 139 | |
| 140 | assert!( |
| 141 | scopes.contains(&"comment.block.documentation.ts"), |
| 142 | "{scopes:?}" |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn tsx_highlights_typescript_and_jsx() { |
| 148 | let grammar = Grammar::from_str(syntaxes::TSX).unwrap(); |
| 149 | let scopes = tokenize_scopes( |
| 150 | &grammar, |
| 151 | "const view: JSX.Element = <Button disabled={true} />;", |
| 152 | ); |
| 153 | |
| 154 | assert!(scopes.contains(&"storage.type.tsx"), "{scopes:?}"); |
| 155 | assert!(scopes.contains(&"entity.name.tag.tsx"), "{scopes:?}"); |
| 156 | assert!( |
| 157 | scopes.contains(&"entity.other.attribute-name.tsx"), |
| 158 | "{scopes:?}" |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | #[test] |
| 163 | fn xml_highlights_tags_attributes_and_strings() { |
| 164 | let grammar = Grammar::from_str(syntaxes::XML).unwrap(); |
| 165 | let scopes = tokenize_scopes(&grammar, "<item enabled=\"true\" />"); |
| 166 | |
| 167 | assert!( |
| 168 | scopes.contains(&"entity.name.tag.localname.xml"), |
| 169 | "{scopes:?}" |
| 170 | ); |
| 171 | assert!( |
| 172 | scopes.contains(&"entity.other.attribute-name.localname.xml"), |
| 173 | "{scopes:?}" |
| 174 | ); |
| 175 | assert!(scopes.contains(&"string.quoted.double.xml"), "{scopes:?}"); |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn rust_highlights_function_signature_parameters() { |
| 180 | let grammar = Grammar::from_str(syntaxes::RUST).unwrap(); |
| 181 | let mut state = LineState::default(); |
| 182 | let mut tokenizer = LineTokenizer::new(&grammar); |
| 183 | let _ = tokenizer.tokenize_line(&mut state, "fn reconcile_with<C, E>("); |
| 184 | let spans = tokenizer.tokenize_line(&mut state, " mut cursor: String,"); |
| 185 | let scopes = spans |
| 186 | .iter() |
| 187 | .filter_map(|span| grammar.scopes.get(span.scope.index())) |
| 188 | .map(String::as_str) |
| 189 | .collect::<Vec<_>>(); |
| 190 | |
| 191 | assert!(scopes.contains(&"storage.modifier.mut.rust")); |
| 192 | assert!(scopes.contains(&"variable.parameter.rust")); |
| 193 | assert!(scopes.contains(&"entity.name.type.rust")); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn fish_highlights_keywords_variables_and_strings() { |
| 198 | let grammar = Grammar::from_str(syntaxes::FISH).unwrap(); |
| 199 | let scopes = tokenize_scopes(&grammar, "if test -n \"$argv\""); |
| 200 | |
| 201 | assert!(scopes.contains(&"keyword.control.fish")); |
| 202 | assert!(scopes.contains(&"source.option.fish")); |
| 203 | assert!(scopes.contains(&"string.quoted.double.fish")); |
| 204 | assert!(scopes.contains(&"variable.language.fish")); |
| 205 | } |
| 206 | |
| 207 | #[test] |
| 208 | fn lua_highlights_keywords_booleans_and_strings() { |
| 209 | let grammar = Grammar::from_str(syntaxes::LUA).unwrap(); |
| 210 | let scopes = tokenize_scopes(&grammar, "local ok = true and \"done\""); |
| 211 | |
| 212 | assert!(scopes.contains(&"keyword.local.lua")); |
| 213 | assert!(scopes.contains(&"constant.language.lua")); |
| 214 | assert!(scopes.contains(&"keyword.operator.logical.lua")); |
| 215 | assert!(scopes.contains(&"string.quoted.double.lua")); |
| 216 | } |
| 217 | |
| 218 | #[test] |
| 219 | fn css_root_comments_are_highlighted() { |
| 220 | let grammar = Grammar::from_str(syntaxes::CSS).unwrap(); |
| 221 | let mut state = LineState::default(); |
| 222 | let line = "/**/"; |
| 223 | let spans = grammar.tokenize_line(&mut state, line); |
| 224 | let scopes = spans |
| 225 | .iter() |
| 226 | .filter_map(|span| grammar.scopes.get(span.scope.index())) |
| 227 | .map(String::as_str) |
| 228 | .collect::<Vec<_>>(); |
| 229 | |
| 230 | assert!(scopes.contains(&"comment.block.css")); |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn css_property_values_highlight_urls_and_colors_inside_rule() { |
| 235 | let grammar = Grammar::from_str(syntaxes::CSS).unwrap(); |
| 236 | let scopes = css_rule_scopes( |
| 237 | &grammar, |
| 238 | "background: url(icon.svg); color: rgb(1 2 3); border-color: #aabbcc;", |
| 239 | ); |
| 240 | |
| 241 | assert!(scopes.contains(&"meta.property-list.css")); |
| 242 | assert!(scopes.contains(&"meta.property-value.css")); |
| 243 | assert!(scopes.contains(&"meta.function.url.css")); |
| 244 | assert!(scopes.contains(&"meta.function.color.css")); |
| 245 | assert!(scopes.contains(&"constant.other.color.rgb-value.hex.css")); |
| 246 | } |
| 247 | |
| 248 | #[test] |
| 249 | fn go_make_calls_highlight_arguments_and_type_brackets() { |
| 250 | let grammar = Grammar::from_str(syntaxes::GO).unwrap(); |
| 251 | let line = "make([]LayerModule, 0, len(layer.Modules))"; |
| 252 | let mut state = LineState::default(); |
| 253 | let mut tokenizer = LineTokenizer::new(&grammar); |
| 254 | let spans = tokenizer.tokenize_line(&mut state, line); |
| 255 | let scopes = spans |
| 256 | .iter() |
| 257 | .filter_map(|span| grammar.scopes.get(span.scope.index())) |
| 258 | .map(String::as_str) |
| 259 | .collect::<Vec<_>>(); |
| 260 | assert!( |
| 261 | scopes.contains(&"constant.numeric.decimal.go"), |
| 262 | "{scopes:?}" |
| 263 | ); |
| 264 | assert!( |
| 265 | scopes.contains(&"entity.name.function.support.builtin.go"), |
| 266 | "{scopes:?}" |
| 267 | ); |
| 268 | assert!( |
| 269 | scopes.contains(&"punctuation.definition.begin.bracket.square.go"), |
| 270 | "{scopes:?}" |
| 271 | ); |
| 272 | assert!( |
| 273 | scopes.contains(&"punctuation.definition.end.bracket.square.go"), |
| 274 | "{scopes:?}" |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | #[test] |
| 279 | fn toml_highlights_keys_sections_strings_and_booleans() { |
| 280 | let grammar = Grammar::from_str(syntaxes::TOML).unwrap(); |
| 281 | let section_scopes = tokenize_scopes(&grammar, "[package]"); |
| 282 | let value_scopes = tokenize_scopes(&grammar, "name = \"rustik\""); |
| 283 | let boolean_scopes = tokenize_scopes(&grammar, "publish = false"); |
| 284 | |
| 285 | assert!(section_scopes.contains(&"entity.name.section.toml")); |
| 286 | assert!(value_scopes.contains(&"variable.other.key.toml")); |
| 287 | assert!(value_scopes.contains(&"punctuation.separator.key-value.toml")); |
| 288 | assert!(value_scopes.contains(&"string.quoted.double.toml")); |
| 289 | assert!(boolean_scopes.contains(&"constant.language.boolean.toml")); |
| 290 | } |
| 291 | |
| 292 | #[test] |
| 293 | fn yaml_highlights_keys_scalars_and_comments() { |
| 294 | let grammar = Grammar::from_str(syntaxes::YAML).unwrap(); |
| 295 | let key_scopes = tokenize_scopes(&grammar, "name: rustik"); |
| 296 | let scalar_scopes = tokenize_scopes(&grammar, "name: \"rustik\""); |
| 297 | let comment_scopes = tokenize_scopes(&grammar, "# generated"); |
| 298 | |
| 299 | assert!(key_scopes.contains(&"entity.name.tag.yaml")); |
| 300 | assert!(key_scopes.contains(&"punctuation.separator.key-value.mapping.yaml")); |
| 301 | assert!(scalar_scopes.contains(&"string.quoted.double.yaml")); |
| 302 | assert!(comment_scopes.contains(&"comment.line.number-sign.yaml")); |
| 303 | } |
| 304 | |
| 305 | #[test] |
| 306 | fn markdown_highlights_reference_links() { |
| 307 | let grammar = Grammar::from_str(syntaxes::MARKDOWN).unwrap(); |
| 308 | let scopes = tokenize_scopes(&grammar, "[Earlier this week][post]"); |
| 309 | |
| 310 | assert!( |
| 311 | scopes.contains(&"meta.link.reference.markdown"), |
| 312 | "{scopes:?}" |
| 313 | ); |
| 314 | assert!( |
| 315 | scopes.contains(&"string.other.link.title.markdown"), |
| 316 | "{scopes:?}" |
| 317 | ); |
| 318 | assert!( |
| 319 | scopes.contains(&"constant.other.reference.link.markdown"), |
| 320 | "{scopes:?}" |
| 321 | ); |
| 322 | } |
| 323 | |
| 324 | #[test] |
| 325 | fn radiance_highlights_language_constructs() { |
| 326 | let grammar = Grammar::from_str(syntaxes::RADIANCE).unwrap(); |
| 327 | let scopes = tokenize_scopes( |
| 328 | &grammar, |
| 329 | "@test export fn decode(ch: u8) -> bool { return ch >= '0'; }", |
| 330 | ); |
| 331 | let module_scopes = tokenize_scopes(&grammar, "mod scanner;"); |
| 332 | let import_scopes = tokenize_scopes(&grammar, "use scanner;"); |
| 333 | |
| 334 | assert!( |
| 335 | scopes.contains(&"support.function.builtin.radiance"), |
| 336 | "{scopes:?}" |
| 337 | ); |
| 338 | assert!( |
| 339 | scopes.contains(&"entity.name.function.radiance"), |
| 340 | "{scopes:?}" |
| 341 | ); |
| 342 | assert!(scopes.contains(&"storage.type.radiance"), "{scopes:?}"); |
| 343 | assert!( |
| 344 | scopes.contains(&"string.quoted.single.radiance"), |
| 345 | "{scopes:?}" |
| 346 | ); |
| 347 | assert!(scopes.contains(&"keyword.operator.radiance"), "{scopes:?}"); |
| 348 | assert!( |
| 349 | module_scopes.contains(&"entity.name.namespace.radiance"), |
| 350 | "{module_scopes:?}" |
| 351 | ); |
| 352 | assert!( |
| 353 | import_scopes.contains(&"entity.name.namespace.radiance"), |
| 354 | "{import_scopes:?}" |
| 355 | ); |
| 356 | } |
| 357 | |
| 358 | #[test] |
| 359 | fn radiance_highlights_lexical_regions() { |
| 360 | let grammar = Grammar::from_str(syntaxes::RADIANCE).unwrap(); |
| 361 | let scopes = tokenize_scopes( |
| 362 | &grammar, |
| 363 | "fn store 'long 'short (dst: &'short mut ?&'long u32) where 'long: 'short {}", |
| 364 | ); |
| 365 | let where_scopes = tokenize_scopes(&grammar, "where"); |
| 366 | let character_scopes = tokenize_scopes(&grammar, "let ch = 'region';"); |
| 367 | |
| 368 | assert!( |
| 369 | scopes.contains(&"entity.name.function.radiance"), |
| 370 | "{scopes:?}" |
| 371 | ); |
| 372 | assert!( |
| 373 | scopes.contains(&"entity.name.type.region.radiance"), |
| 374 | "{scopes:?}" |
| 375 | ); |
| 376 | assert!( |
| 377 | where_scopes.contains(&"keyword.control.radiance"), |
| 378 | "{where_scopes:?}" |
| 379 | ); |
| 380 | assert!( |
| 381 | !scopes.contains(&"string.quoted.single.radiance"), |
| 382 | "{scopes:?}" |
| 383 | ); |
| 384 | assert!( |
| 385 | character_scopes.contains(&"string.quoted.single.radiance"), |
| 386 | "{character_scopes:?}" |
| 387 | ); |
| 388 | assert!( |
| 389 | !character_scopes.contains(&"entity.name.type.region.radiance"), |
| 390 | "{character_scopes:?}" |
| 391 | ); |
| 392 | } |
| 393 | |
| 394 | #[test] |
| 395 | fn radiance_highlights_scoped_region_bindings() { |
| 396 | let grammar = Grammar::from_str(syntaxes::RADIANCE).unwrap(); |
| 397 | let session_scopes = tokenize_scopes(&grammar, "use arena as objects in {"); |
| 398 | let region_scopes = tokenize_scopes( |
| 399 | &grammar, |
| 400 | "let resolved: 'phase = &*res where 'arena: 'phase in {", |
| 401 | ); |
| 402 | |
| 403 | assert!( |
| 404 | session_scopes.contains(&"meta.binding.session.radiance"), |
| 405 | "{session_scopes:?}" |
| 406 | ); |
| 407 | assert!( |
| 408 | session_scopes.contains(&"variable.other.radiance"), |
| 409 | "{session_scopes:?}" |
| 410 | ); |
| 411 | assert!( |
| 412 | !session_scopes.contains(&"entity.name.namespace.radiance"), |
| 413 | "{session_scopes:?}" |
| 414 | ); |
| 415 | assert!( |
| 416 | region_scopes.contains(&"meta.binding.region.radiance"), |
| 417 | "{region_scopes:?}" |
| 418 | ); |
| 419 | assert!( |
| 420 | region_scopes.contains(&"variable.other.radiance"), |
| 421 | "{region_scopes:?}" |
| 422 | ); |
| 423 | assert!( |
| 424 | region_scopes.contains(&"entity.name.type.region.radiance"), |
| 425 | "{region_scopes:?}" |
| 426 | ); |
| 427 | } |
| 428 | |
| 429 | #[test] |
| 430 | fn ril_highlights_language_constructs() { |
| 431 | let grammar = Grammar::from_str(syntaxes::RIL).unwrap(); |
| 432 | let scopes = tokenize_scopes(&grammar, "br.ult w32 %7 3 @guard#pass $WORDS;"); |
| 433 | let quoted_scopes = tokenize_scopes(&grammar, "fn w32 $\"quoted.name\"() {"); |
| 434 | |
| 435 | assert!(scopes.contains(&"keyword.control.branch.ril"), "{scopes:?}"); |
| 436 | assert!( |
| 437 | scopes.contains(&"keyword.operator.comparison.ril"), |
| 438 | "{scopes:?}" |
| 439 | ); |
| 440 | assert!(scopes.contains(&"storage.type.ril"), "{scopes:?}"); |
| 441 | assert!( |
| 442 | scopes.contains(&"variable.other.register.ril"), |
| 443 | "{scopes:?}" |
| 444 | ); |
| 445 | assert!( |
| 446 | scopes.contains(&"constant.numeric.integer.decimal.ril"), |
| 447 | "{scopes:?}" |
| 448 | ); |
| 449 | assert!(scopes.contains(&"entity.name.label.ril"), "{scopes:?}"); |
| 450 | assert!(scopes.contains(&"variable.other.symbol.ril"), "{scopes:?}"); |
| 451 | assert!( |
| 452 | quoted_scopes.contains(&"variable.other.symbol.quoted.ril"), |
| 453 | "{quoted_scopes:?}" |
| 454 | ); |
| 455 | } |
| 456 | |
| 457 | /// Tokenizes `line` and returns its scope names. |
| 458 | fn tokenize_scopes<'a>(grammar: &'a Grammar, line: &str) -> Vec<&'a str> { |
| 459 | let mut state = LineState::default(); |
| 460 | let mut tokenizer = LineTokenizer::new(grammar); |
| 461 | let spans = tokenizer.tokenize_line(&mut state, line); |
| 462 | |
| 463 | spans |
| 464 | .iter() |
| 465 | .filter_map(|span| grammar.scopes.get(span.scope.index())) |
| 466 | .map(String::as_str) |
| 467 | .collect() |
| 468 | } |
| 469 | |
| 470 | /// Tokenizes `line` inside a CSS rule body and returns its scope names. |
| 471 | fn css_rule_scopes<'a>(grammar: &'a Grammar, line: &str) -> Vec<&'a str> { |
| 472 | let mut state = LineState::default(); |
| 473 | let mut tokenizer = LineTokenizer::new(grammar); |
| 474 | let _ = tokenizer.tokenize_line(&mut state, ".x {"); |
| 475 | let spans = tokenizer.tokenize_line(&mut state, line); |
| 476 | |
| 477 | spans |
| 478 | .iter() |
| 479 | .filter_map(|span| grammar.scopes.get(span.scope.index())) |
| 480 | .map(String::as_str) |
| 481 | .collect() |
| 482 | } |