highlight: Reuse cached entries past include limit
980c3791f0e282a92ca9769805b9647e3d1d6bbf
Top-level repository entries can already have a compiled pattern set when a nested include reaches the depth limit. Check that cache before enforcing the limit so grammars such as TypeScript can keep using shared comment rules inside nested interface contexts. Assisted-by: Codex:gpt-5.5
1 parent
2c08b65e
crates/highlight/src/grammar/pattern.rs
+3 -3
| 927 | 927 | &mut self, |
|
| 928 | 928 | current: &'raw RawGrammar, |
|
| 929 | 929 | name: &str, |
|
| 930 | 930 | repositories: &mut Vec<&'raw BTreeMap<String, RawPattern>>, |
|
| 931 | 931 | ) -> Result<Option<PatternSetId>, Error> { |
|
| 932 | - | if self.repository_depth > MAX_INCLUDE_DEPTH { |
|
| 933 | - | return Ok(None); |
|
| 934 | - | } |
|
| 935 | 932 | let Some((pattern, top_level)) = repositories.iter().rev().find_map(|repository| { |
|
| 936 | 933 | repository.get(name).map(|pattern| { |
|
| 937 | 934 | let top_level = std::ptr::eq(*repository, ¤t.repository); |
|
| 938 | 935 | (pattern, top_level) |
|
| 939 | 936 | }) |
| 942 | 939 | }; |
|
| 943 | 940 | let key = (current.scope_name.as_str(), name.to_owned()); |
|
| 944 | 941 | if top_level && let Some(id) = self.repository.get(&key) { |
|
| 945 | 942 | return Ok(Some(*id)); |
|
| 946 | 943 | } |
|
| 944 | + | if self.repository_depth > MAX_INCLUDE_DEPTH { |
|
| 945 | + | return Ok(None); |
|
| 946 | + | } |
|
| 947 | 947 | let id = self.reserve_set(); |
|
| 948 | 948 | ||
| 949 | 949 | if top_level { |
|
| 950 | 950 | self.repository.insert(key, id); |
|
| 951 | 951 | } |
crates/highlight/tests/syntaxes.rs
+19 -0
| 118 | 118 | ||
| 119 | 119 | assert!(scopes.contains(&"string.quoted.single.js")); |
|
| 120 | 120 | assert!(scopes.contains(&"storage.type.function.js")); |
|
| 121 | 121 | } |
|
| 122 | 122 | ||
| 123 | + | #[test] |
|
| 124 | + | fn typescript_highlights_comments_inside_interface_body() { |
|
| 125 | + | let grammar = Grammar::from_str(syntaxes::TYPESCRIPT).unwrap(); |
|
| 126 | + | let mut state = LineState::default(); |
|
| 127 | + | let mut tokenizer = LineTokenizer::new(&grammar); |
|
| 128 | + | let _ = tokenizer.tokenize_line(&mut state, "export interface OperationSpan {"); |
|
| 129 | + | let spans = tokenizer.tokenize_line(&mut state, " /** Semantic role for this group. */"); |
|
| 130 | + | let scopes = spans |
|
| 131 | + | .iter() |
|
| 132 | + | .filter_map(|span| grammar.scopes.get(span.scope.index())) |
|
| 133 | + | .map(String::as_str) |
|
| 134 | + | .collect::<Vec<_>>(); |
|
| 135 | + | ||
| 136 | + | assert!( |
|
| 137 | + | scopes.contains(&"comment.block.documentation.ts"), |
|
| 138 | + | "{scopes:?}" |
|
| 139 | + | ); |
|
| 140 | + | } |
|
| 141 | + | ||
| 123 | 142 | #[test] |
|
| 124 | 143 | fn fish_highlights_keywords_variables_and_strings() { |
|
| 125 | 144 | let grammar = Grammar::from_str(syntaxes::FISH).unwrap(); |
|
| 126 | 145 | let scopes = tokenize_scopes(&grammar, "if test -n \"$argv\""); |
|
| 127 | 146 |