Skip to main content

gpui_deck/input/editor/
mod.rs

1// Modified for GPUI Deck: retain Editor rendering only for shared-engine regression tests.
2use gpui::{App, Div, Entity, InteractiveElement as _, Stateful, Window};
3#[cfg(test)]
4use gpui::{IntoElement, RenderOnce};
5
6use super::{EditorMode, InputBaseState, InputModeKind};
7
8/// State for source-code editing.
9///
10/// This is the shared editing engine in its code-editor kind. Languages, line
11/// numbers, folding, indent guides, diagnostics, decorations, and the LSP
12/// providers exist on this kind only, so an ordinary input or textarea never
13/// exposes them.
14pub type EditorState = InputBaseState<EditorMode>;
15
16impl InputModeKind for EditorMode {
17    const MULTI_LINE: bool = true;
18    const CODE_EDITOR: bool = true;
19
20    type Extras = super::EditorExtras;
21
22    fn hover_definition_style(
23        state: &InputBaseState<Self>,
24        _cx: &App,
25    ) -> Option<(std::ops::Range<usize>, gpui::HighlightStyle)> {
26        state.hover_definition_style()
27    }
28
29    fn hover_definition_hitbox(
30        state: &InputBaseState<Self>,
31        window: &mut Window,
32        _cx: &App,
33    ) -> Option<gpui::Hitbox> {
34        state.hover_definition_hitbox(window)
35    }
36
37    fn reset_language_features(state: &mut InputBaseState<Self>) {
38        state.extras.lsp.reset();
39    }
40
41    fn reset_annotations(state: &mut InputBaseState<Self>) {
42        state.extras.hover_popover = None;
43        state.extras.decorations.clear();
44        state.extras.range_decorations.clear();
45        state.extras.gutter_decorations.clear();
46        state.extras.overview_ruler_decorations.clear();
47    }
48
49    fn editing_syntax_context(state: &InputBaseState<Self>, offset: usize) -> super::SyntaxContext {
50        state.syntax_context_at(offset)
51    }
52
53    fn adjust_annotations(
54        state: &mut InputBaseState<Self>,
55        range: &std::ops::Range<usize>,
56        new_len: usize,
57    ) {
58        state.extras.decorations.adjust_for_edit(range, new_len);
59        state
60            .extras
61            .range_decorations
62            .adjust_for_edit(range, new_len);
63        state
64            .extras
65            .gutter_decorations
66            .adjust_for_edit(range, new_len);
67        state
68            .extras
69            .overview_ruler_decorations
70            .adjust_for_edit(range, new_len);
71    }
72
73    fn refresh_language_features(
74        state: &mut InputBaseState<Self>,
75        window: &mut Window,
76        cx: &mut gpui::Context<InputBaseState<Self>>,
77    ) {
78        let text = state.text().clone();
79        state.extras.lsp.update(&text, window, cx);
80    }
81
82    fn accept_inline_completion(
83        state: &mut InputBaseState<Self>,
84        window: &mut Window,
85        cx: &mut gpui::Context<InputBaseState<Self>>,
86    ) -> bool {
87        state.accept_inline_completion(window, cx)
88    }
89
90    fn has_inline_completion(state: &InputBaseState<Self>) -> bool {
91        state.has_inline_completion()
92    }
93
94    fn on_click(
95        state: &mut InputBaseState<Self>,
96        event: &gpui::MouseDownEvent,
97        offset: usize,
98        window: &mut Window,
99        cx: &mut gpui::Context<InputBaseState<Self>>,
100    ) -> bool {
101        state.handle_click_hover_definition(event, offset, window, cx)
102    }
103
104    fn clear_hover_state(
105        state: &mut InputBaseState<Self>,
106        cx: &mut gpui::Context<InputBaseState<Self>>,
107    ) {
108        state.clear_hover_state(cx);
109    }
110
111    fn on_text_typed(
112        state: &mut InputBaseState<Self>,
113        range: &std::ops::Range<usize>,
114        text: &str,
115        window: &mut Window,
116        cx: &mut gpui::Context<InputBaseState<Self>>,
117    ) {
118        state.handle_completion_trigger(range, text, window, cx);
119    }
120
121    fn clear_inline_completion(
122        state: &mut InputBaseState<Self>,
123        cx: &mut gpui::Context<InputBaseState<Self>>,
124    ) {
125        state.clear_inline_completion(cx);
126    }
127
128    fn hide_context_menu(
129        state: &mut InputBaseState<Self>,
130        cx: &mut gpui::Context<InputBaseState<Self>>,
131    ) {
132        state.hide_context_menu(cx);
133    }
134
135    fn is_context_menu_open(state: &InputBaseState<Self>, cx: &App) -> bool {
136        state.is_context_menu_open(cx)
137    }
138
139    fn handle_context_menu_action(
140        state: &mut InputBaseState<Self>,
141        action: Box<dyn gpui::Action>,
142        window: &mut Window,
143        cx: &mut gpui::Context<InputBaseState<Self>>,
144    ) -> bool {
145        state.handle_action_for_context_menu(action, window, cx)
146    }
147
148    fn on_hover_definition(
149        state: &mut InputBaseState<Self>,
150        offset: usize,
151        window: &mut Window,
152        cx: &mut gpui::Context<InputBaseState<Self>>,
153    ) {
154        state.handle_hover_definition(offset, window, cx);
155    }
156
157    fn on_mouse_move(
158        state: &mut InputBaseState<Self>,
159        offset: usize,
160        event: &gpui::MouseMoveEvent,
161        window: &mut Window,
162        cx: &mut gpui::Context<InputBaseState<Self>>,
163    ) {
164        state.handle_mouse_move(offset, event, window, cx);
165    }
166
167    fn drive_highlighter(
168        highlighter: &std::rc::Rc<std::cell::RefCell<Option<Box<dyn super::InputHighlighter>>>>,
169        edit: super::InputEdit,
170        text: &ropey::Rope,
171        folding: bool,
172        window: &mut Window,
173        cx: &mut gpui::Context<InputBaseState<Self>>,
174    ) {
175        let mut highlighter = highlighter.borrow_mut();
176        let Some(highlighter) = highlighter.as_mut() else {
177            return;
178        };
179        highlighter.update(Some(edit), text, folding, window, cx);
180    }
181
182    fn drive_highlighter_batch(
183        highlighter: &std::rc::Rc<std::cell::RefCell<Option<Box<dyn super::InputHighlighter>>>>,
184        edits: &[(super::InputEdit, ropey::Rope)],
185        folding: bool,
186        window: &mut Window,
187        cx: &mut gpui::Context<InputBaseState<Self>>,
188    ) {
189        let mut highlighter = highlighter.borrow_mut();
190        let Some(highlighter) = highlighter.as_mut() else {
191            return;
192        };
193        highlighter.update_batch(edits, folding, window, cx);
194    }
195
196    fn register_actions(
197        element: Stateful<Div>,
198        entity: &Entity<InputBaseState<Self>>,
199        window: &mut Window,
200    ) -> Stateful<Div> {
201        element
202            .key_context("Input mode=editor")
203            .on_action(window.listener_for(entity, InputBaseState::on_action_toggle_code_actions))
204            .on_action(window.listener_for(entity, InputBaseState::on_action_show_completions))
205            .on_action(window.listener_for(entity, InputBaseState::on_action_go_to_definition))
206    }
207}
208
209impl EditorState {
210    /// The LSP providers and their cached results.
211    ///
212    /// This exists on the editor alone: an ordinary input or textarea has no
213    /// language server, and no field to reach one through.
214    pub fn lsp(&self) -> &super::Lsp {
215        &self.extras.lsp
216    }
217
218    /// The LSP providers, mutably. Configure the providers through this.
219    pub fn lsp_mut(&mut self) -> &mut super::Lsp {
220        &mut self.extras.lsp
221    }
222
223    /// Syntax context at `offset`, or `Code` when the language has no provider.
224    pub(crate) fn syntax_context_at(&self, offset: usize) -> super::SyntaxContext {
225        self.mode.syntax_context_at(&self.text, offset)
226    }
227}
228
229/// An unstyled source-code editor.
230#[cfg(test)]
231#[derive(IntoElement)]
232pub(crate) struct Editor {
233    state: Entity<EditorState>,
234}
235
236#[cfg(test)]
237impl Editor {
238    pub(crate) fn new(state: &Entity<EditorState>) -> Self {
239        Self {
240            state: state.clone(),
241        }
242    }
243}
244
245#[cfg(test)]
246impl RenderOnce for Editor {
247    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
248        self.state
249    }
250}
251
252/// What a code editor exposes to the renderer. See [`crate::input::InputExtras`].
253impl crate::input::InputExtras for super::EditorExtras {
254    fn decoration_layers(&self) -> Vec<&[super::TextDecoration]> {
255        self.decorations.iter().collect()
256    }
257
258    fn range_decorations(&self, ranges: &[std::ops::Range<usize>]) -> Vec<&super::RangeDecoration> {
259        self.range_decorations.intersecting(ranges)
260    }
261
262    fn gutter_decorations_width(&self) -> Option<gpui::Pixels> {
263        self.gutter_decorations
264            .has_collections()
265            .then_some(self.gutter_decorations_width)
266    }
267
268    fn gutter_decorations(&self, range: std::ops::Range<usize>) -> Vec<&super::GutterDecoration> {
269        self.gutter_decorations.intersecting(&[range])
270    }
271
272    fn overview_ruler_decorations(&self) -> Vec<&super::OverviewRulerDecoration> {
273        self.overview_ruler_decorations.iter().flatten().collect()
274    }
275
276    fn overview_ruler_revision(&self) -> u64 {
277        self.overview_ruler_decorations.revision()
278    }
279
280    fn semantic_token_styles(
281        &self,
282        text: &ropey::Rope,
283        range: &std::ops::Range<usize>,
284        resolver: &dyn crate::input::HighlightStyleResolver,
285    ) -> Vec<(std::ops::Range<usize>, gpui::HighlightStyle)> {
286        self.lsp.semantic_tokens_for_range(text, range, resolver)
287    }
288
289    fn document_color_swatches(
290        &self,
291        text: &ropey::Rope,
292        range: &std::ops::Range<usize>,
293    ) -> Vec<(std::ops::Range<usize>, gpui::Hsla)> {
294        self.lsp.document_colors_for_range(text, range)
295    }
296
297    fn hover_symbol_range(&self) -> Option<std::ops::Range<usize>> {
298        self.hover_popover
299            .as_ref()
300            .map(|session| session.symbol_range.clone())
301    }
302
303    fn inline_completion_item(&self) -> Option<&lsp_types::InlineCompletionItem> {
304        self.inline_completion.item.as_ref()
305    }
306
307    fn context_menu_capabilities(&self) -> (bool, bool) {
308        (
309            self.lsp.definition_provider.is_some(),
310            !self.lsp.code_action_providers.is_empty(),
311        )
312    }
313}