1use gpui::{
2 App, Div, Entity, EntityId, InteractiveElement as _, IntoElement, Pixels, ShapedLine,
3 SharedString, Stateful, TextRun, TextStyle, View, ViewElement, Window, px,
4};
5
6use super::{EditorMode, InputBaseState, InputModeKind, element::LINE_NUMBER_RIGHT_MARGIN};
7
8#[doc(hidden)]
14pub trait EditorLineNumberRenderer: 'static {
15 fn measure_line_numbers(
18 &self,
19 digit_count: usize,
20 body_style: &TextStyle,
21 window: &Window,
22 ) -> (Pixels, Pixels);
23
24 fn resolve_line_number_baseline_offset(
27 &self,
28 _body_style: &TextStyle,
29 _window: &Window,
30 ) -> Pixels {
31 px(0.)
32 }
33
34 fn shape_line_number(
36 &self,
37 number: usize,
38 is_active: bool,
39 body_style: &TextStyle,
40 window: &Window,
41 cx: &App,
42 ) -> ShapedLine;
43}
44
45impl EditorLineNumberRenderer for () {
46 fn measure_line_numbers(
47 &self,
48 digit_count: usize,
49 body_style: &TextStyle,
50 window: &Window,
51 ) -> (Pixels, Pixels) {
52 if digit_count == 0 {
53 return (px(0.), LINE_NUMBER_RIGHT_MARGIN);
54 }
55 let font_size = body_style.font_size.to_pixels(window.rem_size());
56 let font_id = window.text_system().resolve_font(&body_style.font());
57 let width = "0123456789"
59 .chars()
60 .map(|digit| window.text_system().layout_width(font_id, font_size, digit))
61 .fold(px(0.), Pixels::max)
62 * digit_count;
63 (width, LINE_NUMBER_RIGHT_MARGIN)
64 }
65
66 fn resolve_line_number_baseline_offset(&self, _: &TextStyle, _: &Window) -> Pixels {
67 px(0.)
69 }
70
71 fn shape_line_number(
72 &self,
73 number: usize,
74 _: bool,
75 body_style: &TextStyle,
76 window: &Window,
77 _: &App,
78 ) -> ShapedLine {
79 let label: SharedString = number.to_string().into();
80 let run = TextRun {
83 len: label.len(),
84 font: body_style.font(),
85 color: body_style.color,
86 background_color: None,
87 underline: None,
88 strikethrough: None,
89 };
90 window.text_system().shape_line(
91 label,
92 body_style.font_size.to_pixels(window.rem_size()),
93 &[run],
94 None,
95 )
96 }
97}
98
99pub type EditorState = InputBaseState<EditorMode>;
106
107impl InputModeKind for EditorMode {
108 const MULTI_LINE: bool = true;
109 const CODE_EDITOR: bool = true;
110
111 type Extras = super::EditorExtras;
112
113 fn hover_definition_style(
114 state: &InputBaseState<Self>,
115 _cx: &App,
116 ) -> Option<(std::ops::Range<usize>, gpui::HighlightStyle)> {
117 state.hover_definition_style()
118 }
119
120 fn hover_definition_hitbox(
121 state: &InputBaseState<Self>,
122 window: &mut Window,
123 _cx: &App,
124 ) -> Option<gpui::Hitbox> {
125 state.hover_definition_hitbox(window)
126 }
127
128 fn reset_language_features(state: &mut InputBaseState<Self>) {
129 state.extras.lsp.reset();
130 }
131
132 fn reset_annotations(state: &mut InputBaseState<Self>) {
133 state.extras.hover_popover = None;
134 state.extras.decorations.clear();
135 state.extras.range_decorations.clear();
136 state.extras.gutter_decorations.clear();
137 state.extras.overview_ruler_decorations.clear();
138 }
139
140 fn editing_syntax_context(state: &InputBaseState<Self>, offset: usize) -> super::SyntaxContext {
141 state.syntax_context_at(offset)
142 }
143
144 fn adjust_annotations(
145 state: &mut InputBaseState<Self>,
146 range: &std::ops::Range<usize>,
147 new_len: usize,
148 ) {
149 state.extras.decorations.adjust_for_edit(range, new_len);
150 state
151 .extras
152 .range_decorations
153 .adjust_for_edit(range, new_len);
154 state
155 .extras
156 .gutter_decorations
157 .adjust_for_edit(range, new_len);
158 state
159 .extras
160 .overview_ruler_decorations
161 .adjust_for_edit(range, new_len);
162 }
163
164 fn refresh_language_features(
165 state: &mut InputBaseState<Self>,
166 window: &mut Window,
167 cx: &mut gpui::Context<InputBaseState<Self>>,
168 ) {
169 let text = state.text().clone();
170 state.extras.lsp.update(&text, window, cx);
171 }
172
173 fn accept_inline_completion(
174 state: &mut InputBaseState<Self>,
175 window: &mut Window,
176 cx: &mut gpui::Context<InputBaseState<Self>>,
177 ) -> bool {
178 state.accept_inline_completion(window, cx)
179 }
180
181 fn has_inline_completion(state: &InputBaseState<Self>) -> bool {
182 state.has_inline_completion()
183 }
184
185 fn on_click(
186 state: &mut InputBaseState<Self>,
187 event: &gpui::MouseDownEvent,
188 offset: usize,
189 window: &mut Window,
190 cx: &mut gpui::Context<InputBaseState<Self>>,
191 ) -> bool {
192 state.handle_click_hover_definition(event, offset, window, cx)
193 }
194
195 fn clear_hover_state(
196 state: &mut InputBaseState<Self>,
197 cx: &mut gpui::Context<InputBaseState<Self>>,
198 ) {
199 state.clear_hover_state(cx);
200 }
201
202 fn on_text_typed(
203 state: &mut InputBaseState<Self>,
204 range: &std::ops::Range<usize>,
205 text: &str,
206 window: &mut Window,
207 cx: &mut gpui::Context<InputBaseState<Self>>,
208 ) {
209 state.handle_completion_trigger(range, text, window, cx);
210 }
211
212 fn clear_inline_completion(
213 state: &mut InputBaseState<Self>,
214 cx: &mut gpui::Context<InputBaseState<Self>>,
215 ) {
216 state.clear_inline_completion(cx);
217 }
218
219 fn hide_context_menu(
220 state: &mut InputBaseState<Self>,
221 cx: &mut gpui::Context<InputBaseState<Self>>,
222 ) {
223 state.hide_context_menu(cx);
224 state.dismiss_hover_popover(cx);
227 }
228
229 fn is_context_menu_open(state: &InputBaseState<Self>, cx: &App) -> bool {
230 state.is_context_menu_open(cx)
231 }
232
233 fn handle_context_menu_action(
234 state: &mut InputBaseState<Self>,
235 action: Box<dyn gpui::Action>,
236 window: &mut Window,
237 cx: &mut gpui::Context<InputBaseState<Self>>,
238 ) -> bool {
239 state.handle_action_for_context_menu(action, window, cx)
240 }
241
242 fn on_hover_definition(
243 state: &mut InputBaseState<Self>,
244 offset: usize,
245 window: &mut Window,
246 cx: &mut gpui::Context<InputBaseState<Self>>,
247 ) {
248 state.handle_hover_definition(offset, window, cx);
249 }
250
251 fn on_mouse_move(
252 state: &mut InputBaseState<Self>,
253 offset: usize,
254 event: &gpui::MouseMoveEvent,
255 window: &mut Window,
256 cx: &mut gpui::Context<InputBaseState<Self>>,
257 ) {
258 state.handle_mouse_move(offset, event, window, cx);
259 }
260
261 fn drive_highlighter(
262 highlighter: &std::rc::Rc<std::cell::RefCell<Option<Box<dyn super::InputHighlighter>>>>,
263 edit: super::InputEdit,
264 text: &ropey::Rope,
265 folding: bool,
266 window: &mut Window,
267 cx: &mut gpui::Context<InputBaseState<Self>>,
268 ) {
269 let mut highlighter = highlighter.borrow_mut();
270 let Some(highlighter) = highlighter.as_mut() else {
271 return;
272 };
273 highlighter.update(Some(edit), text, folding, window, cx);
274 }
275
276 fn drive_highlighter_batch(
277 highlighter: &std::rc::Rc<std::cell::RefCell<Option<Box<dyn super::InputHighlighter>>>>,
278 edits: &[(super::InputEdit, ropey::Rope)],
279 folding: bool,
280 window: &mut Window,
281 cx: &mut gpui::Context<InputBaseState<Self>>,
282 ) {
283 let mut highlighter = highlighter.borrow_mut();
284 let Some(highlighter) = highlighter.as_mut() else {
285 return;
286 };
287 highlighter.update_batch(edits, folding, window, cx);
288 }
289
290 fn register_actions(
291 element: Stateful<Div>,
292 entity: &Entity<InputBaseState<Self>>,
293 window: &mut Window,
294 ) -> Stateful<Div> {
295 element
296 .key_context("Input mode=editor")
297 .on_action(window.listener_for(entity, InputBaseState::on_action_toggle_code_actions))
298 .on_action(window.listener_for(entity, InputBaseState::on_action_show_completions))
299 .on_action(window.listener_for(entity, InputBaseState::on_action_show_hover))
300 .on_action(window.listener_for(entity, InputBaseState::on_action_go_to_definition))
301 }
302}
303
304impl EditorState {
305 pub fn lsp(&self) -> &super::Lsp {
310 &self.extras.lsp
311 }
312
313 pub fn lsp_mut(&mut self) -> &mut super::Lsp {
315 &mut self.extras.lsp
316 }
317
318 pub(crate) fn syntax_context_at(&self, offset: usize) -> super::SyntaxContext {
320 self.mode.syntax_context_at(&self.text, offset)
321 }
322}
323
324pub struct Editor<R = ()> {
326 state: Entity<EditorState>,
327 line_number_renderer: R,
328}
329
330impl Editor {
331 pub fn new(state: &Entity<EditorState>) -> Self {
332 Self {
333 state: state.clone(),
334 line_number_renderer: (),
335 }
336 }
337}
338
339impl<R: EditorLineNumberRenderer> Editor<R> {
340 #[doc(hidden)]
342 pub fn with_line_number_renderer<T: EditorLineNumberRenderer>(self, renderer: T) -> Editor<T> {
343 Editor {
344 state: self.state,
345 line_number_renderer: renderer,
346 }
347 }
348}
349
350impl<R: EditorLineNumberRenderer> IntoElement for Editor<R> {
351 type Element = ViewElement<Self>;
352
353 fn into_element(self) -> Self::Element {
354 ViewElement::new(self)
355 }
356}
357
358impl<R: EditorLineNumberRenderer> View for Editor<R> {
359 fn entity_id(&self) -> Option<EntityId> {
360 Some(self.state.entity_id())
361 }
362
363 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
364 self.state.update(cx, |state, cx| {
366 state
367 .render_with_line_numbers(self.line_number_renderer, window, cx)
368 .into_any_element()
369 })
370 }
371}
372
373impl crate::input::InputExtras for super::EditorExtras {
375 fn decoration_layers(&self) -> Vec<&[super::TextDecoration]> {
376 self.decorations.iter().collect()
377 }
378
379 fn decorations_revision(&self) -> u64 {
380 self.decorations.revision()
381 }
382
383 fn range_decorations(&self, ranges: &[std::ops::Range<usize>]) -> Vec<&super::RangeDecoration> {
384 self.range_decorations.intersecting(ranges)
385 }
386
387 fn gutter_decorations_width(&self) -> Option<gpui::Pixels> {
388 self.gutter_decorations
389 .has_collections()
390 .then_some(self.gutter_decorations_width)
391 }
392
393 fn gutter_decorations(&self, range: std::ops::Range<usize>) -> Vec<&super::GutterDecoration> {
394 self.gutter_decorations.intersecting(&[range])
395 }
396
397 fn overview_ruler_decorations(&self) -> Vec<&super::OverviewRulerDecoration> {
398 self.overview_ruler_decorations.iter().flatten().collect()
399 }
400
401 fn overview_ruler_revision(&self) -> u64 {
402 self.overview_ruler_decorations.revision()
403 }
404
405 fn line_decorations(
406 &self,
407 rows: std::ops::Range<usize>,
408 cx: &App,
409 ) -> Vec<super::LineDecoration> {
410 self.line_decorations.query(rows, cx)
411 }
412
413 fn has_line_decorations(&self) -> bool {
414 self.line_decorations.has_any_provider()
415 }
416
417 fn has_gutter_icons(&self) -> bool {
418 self.line_decorations.has_gutter_icons()
419 }
420
421 fn spacer_provider(&self) -> Option<std::rc::Rc<dyn super::SpacerProvider>> {
422 self.spacer_provider.clone()
423 }
424
425 fn semantic_token_styles(
426 &self,
427 text: &ropey::Rope,
428 range: &std::ops::Range<usize>,
429 resolver: &dyn crate::input::HighlightStyleResolver,
430 ) -> Vec<(std::ops::Range<usize>, gpui::HighlightStyle)> {
431 self.lsp.semantic_tokens_for_range(text, range, resolver)
432 }
433
434 fn document_color_swatches(
435 &self,
436 text: &ropey::Rope,
437 range: &std::ops::Range<usize>,
438 ) -> Vec<(std::ops::Range<usize>, gpui::Hsla)> {
439 self.lsp.document_colors_for_range(text, range)
440 }
441
442 fn hover_symbol_range(&self) -> Option<std::ops::Range<usize>> {
443 self.hover_popover
444 .as_ref()
445 .map(|session| session.symbol_range.clone())
446 }
447
448 fn inline_completion_item(&self) -> Option<&lsp_types::InlineCompletionItem> {
449 self.inline_completion.item.as_ref()
450 }
451
452 fn context_menu_capabilities(&self) -> (bool, bool) {
453 (
454 self.lsp.definition_provider.is_some(),
455 !self.lsp.code_action_providers.is_empty(),
456 )
457 }
458}