use std::rc::Rc;
use gpui::{
App, DefiniteLength, Entity, IntoElement, RenderOnce, SharedString, StyleRefinement, Styled,
Window, prelude::FluentBuilder as _, relative,
};
use super::{EditorState, GutterStyle, Input};
use crate::native_menu::NativeMenu;
use crate::{ActiveTheme as _, RoleOverride, Sizable, Size, StyledExt as _};
const EDITOR_LINE_HEIGHT: f32 = 1.5;
#[derive(IntoElement)]
pub struct Editor {
state: Entity<EditorState>,
gutter_style: GutterStyle,
style: StyleRefinement,
size: Size,
height: Option<DefiniteLength>,
appearance: bool,
bordered: bool,
disabled: bool,
readonly: bool,
tab_index: isize,
role: RoleOverride,
aria_label: Option<SharedString>,
context_menu_builder: Option<Rc<dyn Fn(NativeMenu, &mut Window, &mut App) -> NativeMenu>>,
paste_handler: Option<Rc<dyn Fn(&gpui::ClipboardItem, &mut Window, &mut App) -> bool>>,
}
impl Editor {
pub fn new(state: &Entity<EditorState>) -> Self {
Self {
state: state.clone(),
gutter_style: GutterStyle::default(),
style: StyleRefinement::default(),
size: Size::default(),
height: None,
appearance: true,
bordered: true,
disabled: false,
readonly: false,
tab_index: 0,
role: RoleOverride::default(),
aria_label: None,
context_menu_builder: None,
paste_handler: None,
}
}
pub fn gutter_style(mut self, style: GutterStyle) -> Self {
self.gutter_style.apply_overrides(style);
self
}
pub fn h(mut self, height: impl Into<DefiniteLength>) -> Self {
self.height = Some(height.into());
self
}
pub fn appearance(mut self, appearance: bool) -> Self {
self.appearance = appearance;
self
}
pub fn bordered(mut self, bordered: bool) -> Self {
self.bordered = bordered;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn readonly(mut self, readonly: bool) -> Self {
self.readonly = readonly;
self
}
pub fn tab_index(mut self, index: isize) -> Self {
self.tab_index = index;
self
}
pub fn role(mut self, role: impl Into<RoleOverride>) -> Self {
self.role = role.into();
self
}
pub fn aria_label(mut self, label: impl Into<SharedString>) -> Self {
self.aria_label = Some(label.into());
self
}
pub fn context_menu(
mut self,
f: impl Fn(NativeMenu, &mut Window, &mut App) -> NativeMenu + 'static,
) -> Self {
self.context_menu_builder = Some(Rc::new(f));
self
}
pub fn on_paste(
mut self,
handler: impl Fn(&gpui::ClipboardItem, &mut Window, &mut App) -> bool + 'static,
) -> Self {
self.paste_handler = Some(Rc::new(handler));
self
}
}
impl Sizable for Editor {
fn with_size(mut self, size: impl Into<Size>) -> Self {
self.size = size.into();
self
}
}
impl Styled for Editor {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl RenderOnce for Editor {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
Input::from_state(self.state.clone())
.gutter_style(self.gutter_style)
.font_family(cx.theme().mono_font_family.clone())
.text_size(cx.theme().mono_font_size)
.line_height(relative(EDITOR_LINE_HEIGHT))
.with_size(self.size)
.appearance(self.appearance)
.bordered(self.bordered)
.focus_bordered(false)
.disabled(self.disabled)
.readonly(self.readonly)
.tab_index(self.tab_index)
.role(self.role)
.when_some(self.height, |this, height| this.h(height))
.when_some(self.aria_label, |this, label| this.aria_label(label))
.when_some(self.context_menu_builder, |this, build| {
this.context_menu(move |menu, window, cx| build(menu, window, cx))
})
.when_some(self.paste_handler, |this, handler| {
this.on_paste(move |item, window, cx| handler(item, window, cx))
})
.refine_style(&self.style)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::input::EditorState;
use gpui::{
AppContext as _, Context, InteractiveElement as _, ParentElement as _, Pixels, Render,
TestAppContext, VisualTestContext, div, px, rems,
};
#[gpui::test]
fn test_editor_gutter_style_builder(cx: &mut TestAppContext) {
cx.update(crate::init);
cx.add_window_view(|window, cx| {
let state = cx.new(|cx| EditorState::new(window, cx));
let defaults = GutterStyle::default()
.with_gap(px(12.))
.with_line_number_color(gpui::rgb(0x808080).into())
.with_active_line_number_color(gpui::rgb(0xeeeeee).into())
.with_line_number_font_family("Number Font")
.with_line_number_font_size(px(12.));
let editor = Editor::new(&state)
.gutter_style(defaults.clone())
.gutter_style(GutterStyle::default().with_gap(px(0.)));
assert_eq!(editor.gutter_style, defaults.with_gap(px(0.)));
Harness {
state,
text_size: None,
}
});
}
struct GutterHarness {
state: Entity<EditorState>,
overrides: Option<GutterStyle>,
}
impl Render for GutterHarness {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().size_full().child(
Editor::new(&self.state)
.font_family(".SystemUIFont")
.text_size(rems(1.))
.when_some(self.overrides.clone(), |this, style| {
this.gutter_style(style)
}),
)
}
}
#[gpui::test]
fn gutter_overrides_and_removal_reach_the_rendered_editor(cx: &mut TestAppContext) {
cx.update(crate::init);
let (view, cx) = cx.add_window_view(|window, cx| GutterHarness {
state: cx.new(|cx| EditorState::new(window, cx).default_value("hello\nworld")),
overrides: None,
});
let state = view.read_with(cx, |view, _| view.state.clone());
let measure = |cx: &mut VisualTestContext| {
cx.update(|window, cx| {
window.refresh();
window.draw(cx).clear(cx);
let state = state.read(cx);
let caret = state
.prepaint_caret_geometry()
.expect("editor must prepaint");
(
caret.cursor_bounds().left() - caret.input_bounds().left(),
caret.line_height(),
state.range_to_bounds(&(0..1)).unwrap().size.width,
)
})
};
let baseline = measure(cx);
view.update(cx, |view, cx| {
view.overrides = Some(
GutterStyle::default()
.with_gap(px(18.))
.with_line_number_font_family(".SystemUIFont")
.with_line_number_font_size(px(16.)),
);
cx.notify();
});
let configured = measure(cx);
assert_eq!(configured.0 - baseline.0, px(12.));
assert_eq!((configured.1, configured.2), (baseline.1, baseline.2));
view.update(cx, |view, cx| {
view.overrides = Some(GutterStyle::default().with_gap(px(0.)));
cx.notify();
});
let overridden = measure(cx);
assert_eq!(configured.0 - overridden.0, px(18.));
view.update(cx, |view, cx| {
view.overrides = Some(
GutterStyle::default()
.with_gap(px(0.))
.with_line_number_font_size(px(24.)),
);
cx.notify();
});
let changed = measure(cx);
assert!(changed.0 > overridden.0);
assert_eq!((changed.1, changed.2), (baseline.1, baseline.2));
view.update(cx, |view, cx| {
view.overrides = Some(
GutterStyle::default()
.with_gap(px(24.))
.with_line_number_font_size(px(24.)),
);
cx.notify();
});
let restored = measure(cx);
assert_eq!(restored.0 - changed.0, px(24.));
view.update(cx, |view, cx| {
view.overrides = None;
cx.notify();
});
assert_eq!(measure(cx), baseline);
}
struct Harness {
state: Entity<EditorState>,
text_size: Option<Pixels>,
}
impl Render for Harness {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().size_full().child(
Editor::new(&self.state)
.when_some(self.text_size, |this, size| this.text_size(size)),
)
}
}
#[gpui::test]
fn gutter_and_body_follow_window_zoom_without_compounding(cx: &mut TestAppContext) {
use crate::{ElementExt as _, Root, Theme, init};
use std::cell::Cell;
struct ZoomHarness {
editor: Entity<GutterHarness>,
spacing_edges: Rc<Cell<(Pixels, Pixels)>>,
}
impl Render for ZoomHarness {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
let first = self.spacing_edges.clone();
let second = self.spacing_edges.clone();
div()
.relative()
.size_full()
.child(self.editor.clone())
.child(
div()
.absolute()
.flex()
.gap_2()
.child(div().w(px(1.)).h(px(1.)).on_prepaint(move |bounds, _, _| {
first.set((bounds.right(), first.get().1));
}))
.child(div().w(px(1.)).h(px(1.)).on_prepaint(move |bounds, _, _| {
second.set((second.get().0, bounds.left()));
})),
)
}
}
cx.update(|cx| {
init(cx);
Theme::update(cx, |theme| theme.font_size = px(22.));
});
let spacing_edges = Rc::new(Cell::new((px(0.), px(0.))));
let mut view = None;
let (_, cx) = cx.add_window_view(|window, cx| {
let editor = cx.new(|cx| GutterHarness {
state: cx.new(|cx| {
EditorState::new(window, cx)
.folding(false)
.default_value("hello\nworld")
}),
overrides: Some(
GutterStyle::default()
.with_gap(rems(0.75))
.with_line_number_font_size(rems(0.75)),
),
});
view = Some(editor.clone());
let content = cx.new(|_| ZoomHarness {
editor,
spacing_edges: spacing_edges.clone(),
});
Root::new(content, window, cx)
});
let view = view.unwrap();
let state = view.read_with(cx, |view, _| view.state.clone());
let measure = |cx: &mut VisualTestContext, rem_size: Option<f32>| {
cx.update(|window, cx| {
if let Some(rem_size) = rem_size {
Theme::update(cx, |theme| theme.font_size = px(rem_size));
}
for _ in 0..2 {
window.refresh();
window.draw(cx).clear(cx);
}
let caret = state.read(cx).prepaint_caret_geometry().unwrap();
let (first_right, second_left) = spacing_edges.get();
(
caret.cursor_bounds().left() - caret.input_bounds().left(),
caret.line_height(),
second_left - first_right,
window.rem_size(),
)
})
};
let initial = measure(cx, None);
assert_eq!(
(initial.1, initial.2, initial.3),
(px(33.), px(11.), px(22.))
);
let baseline = measure(cx, Some(16.));
for (rem_size, scale, expected_height) in [
(12., 0.75, 18.),
(24., 1.5, 36.),
(32., 2., 48.),
(16., 1., 24.),
] {
let measured = measure(cx, Some(rem_size));
let expected_width = baseline.0 * scale;
let tolerance = measured.0.abs().max(expected_width.abs()) * (4. * f32::EPSILON);
assert!((measured.0 - expected_width).abs() <= tolerance);
assert_eq!(measured.1, px(expected_height));
assert_eq!(measured.2, px(rem_size * 0.5));
assert_eq!(measured.3, px(rem_size));
}
assert_eq!(measure(cx, Some(16.)), baseline);
assert_eq!(measure(cx, None), baseline);
let zoomed = measure(cx, Some(24.));
assert_eq!(measure(cx, None), zoomed);
view.read_with(cx, |view, _| {
let style = view.overrides.as_ref().unwrap();
assert_eq!(style.gap(), Some(rems(0.75).into()));
assert_eq!(style.line_number_font_size(), Some(rems(0.75).into()));
});
}
fn line_height(cx: &mut TestAppContext, text_size: Option<Pixels>) -> Pixels {
cx.update(crate::init);
let mut state = None;
let (_, cx) = cx.add_window_view(|window, cx| {
let editor = cx.new(|cx| EditorState::new(window, cx).default_value("fn main() {}"));
state = Some(editor.clone());
Harness {
state: editor,
text_size,
}
});
let state = state.unwrap();
VisualTestContext::update(cx, |window, cx| window.draw(cx).clear(cx));
cx.read(|cx| {
state
.read(cx)
.line_height()
.expect("the editor must lay out")
})
}
#[gpui::test]
fn the_rows_follow_the_font_size(cx: &mut TestAppContext) {
assert_eq!(line_height(cx, None), px(20.));
assert_eq!(line_height(cx, Some(px(24.))), px(36.));
assert_eq!(line_height(cx, Some(px(40.))), px(60.));
}
struct SizedHarness {
state: Entity<EditorState>,
size: Option<Size>,
}
impl Render for SizedHarness {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().debug_selector(|| "frame".into()).size_full().child(
Editor::new(&self.state).when_some(self.size, |this, size| this.with_size(size)),
)
}
}
fn top_inset(cx: &mut TestAppContext, size: Option<Size>) -> (Pixels, Pixels) {
cx.update(crate::init);
let mut state = None;
let (_, cx) = cx.add_window_view(|window, cx| {
let editor = cx.new(|cx| EditorState::new(window, cx).default_value("fn main() {}"));
state = Some(editor.clone());
SizedHarness {
state: editor,
size,
}
});
let state = state.unwrap();
VisualTestContext::update(cx, |window, cx| window.draw(cx).clear(cx));
let frame = cx.debug_bounds("frame").expect("the frame must lay out");
let (text, line_height) = cx.read(|cx| {
let state = state.read(cx);
let line_height = state.line_height().expect("the editor must lay out");
(state.input_bounds(), line_height)
});
(text.top() - frame.top(), line_height)
}
#[gpui::test]
fn the_insets_follow_the_size(cx: &mut TestAppContext) {
let (medium, rows) = top_inset(cx, Some(Size::Medium));
assert_eq!(top_inset(cx, None), (medium, rows));
for size in [Size::XSmall, Size::Small, Size::Large] {
let (inset, line_height) = top_inset(cx, Some(size));
assert_eq!(
medium - inset,
Size::Medium.input_py() - size.input_py(),
"{size:?}"
);
assert_eq!(line_height, rows, "{size:?}");
}
}
#[gpui::test]
fn language_config_works_without_render_sync(cx: &mut TestAppContext) {
use crate::input::{AutoClosingPair, language_config::LanguageConfig, set_language_config};
use gpui::EntityInputHandler as _;
cx.update(crate::init);
let mut state = None;
let (_, cx) = cx.add_window_view(|window, cx| {
let editor = cx.new(|cx| EditorState::new(window, cx).language("plaintext"));
editor.update(cx, |state, cx| {
state.replace_text_in_range(None, "(", window, cx);
assert_eq!(state.text().to_string(), "(");
state.set_value("", window, cx);
state.set_highlighter("json", cx);
state.replace_text_in_range(None, "[", window, cx);
assert_eq!(state.text().to_string(), "[]");
});
state = Some(editor.clone());
Harness {
state: editor,
text_size: None,
}
});
let state = state.unwrap();
VisualTestContext::update(cx, |_, cx| {
set_language_config(
"json",
LanguageConfig::default().auto_closing_pairs([AutoClosingPair::new("«", "»")]),
cx,
);
});
VisualTestContext::update(cx, |window, cx| window.draw(cx).clear(cx));
VisualTestContext::update(cx, |window, cx| {
state.update(cx, |state, cx| {
state.set_value("", window, cx);
state.replace_text_in_range(None, "«", window, cx);
assert_eq!(state.text().to_string(), "«»");
state.set_value("if enabled:", window, cx);
state.set_selected_range(11..11, cx);
state.set_highlighter("python", cx);
state.focus(window, cx);
});
});
VisualTestContext::update(cx, |window, cx| window.draw(cx).clear(cx));
cx.simulate_keystrokes("enter");
cx.read(|cx| assert_eq!(state.read(cx).text().to_string(), "if enabled:\n "));
}
#[gpui::test]
fn aliases_share_config_even_when_registered_before_init(cx: &mut TestAppContext) {
use crate::input::{AutoClosingPair, language_config::LanguageConfig, set_language_config};
use gpui::EntityInputHandler as _;
cx.update(|cx| {
set_language_config(
"py",
LanguageConfig::default().auto_closing_pairs([AutoClosingPair::new("«", "»")]),
cx,
)
});
cx.update(crate::init);
cx.add_window_view(|window, cx| {
let editor = cx.new(|cx| EditorState::new(window, cx).language("PYTHON"));
editor.update(cx, |state, cx| {
state.replace_text_in_range(None, "«", window, cx);
assert_eq!(state.text().to_string(), "«»");
});
set_language_config("pyi", LanguageConfig::default().auto_closing_pairs([]), cx);
editor.update(cx, |state, cx| {
state.set_value("", window, cx);
state.set_highlighter("py", cx);
state.replace_text_in_range(None, "(", window, cx);
assert_eq!(state.text().to_string(), "(");
});
Harness {
state: editor,
text_size: None,
}
});
}
#[cfg(all(feature = "tree-sitter-python", feature = "tree-sitter-rust"))]
#[gpui::test]
fn syntax_follows_language_before_first_render_and_after_switch(cx: &mut TestAppContext) {
use gpui::EntityInputHandler as _;
cx.update(crate::init);
cx.add_window_view(|window, cx| {
let editor = cx.new(|cx| {
EditorState::new(window, cx)
.language("python")
.default_value("\"hello world\"")
});
editor.update(cx, |state, cx| {
state.set_selected_range(6..6, cx);
state.replace_text_in_range(None, "(", window, cx);
assert_eq!(state.text().to_string(), "\"hello( world\"");
state.set_value("# comment ", window, cx);
state.set_selected_range(10..10, cx);
state.replace_text_in_range(None, "(", window, cx);
assert_eq!(state.text().to_string(), "# comment (");
state.set_value("# comment ", window, cx);
state.set_selected_range(10..10, cx);
state.set_highlighter("rust", cx);
state.replace_text_in_range(None, "(", window, cx);
assert_eq!(state.text().to_string(), "# comment ()");
});
Harness {
state: editor,
text_size: None,
}
});
}
#[cfg(feature = "tree-sitter-python")]
#[gpui::test]
fn python_pairing_uses_pre_edit_context(cx: &mut TestAppContext) {
use gpui::EntityInputHandler as _;
cx.update(crate::init);
for (before, cursor, typed, expected) in [
("x = ", 4, "\"", "x = \"\""),
("x = f\"{value}\"", 12, "(", "x = f\"{value()}\""),
("x = \"value\"", 7, "(", "x = \"va(lue\""),
("x = \"value\"", 5, "(", "x = \"(value\""),
("x = \"value\"", 10, "(", "x = \"value(\""),
] {
let mut state = None;
let (_, cx) = cx.add_window_view(|window, cx| {
let editor = cx.new(|cx| EditorState::new(window, cx).default_value(before));
state = Some(editor.clone());
Harness {
state: editor,
text_size: None,
}
});
let state = state.unwrap();
VisualTestContext::update(cx, |window, cx| {
state.update(cx, |state, cx| {
state.set_highlighter("python", cx);
state.set_selected_range(cursor..cursor, cx);
state.replace_text_in_range(None, typed, window, cx);
assert_eq!(state.text().to_string(), expected);
});
});
}
}
#[cfg(feature = "tree-sitter-rust")]
#[gpui::test]
fn generated_comment_closer_survives_syntax_changes(cx: &mut TestAppContext) {
use crate::input::{AutoClosingPair, SyntaxContext, language_config::LanguageConfig};
use gpui::EntityInputHandler as _;
cx.update(crate::init);
cx.update(|cx| {
crate::input::set_language_config(
"rust",
LanguageConfig::default().auto_closing_pairs([AutoClosingPair::new("/*", "*/")
.not_in([SyntaxContext::String, SyntaxContext::Comment])]),
cx,
)
});
let mut state = None;
let (_, cx) = cx.add_window_view(|window, cx| {
let editor = cx.new(|cx| EditorState::new(window, cx).language("rust"));
state = Some(editor.clone());
Harness {
state: editor,
text_size: None,
}
});
let state = state.unwrap();
VisualTestContext::update(cx, |window, cx| {
state.update(cx, |state, cx| {
state.set_highlighter("rust", cx);
for text in ["/", "*", "x", "*", "/"] {
state.replace_text_in_range(None, text, window, cx);
}
assert_eq!(state.text().to_string(), "/*x*/");
state.replace_text_in_range(None, "!", window, cx);
assert_eq!(state.text().to_string(), "/*x*/!");
});
});
}
#[gpui::test]
fn test_on_paste_builder(cx: &mut TestAppContext) {
use gpui::{AppContext as _, Render};
struct PasteProbe;
impl Render for PasteProbe {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
}
}
cx.update(crate::init);
let _ = cx.add_window_view(|window, cx| {
let state = cx.new(|cx| EditorState::new(window, cx));
assert!(Editor::new(&state).paste_handler.is_none());
let editor = Editor::new(&state).on_paste(|_, _, _| true);
assert!(editor.paste_handler.is_some());
PasteProbe
});
}
}