mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
Avoid allocations related to markdown parsing (#65409)
Follow-up to https://github.com/zed-industries/zed/pull/65398#pullrequestreview-5471402233 Not implemented from the feedback: - Returning the cached `Arc` runs from `highlight_text` behind a newtype: read-only callers now use `highlight_text_resolved(..).runs` directly, while `CodeLabel` callers still need an owned `Vec`, which `highlight_text` now collects once. - Caching the `Rope` itself to avoid the copy on a cache miss: this needs a new rope-to-rope comparison and roughly doubles the memory each entry retains, so the cache keeps one `String` copy. Release Notes: - N/A
This commit is contained in:
@@ -34,7 +34,7 @@ use gpui::{
|
||||
TextStyle, WeakEntity, Window, WindowHandle, div, ease_in_out, img, linear_color_stop,
|
||||
linear_gradient, list, pulsating_between,
|
||||
};
|
||||
use language::{Buffer, Language, Rope};
|
||||
use language::{Buffer, Language};
|
||||
use language_model::{
|
||||
LanguageModelCompletionError, ProviderErrorCategory, ZED_CLOUD_PROVIDER_NAME,
|
||||
};
|
||||
|
||||
@@ -455,7 +455,7 @@ fn highlight_code_runs(
|
||||
|
||||
let mut runs = Vec::new();
|
||||
let mut offset = 0;
|
||||
for (range, highlight_id) in language.highlight_text(&Rope::from(code), 0..code.len()) {
|
||||
for (range, highlight_id) in language.highlight_text(code, 0..code.len()) {
|
||||
if range.start > offset {
|
||||
runs.push(code_text_style.to_run(range.start - offset));
|
||||
}
|
||||
|
||||
@@ -390,9 +390,10 @@ fn render_highlighted_editor(cx: &mut BenchAppContext) {
|
||||
});
|
||||
let probe = "fn main() {}";
|
||||
assert!(
|
||||
!language
|
||||
language
|
||||
.highlight_text(&Rope::from(probe), 0..probe.len())
|
||||
.is_empty(),
|
||||
.next()
|
||||
.is_some(),
|
||||
"the benchmark language must resolve syntax highlights against the theme"
|
||||
);
|
||||
let buffer = cx.update(|cx| {
|
||||
|
||||
@@ -1023,7 +1023,9 @@ fn signature_label(item: &CallHierarchyItem, language: &Arc<Language>) -> Option
|
||||
let name_start = find_symbol_name(&signature, &item.name)?;
|
||||
let filter_range = name_start..name_start + item.name.len();
|
||||
let source = Rope::from_iter([signature.as_str(), " {}"]);
|
||||
let runs = language.highlight_text(&source, 0..signature.len());
|
||||
let runs = language
|
||||
.highlight_text(&source, 0..signature.len())
|
||||
.collect();
|
||||
Some(CodeLabel::new(signature, filter_range, runs))
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ impl InlineInputPreview {
|
||||
|
||||
struct RenderedPreview {
|
||||
text: SharedString,
|
||||
highlights: Vec<(Range<usize>, HighlightId)>,
|
||||
highlights: Arc<[(Range<usize>, HighlightId)]>,
|
||||
height_in_lines: u32,
|
||||
is_error: bool,
|
||||
}
|
||||
@@ -80,9 +80,11 @@ impl RenderedPreview {
|
||||
let (text, height_in_lines) = preview.display_text(max_lines);
|
||||
let highlights = match (preview, language) {
|
||||
(InlineInputPreview::Text(_), Some(language)) => {
|
||||
language.highlight_text(&Rope::from(text.as_str()), 0..text.len())
|
||||
language
|
||||
.highlight_text_resolved(text.as_str(), 0..text.len())
|
||||
.runs
|
||||
}
|
||||
_ => Vec::new(),
|
||||
_ => Arc::default(),
|
||||
};
|
||||
Self {
|
||||
text: SharedString::from(text),
|
||||
|
||||
@@ -12,7 +12,6 @@ use multi_buffer::{Anchor, MultiBufferOffset, ToOffset};
|
||||
use settings::Settings;
|
||||
use std::ops::Range;
|
||||
use std::time::Duration;
|
||||
use text::Rope;
|
||||
use theme_settings::ThemeSettings;
|
||||
use ui::{
|
||||
ActiveTheme, AnyElement, ButtonCommon, ButtonStyle, Clickable, FluentBuilder, IconButton,
|
||||
@@ -232,10 +231,11 @@ impl Editor {
|
||||
|
||||
if let Some(language) = language {
|
||||
for signature in &mut signature_help.signatures {
|
||||
let text = Rope::from(signature.label.as_ref());
|
||||
let highlights = language
|
||||
.highlight_text(&text, 0..signature.label.len())
|
||||
.into_iter()
|
||||
.highlight_text(
|
||||
signature.label.as_str(),
|
||||
0..signature.label.len(),
|
||||
)
|
||||
.flat_map(|(range, highlight_id)| {
|
||||
Some((range, *cx.theme().syntax().get(highlight_id)?))
|
||||
});
|
||||
|
||||
@@ -2425,13 +2425,12 @@ impl RenderOnce for SyntaxHighlightedText {
|
||||
|
||||
let text = self.text.clone();
|
||||
|
||||
let highlights = self
|
||||
.language
|
||||
.highlight_text(&text.as_ref().into(), 0..text.len());
|
||||
let mut runs = Vec::with_capacity(highlights.len());
|
||||
let mut runs = Vec::new();
|
||||
let mut offset = 0;
|
||||
|
||||
for (highlight_range, highlight_id) in highlights {
|
||||
for (highlight_range, highlight_id) in
|
||||
self.language.highlight_text(text.as_str(), 0..text.len())
|
||||
{
|
||||
// Add un-highlighted text before the current highlight
|
||||
if highlight_range.start > offset {
|
||||
runs.push(text_style.to_run(highlight_range.start - offset));
|
||||
|
||||
+117
-40
@@ -49,6 +49,7 @@ pub use language_core::{
|
||||
|
||||
use futures::future::FutureExt as _;
|
||||
use language_core::highlight_cache::{MAX_TEXT_HIGHLIGHT_ENTRY_BYTES, TextHighlightKey};
|
||||
use language_core::highlight_map::HighlightCaptureRef;
|
||||
pub use language_core::{
|
||||
BlockCommentConfig, BracketPair, BracketPairConfig, BracketPairContent, BracketsConfig,
|
||||
BracketsPatternConfig, CodeLabel, CodeLabelBuilder, DebugVariablesConfig, DebuggerTextObject,
|
||||
@@ -78,13 +79,14 @@ use std::{
|
||||
ffi::OsStr,
|
||||
fmt::Debug,
|
||||
hash::Hash,
|
||||
mem,
|
||||
iter, mem,
|
||||
ops::{DerefMut, Range},
|
||||
path::{Path, PathBuf},
|
||||
str,
|
||||
sync::{Arc, LazyLock},
|
||||
};
|
||||
use syntax_map::{QueryCursorHandle, SyntaxSnapshot, flattened_highlight_regions};
|
||||
use streaming_iterator::StreamingIterator as _;
|
||||
use syntax_map::{QueryCursorHandle, TextProvider, flatten_capture_regions};
|
||||
use task::RunnableTag;
|
||||
pub use task_context::{ContextLocation, ContextProvider};
|
||||
pub use text_diff::{
|
||||
@@ -1130,17 +1132,18 @@ impl Language {
|
||||
self.toolchain.clone()
|
||||
}
|
||||
|
||||
pub fn highlight_text<'a>(
|
||||
self: &'a Arc<Self>,
|
||||
text: &'a Rope,
|
||||
pub fn highlight_text<Source: HighlightSource + ?Sized>(
|
||||
self: &Arc<Self>,
|
||||
text: &Source,
|
||||
range: Range<usize>,
|
||||
) -> Vec<(Range<usize>, HighlightId)> {
|
||||
self.highlight_text_resolved(text, range).runs.to_vec()
|
||||
) -> impl Iterator<Item = (Range<usize>, HighlightId)> + 'static + use<Source> {
|
||||
self.highlight_text_resolved(text, 0..text.source_len())
|
||||
.runs_in_range(range)
|
||||
}
|
||||
|
||||
pub fn highlight_text_resolved(
|
||||
self: &Arc<Self>,
|
||||
text: &Rope,
|
||||
text: &(impl HighlightSource + ?Sized),
|
||||
range: Range<usize>,
|
||||
) -> ResolvedHighlights {
|
||||
let Some(grammar) = &self.grammar else {
|
||||
@@ -1149,58 +1152,62 @@ impl Language {
|
||||
let Some(highlights_config) = &grammar.highlights_config else {
|
||||
return ResolvedHighlights::default();
|
||||
};
|
||||
let highlights = if text.len() > MAX_TEXT_HIGHLIGHT_ENTRY_BYTES {
|
||||
self.compute_resolved_highlights(grammar, text)
|
||||
let text_len = text.source_len();
|
||||
let highlights = if text_len > MAX_TEXT_HIGHLIGHT_ENTRY_BYTES {
|
||||
Self::compute_resolved_highlights(grammar, &highlights_config.query, text)
|
||||
} else {
|
||||
let key = TextHighlightKey::new(text.chunks(), text.len());
|
||||
let key = TextHighlightKey::new(text.source_chunks(), text_len);
|
||||
match highlights_config
|
||||
.text_highlight_cache
|
||||
.get(&key, text.chunks())
|
||||
.get(&key, text.source_chunks())
|
||||
{
|
||||
Some(highlights) => highlights,
|
||||
None => highlights_config.text_highlight_cache.insert(
|
||||
key,
|
||||
Arc::from(text.chunks().collect::<String>()),
|
||||
self.compute_resolved_highlights(grammar, text),
|
||||
),
|
||||
None => {
|
||||
let mut cached_text = String::with_capacity(text_len);
|
||||
cached_text.extend(text.source_chunks());
|
||||
highlights_config.text_highlight_cache.insert(
|
||||
key,
|
||||
cached_text,
|
||||
Self::compute_resolved_highlights(grammar, &highlights_config.query, text),
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
if range.start == 0 && range.end >= text.len() {
|
||||
if range.start == 0 && range.end >= text_len {
|
||||
return highlights;
|
||||
}
|
||||
ResolvedHighlights {
|
||||
sources: highlights.sources.clone(),
|
||||
runs: highlights
|
||||
.runs
|
||||
.iter()
|
||||
.filter(|(run_range, _)| run_range.start < range.end && run_range.end > range.start)
|
||||
.map(|(run_range, highlight_id)| {
|
||||
(
|
||||
run_range.start.max(range.start) - range.start
|
||||
..run_range.end.min(range.end) - range.start,
|
||||
*highlight_id,
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
runs: highlights.runs_in_range(range).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_resolved_highlights(
|
||||
self: &Arc<Self>,
|
||||
grammar: &Arc<Grammar>,
|
||||
text: &Rope,
|
||||
query: &tree_sitter::Query,
|
||||
text: &(impl HighlightSource + ?Sized),
|
||||
) -> ResolvedHighlights {
|
||||
let highlight_map = grammar.highlight_map();
|
||||
let tree = parse_text(grammar, text, None);
|
||||
let captures =
|
||||
SyntaxSnapshot::single_tree_captures(0..text.len(), text, &tree, self, |grammar| {
|
||||
grammar
|
||||
.highlights_config
|
||||
.as_ref()
|
||||
.map(|config| &config.query)
|
||||
let text_len = text.source_len();
|
||||
let tree = text.parse(grammar);
|
||||
let regions = with_query_cursor(|cursor| {
|
||||
cursor.set_byte_range(0..text_len);
|
||||
let mut captures = cursor.captures(query, tree.root_node(), text.text_provider());
|
||||
let capture_refs = iter::from_fn(move || {
|
||||
let (query_match, capture_index) = captures.next()?;
|
||||
let capture = query_match.captures[*capture_index];
|
||||
Some((
|
||||
capture.node.byte_range(),
|
||||
HighlightCaptureRef {
|
||||
grammar_index: 0,
|
||||
capture_id: CaptureId(capture.index),
|
||||
},
|
||||
))
|
||||
});
|
||||
flatten_capture_regions(0..text_len, capture_refs)
|
||||
});
|
||||
let mut runs = Vec::<(Range<usize>, HighlightId)>::new();
|
||||
for region in flattened_highlight_regions(captures, 0..text.len()) {
|
||||
for region in regions {
|
||||
let highlight_id = region
|
||||
.stack
|
||||
.iter()
|
||||
@@ -1460,6 +1467,54 @@ impl Debug for Language {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait HighlightSource {
|
||||
fn source_len(&self) -> usize;
|
||||
fn source_chunks(&self) -> impl Iterator<Item = &str>;
|
||||
fn parse(&self, grammar: &Grammar) -> Tree;
|
||||
fn text_provider<'a>(&'a self) -> impl tree_sitter::TextProvider<&'a [u8]> + 'a;
|
||||
}
|
||||
|
||||
impl HighlightSource for str {
|
||||
fn source_len(&self) -> usize {
|
||||
self.len()
|
||||
}
|
||||
|
||||
fn source_chunks(&self) -> impl Iterator<Item = &str> {
|
||||
iter::once(self)
|
||||
}
|
||||
|
||||
fn parse(&self, grammar: &Grammar) -> Tree {
|
||||
with_parser(|parser| {
|
||||
parser
|
||||
.set_language(&grammar.ts_language)
|
||||
.expect("incompatible grammar");
|
||||
parser.parse(self, None).unwrap()
|
||||
})
|
||||
}
|
||||
|
||||
fn text_provider<'a>(&'a self) -> impl tree_sitter::TextProvider<&'a [u8]> + 'a {
|
||||
self.as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl HighlightSource for Rope {
|
||||
fn source_len(&self) -> usize {
|
||||
self.len()
|
||||
}
|
||||
|
||||
fn source_chunks(&self) -> impl Iterator<Item = &str> {
|
||||
self.chunks()
|
||||
}
|
||||
|
||||
fn parse(&self, grammar: &Grammar) -> Tree {
|
||||
parse_text(grammar, self, None)
|
||||
}
|
||||
|
||||
fn text_provider<'a>(&'a self) -> impl tree_sitter::TextProvider<&'a [u8]> + 'a {
|
||||
TextProvider(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn parse_text(grammar: &Grammar, text: &Rope, old_tree: Option<Tree>) -> Tree {
|
||||
with_parser(|parser| {
|
||||
parser
|
||||
@@ -1838,6 +1893,11 @@ mod tests {
|
||||
Arc::ptr_eq(&highlights.runs, &memoized.runs),
|
||||
"repeated highlighting of the same text must be memoized"
|
||||
);
|
||||
let memoized_str = language.highlight_text_resolved(code, 0..code.len());
|
||||
assert!(
|
||||
Arc::ptr_eq(&highlights.runs, &memoized_str.runs),
|
||||
"str and rope sources of the same text must share memoized highlights"
|
||||
);
|
||||
let partial = language.highlight_text_resolved(&Rope::from(code), 0..2);
|
||||
assert!(
|
||||
!Arc::ptr_eq(&highlights.runs, &partial.runs),
|
||||
@@ -1845,6 +1905,8 @@ mod tests {
|
||||
);
|
||||
assert_eq!(partial.runs.as_ref(), &[(0..2, highlights.runs[0].1)]);
|
||||
|
||||
let runs = language.highlight_text(String::from(code).as_str(), 1..5);
|
||||
|
||||
let richer_theme = SyntaxTheme::new(
|
||||
[
|
||||
("function", rgba(0x100000ff)),
|
||||
@@ -1855,6 +1917,13 @@ mod tests {
|
||||
.map(|(name, color)| (name.to_string(), (*color).into())),
|
||||
);
|
||||
language.set_theme(&richer_theme);
|
||||
assert_eq!(
|
||||
runs.map(|(range, highlight_id)| {
|
||||
(range, theme.get_capture_name(highlight_id).unwrap())
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
vec![(0..1, "keyword"), (1..4, "function")]
|
||||
);
|
||||
assert!(
|
||||
!highlights.is_current(),
|
||||
"a theme change must invalidate previously resolved highlights"
|
||||
@@ -1916,6 +1985,14 @@ mod tests {
|
||||
small_highlights.runs.as_ref(),
|
||||
"texts over the cache entry cap must still be highlighted"
|
||||
);
|
||||
assert_eq!(
|
||||
language
|
||||
.highlight_text_resolved(oversized_code.as_str(), 0..oversized_code.len())
|
||||
.runs
|
||||
.as_ref(),
|
||||
first_highlights.runs.as_ref(),
|
||||
"str and rope sources must be highlighted identically"
|
||||
);
|
||||
let second_highlights =
|
||||
language.highlight_text_resolved(&oversized_rope, 0..oversized_code.len());
|
||||
assert_eq!(
|
||||
|
||||
@@ -2,7 +2,6 @@ use crate::{BufferSnapshot, Language, Point, ToPoint, ToTreeSitterPoint};
|
||||
use fuzzy_nucleo::{Case, LengthPenalty, StringMatch, StringMatchCandidate};
|
||||
use gpui::{BackgroundExecutor, HighlightStyle, SharedString};
|
||||
use std::{ops::Range, sync::Arc};
|
||||
use text::Rope;
|
||||
use theme::SyntaxTheme;
|
||||
|
||||
/// An outline of all the symbols contained in a buffer.
|
||||
@@ -269,9 +268,10 @@ pub fn highlight_ranges_from_text(
|
||||
language: &Arc<Language>,
|
||||
syntax_theme: &SyntaxTheme,
|
||||
) -> Vec<(Range<usize>, HighlightStyle)> {
|
||||
let rope = Rope::from(text);
|
||||
let runs = language.highlight_text(&rope, 0..text.len());
|
||||
syntax_theme.resolve_runs(&runs).collect()
|
||||
language
|
||||
.highlight_text(text, 0..text.len())
|
||||
.filter_map(|(range, highlight_id)| Some((range, *syntax_theme.get(highlight_id)?)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Interleaves synthetic [`OutlineSearchEntry::Ancestor`] rows before each match so callers
|
||||
|
||||
@@ -115,7 +115,7 @@ pub(crate) fn flattened_highlight_regions(
|
||||
flatten_capture_regions(range, capture_refs)
|
||||
}
|
||||
|
||||
fn flatten_capture_regions(
|
||||
pub(crate) fn flatten_capture_regions(
|
||||
range: Range<usize>,
|
||||
mut captures: impl Iterator<Item = (Range<usize>, HighlightCaptureRef)>,
|
||||
) -> Vec<HighlightCaptureRegion> {
|
||||
@@ -347,7 +347,7 @@ struct ChangedRegion {
|
||||
#[derive(Default)]
|
||||
struct ChangeRegionSet(Vec<ChangedRegion>);
|
||||
|
||||
struct TextProvider<'a>(&'a Rope);
|
||||
pub(crate) struct TextProvider<'a>(pub(crate) &'a Rope);
|
||||
|
||||
pub(crate) struct QueryCursorHandle(Option<QueryCursor>);
|
||||
|
||||
@@ -1044,28 +1044,6 @@ impl SyntaxSnapshot {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn single_tree_captures<'a>(
|
||||
range: Range<usize>,
|
||||
text: &'a Rope,
|
||||
tree: &'a tree_sitter::Tree,
|
||||
language: &'a Arc<Language>,
|
||||
query: fn(&Grammar) -> Option<&Query>,
|
||||
) -> SyntaxMapCaptures<'a> {
|
||||
SyntaxMapCaptures::new(
|
||||
range,
|
||||
text,
|
||||
[SyntaxLayer {
|
||||
language,
|
||||
tree,
|
||||
included_sub_ranges: None,
|
||||
depth: 0,
|
||||
offset: (0, tree_sitter::Point::new(0, 0)),
|
||||
}]
|
||||
.into_iter(),
|
||||
query,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn captures<'a>(
|
||||
&'a self,
|
||||
range: Range<usize>,
|
||||
|
||||
@@ -43,6 +43,23 @@ pub struct ResolvedHighlights {
|
||||
}
|
||||
|
||||
impl ResolvedHighlights {
|
||||
pub fn runs_in_range(
|
||||
self,
|
||||
range: Range<usize>,
|
||||
) -> impl Iterator<Item = (Range<usize>, HighlightId)> {
|
||||
let runs = self.runs;
|
||||
(0..runs.len()).filter_map(move |index| {
|
||||
let (run_range, highlight_id) = &runs[index];
|
||||
(run_range.start < range.end && run_range.end > range.start).then(|| {
|
||||
(
|
||||
run_range.start.max(range.start) - range.start
|
||||
..run_range.end.min(range.end) - range.start,
|
||||
*highlight_id,
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_current(&self) -> bool {
|
||||
self.sources
|
||||
.iter()
|
||||
@@ -94,7 +111,7 @@ impl TextHighlightCache {
|
||||
pub fn insert(
|
||||
&self,
|
||||
key: TextHighlightKey,
|
||||
text: Arc<str>,
|
||||
text: String,
|
||||
highlights: ResolvedHighlights,
|
||||
) -> ResolvedHighlights {
|
||||
let cost = text.len() + highlights.cost_bytes();
|
||||
@@ -196,7 +213,7 @@ impl<K: Hash + Eq, V> CostBudgetedLru<K, V> {
|
||||
}
|
||||
|
||||
struct TextHighlightEntry {
|
||||
text: Arc<str>,
|
||||
text: String,
|
||||
highlights: ResolvedHighlights,
|
||||
}
|
||||
|
||||
@@ -215,6 +232,54 @@ fn chunks_match_text<'a>(text: &str, text_chunks: impl Iterator<Item = &'a str>)
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_runs_in_range() {
|
||||
let first = HighlightId::new(0);
|
||||
let second = HighlightId::new(1);
|
||||
let runs = Arc::from([(2..5, first), (7..11, second)]);
|
||||
for (range, expected) in [
|
||||
(0..usize::MAX, vec![(2..5, first), (7..11, second)]),
|
||||
(1..9, vec![(1..4, first), (6..8, second)]),
|
||||
(3..8, vec![(0..2, first), (4..5, second)]),
|
||||
(5..7, Vec::new()),
|
||||
(0..0, Vec::new()),
|
||||
(6..6, Vec::new()),
|
||||
(11..usize::MAX, Vec::new()),
|
||||
(usize::MAX..usize::MAX, Vec::new()),
|
||||
] {
|
||||
let highlights = ResolvedHighlights {
|
||||
runs: Arc::clone(&runs),
|
||||
..ResolvedHighlights::default()
|
||||
};
|
||||
assert_eq!(
|
||||
highlights.runs_in_range(range.clone()).collect::<Vec<_>>(),
|
||||
expected,
|
||||
"{range:?}"
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
ResolvedHighlights::default()
|
||||
.runs_in_range(0..usize::MAX)
|
||||
.next(),
|
||||
None
|
||||
);
|
||||
|
||||
let weak_runs = Arc::downgrade(&runs);
|
||||
let highlights = ResolvedHighlights {
|
||||
runs: Arc::clone(&runs),
|
||||
..ResolvedHighlights::default()
|
||||
};
|
||||
let mut iterator = highlights.runs_in_range(3..9);
|
||||
assert_eq!(Arc::strong_count(&runs), 2);
|
||||
drop(runs);
|
||||
assert_eq!(iterator.next(), Some((0..2, first)));
|
||||
assert_eq!(iterator.next(), Some((4..6, second)));
|
||||
assert_eq!(iterator.next(), None);
|
||||
assert_eq!(iterator.next(), None);
|
||||
drop(iterator);
|
||||
assert!(weak_runs.upgrade().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_budget_evicts_least_recently_used() {
|
||||
let overhead = CostBudgetedLru::<&str, u32>::ENTRY_OVERHEAD_BYTES;
|
||||
@@ -285,7 +350,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_oversized_highlights_are_returned_but_not_cached() {
|
||||
let text = Arc::<str>::from("fn main() {}");
|
||||
let text = "fn main() {}";
|
||||
let run_bytes = size_of::<(Range<usize>, HighlightId)>();
|
||||
let max_entry_cost = text.len() + 2 * run_bytes;
|
||||
let cache = TextHighlightCache(Mutex::new(CostBudgetedLru::new(
|
||||
@@ -300,12 +365,12 @@ mod tests {
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let small_text = Arc::<str>::from("fn f() {}");
|
||||
let small_key = TextHighlightKey::new([small_text.as_ref()].into_iter(), small_text.len());
|
||||
let small_text = "fn f() {}";
|
||||
let small_key = TextHighlightKey::new([small_text].into_iter(), small_text.len());
|
||||
let small_range = 0..2;
|
||||
let small_highlights = cache.insert(
|
||||
small_key.clone(),
|
||||
Arc::clone(&small_text),
|
||||
small_text.to_owned(),
|
||||
resolved(std::slice::from_ref(&small_range)),
|
||||
);
|
||||
assert_eq!(
|
||||
@@ -314,17 +379,17 @@ mod tests {
|
||||
);
|
||||
assert_eq!(
|
||||
cache
|
||||
.get(&small_key, [small_text.as_ref()].into_iter())
|
||||
.get(&small_key, [small_text].into_iter())
|
||||
.map(|highlights| highlights.runs),
|
||||
Some(small_highlights.runs.clone()),
|
||||
"highlights within the entry budget must be cached as-is"
|
||||
);
|
||||
|
||||
let big_key = TextHighlightKey::new([text.as_ref()].into_iter(), text.len());
|
||||
let big_key = TextHighlightKey::new([text].into_iter(), text.len());
|
||||
let big_highlights_source = resolved(&[0..2, 3..7, 8..9]);
|
||||
let big_highlights = cache.insert(
|
||||
big_key.clone(),
|
||||
Arc::clone(&text),
|
||||
text.to_owned(),
|
||||
big_highlights_source.clone(),
|
||||
);
|
||||
assert!(
|
||||
@@ -332,7 +397,7 @@ mod tests {
|
||||
"highlights over the entry budget must be returned unchanged"
|
||||
);
|
||||
assert!(
|
||||
cache.get(&big_key, [text.as_ref()].into_iter()).is_none(),
|
||||
cache.get(&big_key, [text].into_iter()).is_none(),
|
||||
"highlights over the entry budget must not be cached"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -511,9 +511,11 @@ fn labels_from_extension(
|
||||
.map(|label| {
|
||||
let label = label?;
|
||||
let runs = if label.code.is_empty() {
|
||||
Vec::new()
|
||||
Arc::default()
|
||||
} else {
|
||||
language.highlight_text(&label.code.as_str().into(), 0..label.code.len())
|
||||
language
|
||||
.highlight_text_resolved(label.code.as_str(), 0..label.code.len())
|
||||
.runs
|
||||
};
|
||||
build_code_label(&label, &runs, language)
|
||||
})
|
||||
|
||||
@@ -211,8 +211,10 @@ impl super::LspAdapter for CLspAdapter {
|
||||
Some(lsp::CompletionItemKind::FIELD) if completion.detail.is_some() => {
|
||||
let detail = completion.detail.as_ref().unwrap();
|
||||
let text = format!("{} {}", detail, label);
|
||||
let source = Rope::from(format!("struct S {{ {} }}", text).as_str());
|
||||
let runs = language.highlight_text(&source, 11..11 + text.len());
|
||||
let source = format!("struct S {{ {} }}", text);
|
||||
let runs = language
|
||||
.highlight_text(source.as_str(), 11..11 + text.len())
|
||||
.collect();
|
||||
let filter_range = completion
|
||||
.filter_text
|
||||
.as_deref()
|
||||
@@ -228,7 +230,9 @@ impl super::LspAdapter for CLspAdapter {
|
||||
{
|
||||
let detail = completion.detail.as_ref().unwrap();
|
||||
let text = format!("{} {}", detail, label);
|
||||
let runs = language.highlight_text(&Rope::from(text.as_str()), 0..text.len());
|
||||
let runs = language
|
||||
.highlight_text(text.as_str(), 0..text.len())
|
||||
.collect();
|
||||
let filter_range = completion
|
||||
.filter_text
|
||||
.as_deref()
|
||||
@@ -244,7 +248,9 @@ impl super::LspAdapter for CLspAdapter {
|
||||
{
|
||||
let detail = completion.detail.as_ref().unwrap();
|
||||
let text = format!("{} {}", detail, label);
|
||||
let runs = language.highlight_text(&Rope::from(text.as_str()), 0..text.len());
|
||||
let runs = language
|
||||
.highlight_text(text.as_str(), 0..text.len())
|
||||
.collect();
|
||||
let filter_range = completion
|
||||
.filter_text
|
||||
.as_deref()
|
||||
@@ -348,7 +354,9 @@ impl super::LspAdapter for CLspAdapter {
|
||||
Some(CodeLabel::new(
|
||||
text[display_range.clone()].to_string(),
|
||||
filter_range,
|
||||
language.highlight_text(&text.as_str().into(), display_range),
|
||||
language
|
||||
.highlight_text(text.as_str(), display_range)
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
+20
-21
@@ -278,8 +278,10 @@ impl LspAdapter for GoLspAdapter {
|
||||
match completion.kind.zip(completion.detail.as_ref()) {
|
||||
Some((lsp::CompletionItemKind::MODULE, detail)) => {
|
||||
let text = format!("{label} {detail}");
|
||||
let source = Rope::from(format!("import {text}").as_str());
|
||||
let runs = language.highlight_text(&source, 7..7 + text[name_offset..].len());
|
||||
let source = format!("import {text}");
|
||||
let runs = language
|
||||
.highlight_text(source.as_str(), 7..7 + text[name_offset..].len())
|
||||
.collect();
|
||||
let filter_range = completion
|
||||
.filter_text
|
||||
.as_deref()
|
||||
@@ -295,11 +297,10 @@ impl LspAdapter for GoLspAdapter {
|
||||
detail,
|
||||
)) => {
|
||||
let text = format!("{label} {detail}");
|
||||
let source =
|
||||
Rope::from(format!("var {} {}", &text[name_offset..], detail).as_str());
|
||||
let source = format!("var {} {}", &text[name_offset..], detail);
|
||||
let runs = adjust_runs(
|
||||
name_offset,
|
||||
language.highlight_text(&source, 4..4 + text[name_offset..].len()),
|
||||
language.highlight_text(source.as_str(), 4..4 + text[name_offset..].len()),
|
||||
);
|
||||
let filter_range = completion
|
||||
.filter_text
|
||||
@@ -313,10 +314,10 @@ impl LspAdapter for GoLspAdapter {
|
||||
}
|
||||
Some((lsp::CompletionItemKind::STRUCT, _)) => {
|
||||
let text = format!("{label} struct {{}}");
|
||||
let source = Rope::from(format!("type {}", &text[name_offset..]).as_str());
|
||||
let source = format!("type {}", &text[name_offset..]);
|
||||
let runs = adjust_runs(
|
||||
name_offset,
|
||||
language.highlight_text(&source, 5..5 + text[name_offset..].len()),
|
||||
language.highlight_text(source.as_str(), 5..5 + text[name_offset..].len()),
|
||||
);
|
||||
let filter_range = completion
|
||||
.filter_text
|
||||
@@ -330,10 +331,10 @@ impl LspAdapter for GoLspAdapter {
|
||||
}
|
||||
Some((lsp::CompletionItemKind::INTERFACE, _)) => {
|
||||
let text = format!("{label} interface {{}}");
|
||||
let source = Rope::from(format!("type {}", &text[name_offset..]).as_str());
|
||||
let source = format!("type {}", &text[name_offset..]);
|
||||
let runs = adjust_runs(
|
||||
name_offset,
|
||||
language.highlight_text(&source, 5..5 + text[name_offset..].len()),
|
||||
language.highlight_text(source.as_str(), 5..5 + text[name_offset..].len()),
|
||||
);
|
||||
let filter_range = completion
|
||||
.filter_text
|
||||
@@ -347,11 +348,10 @@ impl LspAdapter for GoLspAdapter {
|
||||
}
|
||||
Some((lsp::CompletionItemKind::FIELD, detail)) => {
|
||||
let text = format!("{label} {detail}");
|
||||
let source =
|
||||
Rope::from(format!("type T struct {{ {} }}", &text[name_offset..]).as_str());
|
||||
let source = format!("type T struct {{ {} }}", &text[name_offset..]);
|
||||
let runs = adjust_runs(
|
||||
name_offset,
|
||||
language.highlight_text(&source, 16..16 + text[name_offset..].len()),
|
||||
language.highlight_text(source.as_str(), 16..16 + text[name_offset..].len()),
|
||||
);
|
||||
let filter_range = completion
|
||||
.filter_text
|
||||
@@ -366,10 +366,10 @@ impl LspAdapter for GoLspAdapter {
|
||||
Some((lsp::CompletionItemKind::FUNCTION | lsp::CompletionItemKind::METHOD, detail)) => {
|
||||
if let Some(signature) = detail.strip_prefix("func") {
|
||||
let text = format!("{label}{signature}");
|
||||
let source = Rope::from(format!("func {} {{}}", &text[name_offset..]).as_str());
|
||||
let source = format!("func {} {{}}", &text[name_offset..]);
|
||||
let runs = adjust_runs(
|
||||
name_offset,
|
||||
language.highlight_text(&source, 5..5 + text[name_offset..].len()),
|
||||
language.highlight_text(source.as_str(), 5..5 + text[name_offset..].len()),
|
||||
);
|
||||
let filter_range = completion
|
||||
.filter_text
|
||||
@@ -442,7 +442,9 @@ impl LspAdapter for GoLspAdapter {
|
||||
Some(CodeLabel::new(
|
||||
text[display_range.clone()].to_string(),
|
||||
filter_range,
|
||||
language.highlight_text(&text.as_str().into(), display_range),
|
||||
language
|
||||
.highlight_text(text.as_str(), display_range)
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -575,13 +577,10 @@ async fn get_cached_server_binary(container_dir: &Path) -> Option<LanguageServer
|
||||
|
||||
fn adjust_runs(
|
||||
delta: usize,
|
||||
mut runs: Vec<(Range<usize>, HighlightId)>,
|
||||
runs: impl Iterator<Item = (Range<usize>, HighlightId)>,
|
||||
) -> Vec<(Range<usize>, HighlightId)> {
|
||||
for (range, _) in &mut runs {
|
||||
range.start += delta;
|
||||
range.end += delta;
|
||||
}
|
||||
runs
|
||||
runs.map(|(range, highlight_id)| (range.start + delta..range.end + delta, highlight_id))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) struct GoContextProvider;
|
||||
|
||||
@@ -243,7 +243,9 @@ fn label_for_python_symbol(
|
||||
Some(language::CodeLabel::new(
|
||||
text[display_range.clone()].to_string(),
|
||||
filter_range,
|
||||
language.highlight_text(&text.as_str().into(), display_range),
|
||||
language
|
||||
.highlight_text(text.as_str(), display_range)
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
@@ -381,8 +381,9 @@ impl LspAdapter for RustLspAdapter {
|
||||
let text = format!("{name}: {signature}");
|
||||
let prefix = "struct S { ";
|
||||
let source = Rope::from_iter([prefix, &text, " }"]);
|
||||
let runs =
|
||||
language.highlight_text(&source, prefix.len()..prefix.len() + text.len());
|
||||
let runs = language
|
||||
.highlight_text(&source, prefix.len()..prefix.len() + text.len())
|
||||
.collect();
|
||||
mk_label(text, &|| 0..completion.label.len(), runs)
|
||||
}
|
||||
(
|
||||
@@ -393,8 +394,9 @@ impl LspAdapter for RustLspAdapter {
|
||||
let text = format!("{name}: {signature}",);
|
||||
let prefix = "let ";
|
||||
let source = Rope::from_iter([prefix, &text, " = ();"]);
|
||||
let runs =
|
||||
language.highlight_text(&source, prefix.len()..prefix.len() + text.len());
|
||||
let runs = language
|
||||
.highlight_text(&source, prefix.len()..prefix.len() + text.len())
|
||||
.collect();
|
||||
mk_label(text, &|| 0..completion.label.len(), runs)
|
||||
}
|
||||
(
|
||||
@@ -430,8 +432,9 @@ impl LspAdapter for RustLspAdapter {
|
||||
.filter(|it| it.contains(&label))
|
||||
.and_then(|it| Some((it, FULL_SIGNATURE_REGEX.find(it)?)))
|
||||
{
|
||||
let source = Rope::from(function_signature);
|
||||
let runs = language.highlight_text(&source, 0..function_signature.len());
|
||||
let runs = language
|
||||
.highlight_text(function_signature, 0..function_signature.len())
|
||||
.collect();
|
||||
mk_label(
|
||||
function_signature.to_owned(),
|
||||
&|| match_.range().start + 3..match_.range().end - 1,
|
||||
@@ -441,13 +444,14 @@ impl LspAdapter for RustLspAdapter {
|
||||
let text = format!("{label}{suffix}");
|
||||
let source = Rope::from_iter([prefix, " ", &text, " {}"]);
|
||||
let run_start = prefix.len() + 1;
|
||||
let runs = language.highlight_text(&source, run_start..run_start + text.len());
|
||||
let runs = language
|
||||
.highlight_text(&source, run_start..run_start + text.len())
|
||||
.collect();
|
||||
mk_label(text, &|| 0..label.len(), runs)
|
||||
} else if detail_right.is_some_and(|detail| detail.starts_with("macro_rules! ")) {
|
||||
let text = completion.label.clone();
|
||||
let len = text.len();
|
||||
let source = Rope::from(text.as_str());
|
||||
let runs = language.highlight_text(&source, 0..len);
|
||||
let runs = language.highlight_text(text.as_str(), 0..len).collect();
|
||||
mk_label(text, &|| 0..completion.label.len(), runs)
|
||||
} else if detail_left.is_none() {
|
||||
return None;
|
||||
@@ -544,7 +548,7 @@ impl LspAdapter for RustLspAdapter {
|
||||
detail_left.take();
|
||||
}
|
||||
|
||||
runs.extend(language.highlight_text(&Rope::from(&label), 0..label.len()));
|
||||
runs.extend(language.highlight_text(label.as_str(), 0..label.len()));
|
||||
} else {
|
||||
let highlight_name = kind.and_then(|kind| match kind {
|
||||
lsp::CompletionItemKind::STRUCT
|
||||
@@ -663,10 +667,12 @@ impl LspAdapter for RustLspAdapter {
|
||||
return Some(CodeLabel::new(
|
||||
name.to_string(),
|
||||
0..name.len(),
|
||||
language.highlight_text(
|
||||
&Rope::from_iter([prefix, name, "}"]),
|
||||
prefix.len()..prefix.len() + name.len(),
|
||||
),
|
||||
language
|
||||
.highlight_text(
|
||||
&Rope::from_iter([prefix, name, "}"]),
|
||||
prefix.len()..prefix.len() + name.len(),
|
||||
)
|
||||
.collect(),
|
||||
));
|
||||
}
|
||||
_ => return None,
|
||||
@@ -677,7 +683,9 @@ impl LspAdapter for RustLspAdapter {
|
||||
Some(CodeLabel::new(
|
||||
format!("{prefix}{name}"),
|
||||
filter_range,
|
||||
language.highlight_text(&Rope::from_iter([prefix, name, suffix]), display_range),
|
||||
language
|
||||
.highlight_text(&Rope::from_iter([prefix, name, suffix]), display_range)
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
@@ -945,7 +945,9 @@ mod tests {
|
||||
.expect("type.class highlight should be defined");
|
||||
|
||||
assert_eq!(
|
||||
language.highlight_text(&source, 0..source.len()),
|
||||
language
|
||||
.highlight_text(&source, 0..source.len())
|
||||
.collect::<Vec<_>>(),
|
||||
vec![(6..9, class_highlight), (29..32, class_highlight)],
|
||||
"{} class instantiations should use the type.class highlight",
|
||||
language.name()
|
||||
|
||||
@@ -1674,6 +1674,13 @@ fn compute_code_block_highlights(parsed: &ParsedMarkdown) -> CodeBlockHighlights
|
||||
}
|
||||
|
||||
fn highlight_code_block(block: PendingCodeBlock, code_block_highlights: &mut CodeBlockHighlights) {
|
||||
if let [(event_index, text)] = block.texts.as_slice() {
|
||||
let resolved = block.language.highlight_text_resolved(*text, 0..text.len());
|
||||
if !resolved.runs.is_empty() {
|
||||
code_block_highlights.insert(*event_index, resolved);
|
||||
}
|
||||
return;
|
||||
}
|
||||
let mut combined = String::new();
|
||||
let mut text_offsets = Vec::with_capacity(block.texts.len());
|
||||
for (_, text) in &block.texts {
|
||||
@@ -1682,14 +1689,10 @@ fn highlight_code_block(block: PendingCodeBlock, code_block_highlights: &mut Cod
|
||||
}
|
||||
let resolved = block
|
||||
.language
|
||||
.highlight_text_resolved(&Rope::from(combined.as_str()), 0..combined.len());
|
||||
.highlight_text_resolved(combined.as_str(), 0..combined.len());
|
||||
if resolved.runs.is_empty() {
|
||||
return;
|
||||
}
|
||||
if let [(event_index, _)] = block.texts.as_slice() {
|
||||
code_block_highlights.insert(*event_index, resolved);
|
||||
return;
|
||||
}
|
||||
let mut runs = resolved.runs.iter().peekable();
|
||||
for ((event_index, text), text_offset) in block.texts.iter().zip(text_offsets) {
|
||||
let text_end = text_offset + text.len();
|
||||
@@ -4169,9 +4172,7 @@ impl MarkdownElementBuilder {
|
||||
let runs = if resolved.is_current() {
|
||||
resolved.runs.clone()
|
||||
} else {
|
||||
language
|
||||
.highlight_text_resolved(&Rope::from(text), 0..text.len())
|
||||
.runs
|
||||
language.highlight_text_resolved(text, 0..text.len()).runs
|
||||
};
|
||||
let mut offset = 0;
|
||||
for (run_range, highlight_id) in runs.iter() {
|
||||
@@ -5644,7 +5645,9 @@ mod tests {
|
||||
let code_text = "fn main() {}\n";
|
||||
let code_start = source.find(code_text).unwrap();
|
||||
let cached = cached_code_block_highlights(&markdown, code_start, cx);
|
||||
let expected = language.highlight_text(&Rope::from(code_text), 0..code_text.len());
|
||||
let expected = language
|
||||
.highlight_text(&Rope::from(code_text), 0..code_text.len())
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
cached.runs.to_vec(),
|
||||
expected,
|
||||
|
||||
Reference in New Issue
Block a user