Pass how many lines the editor should expand to in auto height mode

This commit is contained in:
Antonio Scandurra
2021-09-06 12:50:04 +02:00
parent cb62d53b49
commit 77d1574679
3 changed files with 23 additions and 23 deletions
+14 -11
View File
@@ -272,9 +272,10 @@ pub enum SelectPhase {
End,
}
enum EditorMode {
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum EditorMode {
SingleLine,
AutoHeight,
AutoHeight { max_lines: usize },
Full,
}
@@ -301,10 +302,9 @@ pub struct Editor {
}
pub struct Snapshot {
pub mode: EditorMode,
pub display_snapshot: DisplayMapSnapshot,
pub placeholder_text: Option<Arc<str>>,
pub gutter_visible: bool,
pub auto_height: bool,
pub theme: Arc<Theme>,
pub font_family: FamilyId,
pub font_size: f32,
@@ -332,10 +332,14 @@ impl Editor {
view
}
pub fn auto_height(settings: watch::Receiver<Settings>, cx: &mut ViewContext<Self>) -> Self {
pub fn auto_height(
max_lines: usize,
settings: watch::Receiver<Settings>,
cx: &mut ViewContext<Self>,
) -> Self {
let buffer = cx.add_model(|cx| Buffer::new(0, String::new(), cx));
let mut view = Self::for_buffer(buffer, settings, cx);
view.mode = EditorMode::AutoHeight;
view.mode = EditorMode::AutoHeight { max_lines };
view
}
@@ -407,9 +411,8 @@ impl Editor {
let settings = self.settings.borrow();
Snapshot {
mode: self.mode,
display_snapshot: self.display_map.update(cx, |map, cx| map.snapshot(cx)),
gutter_visible: matches!(self.mode, EditorMode::Full),
auto_height: matches!(self.mode, EditorMode::AutoHeight),
scroll_position: self.scroll_position,
scroll_top_anchor: self.scroll_top_anchor.clone(),
theme: settings.theme.clone(),
@@ -464,7 +467,7 @@ impl Editor {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut scroll_position =
compute_scroll_position(&display_map, self.scroll_position, &self.scroll_top_anchor);
let max_scroll_top = if matches!(self.mode, EditorMode::AutoHeight) {
let max_scroll_top = if matches!(self.mode, EditorMode::AutoHeight { .. }) {
(display_map.max_point().row() as f32 - visible_lines + 1.).max(0.)
} else {
display_map.max_point().row().saturating_sub(1) as f32
@@ -496,7 +499,7 @@ impl Editor {
.row() as f32
+ 1.0;
let margin = if matches!(self.mode, EditorMode::AutoHeight) {
let margin = if matches!(self.mode, EditorMode::AutoHeight { .. }) {
0.
} else {
((visible_lines - (last_cursor_bottom - first_cursor_top)) / 2.0)
@@ -760,7 +763,7 @@ impl Editor {
fn newline(&mut self, Newline(insert_newline): &Newline, cx: &mut ViewContext<Self>) {
match self.mode {
EditorMode::SingleLine => cx.propagate_action(),
EditorMode::AutoHeight => {
EditorMode::AutoHeight { .. } => {
if *insert_newline {
self.insert(&Insert("\n".into()), cx);
} else {