element.rs

   1use super::{
   2    display_map::{BlockContext, ToDisplayPoint},
   3    Anchor, DisplayPoint, Editor, EditorMode, EditorSnapshot, Input, Scroll, Select, SelectPhase,
   4    SoftWrap, ToPoint, MAX_LINE_LEN,
   5};
   6use crate::{display_map::TransformBlock, EditorStyle};
   7use clock::ReplicaId;
   8use collections::{BTreeMap, HashMap};
   9use gpui::{
  10    color::Color,
  11    elements::*,
  12    fonts::{HighlightStyle, Underline},
  13    geometry::{
  14        rect::RectF,
  15        vector::{vec2f, Vector2F},
  16        PathBuilder,
  17    },
  18    json::{self, ToJson},
  19    text_layout::{self, Line, RunStyle, TextLayoutCache},
  20    AppContext, Axis, Border, Element, ElementBox, Event, EventContext, LayoutContext,
  21    MutableAppContext, PaintContext, Quad, Scene, SizeConstraint, ViewContext, WeakViewHandle,
  22};
  23use json::json;
  24use language::Bias;
  25use smallvec::SmallVec;
  26use std::{
  27    cmp::{self, Ordering},
  28    fmt::Write,
  29    ops::Range,
  30};
  31
  32pub struct EditorElement {
  33    view: WeakViewHandle<Editor>,
  34    style: EditorStyle,
  35    cursor_shape: CursorShape,
  36}
  37
  38impl EditorElement {
  39    pub fn new(
  40        view: WeakViewHandle<Editor>,
  41        style: EditorStyle,
  42        cursor_shape: CursorShape,
  43    ) -> Self {
  44        Self {
  45            view,
  46            style,
  47            cursor_shape,
  48        }
  49    }
  50
  51    fn view<'a>(&self, cx: &'a AppContext) -> &'a Editor {
  52        self.view.upgrade(cx).unwrap().read(cx)
  53    }
  54
  55    fn update_view<F, T>(&self, cx: &mut MutableAppContext, f: F) -> T
  56    where
  57        F: FnOnce(&mut Editor, &mut ViewContext<Editor>) -> T,
  58    {
  59        self.view.upgrade(cx).unwrap().update(cx, f)
  60    }
  61
  62    fn snapshot(&self, cx: &mut MutableAppContext) -> EditorSnapshot {
  63        self.update_view(cx, |view, cx| view.snapshot(cx))
  64    }
  65
  66    fn mouse_down(
  67        &self,
  68        position: Vector2F,
  69        alt: bool,
  70        shift: bool,
  71        mut click_count: usize,
  72        layout: &mut LayoutState,
  73        paint: &mut PaintState,
  74        cx: &mut EventContext,
  75    ) -> bool {
  76        if paint.gutter_bounds.contains_point(position) {
  77            click_count = 3; // Simulate triple-click when clicking the gutter to select lines
  78        } else if !paint.text_bounds.contains_point(position) {
  79            return false;
  80        }
  81
  82        let snapshot = self.snapshot(cx.app);
  83        let (position, overshoot) = paint.point_for_position(&snapshot, layout, position);
  84
  85        if shift && alt {
  86            cx.dispatch_action(Select(SelectPhase::BeginColumnar {
  87                position,
  88                overshoot,
  89            }));
  90        } else if shift {
  91            cx.dispatch_action(Select(SelectPhase::Extend {
  92                position,
  93                click_count,
  94            }));
  95        } else {
  96            cx.dispatch_action(Select(SelectPhase::Begin {
  97                position,
  98                add: alt,
  99                click_count,
 100            }));
 101        }
 102
 103        true
 104    }
 105
 106    fn mouse_up(&self, _position: Vector2F, cx: &mut EventContext) -> bool {
 107        if self.view(cx.app.as_ref()).is_selecting() {
 108            cx.dispatch_action(Select(SelectPhase::End));
 109            true
 110        } else {
 111            false
 112        }
 113    }
 114
 115    fn mouse_dragged(
 116        &self,
 117        position: Vector2F,
 118        layout: &mut LayoutState,
 119        paint: &mut PaintState,
 120        cx: &mut EventContext,
 121    ) -> bool {
 122        let view = self.view(cx.app.as_ref());
 123
 124        if view.is_selecting() {
 125            let rect = paint.text_bounds;
 126            let mut scroll_delta = Vector2F::zero();
 127
 128            let vertical_margin = layout.line_height.min(rect.height() / 3.0);
 129            let top = rect.origin_y() + vertical_margin;
 130            let bottom = rect.lower_left().y() - vertical_margin;
 131            if position.y() < top {
 132                scroll_delta.set_y(-scale_vertical_mouse_autoscroll_delta(top - position.y()))
 133            }
 134            if position.y() > bottom {
 135                scroll_delta.set_y(scale_vertical_mouse_autoscroll_delta(position.y() - bottom))
 136            }
 137
 138            let horizontal_margin = layout.line_height.min(rect.width() / 3.0);
 139            let left = rect.origin_x() + horizontal_margin;
 140            let right = rect.upper_right().x() - horizontal_margin;
 141            if position.x() < left {
 142                scroll_delta.set_x(-scale_horizontal_mouse_autoscroll_delta(
 143                    left - position.x(),
 144                ))
 145            }
 146            if position.x() > right {
 147                scroll_delta.set_x(scale_horizontal_mouse_autoscroll_delta(
 148                    position.x() - right,
 149                ))
 150            }
 151
 152            let snapshot = self.snapshot(cx.app);
 153            let (position, overshoot) = paint.point_for_position(&snapshot, layout, position);
 154
 155            cx.dispatch_action(Select(SelectPhase::Update {
 156                position,
 157                overshoot,
 158                scroll_position: (snapshot.scroll_position() + scroll_delta)
 159                    .clamp(Vector2F::zero(), layout.scroll_max),
 160            }));
 161            true
 162        } else {
 163            false
 164        }
 165    }
 166
 167    fn key_down(&self, input: Option<&str>, cx: &mut EventContext) -> bool {
 168        let view = self.view.upgrade(cx.app).unwrap();
 169
 170        if view.is_focused(cx.app) {
 171            if let Some(input) = input {
 172                cx.dispatch_action(Input(input.to_string()));
 173                true
 174            } else {
 175                false
 176            }
 177        } else {
 178            false
 179        }
 180    }
 181
 182    fn scroll(
 183        &self,
 184        position: Vector2F,
 185        mut delta: Vector2F,
 186        precise: bool,
 187        layout: &mut LayoutState,
 188        paint: &mut PaintState,
 189        cx: &mut EventContext,
 190    ) -> bool {
 191        if !paint.bounds.contains_point(position) {
 192            return false;
 193        }
 194
 195        let snapshot = self.snapshot(cx.app);
 196        let max_glyph_width = layout.em_width;
 197        if !precise {
 198            delta *= vec2f(max_glyph_width, layout.line_height);
 199        }
 200
 201        let scroll_position = snapshot.scroll_position();
 202        let x = (scroll_position.x() * max_glyph_width - delta.x()) / max_glyph_width;
 203        let y = (scroll_position.y() * layout.line_height - delta.y()) / layout.line_height;
 204        let scroll_position = vec2f(x, y).clamp(Vector2F::zero(), layout.scroll_max);
 205
 206        cx.dispatch_action(Scroll(scroll_position));
 207
 208        true
 209    }
 210
 211    fn paint_background(
 212        &self,
 213        gutter_bounds: RectF,
 214        text_bounds: RectF,
 215        layout: &LayoutState,
 216        cx: &mut PaintContext,
 217    ) {
 218        let bounds = gutter_bounds.union_rect(text_bounds);
 219        let scroll_top = layout.snapshot.scroll_position().y() * layout.line_height;
 220        let editor = self.view(cx.app);
 221        cx.scene.push_quad(Quad {
 222            bounds: gutter_bounds,
 223            background: Some(self.style.gutter_background),
 224            border: Border::new(0., Color::transparent_black()),
 225            corner_radius: 0.,
 226        });
 227        cx.scene.push_quad(Quad {
 228            bounds: text_bounds,
 229            background: Some(self.style.background),
 230            border: Border::new(0., Color::transparent_black()),
 231            corner_radius: 0.,
 232        });
 233
 234        if let EditorMode::Full = editor.mode {
 235            let mut active_rows = layout.active_rows.iter().peekable();
 236            while let Some((start_row, contains_non_empty_selection)) = active_rows.next() {
 237                let mut end_row = *start_row;
 238                while active_rows.peek().map_or(false, |r| {
 239                    *r.0 == end_row + 1 && r.1 == contains_non_empty_selection
 240                }) {
 241                    active_rows.next().unwrap();
 242                    end_row += 1;
 243                }
 244
 245                if !contains_non_empty_selection {
 246                    let origin = vec2f(
 247                        bounds.origin_x(),
 248                        bounds.origin_y() + (layout.line_height * *start_row as f32) - scroll_top,
 249                    );
 250                    let size = vec2f(
 251                        bounds.width(),
 252                        layout.line_height * (end_row - start_row + 1) as f32,
 253                    );
 254                    cx.scene.push_quad(Quad {
 255                        bounds: RectF::new(origin, size),
 256                        background: Some(self.style.active_line_background),
 257                        border: Border::default(),
 258                        corner_radius: 0.,
 259                    });
 260                }
 261            }
 262
 263            if let Some(highlighted_rows) = &layout.highlighted_rows {
 264                let origin = vec2f(
 265                    bounds.origin_x(),
 266                    bounds.origin_y() + (layout.line_height * highlighted_rows.start as f32)
 267                        - scroll_top,
 268                );
 269                let size = vec2f(
 270                    bounds.width(),
 271                    layout.line_height * highlighted_rows.len() as f32,
 272                );
 273                cx.scene.push_quad(Quad {
 274                    bounds: RectF::new(origin, size),
 275                    background: Some(self.style.highlighted_line_background),
 276                    border: Border::default(),
 277                    corner_radius: 0.,
 278                });
 279            }
 280        }
 281    }
 282
 283    fn paint_gutter(
 284        &mut self,
 285        bounds: RectF,
 286        visible_bounds: RectF,
 287        layout: &mut LayoutState,
 288        cx: &mut PaintContext,
 289    ) {
 290        let scroll_top = layout.snapshot.scroll_position().y() * layout.line_height;
 291        for (ix, line) in layout.line_number_layouts.iter().enumerate() {
 292            if let Some(line) = line {
 293                let line_origin = bounds.origin()
 294                    + vec2f(
 295                        bounds.width() - line.width() - layout.gutter_padding,
 296                        ix as f32 * layout.line_height - (scroll_top % layout.line_height),
 297                    );
 298                line.paint(line_origin, visible_bounds, layout.line_height, cx);
 299            }
 300        }
 301
 302        if let Some((row, indicator)) = layout.code_actions_indicator.as_mut() {
 303            let mut x = bounds.width() - layout.gutter_padding;
 304            let mut y = *row as f32 * layout.line_height - scroll_top;
 305            x += ((layout.gutter_padding + layout.gutter_margin) - indicator.size().x()) / 2.;
 306            y += (layout.line_height - indicator.size().y()) / 2.;
 307            indicator.paint(bounds.origin() + vec2f(x, y), visible_bounds, cx);
 308        }
 309    }
 310
 311    fn paint_text(
 312        &mut self,
 313        bounds: RectF,
 314        visible_bounds: RectF,
 315        layout: &mut LayoutState,
 316        cx: &mut PaintContext,
 317    ) {
 318        let view = self.view(cx.app);
 319        let style = &self.style;
 320        let local_replica_id = view.replica_id(cx);
 321        let scroll_position = layout.snapshot.scroll_position();
 322        let start_row = scroll_position.y() as u32;
 323        let scroll_top = scroll_position.y() * layout.line_height;
 324        let end_row = ((scroll_top + bounds.height()) / layout.line_height).ceil() as u32 + 1; // Add 1 to ensure selections bleed off screen
 325        let max_glyph_width = layout.em_width;
 326        let scroll_left = scroll_position.x() * max_glyph_width;
 327        let content_origin = bounds.origin() + vec2f(layout.gutter_margin, 0.);
 328
 329        cx.scene.push_layer(Some(bounds));
 330
 331        for (range, color) in &layout.highlighted_ranges {
 332            self.paint_highlighted_range(
 333                range.clone(),
 334                start_row,
 335                end_row,
 336                *color,
 337                0.,
 338                0.15 * layout.line_height,
 339                layout,
 340                content_origin,
 341                scroll_top,
 342                scroll_left,
 343                bounds,
 344                cx,
 345            );
 346        }
 347
 348        let mut cursors = SmallVec::<[Cursor; 32]>::new();
 349        for (replica_id, selections) in &layout.selections {
 350            let selection_style = style.replica_selection_style(*replica_id);
 351            let corner_radius = 0.15 * layout.line_height;
 352
 353            for selection in selections {
 354                self.paint_highlighted_range(
 355                    selection.start..selection.end,
 356                    start_row,
 357                    end_row,
 358                    selection_style.selection,
 359                    corner_radius,
 360                    corner_radius * 2.,
 361                    layout,
 362                    content_origin,
 363                    scroll_top,
 364                    scroll_left,
 365                    bounds,
 366                    cx,
 367                );
 368
 369                if view.show_local_cursors() || *replica_id != local_replica_id {
 370                    let cursor_position = selection.head();
 371                    if (start_row..end_row).contains(&cursor_position.row()) {
 372                        let cursor_row_layout =
 373                            &layout.line_layouts[(cursor_position.row() - start_row) as usize];
 374                        let cursor_column = cursor_position.column() as usize;
 375
 376                        let cursor_character_x = cursor_row_layout.x_for_index(cursor_column);
 377                        let mut block_width =
 378                            cursor_row_layout.x_for_index(cursor_column + 1) - cursor_character_x;
 379                        if block_width == 0.0 {
 380                            block_width = layout.em_width;
 381                        }
 382
 383                        let block_text =
 384                            if matches!(self.cursor_shape, CursorShape::Block) {
 385                                layout.snapshot.chars_at(cursor_position).next().and_then(
 386                                    |character| {
 387                                        let font_id =
 388                                            cursor_row_layout.font_for_index(cursor_column)?;
 389                                        let text = character.to_string();
 390
 391                                        Some(cx.text_layout_cache.layout_str(
 392                                            &text,
 393                                            cursor_row_layout.font_size(),
 394                                            &[(
 395                                                text.len(),
 396                                                RunStyle {
 397                                                    font_id,
 398                                                    color: style.background,
 399                                                    underline: None,
 400                                                },
 401                                            )],
 402                                        ))
 403                                    },
 404                                )
 405                            } else {
 406                                None
 407                            };
 408
 409                        let x = cursor_character_x - scroll_left;
 410                        let y = cursor_position.row() as f32 * layout.line_height - scroll_top;
 411                        cursors.push(Cursor {
 412                            color: selection_style.cursor,
 413                            block_width,
 414                            origin: content_origin + vec2f(x, y),
 415                            line_height: layout.line_height,
 416                            shape: self.cursor_shape,
 417                            block_text,
 418                        });
 419                    }
 420                }
 421            }
 422        }
 423
 424        if let Some(visible_text_bounds) = bounds.intersection(visible_bounds) {
 425            // Draw glyphs
 426            for (ix, line) in layout.line_layouts.iter().enumerate() {
 427                let row = start_row + ix as u32;
 428                line.paint(
 429                    content_origin
 430                        + vec2f(-scroll_left, row as f32 * layout.line_height - scroll_top),
 431                    visible_text_bounds,
 432                    layout.line_height,
 433                    cx,
 434                );
 435            }
 436        }
 437
 438        cx.scene.push_layer(Some(bounds));
 439        for cursor in cursors {
 440            cursor.paint(cx);
 441        }
 442        cx.scene.pop_layer();
 443
 444        if let Some((position, context_menu)) = layout.context_menu.as_mut() {
 445            cx.scene.push_stacking_context(None);
 446
 447            let cursor_row_layout = &layout.line_layouts[(position.row() - start_row) as usize];
 448            let x = cursor_row_layout.x_for_index(position.column() as usize) - scroll_left;
 449            let y = (position.row() + 1) as f32 * layout.line_height - scroll_top;
 450            let mut list_origin = content_origin + vec2f(x, y);
 451            let list_height = context_menu.size().y();
 452
 453            if list_origin.y() + list_height > bounds.lower_left().y() {
 454                list_origin.set_y(list_origin.y() - layout.line_height - list_height);
 455            }
 456
 457            context_menu.paint(
 458                list_origin,
 459                RectF::from_points(Vector2F::zero(), vec2f(f32::MAX, f32::MAX)), // Let content bleed outside of editor
 460                cx,
 461            );
 462
 463            cx.scene.pop_stacking_context();
 464        }
 465
 466        cx.scene.pop_layer();
 467    }
 468
 469    fn paint_highlighted_range(
 470        &self,
 471        range: Range<DisplayPoint>,
 472        start_row: u32,
 473        end_row: u32,
 474        color: Color,
 475        corner_radius: f32,
 476        line_end_overshoot: f32,
 477        layout: &LayoutState,
 478        content_origin: Vector2F,
 479        scroll_top: f32,
 480        scroll_left: f32,
 481        bounds: RectF,
 482        cx: &mut PaintContext,
 483    ) {
 484        if range.start != range.end {
 485            let row_range = if range.end.column() == 0 {
 486                cmp::max(range.start.row(), start_row)..cmp::min(range.end.row(), end_row)
 487            } else {
 488                cmp::max(range.start.row(), start_row)..cmp::min(range.end.row() + 1, end_row)
 489            };
 490
 491            let highlighted_range = HighlightedRange {
 492                color,
 493                line_height: layout.line_height,
 494                corner_radius,
 495                start_y: content_origin.y() + row_range.start as f32 * layout.line_height
 496                    - scroll_top,
 497                lines: row_range
 498                    .into_iter()
 499                    .map(|row| {
 500                        let line_layout = &layout.line_layouts[(row - start_row) as usize];
 501                        HighlightedRangeLine {
 502                            start_x: if row == range.start.row() {
 503                                content_origin.x()
 504                                    + line_layout.x_for_index(range.start.column() as usize)
 505                                    - scroll_left
 506                            } else {
 507                                content_origin.x() - scroll_left
 508                            },
 509                            end_x: if row == range.end.row() {
 510                                content_origin.x()
 511                                    + line_layout.x_for_index(range.end.column() as usize)
 512                                    - scroll_left
 513                            } else {
 514                                content_origin.x() + line_layout.width() + line_end_overshoot
 515                                    - scroll_left
 516                            },
 517                        }
 518                    })
 519                    .collect(),
 520            };
 521
 522            highlighted_range.paint(bounds, cx.scene);
 523        }
 524    }
 525
 526    fn paint_blocks(
 527        &mut self,
 528        bounds: RectF,
 529        visible_bounds: RectF,
 530        layout: &mut LayoutState,
 531        cx: &mut PaintContext,
 532    ) {
 533        let scroll_position = layout.snapshot.scroll_position();
 534        let scroll_left = scroll_position.x() * layout.em_width;
 535        let scroll_top = scroll_position.y() * layout.line_height;
 536
 537        for (row, element) in &mut layout.blocks {
 538            let origin = bounds.origin()
 539                + vec2f(-scroll_left, *row as f32 * layout.line_height - scroll_top);
 540            element.paint(origin, visible_bounds, cx);
 541        }
 542    }
 543
 544    fn max_line_number_width(&self, snapshot: &EditorSnapshot, cx: &LayoutContext) -> f32 {
 545        let digit_count = (snapshot.max_buffer_row() as f32).log10().floor() as usize + 1;
 546        let style = &self.style;
 547
 548        cx.text_layout_cache
 549            .layout_str(
 550                "1".repeat(digit_count).as_str(),
 551                style.text.font_size,
 552                &[(
 553                    digit_count,
 554                    RunStyle {
 555                        font_id: style.text.font_id,
 556                        color: Color::black(),
 557                        underline: None,
 558                    },
 559                )],
 560            )
 561            .width()
 562    }
 563
 564    fn layout_line_numbers(
 565        &self,
 566        rows: Range<u32>,
 567        active_rows: &BTreeMap<u32, bool>,
 568        snapshot: &EditorSnapshot,
 569        cx: &LayoutContext,
 570    ) -> Vec<Option<text_layout::Line>> {
 571        let style = &self.style;
 572        let include_line_numbers = snapshot.mode == EditorMode::Full;
 573        let mut line_number_layouts = Vec::with_capacity(rows.len());
 574        let mut line_number = String::new();
 575        for (ix, row) in snapshot
 576            .buffer_rows(rows.start)
 577            .take((rows.end - rows.start) as usize)
 578            .enumerate()
 579        {
 580            let display_row = rows.start + ix as u32;
 581            let color = if active_rows.contains_key(&display_row) {
 582                style.line_number_active
 583            } else {
 584                style.line_number
 585            };
 586            if let Some(buffer_row) = row {
 587                if include_line_numbers {
 588                    line_number.clear();
 589                    write!(&mut line_number, "{}", buffer_row + 1).unwrap();
 590                    line_number_layouts.push(Some(cx.text_layout_cache.layout_str(
 591                        &line_number,
 592                        style.text.font_size,
 593                        &[(
 594                            line_number.len(),
 595                            RunStyle {
 596                                font_id: style.text.font_id,
 597                                color,
 598                                underline: None,
 599                            },
 600                        )],
 601                    )));
 602                }
 603            } else {
 604                line_number_layouts.push(None);
 605            }
 606        }
 607
 608        line_number_layouts
 609    }
 610
 611    fn layout_lines(
 612        &mut self,
 613        mut rows: Range<u32>,
 614        snapshot: &mut EditorSnapshot,
 615        cx: &LayoutContext,
 616    ) -> Vec<text_layout::Line> {
 617        rows.end = cmp::min(rows.end, snapshot.max_point().row() + 1);
 618        if rows.start >= rows.end {
 619            return Vec::new();
 620        }
 621
 622        // When the editor is empty and unfocused, then show the placeholder.
 623        if snapshot.is_empty() && !snapshot.is_focused() {
 624            let placeholder_style = self
 625                .style
 626                .placeholder_text
 627                .as_ref()
 628                .unwrap_or_else(|| &self.style.text);
 629            let placeholder_text = snapshot.placeholder_text();
 630            let placeholder_lines = placeholder_text
 631                .as_ref()
 632                .map_or("", AsRef::as_ref)
 633                .split('\n')
 634                .skip(rows.start as usize)
 635                .take(rows.len());
 636            return placeholder_lines
 637                .map(|line| {
 638                    cx.text_layout_cache.layout_str(
 639                        line,
 640                        placeholder_style.font_size,
 641                        &[(
 642                            line.len(),
 643                            RunStyle {
 644                                font_id: placeholder_style.font_id,
 645                                color: placeholder_style.color,
 646                                underline: None,
 647                            },
 648                        )],
 649                    )
 650                })
 651                .collect();
 652        } else {
 653            let style = &self.style;
 654            let chunks = snapshot.chunks(rows.clone(), true).map(|chunk| {
 655                let highlight_style = chunk
 656                    .highlight_id
 657                    .and_then(|highlight_id| highlight_id.style(&style.syntax));
 658                let highlight = if let Some(severity) = chunk.diagnostic {
 659                    let diagnostic_style = super::diagnostic_style(severity, true, style);
 660                    let underline = Some(Underline {
 661                        color: diagnostic_style.message.text.color,
 662                        thickness: 1.0.into(),
 663                        squiggly: true,
 664                    });
 665                    if let Some(mut highlight) = highlight_style {
 666                        highlight.underline = underline;
 667                        Some(highlight)
 668                    } else {
 669                        Some(HighlightStyle {
 670                            underline,
 671                            color: style.text.color,
 672                            font_properties: style.text.font_properties,
 673                        })
 674                    }
 675                } else {
 676                    highlight_style
 677                };
 678                (chunk.text, highlight)
 679            });
 680            layout_highlighted_chunks(
 681                chunks,
 682                &style.text,
 683                &cx.text_layout_cache,
 684                &cx.font_cache,
 685                MAX_LINE_LEN,
 686                rows.len() as usize,
 687            )
 688        }
 689    }
 690
 691    fn layout_blocks(
 692        &mut self,
 693        rows: Range<u32>,
 694        snapshot: &EditorSnapshot,
 695        width: f32,
 696        gutter_padding: f32,
 697        gutter_width: f32,
 698        em_width: f32,
 699        text_x: f32,
 700        line_height: f32,
 701        style: &EditorStyle,
 702        line_layouts: &[text_layout::Line],
 703        cx: &mut LayoutContext,
 704    ) -> Vec<(u32, ElementBox)> {
 705        let scroll_x = snapshot.scroll_position.x();
 706        snapshot
 707            .blocks_in_range(rows.clone())
 708            .map(|(block_row, block)| {
 709                let mut element = match block {
 710                    TransformBlock::Custom(block) => {
 711                        let align_to = block
 712                            .position()
 713                            .to_point(&snapshot.buffer_snapshot)
 714                            .to_display_point(snapshot);
 715                        let anchor_x = text_x
 716                            + if rows.contains(&align_to.row()) {
 717                                line_layouts[(align_to.row() - rows.start) as usize]
 718                                    .x_for_index(align_to.column() as usize)
 719                            } else {
 720                                layout_line(align_to.row(), snapshot, style, cx.text_layout_cache)
 721                                    .x_for_index(align_to.column() as usize)
 722                            };
 723
 724                        block.render(&BlockContext {
 725                            cx,
 726                            anchor_x,
 727                            gutter_padding,
 728                            line_height,
 729                            scroll_x,
 730                            gutter_width,
 731                            em_width,
 732                        })
 733                    }
 734                    TransformBlock::ExcerptHeader {
 735                        buffer,
 736                        starts_new_buffer,
 737                        ..
 738                    } => {
 739                        if *starts_new_buffer {
 740                            let style = &self.style.diagnostic_path_header;
 741                            let font_size =
 742                                (style.text_scale_factor * self.style.text.font_size).round();
 743
 744                            let mut filename = None;
 745                            let mut parent_path = None;
 746                            if let Some(path) = buffer.path() {
 747                                filename =
 748                                    path.file_name().map(|f| f.to_string_lossy().to_string());
 749                                parent_path =
 750                                    path.parent().map(|p| p.to_string_lossy().to_string() + "/");
 751                            }
 752
 753                            Flex::row()
 754                                .with_child(
 755                                    Label::new(
 756                                        filename.unwrap_or_else(|| "untitled".to_string()),
 757                                        style.filename.text.clone().with_font_size(font_size),
 758                                    )
 759                                    .contained()
 760                                    .with_style(style.filename.container)
 761                                    .boxed(),
 762                                )
 763                                .with_children(parent_path.map(|path| {
 764                                    Label::new(
 765                                        path,
 766                                        style.path.text.clone().with_font_size(font_size),
 767                                    )
 768                                    .contained()
 769                                    .with_style(style.path.container)
 770                                    .boxed()
 771                                }))
 772                                .aligned()
 773                                .left()
 774                                .contained()
 775                                .with_style(style.container)
 776                                .with_padding_left(gutter_padding + scroll_x * em_width)
 777                                .expanded()
 778                                .named("path header block")
 779                        } else {
 780                            let text_style = self.style.text.clone();
 781                            Label::new("".to_string(), text_style)
 782                                .contained()
 783                                .with_padding_left(gutter_padding + scroll_x * em_width)
 784                                .named("collapsed context")
 785                        }
 786                    }
 787                };
 788
 789                element.layout(
 790                    SizeConstraint {
 791                        min: Vector2F::zero(),
 792                        max: vec2f(width, block.height() as f32 * line_height),
 793                    },
 794                    cx,
 795                );
 796                (block_row, element)
 797            })
 798            .collect()
 799    }
 800}
 801
 802impl Element for EditorElement {
 803    type LayoutState = LayoutState;
 804    type PaintState = PaintState;
 805
 806    fn layout(
 807        &mut self,
 808        constraint: SizeConstraint,
 809        cx: &mut LayoutContext,
 810    ) -> (Vector2F, Self::LayoutState) {
 811        let mut size = constraint.max;
 812        if size.x().is_infinite() {
 813            unimplemented!("we don't yet handle an infinite width constraint on buffer elements");
 814        }
 815
 816        let snapshot = self.snapshot(cx.app);
 817        let style = self.style.clone();
 818        let line_height = style.text.line_height(cx.font_cache);
 819
 820        let gutter_padding;
 821        let gutter_width;
 822        let gutter_margin;
 823        if snapshot.mode == EditorMode::Full {
 824            gutter_padding = style.text.em_width(cx.font_cache) * style.gutter_padding_factor;
 825            gutter_width = self.max_line_number_width(&snapshot, cx) + gutter_padding * 2.0;
 826            gutter_margin = -style.text.descent(cx.font_cache);
 827        } else {
 828            gutter_padding = 0.0;
 829            gutter_width = 0.0;
 830            gutter_margin = 0.0;
 831        };
 832
 833        let text_width = size.x() - gutter_width;
 834        let em_width = style.text.em_width(cx.font_cache);
 835        let em_advance = style.text.em_advance(cx.font_cache);
 836        let overscroll = vec2f(em_width, 0.);
 837        let snapshot = self.update_view(cx.app, |view, cx| {
 838            let wrap_width = match view.soft_wrap_mode(cx) {
 839                SoftWrap::None => None,
 840                SoftWrap::EditorWidth => {
 841                    Some(text_width - gutter_margin - overscroll.x() - em_width)
 842                }
 843                SoftWrap::Column(column) => Some(column as f32 * em_advance),
 844            };
 845
 846            if view.set_wrap_width(wrap_width, cx) {
 847                view.snapshot(cx)
 848            } else {
 849                snapshot
 850            }
 851        });
 852
 853        let scroll_height = (snapshot.max_point().row() + 1) as f32 * line_height;
 854        if let EditorMode::AutoHeight { max_lines } = snapshot.mode {
 855            size.set_y(
 856                scroll_height
 857                    .min(constraint.max_along(Axis::Vertical))
 858                    .max(constraint.min_along(Axis::Vertical))
 859                    .min(line_height * max_lines as f32),
 860            )
 861        } else if size.y().is_infinite() {
 862            size.set_y(scroll_height);
 863        }
 864        let gutter_size = vec2f(gutter_width, size.y());
 865        let text_size = vec2f(text_width, size.y());
 866
 867        let (autoscroll_horizontally, mut snapshot) = self.update_view(cx.app, |view, cx| {
 868            let autoscroll_horizontally = view.autoscroll_vertically(size.y(), line_height, cx);
 869            let snapshot = view.snapshot(cx);
 870            (autoscroll_horizontally, snapshot)
 871        });
 872
 873        let scroll_position = snapshot.scroll_position();
 874        let start_row = scroll_position.y() as u32;
 875        let scroll_top = scroll_position.y() * line_height;
 876        let end_row = ((scroll_top + size.y()) / line_height).ceil() as u32 + 1; // Add 1 to ensure selections bleed off screen
 877
 878        let start_anchor = if start_row == 0 {
 879            Anchor::min()
 880        } else {
 881            snapshot
 882                .buffer_snapshot
 883                .anchor_before(DisplayPoint::new(start_row, 0).to_offset(&snapshot, Bias::Left))
 884        };
 885        let end_anchor = if end_row > snapshot.max_point().row() {
 886            Anchor::max()
 887        } else {
 888            snapshot
 889                .buffer_snapshot
 890                .anchor_before(DisplayPoint::new(end_row, 0).to_offset(&snapshot, Bias::Right))
 891        };
 892
 893        let mut selections = HashMap::default();
 894        let mut active_rows = BTreeMap::new();
 895        let mut highlighted_rows = None;
 896        let mut highlighted_ranges = Vec::new();
 897        self.update_view(cx.app, |view, cx| {
 898            let display_map = view.display_map.update(cx, |map, cx| map.snapshot(cx));
 899
 900            highlighted_rows = view.highlighted_rows();
 901            highlighted_ranges = view.highlighted_ranges_in_range(
 902                start_anchor.clone()..end_anchor.clone(),
 903                &display_map,
 904            );
 905
 906            let local_selections = view
 907                .local_selections_in_range(start_anchor.clone()..end_anchor.clone(), &display_map);
 908            for selection in &local_selections {
 909                let is_empty = selection.start == selection.end;
 910                let selection_start = snapshot.prev_line_boundary(selection.start).1;
 911                let selection_end = snapshot.next_line_boundary(selection.end).1;
 912                for row in cmp::max(selection_start.row(), start_row)
 913                    ..=cmp::min(selection_end.row(), end_row)
 914                {
 915                    let contains_non_empty_selection = active_rows.entry(row).or_insert(!is_empty);
 916                    *contains_non_empty_selection |= !is_empty;
 917                }
 918            }
 919            selections.insert(
 920                view.replica_id(cx),
 921                local_selections
 922                    .into_iter()
 923                    .map(|selection| crate::Selection {
 924                        id: selection.id,
 925                        goal: selection.goal,
 926                        reversed: selection.reversed,
 927                        start: selection.start.to_display_point(&display_map),
 928                        end: selection.end.to_display_point(&display_map),
 929                    })
 930                    .collect(),
 931            );
 932
 933            for (replica_id, selection) in display_map
 934                .buffer_snapshot
 935                .remote_selections_in_range(&(start_anchor..end_anchor))
 936            {
 937                selections
 938                    .entry(replica_id)
 939                    .or_insert(Vec::new())
 940                    .push(crate::Selection {
 941                        id: selection.id,
 942                        goal: selection.goal,
 943                        reversed: selection.reversed,
 944                        start: selection.start.to_display_point(&display_map),
 945                        end: selection.end.to_display_point(&display_map),
 946                    });
 947            }
 948        });
 949
 950        let line_number_layouts =
 951            self.layout_line_numbers(start_row..end_row, &active_rows, &snapshot, cx);
 952
 953        let mut max_visible_line_width = 0.0;
 954        let line_layouts = self.layout_lines(start_row..end_row, &mut snapshot, cx);
 955        for line in &line_layouts {
 956            if line.width() > max_visible_line_width {
 957                max_visible_line_width = line.width();
 958            }
 959        }
 960
 961        let style = self.style.clone();
 962        let longest_line_width = layout_line(
 963            snapshot.longest_row(),
 964            &snapshot,
 965            &style,
 966            cx.text_layout_cache,
 967        )
 968        .width();
 969        let scroll_width = longest_line_width.max(max_visible_line_width) + overscroll.x();
 970        let em_width = style.text.em_width(cx.font_cache);
 971        let max_row = snapshot.max_point().row();
 972        let scroll_max = vec2f(
 973            ((scroll_width - text_size.x()) / em_width).max(0.0),
 974            max_row.saturating_sub(1) as f32,
 975        );
 976
 977        let mut context_menu = None;
 978        let mut code_actions_indicator = None;
 979        self.update_view(cx.app, |view, cx| {
 980            let clamped = view.clamp_scroll_left(scroll_max.x());
 981            let autoscrolled;
 982            if autoscroll_horizontally {
 983                autoscrolled = view.autoscroll_horizontally(
 984                    start_row,
 985                    text_size.x(),
 986                    scroll_width,
 987                    em_width,
 988                    &line_layouts,
 989                    cx,
 990                );
 991            } else {
 992                autoscrolled = false;
 993            }
 994
 995            if clamped || autoscrolled {
 996                snapshot = view.snapshot(cx);
 997            }
 998
 999            let newest_selection_head = view
1000                .newest_selection_with_snapshot::<usize>(&snapshot.buffer_snapshot)
1001                .head()
1002                .to_display_point(&snapshot);
1003
1004            if (start_row..end_row).contains(&newest_selection_head.row()) {
1005                let style = view.style(cx);
1006                if view.context_menu_visible() {
1007                    context_menu =
1008                        view.render_context_menu(newest_selection_head, style.clone(), cx);
1009                }
1010
1011                code_actions_indicator = view
1012                    .render_code_actions_indicator(&style, cx)
1013                    .map(|indicator| (newest_selection_head.row(), indicator));
1014            }
1015        });
1016
1017        if let Some((_, context_menu)) = context_menu.as_mut() {
1018            context_menu.layout(
1019                SizeConstraint {
1020                    min: Vector2F::zero(),
1021                    max: vec2f(
1022                        f32::INFINITY,
1023                        (12. * line_height).min((size.y() - line_height) / 2.),
1024                    ),
1025                },
1026                cx,
1027            );
1028        }
1029
1030        if let Some((_, indicator)) = code_actions_indicator.as_mut() {
1031            indicator.layout(
1032                SizeConstraint::strict_along(Axis::Vertical, line_height * 0.618),
1033                cx,
1034            );
1035        }
1036
1037        let blocks = self.layout_blocks(
1038            start_row..end_row,
1039            &snapshot,
1040            size.x().max(scroll_width + gutter_width),
1041            gutter_padding,
1042            gutter_width,
1043            em_width,
1044            gutter_width + gutter_margin,
1045            line_height,
1046            &style,
1047            &line_layouts,
1048            cx,
1049        );
1050
1051        (
1052            size,
1053            LayoutState {
1054                size,
1055                scroll_max,
1056                gutter_size,
1057                gutter_padding,
1058                text_size,
1059                gutter_margin,
1060                snapshot,
1061                active_rows,
1062                highlighted_rows,
1063                highlighted_ranges,
1064                line_layouts,
1065                line_number_layouts,
1066                blocks,
1067                line_height,
1068                em_width,
1069                em_advance,
1070                selections,
1071                context_menu,
1072                code_actions_indicator,
1073            },
1074        )
1075    }
1076
1077    fn paint(
1078        &mut self,
1079        bounds: RectF,
1080        visible_bounds: RectF,
1081        layout: &mut Self::LayoutState,
1082        cx: &mut PaintContext,
1083    ) -> Self::PaintState {
1084        cx.scene.push_layer(Some(bounds));
1085
1086        let gutter_bounds = RectF::new(bounds.origin(), layout.gutter_size);
1087        let text_bounds = RectF::new(
1088            bounds.origin() + vec2f(layout.gutter_size.x(), 0.0),
1089            layout.text_size,
1090        );
1091
1092        self.paint_background(gutter_bounds, text_bounds, layout, cx);
1093        if layout.gutter_size.x() > 0. {
1094            self.paint_gutter(gutter_bounds, visible_bounds, layout, cx);
1095        }
1096        self.paint_text(text_bounds, visible_bounds, layout, cx);
1097
1098        if !layout.blocks.is_empty() {
1099            cx.scene.push_layer(Some(bounds));
1100            self.paint_blocks(bounds, visible_bounds, layout, cx);
1101            cx.scene.pop_layer();
1102        }
1103
1104        cx.scene.pop_layer();
1105
1106        PaintState {
1107            bounds,
1108            gutter_bounds,
1109            text_bounds,
1110        }
1111    }
1112
1113    fn dispatch_event(
1114        &mut self,
1115        event: &Event,
1116        _: RectF,
1117        layout: &mut LayoutState,
1118        paint: &mut PaintState,
1119        cx: &mut EventContext,
1120    ) -> bool {
1121        if let Some((_, context_menu)) = &mut layout.context_menu {
1122            if context_menu.dispatch_event(event, cx) {
1123                return true;
1124            }
1125        }
1126
1127        if let Some((_, indicator)) = &mut layout.code_actions_indicator {
1128            if indicator.dispatch_event(event, cx) {
1129                return true;
1130            }
1131        }
1132
1133        for (_, block) in &mut layout.blocks {
1134            if block.dispatch_event(event, cx) {
1135                return true;
1136            }
1137        }
1138
1139        match event {
1140            Event::LeftMouseDown {
1141                position,
1142                alt,
1143                shift,
1144                click_count,
1145                ..
1146            } => self.mouse_down(*position, *alt, *shift, *click_count, layout, paint, cx),
1147            Event::LeftMouseUp { position } => self.mouse_up(*position, cx),
1148            Event::LeftMouseDragged { position } => {
1149                self.mouse_dragged(*position, layout, paint, cx)
1150            }
1151            Event::ScrollWheel {
1152                position,
1153                delta,
1154                precise,
1155            } => self.scroll(*position, *delta, *precise, layout, paint, cx),
1156            Event::KeyDown { input, .. } => self.key_down(input.as_deref(), cx),
1157            _ => false,
1158        }
1159    }
1160
1161    fn debug(
1162        &self,
1163        bounds: RectF,
1164        _: &Self::LayoutState,
1165        _: &Self::PaintState,
1166        _: &gpui::DebugContext,
1167    ) -> json::Value {
1168        json!({
1169            "type": "BufferElement",
1170            "bounds": bounds.to_json()
1171        })
1172    }
1173}
1174
1175pub struct LayoutState {
1176    size: Vector2F,
1177    scroll_max: Vector2F,
1178    gutter_size: Vector2F,
1179    gutter_padding: f32,
1180    gutter_margin: f32,
1181    text_size: Vector2F,
1182    snapshot: EditorSnapshot,
1183    active_rows: BTreeMap<u32, bool>,
1184    highlighted_rows: Option<Range<u32>>,
1185    line_layouts: Vec<text_layout::Line>,
1186    line_number_layouts: Vec<Option<text_layout::Line>>,
1187    blocks: Vec<(u32, ElementBox)>,
1188    line_height: f32,
1189    em_width: f32,
1190    em_advance: f32,
1191    highlighted_ranges: Vec<(Range<DisplayPoint>, Color)>,
1192    selections: HashMap<ReplicaId, Vec<text::Selection<DisplayPoint>>>,
1193    context_menu: Option<(DisplayPoint, ElementBox)>,
1194    code_actions_indicator: Option<(u32, ElementBox)>,
1195}
1196
1197fn layout_line(
1198    row: u32,
1199    snapshot: &EditorSnapshot,
1200    style: &EditorStyle,
1201    layout_cache: &TextLayoutCache,
1202) -> text_layout::Line {
1203    let mut line = snapshot.line(row);
1204
1205    if line.len() > MAX_LINE_LEN {
1206        let mut len = MAX_LINE_LEN;
1207        while !line.is_char_boundary(len) {
1208            len -= 1;
1209        }
1210
1211        line.truncate(len);
1212    }
1213
1214    layout_cache.layout_str(
1215        &line,
1216        style.text.font_size,
1217        &[(
1218            snapshot.line_len(row) as usize,
1219            RunStyle {
1220                font_id: style.text.font_id,
1221                color: Color::black(),
1222                underline: None,
1223            },
1224        )],
1225    )
1226}
1227
1228pub struct PaintState {
1229    bounds: RectF,
1230    gutter_bounds: RectF,
1231    text_bounds: RectF,
1232}
1233
1234impl PaintState {
1235    fn point_for_position(
1236        &self,
1237        snapshot: &EditorSnapshot,
1238        layout: &LayoutState,
1239        position: Vector2F,
1240    ) -> (DisplayPoint, u32) {
1241        let scroll_position = snapshot.scroll_position();
1242        let position = position - self.text_bounds.origin();
1243        let y = position.y().max(0.0).min(layout.size.y());
1244        let row = ((y / layout.line_height) + scroll_position.y()) as u32;
1245        let row = cmp::min(row, snapshot.max_point().row());
1246        let line = &layout.line_layouts[(row - scroll_position.y() as u32) as usize];
1247        let x = position.x() + (scroll_position.x() * layout.em_width);
1248
1249        let column = if x >= 0.0 {
1250            line.index_for_x(x)
1251                .map(|ix| ix as u32)
1252                .unwrap_or_else(|| snapshot.line_len(row))
1253        } else {
1254            0
1255        };
1256        let overshoot = (0f32.max(x - line.width()) / layout.em_advance) as u32;
1257
1258        (DisplayPoint::new(row, column), overshoot)
1259    }
1260}
1261
1262#[derive(Copy, Clone)]
1263pub enum CursorShape {
1264    Bar,
1265    Block,
1266    Underscore,
1267}
1268
1269impl Default for CursorShape {
1270    fn default() -> Self {
1271        CursorShape::Block
1272    }
1273}
1274
1275struct Cursor {
1276    origin: Vector2F,
1277    block_width: f32,
1278    line_height: f32,
1279    color: Color,
1280    shape: CursorShape,
1281    block_text: Option<Line>,
1282}
1283
1284impl Cursor {
1285    fn paint(&self, cx: &mut PaintContext) {
1286        let bounds = match self.shape {
1287            CursorShape::Bar => RectF::new(self.origin, vec2f(2.0, self.line_height)),
1288            CursorShape::Block => {
1289                RectF::new(self.origin, vec2f(self.block_width, self.line_height))
1290            }
1291            CursorShape::Underscore => RectF::new(
1292                self.origin + Vector2F::new(0.0, self.line_height - 2.0),
1293                vec2f(self.block_width, 2.0),
1294            ),
1295        };
1296
1297        cx.scene.push_quad(Quad {
1298            bounds,
1299            background: Some(self.color),
1300            border: Border::new(0., Color::black()),
1301            corner_radius: 0.,
1302        });
1303
1304        if let Some(block_text) = &self.block_text {
1305            block_text.paint(self.origin, bounds, self.line_height, cx);
1306        }
1307    }
1308}
1309
1310#[derive(Debug)]
1311struct HighlightedRange {
1312    start_y: f32,
1313    line_height: f32,
1314    lines: Vec<HighlightedRangeLine>,
1315    color: Color,
1316    corner_radius: f32,
1317}
1318
1319#[derive(Debug)]
1320struct HighlightedRangeLine {
1321    start_x: f32,
1322    end_x: f32,
1323}
1324
1325impl HighlightedRange {
1326    fn paint(&self, bounds: RectF, scene: &mut Scene) {
1327        if self.lines.len() >= 2 && self.lines[0].start_x > self.lines[1].end_x {
1328            self.paint_lines(self.start_y, &self.lines[0..1], bounds, scene);
1329            self.paint_lines(
1330                self.start_y + self.line_height,
1331                &self.lines[1..],
1332                bounds,
1333                scene,
1334            );
1335        } else {
1336            self.paint_lines(self.start_y, &self.lines, bounds, scene);
1337        }
1338    }
1339
1340    fn paint_lines(
1341        &self,
1342        start_y: f32,
1343        lines: &[HighlightedRangeLine],
1344        bounds: RectF,
1345        scene: &mut Scene,
1346    ) {
1347        if lines.is_empty() {
1348            return;
1349        }
1350
1351        let mut path = PathBuilder::new();
1352        let first_line = lines.first().unwrap();
1353        let last_line = lines.last().unwrap();
1354
1355        let first_top_left = vec2f(first_line.start_x, start_y);
1356        let first_top_right = vec2f(first_line.end_x, start_y);
1357
1358        let curve_height = vec2f(0., self.corner_radius);
1359        let curve_width = |start_x: f32, end_x: f32| {
1360            let max = (end_x - start_x) / 2.;
1361            let width = if max < self.corner_radius {
1362                max
1363            } else {
1364                self.corner_radius
1365            };
1366
1367            vec2f(width, 0.)
1368        };
1369
1370        let top_curve_width = curve_width(first_line.start_x, first_line.end_x);
1371        path.reset(first_top_right - top_curve_width);
1372        path.curve_to(first_top_right + curve_height, first_top_right);
1373
1374        let mut iter = lines.iter().enumerate().peekable();
1375        while let Some((ix, line)) = iter.next() {
1376            let bottom_right = vec2f(line.end_x, start_y + (ix + 1) as f32 * self.line_height);
1377
1378            if let Some((_, next_line)) = iter.peek() {
1379                let next_top_right = vec2f(next_line.end_x, bottom_right.y());
1380
1381                match next_top_right.x().partial_cmp(&bottom_right.x()).unwrap() {
1382                    Ordering::Equal => {
1383                        path.line_to(bottom_right);
1384                    }
1385                    Ordering::Less => {
1386                        let curve_width = curve_width(next_top_right.x(), bottom_right.x());
1387                        path.line_to(bottom_right - curve_height);
1388                        if self.corner_radius > 0. {
1389                            path.curve_to(bottom_right - curve_width, bottom_right);
1390                        }
1391                        path.line_to(next_top_right + curve_width);
1392                        if self.corner_radius > 0. {
1393                            path.curve_to(next_top_right + curve_height, next_top_right);
1394                        }
1395                    }
1396                    Ordering::Greater => {
1397                        let curve_width = curve_width(bottom_right.x(), next_top_right.x());
1398                        path.line_to(bottom_right - curve_height);
1399                        if self.corner_radius > 0. {
1400                            path.curve_to(bottom_right + curve_width, bottom_right);
1401                        }
1402                        path.line_to(next_top_right - curve_width);
1403                        if self.corner_radius > 0. {
1404                            path.curve_to(next_top_right + curve_height, next_top_right);
1405                        }
1406                    }
1407                }
1408            } else {
1409                let curve_width = curve_width(line.start_x, line.end_x);
1410                path.line_to(bottom_right - curve_height);
1411                if self.corner_radius > 0. {
1412                    path.curve_to(bottom_right - curve_width, bottom_right);
1413                }
1414
1415                let bottom_left = vec2f(line.start_x, bottom_right.y());
1416                path.line_to(bottom_left + curve_width);
1417                if self.corner_radius > 0. {
1418                    path.curve_to(bottom_left - curve_height, bottom_left);
1419                }
1420            }
1421        }
1422
1423        if first_line.start_x > last_line.start_x {
1424            let curve_width = curve_width(last_line.start_x, first_line.start_x);
1425            let second_top_left = vec2f(last_line.start_x, start_y + self.line_height);
1426            path.line_to(second_top_left + curve_height);
1427            if self.corner_radius > 0. {
1428                path.curve_to(second_top_left + curve_width, second_top_left);
1429            }
1430            let first_bottom_left = vec2f(first_line.start_x, second_top_left.y());
1431            path.line_to(first_bottom_left - curve_width);
1432            if self.corner_radius > 0. {
1433                path.curve_to(first_bottom_left - curve_height, first_bottom_left);
1434            }
1435        }
1436
1437        path.line_to(first_top_left + curve_height);
1438        if self.corner_radius > 0. {
1439            path.curve_to(first_top_left + top_curve_width, first_top_left);
1440        }
1441        path.line_to(first_top_right - top_curve_width);
1442
1443        scene.push_path(path.build(self.color, Some(bounds)));
1444    }
1445}
1446
1447fn scale_vertical_mouse_autoscroll_delta(delta: f32) -> f32 {
1448    delta.powf(1.5) / 100.0
1449}
1450
1451fn scale_horizontal_mouse_autoscroll_delta(delta: f32) -> f32 {
1452    delta.powf(1.2) / 300.0
1453}
1454
1455#[cfg(test)]
1456mod tests {
1457    use super::*;
1458    use crate::{Editor, MultiBuffer};
1459    use postage::watch;
1460    use util::test::sample_text;
1461    use workspace::Settings;
1462
1463    #[gpui::test]
1464    fn test_layout_line_numbers(cx: &mut gpui::MutableAppContext) {
1465        let settings = watch::channel_with(Settings::test(cx));
1466        let buffer = MultiBuffer::build_simple(&sample_text(6, 6, 'a'), cx);
1467        let (window_id, editor) = cx.add_window(Default::default(), |cx| {
1468            Editor::new(EditorMode::Full, buffer, None, settings.1, None, cx)
1469        });
1470        let element = EditorElement::new(editor.downgrade(), editor.read(cx).style(cx), CursorShape::Bar);
1471
1472        let layouts = editor.update(cx, |editor, cx| {
1473            let snapshot = editor.snapshot(cx);
1474            let mut presenter = cx.build_presenter(window_id, 30.);
1475            let mut layout_cx = presenter.build_layout_context(false, cx);
1476            element.layout_line_numbers(0..6, &Default::default(), &snapshot, &mut layout_cx)
1477        });
1478        assert_eq!(layouts.len(), 6);
1479    }
1480}