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 text(&self) -> &Rope {
108 static COLOR_TEXT: OnceLock<Rope> = OnceLock::new();
109 match &self.content {
110 InlayContent::Text(text) => text,
111 InlayContent::Color(_) => COLOR_TEXT.get_or_init(|| Rope::from("◼")),
112 }
113 }
114
115 #[cfg(any(test, feature = "test-support"))]
116 pub fn get_color(&self) -> Option<Hsla> {
117 match self.content {
118 InlayContent::Color(color) => Some(color),
119 _ => None,
120 }
121 }
122}
123
124pub struct InlineValueCache {
125 pub enabled: bool,
126 pub inlays: Vec<InlayId>,
127 pub refresh_task: Task<Option<()>>,
128}
129
130impl InlineValueCache {
131 pub fn new(enabled: bool) -> Self {
132 Self {
133 enabled,
134 inlays: Vec::new(),
135 refresh_task: Task::ready(None),
136 }
137 }
138}
139
140impl Editor {
141 /// Modify which hints are displayed in the editor.
142 pub fn splice_inlays(
143 &mut self,
144 to_remove: &[InlayId],
145 to_insert: Vec<Inlay>,
146 cx: &mut Context<Self>,
147 ) {
148 if let Some(inlay_hints) = &mut self.inlay_hints {
149 for id_to_remove in to_remove {
150 inlay_hints.added_hints.remove(id_to_remove);
151 }
152 }
153 let a = to_insert
154 .iter()
155 .map(|inlay| (inlay.id, inlay.text().to_string()))
156 .collect::<Vec<_>>();
157 dbg!((
158 self.buffer.read(cx).is_singleton(),
159 "splice_inlays",
160 &to_remove,
161 a,
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<T: 'static>(
170 &mut self,
171 highlights: Vec<InlayHighlight>,
172 style: HighlightStyle,
173 cx: &mut Context<Self>,
174 ) {
175 self.display_map.update(cx, |map, _| {
176 map.highlight_inlays(TypeId::of::<T>(), highlights, style)
177 });
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}