use gpui::{App, Div, Entity, InteractiveElement as _, IntoElement, RenderOnce, Stateful, Window}; use super::{EditorMode, InputBaseState, InputModeKind}; /// State for source-code editing. /// /// This is the shared editing engine in its code-editor kind. Languages, line /// numbers, folding, indent guides, diagnostics, and the LSP /// providers exist on this kind only, so an ordinary input or textarea never /// exposes them. pub type EditorState = InputBaseState; impl InputModeKind for EditorMode { const MULTI_LINE: bool = true; const CODE_EDITOR: bool = true; type Extras = super::EditorExtras; fn hover_definition_style( state: &InputBaseState, _cx: &App, ) -> Option<(std::ops::Range, gpui::HighlightStyle)> { state.hover_definition_style() } fn hover_definition_hitbox( state: &InputBaseState, window: &mut Window, _cx: &App, ) -> Option { state.hover_definition_hitbox(window) } fn reset_language_features(state: &mut InputBaseState) { state.extras.lsp.reset(); } fn reset_annotations(state: &mut InputBaseState) { state.extras.hover_popover = None; state.extras.decorations.clear(); state.extras.range_decorations.clear(); } fn editing_syntax_context(state: &InputBaseState, offset: usize) -> super::SyntaxContext { state.syntax_context_at(offset) } fn adjust_annotations( state: &mut InputBaseState, range: &std::ops::Range, new_len: usize, ) { state.extras.decorations.adjust_for_edit(range, new_len); state .extras .range_decorations .adjust_for_edit(range, new_len); } fn refresh_language_features( state: &mut InputBaseState, window: &mut Window, cx: &mut gpui::Context>, ) { let text = state.text().clone(); state.extras.lsp.update(&text, window, cx); } fn accept_inline_completion( state: &mut InputBaseState, window: &mut Window, cx: &mut gpui::Context>, ) -> bool { state.accept_inline_completion(window, cx) } fn has_inline_completion(state: &InputBaseState) -> bool { state.has_inline_completion() } fn on_click( state: &mut InputBaseState, event: &gpui::MouseDownEvent, offset: usize, window: &mut Window, cx: &mut gpui::Context>, ) -> bool { state.handle_click_hover_definition(event, offset, window, cx) } fn clear_hover_state( state: &mut InputBaseState, cx: &mut gpui::Context>, ) { state.clear_hover_state(cx); } fn on_text_typed( state: &mut InputBaseState, range: &std::ops::Range, text: &str, window: &mut Window, cx: &mut gpui::Context>, ) { state.handle_completion_trigger(range, text, window, cx); } fn clear_inline_completion( state: &mut InputBaseState, cx: &mut gpui::Context>, ) { state.clear_inline_completion(cx); } fn hide_context_menu( state: &mut InputBaseState, cx: &mut gpui::Context>, ) { state.hide_context_menu(cx); } fn is_context_menu_open(state: &InputBaseState, cx: &App) -> bool { state.is_context_menu_open(cx) } fn handle_context_menu_action( state: &mut InputBaseState, action: Box, window: &mut Window, cx: &mut gpui::Context>, ) -> bool { state.handle_action_for_context_menu(action, window, cx) } fn on_hover_definition( state: &mut InputBaseState, offset: usize, window: &mut Window, cx: &mut gpui::Context>, ) { state.handle_hover_definition(offset, window, cx); } fn on_mouse_move( state: &mut InputBaseState, offset: usize, event: &gpui::MouseMoveEvent, window: &mut Window, cx: &mut gpui::Context>, ) { state.handle_mouse_move(offset, event, window, cx); } fn drive_highlighter( highlighter: &std::rc::Rc>>>, edit: super::InputEdit, text: &ropey::Rope, folding: bool, window: &mut Window, cx: &mut gpui::Context>, ) { let mut highlighter = highlighter.borrow_mut(); let Some(highlighter) = highlighter.as_mut() else { return; }; highlighter.update(Some(edit), text, folding, window, cx); } fn drive_highlighter_batch( highlighter: &std::rc::Rc>>>, edits: &[(super::InputEdit, ropey::Rope)], folding: bool, window: &mut Window, cx: &mut gpui::Context>, ) { let mut highlighter = highlighter.borrow_mut(); let Some(highlighter) = highlighter.as_mut() else { return; }; highlighter.update_batch(edits, folding, window, cx); } fn register_actions( element: Stateful
, entity: &Entity>, window: &mut Window, ) -> Stateful
{ element .on_action(window.listener_for(entity, InputBaseState::on_action_toggle_code_actions)) .on_action(window.listener_for(entity, InputBaseState::on_action_go_to_definition)) } } impl EditorState { /// The LSP providers and their cached results. /// /// This exists on the editor alone: an ordinary input or textarea has no /// language server, and no field to reach one through. pub fn lsp(&self) -> &super::Lsp { &self.extras.lsp } /// The LSP providers, mutably. Configure the providers through this. pub fn lsp_mut(&mut self) -> &mut super::Lsp { &mut self.extras.lsp } /// Syntax context at `offset`, or `Code` when the language has no provider. pub(crate) fn syntax_context_at(&self, offset: usize) -> super::SyntaxContext { self.mode.syntax_context_at(&self.text, offset) } } /// An unstyled source-code editor. #[derive(IntoElement)] pub struct Editor { state: Entity, } impl Editor { pub fn new(state: &Entity) -> Self { Self { state: state.clone(), } } } impl RenderOnce for Editor { fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement { self.state } } /// What a code editor exposes to the renderer. See [`crate::input::InputExtras`]. impl crate::input::InputExtras for super::EditorExtras { fn decoration_layers(&self) -> Vec<&[super::TextDecoration]> { self.decorations.iter().collect() } fn range_decorations(&self, ranges: &[std::ops::Range]) -> Vec<&super::RangeDecoration> { self.range_decorations.intersecting(ranges) } fn semantic_token_styles( &self, text: &ropey::Rope, range: &std::ops::Range, resolver: &dyn crate::input::HighlightStyleResolver, ) -> Vec<(std::ops::Range, gpui::HighlightStyle)> { self.lsp.semantic_tokens_for_range(text, range, resolver) } fn document_color_swatches( &self, text: &ropey::Rope, range: &std::ops::Range, ) -> Vec<(std::ops::Range, gpui::Hsla)> { self.lsp.document_colors_for_range(text, range) } fn hover_symbol_range(&self) -> Option> { self.hover_popover .as_ref() .map(|session| session.symbol_range.clone()) } fn inline_completion_item(&self) -> Option<&lsp_types::InlineCompletionItem> { self.inline_completion.item.as_ref() } fn context_menu_capabilities(&self) -> (bool, bool) { ( self.lsp.definition_provider.is_some(), !self.lsp.code_action_providers.is_empty(), ) } }