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