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
32 changes: 31 additions & 1 deletion crates/phosphor/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use tree_sitter::StreamingIterator;
use carbon::{TextByteRange, u32_to_usize_saturating, usize_to_u32_saturating};

use crate::error::{PhosphorError, Result};
use crate::{HighlightKind, HighlightLineBuffer, HighlightSpan, LanguageId, LanguageMetadata};
use crate::{
HighlightKind, HighlightLineBuffer, HighlightSpan, LanguageId, LanguageMetadata, ParsedSyntax,
};

#[derive(Debug)]
struct CompiledLanguage {
Expand Down Expand Up @@ -140,6 +142,16 @@ pub(crate) fn highlight(language: LanguageId, source: &str) -> Result<Vec<Highli
compact_spans(language, raw_spans)
}

pub(crate) fn parse(language: LanguageId, source: &str) -> Result<ParsedSyntax> {
if !is_parser_available(language) {
return Err(PhosphorError::MissingParser { language });
}

let compiled = compiled_language(language)?;
let tree = parse_source(&compiled, source)?;
Ok(ParsedSyntax { language, tree })
}

pub(crate) fn highlight_text_ranges(
language: LanguageId,
source: &str,
Expand All @@ -163,6 +175,24 @@ pub(crate) fn highlight_text_ranges(
compact_spans(language, raw_spans)
}

pub(crate) fn highlight_text_ranges_with_parse(
parsed: &ParsedSyntax,
source: &str,
byte_ranges: &[TextByteRange],
) -> Result<Vec<HighlightSpan>> {
if source.is_empty() || byte_ranges.is_empty() {
return Ok(Vec::new());
}
let ranges = merged_text_ranges(byte_ranges, source.len());
if ranges.is_empty() {
return Ok(Vec::new());
}

let compiled = compiled_language(parsed.language)?;
let raw_spans = collect_spans_in_ranges(&compiled, &parsed.tree, source, &ranges);
compact_spans(parsed.language, raw_spans)
}

pub(crate) fn highlight_text_lines(
language: LanguageId,
source: &str,
Expand Down
21 changes: 20 additions & 1 deletion crates/phosphor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use error::{PhosphorError, Result};
pub use pack::PackInstaller;
pub use types::{
HighlightKind, HighlightLine, HighlightLineBuffer, HighlightSpan, HighlightSpanRange,
LanguageId, LanguageMetadata,
LanguageId, LanguageMetadata, ParsedSyntax,
};

#[derive(Debug, Default, Clone, Copy)]
Expand Down Expand Up @@ -77,6 +77,25 @@ impl Highlighter {
language::highlight_text_ranges(language, source, byte_ranges)
}

pub fn parse_text_store_language(
&self,
language: LanguageId,
text: &TextStore,
) -> Result<ParsedSyntax> {
let source = text.as_str().ok_or(PhosphorError::InvalidUtf8)?;
language::parse(language, source)
}

pub fn highlight_text_store_language_ranges_with_parse(
&self,
parsed: &ParsedSyntax,
text: &TextStore,
byte_ranges: &[TextByteRange],
) -> Result<Vec<HighlightSpan>> {
let source = text.as_str().ok_or(PhosphorError::InvalidUtf8)?;
language::highlight_text_ranges_with_parse(parsed, source, byte_ranges)
}

pub fn highlight_text_store_language_lines(
&self,
language: LanguageId,
Expand Down
7 changes: 7 additions & 0 deletions crates/phosphor/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use carbon::{TextByteRange, u32_to_usize_saturating, usize_to_u32_saturating};
use tree_sitter as ts;

#[repr(u8)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
Expand Down Expand Up @@ -110,6 +111,12 @@ impl HighlightLineBuffer {
}
}

#[derive(Debug, Clone)]
pub struct ParsedSyntax {
pub(crate) language: LanguageId,
pub(crate) tree: ts::Tree,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LanguageId {
Bash,
Expand Down
Loading