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