Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Calculate buffer chunks lazily (#61523)
Kirill Bulatov146a6bf9cd0d
diff --git a/crates/editor/src/bracket_colorization.rs b/crates/editor/src/bracket_colorization.rsindex a0ae9022b32..d69f6162bd2 100644--- a/crates/editor/src/bracket_colorization.rs+++ b/crates/editor/src/bracket_colorization.rs@@ -43,10 +43,18 @@ impl Editor {else {return false;};- LanguageSettings::for_buffer(buffer.read(cx), cx).colorize_brackets+ let buffer = buffer.read(cx);+ buffer+ .language()+ .is_some_and(|language| language.grammar().is_some())+ && LanguageSettings::for_buffer(buffer, cx).colorize_brackets}).collect();+ if !invalidate && (accents.is_empty() || excerpt_data.is_empty()) {+ return;+ }+let mut fetched_tree_sitter_chunks = excerpt_data.iter().filter_map(|(_, _, excerpt_range)| {diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex 1756d19d490..f5a05444fc4 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -2064,8 +2064,12 @@ impl Editor {if editor.buffer().read(cx).buffer(buffer_id).is_some() {editor.register_buffer(buffer_id, cx);editor.refresh_runnables(Some(buffer_id), window, cx);+ editor.invalidate_semantic_tokens(Some(buffer_id));editor.update_lsp_data(Some(buffer_id), window, cx);- editor.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx);+ editor.refresh_inlay_hints(+ InlayHintRefreshReason::LanguageServerRegistered,+ cx,+ );refresh_linked_ranges(editor, window, cx);editor.refresh_code_actions_for_selection(window, cx);editor.refresh_document_highlights(cx);diff --git a/crates/editor/src/inlays/inlay_hints.rs b/crates/editor/src/inlays/inlay_hints.rsindex de657c4aa62..56cd3e4fc6e 100644--- a/crates/editor/src/inlays/inlay_hints.rs+++ b/crates/editor/src/inlays/inlay_hints.rs@@ -234,6 +234,7 @@ pub enum InlayHintRefreshReason {NewLinesShown,BufferEdited(BufferId),ServerRemoved,+ LanguageServerRegistered,RefreshRequested { server_id: LanguageServerId },BuffersRemoved(Vec<BufferId>),}@@ -320,10 +321,12 @@ impl Editor {InlayHintRefreshReason::ModifiersChanged(_)| InlayHintRefreshReason::Toggle(_)| InlayHintRefreshReason::SettingsChange(_)- | InlayHintRefreshReason::ServerRemoved => true,- InlayHintRefreshReason::NewLinesShown- | InlayHintRefreshReason::RefreshRequested { .. }- | InlayHintRefreshReason::BuffersRemoved(_) => false,+ | InlayHintRefreshReason::ServerRemoved+ | InlayHintRefreshReason::LanguageServerRegistered+ | InlayHintRefreshReason::RefreshRequested { .. } => true,+ InlayHintRefreshReason::NewLinesShown | InlayHintRefreshReason::BuffersRemoved(_) => {+ false+ }InlayHintRefreshReason::BufferEdited(buffer_id) => {let Some(affected_language) = self.buffer()@@ -363,7 +366,9 @@ impl Editor {return;};- if invalidate_cache.should_invalidate() {+ if invalidate_cache.should_invalidate()+ && !matches!(reason, InlayHintRefreshReason::RefreshRequested { .. })+ {if invalidate_hints_for_buffers.is_empty() {inlay_hints.clear();} else {@@ -536,7 +541,8 @@ impl Editor {return None;}InlayHintRefreshReason::ServerRemoved => InvalidationStrategy::BufferEdited,- InlayHintRefreshReason::NewLinesShown => InvalidationStrategy::None,+ InlayHintRefreshReason::NewLinesShown+ | InlayHintRefreshReason::LanguageServerRegistered => InvalidationStrategy::None,InlayHintRefreshReason::BufferEdited(_) => InvalidationStrategy::BufferEdited,InlayHintRefreshReason::RefreshRequested { server_id } => {InvalidationStrategy::RefreshRequested {@@ -820,6 +826,9 @@ impl Editor {// Hence, clear all excerpts' hints in the multi buffer: later, the invalidated ones will re-trigger the LSP query, the rest will be restored// from the cache.if invalidate_cache.should_invalidate() {+ for hint_id in &visible_inlay_hint_ids {+ inlay_hints.added_hints.remove(hint_id);+ }hints_to_remove.extend(visible_inlay_hint_ids);// When invalidating, this task removes ALL visible hints for the bufferdiff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rsindex d63e588a364..67db23e04ce 100644--- a/crates/language/src/buffer.rs+++ b/crates/language/src/buffer.rs@@ -144,25 +144,21 @@ pub struct Buffer {#[derive(Debug)]pub struct TreeSitterData {chunks: RowChunks,- brackets_by_chunks: Mutex<Vec<Option<Vec<BracketMatch<usize>>>>>,+ brackets_by_chunks: Mutex<HashMap<usize, Vec<BracketMatch<usize>>>>,}const MAX_ROWS_IN_A_CHUNK: u32 = 50;impl TreeSitterData {fn clear(&mut self, snapshot: &text::BufferSnapshot) {- self.chunks = RowChunks::new(&snapshot, MAX_ROWS_IN_A_CHUNK);+ self.chunks = RowChunks::new(snapshot, MAX_ROWS_IN_A_CHUNK);self.brackets_by_chunks.get_mut().clear();- self.brackets_by_chunks- .get_mut()- .resize(self.chunks.len(), None);}fn new(snapshot: &text::BufferSnapshot) -> Self {- let chunks = RowChunks::new(&snapshot, MAX_ROWS_IN_A_CHUNK);Self {- brackets_by_chunks: Mutex::new(vec![None; chunks.len()]),- chunks,+ chunks: RowChunks::new(snapshot, MAX_ROWS_IN_A_CHUNK),+ brackets_by_chunks: Mutex::new(HashMap::default()),}}@@ -4824,6 +4820,13 @@ impl BufferSnapshot {known_chunks: Option<&HashSet<Range<BufferRow>>>,) -> HashMap<Range<BufferRow>, Vec<BracketMatch<usize>>> {let mut all_bracket_matches = HashMap::default();+ if self+ .language+ .as_ref()+ .is_none_or(|language| language.grammar().is_none())+ {+ return all_bracket_matches;+ }for chunk in self.tree_sitter_data@@ -4836,8 +4839,11 @@ impl BufferSnapshot {let chunk_range = chunk.anchor_range();let chunk_range = chunk_range.to_offset(&self);- if let Some(cached_brackets) =- &self.tree_sitter_data.brackets_by_chunks.lock()[chunk.id]+ if let Some(cached_brackets) = self+ .tree_sitter_data+ .brackets_by_chunks+ .lock()+ .get(&chunk.id){all_bracket_matches.insert(chunk.row_range(), cached_brackets.clone());continue;@@ -5101,11 +5107,11 @@ impl BufferSnapshot {(bracket_match.open_range.start, bracket_match.open_range.end)});- if let empty_slot @ None =- &mut self.tree_sitter_data.brackets_by_chunks.lock()[chunk.id]- {- *empty_slot = Some(all_brackets.clone());- }+ self.tree_sitter_data+ .brackets_by_chunks+ .lock()+ .entry(chunk.id)+ .or_insert_with(|| all_brackets.clone());all_bracket_matches.insert(chunk.row_range(), all_brackets);}diff --git a/crates/language/src/buffer/row_chunk.rs b/crates/language/src/buffer/row_chunk.rsindex c0528544c15..605299f67f0 100644--- a/crates/language/src/buffer/row_chunk.rs+++ b/crates/language/src/buffer/row_chunk.rs@@ -1,9 +1,11 @@//! A row chunk is an exclusive range of rows, [`BufferRow`] within a buffer of a certain version, [`Global`].//! All but the last chunk are of a constant, given size.-use std::{ops::Range, sync::Arc};+use std::{collections::BTreeSet, ops::Range};-use text::{Anchor, OffsetRangeExt as _, Point};+use collections::HashMap;+use parking_lot::Mutex;+use text::{Anchor, Point};use util::RangeExt;use crate::BufferRow;@@ -16,87 +18,97 @@ use crate::BufferRow;/// Together, chunks form entire document at a particular version [`Global`]./// Each chunk is queried for inlays as `(start_row, 0)..(end_exclusive, 0)` via/// <https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#inlayHintParams>-#[derive(Clone)]+///+/// Chunk boundaries are derived arithmetically from the buffer's last row.+/// Each chunk's anchors are computed when the chunk is first queried and memoized:+/// resolving an anchor requires sum tree seeks, which gets expensive if done for+/// all chunks of a large buffer at once.pub struct RowChunks {- chunks: Arc<[RowChunk]>,- version: clock::Global,+ buffer_snapshot: text::BufferSnapshot,+ last_row: BufferRow,+ max_rows_per_chunk: u32,+ computed_chunks: Mutex<HashMap<usize, RowChunk>>,}impl std::fmt::Debug for RowChunks {fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {f.debug_struct("RowChunks")- .field("chunks", &self.chunks)+ .field("len", &self.len())+ .field("max_rows_per_chunk", &self.max_rows_per_chunk)+ .field("computed_chunks", &self.computed_chunks.lock().len()).finish()}}impl RowChunks {pub fn new(snapshot: &text::BufferSnapshot, max_rows_per_chunk: u32) -> Self {- let buffer_point_range = (0..snapshot.len()).to_point(&snapshot);- let last_row = buffer_point_range.end.row;- let chunks = (buffer_point_range.start.row..=last_row)- .step_by(max_rows_per_chunk as usize)- .collect::<Vec<_>>();- let last_chunk_id = chunks.len() - 1;- let chunks = chunks- .into_iter()- .enumerate()- .map(|(id, chunk_start)| {- let start = Point::new(chunk_start, 0);- let end_exclusive = (chunk_start + max_rows_per_chunk).min(last_row);- let end = if id == last_chunk_id {- Point::new(end_exclusive, snapshot.line_len(end_exclusive))- } else {- Point::new(end_exclusive, 0)- };- RowChunk {- id,- start: chunk_start,- end_exclusive,- start_anchor: snapshot.anchor_before(start),- end_anchor: snapshot.anchor_after(end),- }- })- .collect::<Vec<_>>();Self {- chunks: Arc::from(chunks),- version: snapshot.version().clone(),+ last_row: snapshot.max_point().row,+ buffer_snapshot: snapshot.clone(),+ max_rows_per_chunk,+ computed_chunks: Mutex::new(HashMap::default()),}}pub fn version(&self) -> &clock::Global {- &self.version+ self.buffer_snapshot.version()}pub fn len(&self) -> usize {- self.chunks.len()+ (self.last_row / self.max_rows_per_chunk) as usize + 1}pub fn applicable_chunks(&self, ranges: &[Range<Point>]) -> impl Iterator<Item = RowChunk> {- let row_ranges = ranges- .iter()+ let last_chunk_id = self.len() - 1;+ let mut chunk_ids = BTreeSet::new();+ for point_range in ranges {// Be lenient and yield multiple chunks if they "touch" the exclusive part of the range.// This will result in LSP hints [re-]queried for more ranges, but also more hints already visible when scrolling around.- .map(|point_range| point_range.start.row..point_range.end.row + 1)- .collect::<Vec<_>>();- self.chunks- .iter()- .filter(move |chunk| -> bool {- let chunk_range = chunk.row_range().to_inclusive();- row_ranges- .iter()- .any(|row_range| chunk_range.overlaps(&row_range))- })- .copied()+ let row_range = point_range.start.row..point_range.end.row + 1;+ let first_id = (point_range.start.row.div_ceil(self.max_rows_per_chunk) as usize)+ .saturating_sub(1);+ let last_id =+ ((point_range.end.row / self.max_rows_per_chunk) as usize).min(last_chunk_id);+ for id in first_id..=last_id {+ if self.chunk_row_range(id).to_inclusive().overlaps(&row_range) {+ chunk_ids.insert(id);+ }+ }+ }+ chunk_ids.into_iter().map(|id| self.chunk(id))}pub fn previous_chunk(&self, chunk: RowChunk) -> Option<RowChunk> {- if chunk.id == 0 {+ if chunk.id == 0 || chunk.id > self.len() {None} else {- self.chunks.get(chunk.id - 1).copied()+ Some(self.chunk(chunk.id - 1))}}++ fn chunk_row_range(&self, id: usize) -> Range<BufferRow> {+ let start = id as u32 * self.max_rows_per_chunk;+ start..(start + self.max_rows_per_chunk).min(self.last_row)+ }++ fn chunk(&self, id: usize) -> RowChunk {+ *self.computed_chunks.lock().entry(id).or_insert_with(|| {+ let row_range = self.chunk_row_range(id);+ let start = Point::new(row_range.start, 0);+ let end = if id == self.len() - 1 {+ Point::new(row_range.end, self.buffer_snapshot.line_len(row_range.end))+ } else {+ Point::new(row_range.end, 0)+ };+ RowChunk {+ id,+ start: row_range.start,+ end_exclusive: row_range.end,+ start_anchor: self.buffer_snapshot.anchor_before(start),+ end_anchor: self.buffer_snapshot.anchor_after(end),+ }+ })+ }}#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]diff --git a/crates/language/src/buffer_tests.rs b/crates/language/src/buffer_tests.rsindex bef99224bd5..57068230404 100644--- a/crates/language/src/buffer_tests.rs+++ b/crates/language/src/buffer_tests.rs@@ -1474,6 +1474,80 @@ fn test_bracket_colorization_indices_remain_stable_across_row_chunks(cx: &mut Ap}}+#[test]+fn test_applicable_row_chunks() {+ let text = (0..125)+ .map(|row| format!("line {row}\n"))+ .collect::<String>();+ let buffer = TextBuffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), text);+ let snapshot = buffer.snapshot();+ let chunks = row_chunk::RowChunks::new(snapshot, 10);+ assert_eq!(chunks.len(), 13);++ let row_ranges = |ranges: &[Range<Point>]| {+ chunks+ .applicable_chunks(ranges)+ .map(|chunk| chunk.row_range())+ .collect::<Vec<_>>()+ };++ assert_eq!(+ row_ranges(&[Point::new(15, 0)..Point::new(17, 3)]),+ vec![10..20],+ "Range in the middle of a chunk should yield that chunk only"+ );+ assert_eq!(+ row_ranges(&[Point::new(10, 0)..Point::new(12, 0)]),+ vec![0..10, 10..20],+ "Range starting exactly at a chunk boundary should also touch the previous chunk"+ );+ assert_eq!(+ row_ranges(&[Point::new(5, 0)..Point::new(9, 0)]),+ vec![0..10],+ "Range ending before a chunk boundary should not touch the next chunk"+ );+ assert_eq!(+ row_ranges(&[Point::new(5, 0)..Point::new(10, 0)]),+ vec![0..10, 10..20],+ "Range ending exactly at a chunk boundary should touch the next chunk"+ );+ assert_eq!(+ row_ranges(&[+ Point::new(112, 0)..Point::new(112, 0),+ Point::new(15, 0)..Point::new(16, 0),+ Point::new(11, 0)..Point::new(18, 0),+ ]),+ vec![10..20, 110..120],+ "Chunks for multiple ranges should be deduplicated and sorted"+ );++ let all_chunks = chunks+ .applicable_chunks(&[Point::zero()..Point::new(125, 0)])+ .collect::<Vec<_>>();+ assert_eq!(+ all_chunks.iter().map(|chunk| chunk.id).collect::<Vec<_>>(),+ (0..13).collect::<Vec<_>>()+ );+ assert_eq!(all_chunks[12].row_range(), 120..125);+ for chunk in &all_chunks {+ assert_eq!(+ chunk.start_anchor.to_point(snapshot),+ Point::new(chunk.start, 0)+ );+ assert_eq!(+ chunk.end_anchor.to_point(snapshot),+ Point::new(chunk.end_exclusive, 0)+ );+ }+ assert_eq!(+ chunks+ .applicable_chunks(&[Point::zero()..Point::new(125, 0)])+ .collect::<Vec<_>>(),+ all_chunks,+ "Memoized chunks should be identical to the initially computed ones"+ );+}+#[gpui::test]fn test_enclosing_bracket_ranges_where_brackets_are_not_outermost_children(cx: &mut App) {let mut assert = |selection_text, bracket_pair_texts| {diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rsindex 4d2795b8627..8a0d37adda8 100644--- a/crates/project/src/lsp_store.rs+++ b/crates/project/src/lsp_store.rs@@ -156,7 +156,7 @@ pub use document_links::{pub use folding_ranges::LspFoldingRange;pub use fs::*;pub use language::Location;-pub use lsp_store::inlay_hints::{CacheInlayHints, InvalidationStrategy};+pub use lsp_store::inlay_hints::{CacheInlayHints, CacheInlayHintsTask, InvalidationStrategy};#[cfg(any(test, feature = "test-support"))]pub use prettier::FORMAT_SUFFIX as TEST_PRETTIER_FORMAT_SUFFIX;#[cfg(any(test, feature = "test-support"))]@@ -4161,6 +4161,11 @@ impl<T> RunningFetch<T> {}}+enum ChunkFetch {+ Cached(CacheInlayHints),+ Running(CacheInlayHintsTask),+}+fn notify_server_capabilities_updated(server: &LanguageServer, cx: &mut Context<LspStore>) {if let Some(capabilities) = serde_json::to_string(&server.capabilities()).ok() {cx.emit(LspStoreEvent::LanguageServerUpdate {@@ -7694,20 +7699,24 @@ impl LspStore {cx: &mut Context<Self>,) -> HashMap<Range<BufferRow>, Task<Result<CacheInlayHints>>> {let next_hint_id = self.next_hint_id.clone();+ let current_servers = self.relevant_server_ids_for_capability_check(&buffer, cx);let lsp_data = self.latest_lsp_data(&buffer, cx);let query_version = lsp_data.buffer_version.clone();- let mut lsp_refresh_requested = false;- let for_server = if let InvalidationStrategy::RefreshRequested { server_id } = invalidate {- lsp_refresh_requested = lsp_data+ if let InvalidationStrategy::RefreshRequested { server_id } = invalidate {+ lsp_data.inlay_hints.invalidate_for_server_refresh(server_id);- Some(server_id)- } else {- None- };+ }let existing_inlay_hints = &mut lsp_data.inlay_hints;+ existing_inlay_hints+ .fetched_servers+ .retain(|server_id| current_servers.contains(server_id));+ let missing_servers = current_servers+ .difference(&existing_inlay_hints.fetched_servers)+ .copied()+ .collect::<HashSet<_>>();let known_chunks = known_chunks- .filter(|(known_version, _)| !lsp_data.buffer_version.changed_since(known_version))+ .filter(|(known_version, _)| !query_version.changed_since(known_version)).map(|(_, known_chunks)| known_chunks).unwrap_or_default();@@ -7717,9 +7726,6 @@ impl LspStore {.map(|range| range.to_point(&buffer_snapshot)).collect::<Vec<_>>();- let mut hint_fetch_tasks = Vec::new();- let mut cached_inlay_hints = None;- let mut ranges_to_query = None;let applicable_chunks = existing_inlay_hints.applicable_chunks(ranges.as_slice()).filter(|chunk| !known_chunks.contains(&chunk.row_range()))@@ -7728,103 +7734,65 @@ impl LspStore {return HashMap::default();}- for row_chunk in applicable_chunks {- match (- existing_inlay_hints- .cached_hints(&row_chunk)- .filter(|_| !lsp_refresh_requested)- .cloned(),- existing_inlay_hints- .fetched_hints(&row_chunk)- .as_ref()- .filter(|_| !lsp_refresh_requested)- .cloned(),- ) {- (None, None) => {- let chunk_range = row_chunk.anchor_range();- ranges_to_query- .get_or_insert_with(Vec::new)- .push((row_chunk, chunk_range));- }- (None, Some(fetched_hints)) => hint_fetch_tasks.push((row_chunk, fetched_hints)),- (Some(cached_hints), None) => {- for (server_id, cached_hints) in cached_hints {- if for_server.is_none_or(|for_server| for_server == server_id) {- cached_inlay_hints- .get_or_insert_with(HashMap::default)- .entry(row_chunk.row_range())- .or_insert_with(HashMap::default)- .entry(server_id)- .or_insert_with(Vec::new)- .extend(cached_hints);- }- }- }- (Some(cached_hints), Some(fetched_hints)) => {- hint_fetch_tasks.push((row_chunk, fetched_hints));- for (server_id, cached_hints) in cached_hints {- if for_server.is_none_or(|for_server| for_server == server_id) {- cached_inlay_hints- .get_or_insert_with(HashMap::default)- .entry(row_chunk.row_range())- .or_insert_with(HashMap::default)- .entry(server_id)- .or_insert_with(Vec::new)- .extend(cached_hints);- }- }- }- }- }+ let mut chunk_hint_tasks = HashMap::default();+ for chunk in applicable_chunks {+ let cached_hints = existing_inlay_hints.cached_hints(&chunk).cloned();+ let running_fetch = existing_inlay_hints.fetched_hints(&chunk).clone();+ let servers_to_query = if cached_hints.is_none() && running_fetch.is_none() {+ Some(None)+ } else if missing_servers.is_empty() {+ None+ } else {+ Some(Some(missing_servers.clone()))+ };- if hint_fetch_tasks.is_empty()- && ranges_to_query- .as_ref()- .is_none_or(|ranges| ranges.is_empty())- && let Some(cached_inlay_hints) = cached_inlay_hints- {- cached_inlay_hints- .into_iter()- .map(|(row_chunk, hints)| (row_chunk, Task::ready(Ok(hints))))- .collect()- } else {- for (chunk, range_to_query) in ranges_to_query.into_iter().flatten() {- // When a server refresh was requested, other servers' cached hints- // are unaffected by the refresh and must be included in the result.- // Otherwise apply_fetched_hints (with should_invalidate()=true)- // removes all visible hints but only adds back the requesting- // server's new hints, permanently losing other servers' hints.- let other_servers_cached: CacheInlayHints = if lsp_refresh_requested {- lsp_data- .inlay_hints- .cached_hints(&chunk)- .cloned()- .unwrap_or_default()- } else {- HashMap::default()+ let Some(servers_to_query) = servers_to_query else {+ match running_fetch {+ Some(running_fetch) => chunk_hint_tasks+ .insert(chunk.row_range(), ChunkFetch::Running(running_fetch)),+ None => chunk_hint_tasks.insert(+ chunk.row_range(),+ ChunkFetch::Cached(cached_hints.unwrap_or_default()),+ ),};+ continue;+ };- let next_hint_id = next_hint_id.clone();- let buffer = buffer.clone();- let query_version = query_version.clone();- let new_inlay_hints = cx- .spawn(async move |lsp_store, cx| {- let new_fetch_task = lsp_store.update(cx, |lsp_store, cx| {- lsp_store.fetch_inlay_hints(for_server, &buffer, range_to_query, cx)- })?;- new_fetch_task- .await- .and_then(|new_hints_by_server| {- lsp_store.update(cx, |lsp_store, cx| {- let lsp_data = lsp_store.latest_lsp_data(&buffer, cx);- let update_cache = lsp_data.buffer_version == query_version;+ existing_inlay_hints+ .fetched_servers+ .extend(servers_to_query.iter().flatten().copied());+ if servers_to_query.is_none() {+ existing_inlay_hints+ .fetched_servers+ .extend(current_servers.iter().copied());+ }+ let queried_servers = match &servers_to_query {+ Some(servers) => servers.clone(),+ None => current_servers.clone(),+ };++ let next_hint_id = next_hint_id.clone();+ let buffer = buffer.clone();+ let query_version = query_version.clone();+ let range_to_query = chunk.anchor_range();+ let new_inlay_hints = cx+ .spawn(async move |lsp_store, cx| {+ // Let the previous fetch merge its hints into the cache first, so+ // that the chunk state read below contains its servers' hints too.+ if let Some(running_fetch) = running_fetch {+ running_fetch.await.ok();+ }+ let new_fetch_task = lsp_store.update(cx, |lsp_store, cx| {+ lsp_store.fetch_inlay_hints(servers_to_query, &buffer, range_to_query, cx)+ })?;+ match new_fetch_task.await {+ Ok(new_hints_by_server) => lsp_store+ .update(cx, |lsp_store, cx| {+ let lsp_data = lsp_store.latest_lsp_data(&buffer, cx);+ if lsp_data.buffer_version == query_version {if new_hints_by_server.is_empty() {- if update_cache {- lsp_data.inlay_hints.invalidate_for_chunk(chunk);- }- other_servers_cached+ lsp_data.inlay_hints.invalidate_for_chunk(chunk);} else {- let mut result = other_servers_cached;for (server_id, new_hints) in new_hints_by_server {let new_hints = new_hints.into_iter()@@ -7838,53 +7806,63 @@ impl LspStore {)}).collect::<Vec<_>>();- if update_cache {- lsp_data.inlay_hints.insert_new_hints(- chunk,- server_id,- new_hints.clone(),- );- }- result.insert(server_id, new_hints);+ lsp_data+ .inlay_hints+ .insert_new_hints(chunk, server_id, new_hints);}- result}- })- })- .map_err(Arc::new)- })- .shared();-- let fetch_task = lsp_data.inlay_hints.fetched_hints(&chunk);- *fetch_task = Some(new_inlay_hints.clone());- hint_fetch_tasks.push((chunk, new_inlay_hints));- }-- cached_inlay_hints- .unwrap_or_default()- .into_iter()- .map(|(row_chunk, hints)| (row_chunk, Task::ready(Ok(hints))))- .chain(hint_fetch_tasks.into_iter().map(|(chunk, hints_fetch)| {- (- chunk.row_range(),- cx.spawn(async move |_, _| {- hints_fetch.await.map_err(|e| {- if e.error_code() != ErrorCode::Internal {- anyhow!(e.error_code())- } else {- anyhow!("{e:#}")}+ lsp_data+ .inlay_hints+ .cached_hints(&chunk)+ .cloned()+ .unwrap_or_default()})- }),- )- }))- .collect()+ .map_err(Arc::new),+ Err(e) => {+ lsp_store+ .update(cx, |lsp_store, cx| {+ let lsp_data = lsp_store.latest_lsp_data(&buffer, cx);+ if lsp_data.buffer_version == query_version {+ for server_id in &queried_servers {+ lsp_data.inlay_hints.fetched_servers.remove(server_id);+ }+ }+ })+ .ok();+ Err(Arc::new(e))+ }+ }+ })+ .shared();++ *existing_inlay_hints.fetched_hints(&chunk) = Some(new_inlay_hints.clone());+ chunk_hint_tasks.insert(chunk.row_range(), ChunkFetch::Running(new_inlay_hints));}++ chunk_hint_tasks+ .into_iter()+ .map(|(row_range, chunk_fetch)| {+ let task = match chunk_fetch {+ ChunkFetch::Cached(hints) => Task::ready(Ok(hints)),+ ChunkFetch::Running(hints_fetch) => cx.spawn(async move |_, _| {+ hints_fetch.await.map_err(|e| {+ if e.error_code() != ErrorCode::Internal {+ anyhow!(e.error_code())+ } else {+ anyhow!("{e:#}")+ }+ })+ }),+ };+ (row_range, task)+ })+ .collect()}fn fetch_inlay_hints(&mut self,- for_server: Option<LanguageServerId>,+ for_servers: Option<HashSet<LanguageServerId>>,buffer: &Entity<Buffer>,range: Range<Anchor>,cx: &mut Context<Self>,@@ -7901,7 +7879,7 @@ impl LspStore {.get_request_timeout();let request_task = upstream_client.request_lsp(project_id,- for_server.map(|id| id.to_proto()),+ upstream_lsp_query_server_filter(for_servers.as_ref()),request_timeout,cx.background_executor().clone(),request.to_proto(project_id, buffer.read(cx)),@@ -7961,29 +7939,13 @@ impl LspStore {Ok(inlay_hints)})} else {- let inlay_hints_task = match for_server {- Some(server_id) => {- let server_task = self.request_lsp(- buffer.clone(),- LanguageServerToQuery::Other(server_id),- request,- cx,- );- cx.background_spawn(async move {- let mut responses = Vec::new();- match server_task.await {- Ok(response) => responses.push((server_id, response)),- // rust-analyzer likes to error with this when its still loading up- Err(e) if format!("{e:#}").ends_with("content modified") => (),- Err(e) => log::error!(- "Error handling response for inlay hints request: {e:#}"- ),- }- responses- })- }- None => self.request_multiple_lsp_locally(buffer, None::<usize>, request, cx),- };+ let inlay_hints_task = self.request_filtered_lsp_locally(+ buffer,+ None::<usize>,+ request,+ for_servers.as_ref(),+ cx,+ );let buffer_snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());cx.background_spawn(async move {Ok(inlay_hints_taskdiff --git a/crates/project/src/lsp_store/inlay_hints.rs b/crates/project/src/lsp_store/inlay_hints.rsindex e688b163040..e7e110a628a 100644--- a/crates/project/src/lsp_store/inlay_hints.rs+++ b/crates/project/src/lsp_store/inlay_hints.rs@@ -58,6 +58,7 @@ pub struct BufferInlayHints {hints_by_id: HashMap<InlayId, HintForId>,pending_refreshes: HashSet<LanguageServerId>,work_end_refreshes: HashSet<LanguageServerId>,+ pub(super) fetched_servers: HashSet<LanguageServerId>,pub(super) hint_resolves: HashMap<InlayId, Shared<Task<()>>>,}@@ -90,6 +91,7 @@ impl BufferInlayHints {fetches_by_chunks: vec![None; chunks.len()],pending_refreshes: HashSet::default(),work_end_refreshes: HashSet::default(),+ fetched_servers: HashSet::default(),hints_by_id: HashMap::default(),hint_resolves: HashMap::default(),chunks,@@ -137,6 +139,7 @@ impl BufferInlayHints {}self.pending_refreshes.remove(&for_server);self.work_end_refreshes.remove(&for_server);+ self.fetched_servers.remove(&for_server);}fn mark_refresh_pending(&mut self, server_id: LanguageServerId) {@@ -165,6 +168,7 @@ impl BufferInlayHints {self.hint_resolves.clear();self.pending_refreshes.clear();self.work_end_refreshes.clear();+ self.fetched_servers.clear();}pub fn insert_new_hints(@@ -228,6 +232,7 @@ impl BufferInlayHints {self.fetches_by_chunks[chunk_id] = None;}}+ self.fetched_servers.remove(&for_server);true}diff --git a/crates/project/src/lsp_store/semantic_tokens.rs b/crates/project/src/lsp_store/semantic_tokens.rsindex e4fafcfcaf7..6b5c40253ba 100644--- a/crates/project/src/lsp_store/semantic_tokens.rs+++ b/crates/project/src/lsp_store/semantic_tokens.rs@@ -26,6 +26,7 @@ use crate::{LspCommand, SemanticTokensDelta, SemanticTokensEdit, SemanticTokensFull,SemanticTokensResponse,},+ lsp_store::missing_servers_to_query,project_settings::ProjectSettings,};@@ -87,6 +88,7 @@ impl LspStore {cx: &mut Context<Self>,) -> SemanticTokensTask {let version_queried_for = buffer.read(cx).version();+ let current_servers = self.relevant_server_ids_for_capability_check(&buffer, cx);let latest_lsp_data = self.latest_lsp_data(&buffer, cx);let semantic_tokens_data = latest_lsp_data.semantic_tokens.get_or_insert_default();let refreshed_servers = std::mem::take(&mut semantic_tokens_data.pending_refreshes);@@ -100,6 +102,15 @@ impl LspStore {.servers.remove(refreshed_server);}+ let missing_servers = missing_servers_to_query(+ &mut semantic_tokens_data.raw_tokens.servers,+ &mut semantic_tokens_data.fetched_servers,+ ¤t_servers,+ )+ .unwrap_or_default();+ if !missing_servers.is_empty() {+ semantic_tokens_data.update = None;+ }let query_generation = semantic_tokens_data.generation;if let Some((updating_for, task)) = &semantic_tokens_data.update@@ -108,14 +119,22 @@ impl LspStore {return task.clone();}- let for_server = if refreshed_servers.len() == 1 {- refreshed_servers.iter().next().copied()+ let mut servers_to_fetch = refreshed_servers;+ servers_to_fetch.extend(missing_servers);+ let for_server = if servers_to_fetch.len() == 1 {+ servers_to_fetch.iter().next().copied()} else {// With multiple servers refreshed, query all of them instead of fanning out// filtered queries — the non-refreshed ones kept their tokens and answer// with cheap deltas.None};+ semantic_tokens_data+ .fetched_servers+ .extend(match for_server {+ Some(server_id) => HashSet::from_iter([server_id]),+ None => current_servers,+ });let new_tokens = self.fetch_semantic_tokens_for_buffer(&buffer, for_server, cx);let task_buffer = buffer.clone();@@ -656,6 +675,7 @@ async fn raw_to_buffer_semantic_tokens(pub struct SemanticTokensData {pub(super) raw_tokens: RawSemanticTokens,pub(super) pending_refreshes: HashSet<LanguageServerId>,+ fetched_servers: HashSet<LanguageServerId>,update: Option<(Global, SemanticTokensTask)>,/// Bumped on every eviction so that fetches started against the evicted state/// cannot write their stale results into the new one.@@ -666,12 +686,14 @@ impl SemanticTokensData {pub(super) fn remove_server_data(&mut self, server_id: LanguageServerId) {self.raw_tokens.servers.remove(&server_id);self.pending_refreshes.remove(&server_id);+ self.fetched_servers.remove(&server_id);self.update = None;self.generation += 1;}fn evict_all(&mut self) {self.raw_tokens.servers.clear();+ self.fetched_servers.clear();self.update = None;self.generation += 1;}