This commit is contained in:
Antonio Scandurra 2023-11-10 17:16:33 +01:00
parent 58f9ef99f7
commit fc5ec47cc8
3 changed files with 218 additions and 247 deletions

View file

@ -4,7 +4,7 @@ use super::{
}; };
use crate::{Anchor, Editor, ExcerptId, ExcerptRange, ToPoint as _}; use crate::{Anchor, Editor, ExcerptId, ExcerptRange, ToPoint as _};
use collections::{Bound, HashMap, HashSet}; use collections::{Bound, HashMap, HashSet};
use gpui::{AnyElement, ViewContext}; use gpui::{AnyElement, Pixels, ViewContext};
use language::{BufferSnapshot, Chunk, Patch, Point}; use language::{BufferSnapshot, Chunk, Patch, Point};
use parking_lot::Mutex; use parking_lot::Mutex;
use std::{ use std::{
@ -82,12 +82,11 @@ pub enum BlockStyle {
pub struct BlockContext<'a, 'b> { pub struct BlockContext<'a, 'b> {
pub view_context: &'b mut ViewContext<'a, Editor>, pub view_context: &'b mut ViewContext<'a, Editor>,
pub anchor_x: f32, pub anchor_x: Pixels,
pub scroll_x: f32, pub gutter_width: Pixels,
pub gutter_width: f32, pub gutter_padding: Pixels,
pub gutter_padding: f32, pub em_width: Pixels,
pub em_width: f32, pub line_height: Pixels,
pub line_height: f32,
pub block_id: usize, pub block_id: usize,
} }

View file

@ -1,5 +1,8 @@
use crate::{ use crate::{
display_map::{BlockStyle, DisplaySnapshot, FoldStatus, HighlightedChunk, ToDisplayPoint}, display_map::{
BlockContext, BlockStyle, DisplaySnapshot, FoldStatus, HighlightedChunk, ToDisplayPoint,
TransformBlock,
},
editor_settings::ShowScrollbar, editor_settings::ShowScrollbar,
git::{diff_hunk_to_display, DisplayDiffHunk}, git::{diff_hunk_to_display, DisplayDiffHunk},
hover_popover::hover_at, hover_popover::hover_at,
@ -15,17 +18,18 @@ use crate::{
use anyhow::Result; use anyhow::Result;
use collections::{BTreeMap, HashMap}; use collections::{BTreeMap, HashMap};
use gpui::{ use gpui::{
black, hsla, point, px, relative, size, transparent_black, Action, AnyElement, AvailableSpace, point, px, relative, size, transparent_black, Action, AnyElement, AvailableSpace, BorrowWindow,
BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners, DispatchPhase, Edges, Element, Bounds, ContentMask, Corners, DispatchPhase, Edges, Element, ElementId, ElementInputHandler,
ElementId, ElementInputHandler, Entity, FocusHandle, GlobalElementId, Hsla, InputHandler, Entity, Hsla, Line, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement,
KeyContext, KeyDownEvent, KeyMatch, Line, LineLayout, Modifiers, MouseButton, MouseDownEvent, Pixels, ScrollWheelEvent, Size, Style, TextRun, TextStyle, ViewContext, WindowContext,
MouseMoveEvent, MouseUpEvent, Pixels, ScrollWheelEvent, ShapedGlyph, Size, Style, TextRun,
TextStyle, TextSystem, ViewContext, WindowContext, WrappedLineLayout,
}; };
use itertools::Itertools; use itertools::Itertools;
use language::language_settings::ShowWhitespaceSetting; use language::language_settings::ShowWhitespaceSetting;
use multi_buffer::Anchor; use multi_buffer::Anchor;
use project::project_settings::{GitGutterSetting, ProjectSettings}; use project::{
project_settings::{GitGutterSetting, ProjectSettings},
ProjectPath,
};
use settings::Settings; use settings::Settings;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{ use std::{
@ -39,6 +43,7 @@ use std::{
}; };
use sum_tree::Bias; use sum_tree::Bias;
use theme::{ActiveTheme, PlayerColor}; use theme::{ActiveTheme, PlayerColor};
use ui::{h_stack, IconButton};
use util::ResultExt; use util::ResultExt;
use workspace::item::Item; use workspace::item::Item;
@ -1741,22 +1746,22 @@ impl EditorElement {
.unwrap() .unwrap()
.width; .width;
let scroll_width = longest_line_width.max(max_visible_line_width) + overscroll.width; let scroll_width = longest_line_width.max(max_visible_line_width) + overscroll.width;
// todo!("blocks")
// let (scroll_width, blocks) = self.layout_blocks( let (scroll_width, blocks) = self.layout_blocks(
// start_row..end_row, start_row..end_row,
// &snapshot, &snapshot,
// size.x, bounds.size.width,
// scroll_width, scroll_width,
// gutter_padding, gutter_padding,
// gutter_width, gutter_width,
// em_width, em_width,
// gutter_width + gutter_margin, gutter_width + gutter_margin,
// line_height, line_height,
// &style, &style,
// &line_layouts, &line_layouts,
// editor, editor,
// cx, cx,
// ); );
let scroll_max = point( let scroll_max = point(
f32::from((scroll_width - text_size.width) / em_width).max(0.0), f32::from((scroll_width - text_size.width) / em_width).max(0.0),
@ -1948,226 +1953,181 @@ impl EditorElement {
} }
} }
// #[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
// fn layout_blocks( fn layout_blocks(
// &mut self, &mut self,
// rows: Range<u32>, rows: Range<u32>,
// snapshot: &EditorSnapshot, snapshot: &EditorSnapshot,
// editor_width: f32, editor_width: Pixels,
// scroll_width: f32, scroll_width: Pixels,
// gutter_padding: f32, gutter_padding: Pixels,
// gutter_width: f32, gutter_width: Pixels,
// em_width: f32, em_width: Pixels,
// text_x: f32, text_x: Pixels,
// line_height: f32, line_height: Pixels,
// style: &EditorStyle, style: &EditorStyle,
// line_layouts: &[LineWithInvisibles], line_layouts: &[LineWithInvisibles],
// editor: &mut Editor, editor: &mut Editor,
// cx: &mut ViewContext<Editor>, cx: &mut ViewContext<Editor>,
// ) -> (f32, Vec<BlockLayout>) { ) -> (Pixels, Vec<BlockLayout>) {
// let mut block_id = 0; let mut block_id = 0;
// let scroll_x = snapshot.scroll_anchor.offset.x; let scroll_x = snapshot.scroll_anchor.offset.x;
// let (fixed_blocks, non_fixed_blocks) = snapshot let (fixed_blocks, non_fixed_blocks) = snapshot
// .blocks_in_range(rows.clone()) .blocks_in_range(rows.clone())
// .partition::<Vec<_>, _>(|(_, block)| match block { .partition::<Vec<_>, _>(|(_, block)| match block {
// TransformBlock::ExcerptHeader { .. } => false, TransformBlock::ExcerptHeader { .. } => false,
// TransformBlock::Custom(block) => block.style() == BlockStyle::Fixed, TransformBlock::Custom(block) => block.style() == BlockStyle::Fixed,
// }); });
// let mut render_block = |block: &TransformBlock, width: f32, block_id: usize| { let mut render_block = |block: &TransformBlock, width: Pixels, block_id: usize| {
// let mut element = match block { let mut element = match block {
// TransformBlock::Custom(block) => { TransformBlock::Custom(block) => {
// let align_to = block let align_to = block
// .position() .position()
// .to_point(&snapshot.buffer_snapshot) .to_point(&snapshot.buffer_snapshot)
// .to_display_point(snapshot); .to_display_point(snapshot);
// let anchor_x = text_x let anchor_x = text_x
// + if rows.contains(&align_to.row()) { + if rows.contains(&align_to.row()) {
// line_layouts[(align_to.row() - rows.start) as usize] line_layouts[(align_to.row() - rows.start) as usize]
// .line .line
// .x_for_index(align_to.column() as usize) .x_for_index(align_to.column() as usize)
// } else { } else {
// layout_line(align_to.row(), snapshot, style, cx.text_layout_cache()) layout_line(align_to.row(), snapshot, style, cx)
// .x_for_index(align_to.column() as usize) .unwrap()
// }; .x_for_index(align_to.column() as usize)
};
// block.render(&mut BlockContext { block.render(&mut BlockContext {
// view_context: cx, view_context: cx,
// anchor_x, anchor_x,
// gutter_padding, gutter_padding,
// line_height, line_height,
// scroll_x, // scroll_x,
// gutter_width, gutter_width,
// em_width, em_width,
// block_id, block_id,
// }) })
// } }
// TransformBlock::ExcerptHeader { TransformBlock::ExcerptHeader {
// id, id,
// buffer, buffer,
// range, range,
// starts_new_buffer, starts_new_buffer,
// .. ..
// } => { } => {
// let tooltip_style = theme::current(cx).tooltip.clone(); let include_root = editor
// let include_root = editor .project
// .project .as_ref()
// .as_ref() .map(|project| project.read(cx).visible_worktrees(cx).count() > 1)
// .map(|project| project.read(cx).visible_worktrees(cx).count() > 1) .unwrap_or_default();
// .unwrap_or_default(); let jump_icon = project::File::from_dyn(buffer.file()).map(|file| {
// let jump_icon = project::File::from_dyn(buffer.file()).map(|file| { let jump_path = ProjectPath {
// let jump_path = ProjectPath { worktree_id: file.worktree_id(cx),
// worktree_id: file.worktree_id(cx), path: file.path.clone(),
// path: file.path.clone(), };
// }; let jump_anchor = range
// let jump_anchor = range .primary
// .primary .as_ref()
// .as_ref() .map_or(range.context.start, |primary| primary.start);
// .map_or(range.context.start, |primary| primary.start); let jump_position = language::ToPoint::to_point(&jump_anchor, buffer);
// let jump_position = language::ToPoint::to_point(&jump_anchor, buffer);
// enum JumpIcon {} // todo!("avoid ElementId collision risk here")
// MouseEventHandler::new::<JumpIcon, _>((*id).into(), cx, |state, _| { IconButton::new(usize::from(*id), ui::Icon::ArrowUpRight)
// let style = style.jump_icon.style_for(state); .on_click(move |editor, cx| {
// Svg::new("icons/arrow_up_right.svg") if let Some(workspace) = editor
// .with_color(style.color) .workspace
// .constrained() .as_ref()
// .with_width(style.icon_width) .and_then(|(workspace, _)| workspace.upgrade(cx))
// .aligned() {
// .contained() workspace.update(cx, |workspace, cx| {
// .with_style(style.container) Editor::jump(
// .constrained() workspace,
// .with_width(style.button_width) jump_path.clone(),
// .with_height(style.button_width) jump_position,
// }) jump_anchor,
// .with_cursor_style(CursorStyle::PointingHand) cx,
// .on_click(MouseButton::Left, move |_, editor, cx| { );
// if let Some(workspace) = editor });
// .workspace }
// .as_ref() })
// .and_then(|(workspace, _)| workspace.upgrade(cx)) .tooltip("Jump to Buffer") // todo!(pass an action as well to show key binding)
// { });
// workspace.update(cx, |workspace, cx| {
// Editor::jump(
// workspace,
// jump_path.clone(),
// jump_position,
// jump_anchor,
// cx,
// );
// });
// }
// })
// .with_tooltip::<JumpIcon>(
// (*id).into(),
// "Jump to Buffer".to_string(),
// Some(Box::new(crate::OpenExcerpts)),
// tooltip_style.clone(),
// cx,
// )
// .aligned()
// .flex_float()
// });
// if *starts_new_buffer { if *starts_new_buffer {
// let editor_font_size = style.text.font_size; let path = buffer.resolve_file_path(cx, include_root);
// let style = &style.diagnostic_path_header; let mut filename = None;
// let font_size = (style.text_scale_factor * editor_font_size).round(); let mut parent_path = None;
// Can't use .and_then() because `.file_name()` and `.parent()` return references :(
if let Some(path) = path {
filename = path.file_name().map(|f| f.to_string_lossy().to_string());
parent_path =
path.parent().map(|p| p.to_string_lossy().to_string() + "/");
}
// let path = buffer.resolve_file_path(cx, include_root); h_stack()
// let mut filename = None; .child(filename.unwrap_or_else(|| "untitled".to_string()))
// let mut parent_path = None; .children(parent_path)
// // Can't use .and_then() because `.file_name()` and `.parent()` return references :( .children(jump_icon)
// if let Some(path) = path { .p_x(gutter_padding)
// filename = path.file_name().map(|f| f.to_string_lossy.to_string()); } else {
// parent_path = let text_style = style.text.clone();
// path.parent().map(|p| p.to_string_lossy.to_string() + "/"); h_stack()
// } .child("")
.children(jump_icon)
.p_x(gutter_padding)
.expanded()
.into_any_named("collapsed context")
}
}
};
// Flex::row() // element.layout(
// .with_child( // SizeConstraint {
// Label::new( // min: gpui::Point::<Pixels>::zero(),
// filename.unwrap_or_else(|| "untitled".to_string()), // max: point(width, block.height() as f32 * line_height),
// style.filename.text.clone().with_font_size(font_size), // },
// ) // editor,
// .contained() // cx,
// .with_style(style.filename.container) // );
// .aligned(), element
// ) };
// .with_children(parent_path.map(|path| {
// Label::new(path, style.path.text.clone().with_font_size(font_size))
// .contained()
// .with_style(style.path.container)
// .aligned()
// }))
// .with_children(jump_icon)
// .contained()
// .with_style(style.container)
// .with_padding_left(gutter_padding)
// .with_padding_right(gutter_padding)
// .expanded()
// .into_any_named("path header block")
// } else {
// let text_style = style.text.clone();
// Flex::row()
// .with_child(Label::new("⋯", text_style))
// .with_children(jump_icon)
// .contained()
// .with_padding_left(gutter_padding)
// .with_padding_right(gutter_padding)
// .expanded()
// .into_any_named("collapsed context")
// }
// }
// };
// element.layout( let mut fixed_block_max_width = Pixels::ZERO;
// SizeConstraint { let mut blocks = Vec::new();
// min: gpui::Point::<Pixels>::zero(), for (row, block) in fixed_blocks {
// max: point(width, block.height() as f32 * line_height), let element = render_block(block, f32::INFINITY, block_id);
// }, block_id += 1;
// editor, fixed_block_max_width = fixed_block_max_width.max(element.size().x + em_width);
// cx, blocks.push(BlockLayout {
// ); row,
// element element,
// }; style: BlockStyle::Fixed,
});
// let mut fixed_block_max_width = 0f32; }
// let mut blocks = Vec::new(); for (row, block) in non_fixed_blocks {
// for (row, block) in fixed_blocks { let style = match block {
// let element = render_block(block, f32::INFINITY, block_id); TransformBlock::Custom(block) => block.style(),
// block_id += 1; TransformBlock::ExcerptHeader { .. } => BlockStyle::Sticky,
// fixed_block_max_width = fixed_block_max_width.max(element.size().x + em_width); };
// blocks.push(BlockLayout { let width = match style {
// row, BlockStyle::Sticky => editor_width,
// element, BlockStyle::Flex => editor_width
// style: BlockStyle::Fixed, .max(fixed_block_max_width)
// }); .max(gutter_width + scroll_width),
// } BlockStyle::Fixed => unreachable!(),
// for (row, block) in non_fixed_blocks { };
// let style = match block { let element = render_block(block, width, block_id);
// TransformBlock::Custom(block) => block.style(), block_id += 1;
// TransformBlock::ExcerptHeader { .. } => BlockStyle::Sticky, blocks.push(BlockLayout {
// }; row,
// let width = match style { element,
// BlockStyle::Sticky => editor_width, style,
// BlockStyle::Flex => editor_width });
// .max(fixed_block_max_width) }
// .max(gutter_width + scroll_width), (
// BlockStyle::Fixed => unreachable!(), scroll_width.max(fixed_block_max_width - gutter_width),
// }; blocks,
// let element = render_block(block, width, block_id); )
// block_id += 1; }
// blocks.push(BlockLayout {
// row,
// element,
// style,
// });
// }
// (
// scroll_width.max(fixed_block_max_width - gutter_width),
// blocks,
// )
// }
fn paint_mouse_listeners( fn paint_mouse_listeners(
&mut self, &mut self,

View file

@ -1,8 +1,8 @@
use std::sync::Arc; use std::sync::Arc;
use gpui::{rems, MouseButton}; use gpui::{rems, MouseButton, VisualContext};
use crate::{h_stack, prelude::*}; use crate::{h_stack, prelude::*, TextTooltip};
use crate::{ClickHandler, Icon, IconColor, IconElement}; use crate::{ClickHandler, Icon, IconColor, IconElement};
struct IconButtonHandlers<V: 'static> { struct IconButtonHandlers<V: 'static> {
@ -22,6 +22,7 @@ pub struct IconButton<V: 'static> {
color: IconColor, color: IconColor,
variant: ButtonVariant, variant: ButtonVariant,
state: InteractionState, state: InteractionState,
tooltip: Option<SharedString>,
handlers: IconButtonHandlers<V>, handlers: IconButtonHandlers<V>,
} }
@ -33,6 +34,7 @@ impl<V: 'static> IconButton<V> {
color: IconColor::default(), color: IconColor::default(),
variant: ButtonVariant::default(), variant: ButtonVariant::default(),
state: InteractionState::default(), state: InteractionState::default(),
tooltip: None,
handlers: IconButtonHandlers::default(), handlers: IconButtonHandlers::default(),
} }
} }
@ -57,6 +59,11 @@ impl<V: 'static> IconButton<V> {
self self
} }
pub fn tooltip(mut self, tooltip: impl Into<SharedString>) -> Self {
self.tooltip = Some(tooltip.into());
self
}
pub fn on_click( pub fn on_click(
mut self, mut self,
handler: impl 'static + Fn(&mut V, &mut ViewContext<V>) + Send + Sync, handler: impl 'static + Fn(&mut V, &mut ViewContext<V>) + Send + Sync,
@ -103,6 +110,11 @@ impl<V: 'static> IconButton<V> {
}); });
} }
if let Some(tooltip) = self.tooltip.clone() {
button =
button.tooltip(move |_, cx| cx.build_view(|cx| TextTooltip::new(tooltip.clone())));
}
button button
} }
} }