@@ -12,7 +12,9 @@ use tree_sitter::StreamingIterator;
1212use carbon:: { TextByteRange , u32_to_usize_saturating, usize_to_u32_saturating} ;
1313
1414use crate :: error:: { PhosphorError , Result } ;
15- use crate :: { HighlightKind , HighlightLineBuffer , HighlightSpan , LanguageId , LanguageMetadata } ;
15+ use crate :: {
16+ HighlightKind , HighlightLineBuffer , HighlightSpan , LanguageId , LanguageMetadata , ParsedSyntax ,
17+ } ;
1618
1719#[ derive( Debug ) ]
1820struct CompiledLanguage {
@@ -140,6 +142,16 @@ pub(crate) fn highlight(language: LanguageId, source: &str) -> Result<Vec<Highli
140142 compact_spans ( language, raw_spans)
141143}
142144
145+ pub ( crate ) fn parse ( language : LanguageId , source : & str ) -> Result < ParsedSyntax > {
146+ if !is_parser_available ( language) {
147+ return Err ( PhosphorError :: MissingParser { language } ) ;
148+ }
149+
150+ let compiled = compiled_language ( language) ?;
151+ let tree = parse_source ( & compiled, source) ?;
152+ Ok ( ParsedSyntax { language, tree } )
153+ }
154+
143155pub ( crate ) fn highlight_text_ranges (
144156 language : LanguageId ,
145157 source : & str ,
@@ -163,6 +175,24 @@ pub(crate) fn highlight_text_ranges(
163175 compact_spans ( language, raw_spans)
164176}
165177
178+ pub ( crate ) fn highlight_text_ranges_with_parse (
179+ parsed : & ParsedSyntax ,
180+ source : & str ,
181+ byte_ranges : & [ TextByteRange ] ,
182+ ) -> Result < Vec < HighlightSpan > > {
183+ if source. is_empty ( ) || byte_ranges. is_empty ( ) {
184+ return Ok ( Vec :: new ( ) ) ;
185+ }
186+ let ranges = merged_text_ranges ( byte_ranges, source. len ( ) ) ;
187+ if ranges. is_empty ( ) {
188+ return Ok ( Vec :: new ( ) ) ;
189+ }
190+
191+ let compiled = compiled_language ( parsed. language ) ?;
192+ let raw_spans = collect_spans_in_ranges ( & compiled, & parsed. tree , source, & ranges) ;
193+ compact_spans ( parsed. language , raw_spans)
194+ }
195+
166196pub ( crate ) fn highlight_text_lines (
167197 language : LanguageId ,
168198 source : & str ,
0 commit comments