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::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, HighlightKey, 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 // TODO this could be an ExcerptAnchor
49 pub position: Anchor,
50 pub content: InlayContent,
51}
52
53#[derive(Debug, Clone)]
54pub enum InlayContent {
55 Text(text::Rope),
56 Color(Hsla),
57}
58
59impl Inlay {
60 pub fn hint(id: InlayId, position: Anchor, hint: &InlayHint) -> Self {
61 let mut text = hint.text();
62 let needs_right_padding = hint.padding_right && !text.ends_with(" ");
63 let needs_left_padding = hint.padding_left && !text.starts_with(" ");
64 if needs_right_padding {
65 text.push(" ");
66 }
67 if needs_left_padding {
68 text.push_front(" ");
69 }
70 Self {
71 id,
72 position,
73 content: InlayContent::Text(text),
74 }
75 }
76
77 #[cfg(any(test, feature = "test-support"))]
78 pub fn mock_hint(id: usize, position: Anchor, text: impl Into<Rope>) -> Self {
79 Self {
80 id: InlayId::Hint(id),
81 position,
82 content: InlayContent::Text(text.into()),
83 }
84 }
85
86 pub fn color(id: usize, position: Anchor, color: Rgba) -> Self {
87 Self {
88 id: InlayId::Color(id),
89 position,
90 content: InlayContent::Color(color.into()),
91 }
92 }
93
94 pub fn edit_prediction<T: Into<Rope>>(id: usize, position: Anchor, text: T) -> Self {
95 Self {
96 id: InlayId::EditPrediction(id),
97 position,
98 content: InlayContent::Text(text.into()),
99 }
100 }
101
102 pub fn debugger<T: Into<Rope>>(id: usize, position: Anchor, text: T) -> Self {
103 Self {
104 id: InlayId::DebuggerValue(id),
105 position,
106 content: InlayContent::Text(text.into()),
107 }
108 }
109
110 pub fn repl_result<T: Into<Rope>>(id: usize, position: Anchor, text: T) -> Self {
111 Self {
112 id: InlayId::ReplResult(id),
113 position,
114 content: InlayContent::Text(text.into()),
115 }
116 }
117
118 pub fn text(&self) -> &Rope {
119 static COLOR_TEXT: OnceLock<Rope> = OnceLock::new();
120 match &self.content {
121 InlayContent::Text(text) => text,
122 InlayContent::Color(_) => COLOR_TEXT.get_or_init(|| Rope::from("◼")),
123 }
124 }
125
126 #[cfg(any(test, feature = "test-support"))]
127 pub fn get_color(&self) -> Option<Hsla> {
128 match self.content {
129 InlayContent::Color(color) => Some(color),
130 _ => None,
131 }
132 }
133}
134
135pub struct InlineValueCache {
136 pub enabled: bool,
137 pub inlays: Vec<InlayId>,
138 pub refresh_task: Task<Option<()>>,
139}
140
141impl InlineValueCache {
142 pub fn new(enabled: bool) -> Self {
143 Self {
144 enabled,
145 inlays: Vec::new(),
146 refresh_task: Task::ready(None),
147 }
148 }
149}
150
151impl Editor {
152 /// Modify which hints are displayed in the editor.
153 pub fn splice_inlays(
154 &mut self,
155 to_remove: &[InlayId],
156 to_insert: Vec<Inlay>,
157 cx: &mut Context<Self>,
158 ) {
159 if let Some(inlay_hints) = &mut self.inlay_hints {
160 for id_to_remove in to_remove {
161 inlay_hints.added_hints.remove(id_to_remove);
162 }
163 }
164 self.display_map.update(cx, |display_map, cx| {
165 display_map.splice_inlays(to_remove, to_insert, cx)
166 });
167 cx.notify();
168 }
169
170 pub(crate) fn highlight_inlays(
171 &mut self,
172 key: HighlightKey,
173 highlights: Vec<InlayHighlight>,
174 style: HighlightStyle,
175 cx: &mut Context<Self>,
176 ) {
177 self.display_map
178 .update(cx, |map, _| map.highlight_inlays(key, highlights, style));
179 cx.notify();
180 }
181
182 pub fn inline_values_enabled(&self) -> bool {
183 self.inline_value_cache.enabled
184 }
185
186 #[cfg(any(test, feature = "test-support"))]
187 pub fn inline_value_inlays(&self, cx: &gpui::App) -> Vec<Inlay> {
188 self.display_map
189 .read(cx)
190 .current_inlays()
191 .filter(|inlay| matches!(inlay.id, InlayId::DebuggerValue(_)))
192 .cloned()
193 .collect()
194 }
195
196 #[cfg(any(test, feature = "test-support"))]
197 pub fn all_inlays(&self, cx: &gpui::App) -> Vec<Inlay> {
198 self.display_map
199 .read(cx)
200 .current_inlays()
201 .cloned()
202 .collect()
203 }
204}