use gpui::{
App, Div, Entity, EntityId, InteractiveElement as _, IntoElement, Pixels, ShapedLine,
SharedString, Stateful, TextRun, TextStyle, View, ViewElement, Window, px,
};
use super::{EditorMode, InputBaseState, InputModeKind, element::LINE_NUMBER_RIGHT_MARGIN};
#[doc(hidden)]
pub trait EditorLineNumberRenderer: 'static {
fn measure_line_numbers(
&self,
digit_count: usize,
body_style: &TextStyle,
window: &Window,
) -> (Pixels, Pixels);
fn resolve_line_number_baseline_offset(
&self,
_body_style: &TextStyle,
_window: &Window,
) -> Pixels {
px(0.)
}
fn shape_line_number(
&self,
number: usize,
is_active: bool,
body_style: &TextStyle,
window: &Window,
cx: &App,
) -> ShapedLine;
}
impl EditorLineNumberRenderer for () {
fn measure_line_numbers(
&self,
digit_count: usize,
body_style: &TextStyle,
window: &Window,
) -> (Pixels, Pixels) {
if digit_count == 0 {
return (px(0.), LINE_NUMBER_RIGHT_MARGIN);
}
let font_size = body_style.font_size.to_pixels(window.rem_size());
let font_id = window.text_system().resolve_font(&body_style.font());
let width = "0123456789"
.chars()
.map(|digit| window.text_system().layout_width(font_id, font_size, digit))
.fold(px(0.), Pixels::max)
* digit_count;
(width, LINE_NUMBER_RIGHT_MARGIN)
}
fn resolve_line_number_baseline_offset(&self, _: &TextStyle, _: &Window) -> Pixels {
px(0.)
}
fn shape_line_number(
&self,
number: usize,
_: bool,
body_style: &TextStyle,
window: &Window,
_: &App,
) -> ShapedLine {
let label: SharedString = number.to_string().into();
let run = TextRun {
len: label.len(),
font: body_style.font(),
color: body_style.color,
background_color: None,
underline: None,
strikethrough: None,
};
window.text_system().shape_line(
label,
body_style.font_size.to_pixels(window.rem_size()),
&[run],
None,
)
}
}
pub type EditorState = InputBaseState<EditorMode>;
impl InputModeKind for EditorMode {
const MULTI_LINE: bool = true;
const CODE_EDITOR: bool = true;
type Extras = super::EditorExtras;
fn hover_definition_style(
state: &InputBaseState<Self>,
_cx: &App,
) -> Option<(std::ops::Range<usize>, gpui::HighlightStyle)> {
state.hover_definition_style()
}
fn hover_definition_hitbox(
state: &InputBaseState<Self>,
window: &mut Window,
_cx: &App,
) -> Option<gpui::Hitbox> {
state.hover_definition_hitbox(window)
}
fn reset_language_features(state: &mut InputBaseState<Self>) {
state.extras.lsp.reset();
}
fn reset_annotations(state: &mut InputBaseState<Self>) {
state.extras.hover_popover = None;
state.extras.decorations.clear();
state.extras.range_decorations.clear();
state.extras.gutter_decorations.clear();
state.extras.overview_ruler_decorations.clear();
}
fn editing_syntax_context(state: &InputBaseState<Self>, offset: usize) -> super::SyntaxContext {
state.syntax_context_at(offset)
}
fn adjust_annotations(
state: &mut InputBaseState<Self>,
range: &std::ops::Range<usize>,
new_len: usize,
) {
state.extras.decorations.adjust_for_edit(range, new_len);
state
.extras
.range_decorations
.adjust_for_edit(range, new_len);
state
.extras
.gutter_decorations
.adjust_for_edit(range, new_len);
state
.extras
.overview_ruler_decorations
.adjust_for_edit(range, new_len);
}
fn refresh_language_features(
state: &mut InputBaseState<Self>,
window: &mut Window,
cx: &mut gpui::Context<InputBaseState<Self>>,
) {
let text = state.text().clone();
state.extras.lsp.update(&text, window, cx);
}
fn accept_inline_completion(
state: &mut InputBaseState<Self>,
window: &mut Window,
cx: &mut gpui::Context<InputBaseState<Self>>,
) -> bool {
state.accept_inline_completion(window, cx)
}
fn has_inline_completion(state: &InputBaseState<Self>) -> bool {
state.has_inline_completion()
}
fn on_click(
state: &mut InputBaseState<Self>,
event: &gpui::MouseDownEvent,
offset: usize,
window: &mut Window,
cx: &mut gpui::Context<InputBaseState<Self>>,
) -> bool {
state.handle_click_hover_definition(event, offset, window, cx)
}
fn clear_hover_state(
state: &mut InputBaseState<Self>,
cx: &mut gpui::Context<InputBaseState<Self>>,
) {
state.clear_hover_state(cx);
}
fn on_text_typed(
state: &mut InputBaseState<Self>,
range: &std::ops::Range<usize>,
text: &str,
window: &mut Window,
cx: &mut gpui::Context<InputBaseState<Self>>,
) {
state.handle_completion_trigger(range, text, window, cx);
}
fn clear_inline_completion(
state: &mut InputBaseState<Self>,
cx: &mut gpui::Context<InputBaseState<Self>>,
) {
state.clear_inline_completion(cx);
}
fn hide_context_menu(
state: &mut InputBaseState<Self>,
cx: &mut gpui::Context<InputBaseState<Self>>,
) {
state.hide_context_menu(cx);
}
fn is_context_menu_open(state: &InputBaseState<Self>, cx: &App) -> bool {
state.is_context_menu_open(cx)
}
fn handle_context_menu_action(
state: &mut InputBaseState<Self>,
action: Box<dyn gpui::Action>,
window: &mut Window,
cx: &mut gpui::Context<InputBaseState<Self>>,
) -> bool {
state.handle_action_for_context_menu(action, window, cx)
}
fn on_hover_definition(
state: &mut InputBaseState<Self>,
offset: usize,
window: &mut Window,
cx: &mut gpui::Context<InputBaseState<Self>>,
) {
state.handle_hover_definition(offset, window, cx);
}
fn on_mouse_move(
state: &mut InputBaseState<Self>,
offset: usize,
event: &gpui::MouseMoveEvent,
window: &mut Window,
cx: &mut gpui::Context<InputBaseState<Self>>,
) {
state.handle_mouse_move(offset, event, window, cx);
}
fn drive_highlighter(
highlighter: &std::rc::Rc<std::cell::RefCell<Option<Box<dyn super::InputHighlighter>>>>,
edit: super::InputEdit,
text: &ropey::Rope,
folding: bool,
window: &mut Window,
cx: &mut gpui::Context<InputBaseState<Self>>,
) {
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<std::cell::RefCell<Option<Box<dyn super::InputHighlighter>>>>,
edits: &[(super::InputEdit, ropey::Rope)],
folding: bool,
window: &mut Window,
cx: &mut gpui::Context<InputBaseState<Self>>,
) {
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<Div>,
entity: &Entity<InputBaseState<Self>>,
window: &mut Window,
) -> Stateful<Div> {
element
.key_context("Input mode=editor")
.on_action(window.listener_for(entity, InputBaseState::on_action_toggle_code_actions))
.on_action(window.listener_for(entity, InputBaseState::on_action_show_completions))
.on_action(window.listener_for(entity, InputBaseState::on_action_go_to_definition))
}
}
impl EditorState {
pub fn lsp(&self) -> &super::Lsp {
&self.extras.lsp
}
pub fn lsp_mut(&mut self) -> &mut super::Lsp {
&mut self.extras.lsp
}
pub(crate) fn syntax_context_at(&self, offset: usize) -> super::SyntaxContext {
self.mode.syntax_context_at(&self.text, offset)
}
}
pub struct Editor<R = ()> {
state: Entity<EditorState>,
line_number_renderer: R,
}
impl Editor {
pub fn new(state: &Entity<EditorState>) -> Self {
Self {
state: state.clone(),
line_number_renderer: (),
}
}
}
impl<R: EditorLineNumberRenderer> Editor<R> {
#[doc(hidden)]
pub fn with_line_number_renderer<T: EditorLineNumberRenderer>(self, renderer: T) -> Editor<T> {
Editor {
state: self.state,
line_number_renderer: renderer,
}
}
}
impl<R: EditorLineNumberRenderer> IntoElement for Editor<R> {
type Element = ViewElement<Self>;
fn into_element(self) -> Self::Element {
ViewElement::new(self)
}
}
impl<R: EditorLineNumberRenderer> View for Editor<R> {
fn entity_id(&self) -> Option<EntityId> {
Some(self.state.entity_id())
}
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
self.state.update(cx, |state, cx| {
state
.render_with_line_numbers(self.line_number_renderer, window, cx)
.into_any_element()
})
}
}
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<usize>]) -> Vec<&super::RangeDecoration> {
self.range_decorations.intersecting(ranges)
}
fn gutter_decorations_width(&self) -> Option<gpui::Pixels> {
self.gutter_decorations
.has_collections()
.then_some(self.gutter_decorations_width)
}
fn gutter_decorations(&self, range: std::ops::Range<usize>) -> Vec<&super::GutterDecoration> {
self.gutter_decorations.intersecting(&[range])
}
fn overview_ruler_decorations(&self) -> Vec<&super::OverviewRulerDecoration> {
self.overview_ruler_decorations.iter().flatten().collect()
}
fn overview_ruler_revision(&self) -> u64 {
self.overview_ruler_decorations.revision()
}
fn semantic_token_styles(
&self,
text: &ropey::Rope,
range: &std::ops::Range<usize>,
resolver: &dyn crate::input::HighlightStyleResolver,
) -> Vec<(std::ops::Range<usize>, gpui::HighlightStyle)> {
self.lsp.semantic_tokens_for_range(text, range, resolver)
}
fn document_color_swatches(
&self,
text: &ropey::Rope,
range: &std::ops::Range<usize>,
) -> Vec<(std::ops::Range<usize>, gpui::Hsla)> {
self.lsp.document_colors_for_range(text, range)
}
fn hover_symbol_range(&self) -> Option<std::ops::Range<usize>> {
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(),
)
}
}