inlays.rs

  1//! The logic, responsible for managing [`Inlay`]s in the editor.
  2//!
  3//! Inlays are "not real" text that gets mixed into the "real" buffer's text.
  4//! They are attached to a certain [`Anchor`], and display certain contents (usually, strings)
  5//! between real text around that anchor.
  6//!
  7//! Inlay examples in Zed:
  8//! * inlay hints, received from LSP
  9//! * inline values, shown in the debugger
 10//! * inline predictions, showing the Zeta/Copilot/etc. predictions
 11//! * document color values, if configured to be displayed as inlays
 12//! * ... anything else, potentially.
 13//!
 14//! Editor uses [`crate::DisplayMap`] and [`crate::display_map::InlayMap`] to manage what's rendered inside the editor, using
 15//! [`InlaySplice`] to update this state.
 16
 17/// Logic, related to managing LSP inlay hint inlays.
 18pub mod inlay_hints;
 19
 20use std::{any::TypeId, sync::OnceLock};
 21
 22use gpui::{Context, HighlightStyle, Hsla, Rgba, Task};
 23use multi_buffer::Anchor;
 24use project::{InlayHint, InlayId};
 25use text::Rope;
 26
 27use crate::{Editor, hover_links::InlayHighlight};
 28
 29/// A splice to send into the `inlay_map` for updating the visible inlays on the screen.
 30/// "Visible" inlays may not be displayed in the buffer right away, but those are ready to be displayed on further buffer scroll, pane item activations, etc. right away without additional LSP queries or settings changes.
 31/// The data in the cache is never used directly for displaying inlays on the screen, to avoid races with updates from LSP queries and sync overhead.
 32/// Splice is picked to help avoid extra hint flickering and "jumps" on the screen.
 33#[derive(Debug, Default)]
 34pub struct InlaySplice {
 35    pub to_remove: Vec<InlayId>,
 36    pub to_insert: Vec<Inlay>,
 37}
 38
 39impl InlaySplice {
 40    pub fn is_empty(&self) -> bool {
 41        self.to_remove.is_empty() && self.to_insert.is_empty()
 42    }
 43}
 44
 45#[derive(Debug, Clone)]
 46pub struct Inlay {
 47    pub id: InlayId,
 48    pub position: Anchor,
 49    pub content: InlayContent,
 50}
 51
 52#[derive(Debug, Clone)]
 53pub enum InlayContent {
 54    Text(text::Rope),
 55    Color(Hsla),
 56}
 57
 58impl Inlay {
 59    pub fn hint(id: InlayId, position: Anchor, hint: &InlayHint) -> Self {
 60        let mut text = hint.text();
 61        if hint.padding_right && text.reversed_chars_at(text.len()).next() != Some(' ') {
 62            text.push(" ");
 63        }
 64        if hint.padding_left && text.chars_at(0).next() != Some(' ') {
 65            text.push_front(" ");
 66        }
 67        Self {
 68            id,
 69            position,
 70            content: InlayContent::Text(text),
 71        }
 72    }
 73
 74    #[cfg(any(test, feature = "test-support"))]
 75    pub fn mock_hint(id: usize, position: Anchor, text: impl Into<Rope>) -> Self {
 76        Self {
 77            id: InlayId::Hint(id),
 78            position,
 79            content: InlayContent::Text(text.into()),
 80        }
 81    }
 82
 83    pub fn color(id: usize, position: Anchor, color: Rgba) -> Self {
 84        Self {
 85            id: InlayId::Color(id),
 86            position,
 87            content: InlayContent::Color(color.into()),
 88        }
 89    }
 90
 91    pub fn edit_prediction<T: Into<Rope>>(id: usize, position: Anchor, text: T) -> Self {
 92        Self {
 93            id: InlayId::EditPrediction(id),
 94            position,
 95            content: InlayContent::Text(text.into()),
 96        }
 97    }
 98
 99    pub fn debugger<T: Into<Rope>>(id: usize, position: Anchor, text: T) -> Self {
100        Self {
101            id: InlayId::DebuggerValue(id),
102            position,
103            content: InlayContent::Text(text.into()),
104        }
105    }
106
107    pub fn repl_result<T: Into<Rope>>(id: usize, position: Anchor, text: T) -> Self {
108        Self {
109            id: InlayId::ReplResult(id),
110            position,
111            content: InlayContent::Text(text.into()),
112        }
113    }
114
115    pub fn text(&self) -> &Rope {
116        static COLOR_TEXT: OnceLock<Rope> = OnceLock::new();
117        match &self.content {
118            InlayContent::Text(text) => text,
119            InlayContent::Color(_) => COLOR_TEXT.get_or_init(|| Rope::from("")),
120        }
121    }
122
123    #[cfg(any(test, feature = "test-support"))]
124    pub fn get_color(&self) -> Option<Hsla> {
125        match self.content {
126            InlayContent::Color(color) => Some(color),
127            _ => None,
128        }
129    }
130}
131
132pub struct InlineValueCache {
133    pub enabled: bool,
134    pub inlays: Vec<InlayId>,
135    pub refresh_task: Task<Option<()>>,
136}
137
138impl InlineValueCache {
139    pub fn new(enabled: bool) -> Self {
140        Self {
141            enabled,
142            inlays: Vec::new(),
143            refresh_task: Task::ready(None),
144        }
145    }
146}
147
148impl Editor {
149    /// Modify which hints are displayed in the editor.
150    pub fn splice_inlays(
151        &mut self,
152        to_remove: &[InlayId],
153        to_insert: Vec<Inlay>,
154        cx: &mut Context<Self>,
155    ) {
156        if let Some(inlay_hints) = &mut self.inlay_hints {
157            for id_to_remove in to_remove {
158                inlay_hints.added_hints.remove(id_to_remove);
159            }
160        }
161        self.display_map.update(cx, |display_map, cx| {
162            display_map.splice_inlays(to_remove, to_insert, cx)
163        });
164        cx.notify();
165    }
166
167    pub(crate) fn highlight_inlays<T: 'static>(
168        &mut self,
169        highlights: Vec<InlayHighlight>,
170        style: HighlightStyle,
171        cx: &mut Context<Self>,
172    ) {
173        self.display_map.update(cx, |map, _| {
174            map.highlight_inlays(TypeId::of::<T>(), highlights, style)
175        });
176        cx.notify();
177    }
178
179    pub fn inline_values_enabled(&self) -> bool {
180        self.inline_value_cache.enabled
181    }
182
183    #[cfg(any(test, feature = "test-support"))]
184    pub fn inline_value_inlays(&self, cx: &gpui::App) -> Vec<Inlay> {
185        self.display_map
186            .read(cx)
187            .current_inlays()
188            .filter(|inlay| matches!(inlay.id, InlayId::DebuggerValue(_)))
189            .cloned()
190            .collect()
191    }
192
193    #[cfg(any(test, feature = "test-support"))]
194    pub fn all_inlays(&self, cx: &gpui::App) -> Vec<Inlay> {
195        self.display_map
196            .read(cx)
197            .current_inlays()
198            .cloned()
199            .collect()
200    }
201}