Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 104 additions & 5 deletions crates/base/src/input/base/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3141,6 +3141,7 @@ pub(super) struct PrepaintState {
cursor_scroll_offset: Point<Pixels>,
/// row index (zero based), no wrap, same line as the cursor.
current_row: Option<usize>,
active_row: Option<usize>,
selection_paths: Vec<Path<Pixels>>,
hover_highlight_path: Option<Path<Pixels>>,
search_match_paths: Vec<(Path<Pixels>, bool)>,
Expand Down Expand Up @@ -3813,6 +3814,13 @@ impl<M: InputModeKind, R: EditorLineNumberRenderer> Element for TextElement<M, R
cx,
);
cursor_scroll_offset.y = px(final_scroll_y_f64 as f32);
let active_row = {
let state = self.state.read(cx);
current_row.filter(|_| {
!state.mode.is_active_line_only_when_focused()
|| state.has_selection_focus(window, cx)
})
};
// Completion/code-action menus position at the active caret.
last_layout.cursor_bounds = cursor_infos
.iter()
Expand Down Expand Up @@ -3868,7 +3876,7 @@ impl<M: InputModeKind, R: EditorLineNumberRenderer> Element for TextElement<M, R
.iter()
.zip(last_layout.visible_buffer_lines.iter())
{
let is_active = current_row == Some(buffer_line);
let is_active = active_row == Some(buffer_line);
let number_style = if is_active {
&current_line_style
} else {
Expand Down Expand Up @@ -4026,6 +4034,7 @@ impl<M: InputModeKind, R: EditorLineNumberRenderer> Element for TextElement<M, R
cursor_infos,
cursor_scroll_offset,
current_row,
active_row,
selection_paths,
search_match_paths,
hover_highlight_path,
Expand Down Expand Up @@ -4112,7 +4121,7 @@ impl<M: InputModeKind, R: EditorLineNumberRenderer> Element for TextElement<M, R
.iter()
.zip(prepaint.last_layout.visible_buffer_lines.iter())
{
let is_active = prepaint.current_row == Some(buffer_line);
let is_active = prepaint.active_row == Some(buffer_line);
let p = point(
input_bounds.origin.x,
origin.y + prepaint.last_layout.local_row_y(row_offset),
Expand Down Expand Up @@ -4334,7 +4343,7 @@ impl<M: InputModeKind, R: EditorLineNumberRenderer> Element for TextElement<M, R
input_bounds.origin.x,
origin.y + prepaint.last_layout.local_row_y(row_offset) + ghost_offset,
);
let is_active = prepaint.current_row == Some(buffer_line);
let is_active = prepaint.active_row == Some(buffer_line);

let height = line_height * *rows as f32;
// paint active line number background
Expand Down Expand Up @@ -4378,7 +4387,7 @@ impl<M: InputModeKind, R: EditorLineNumberRenderer> Element for TextElement<M, R
// Paint fold icons (only visible on hover or for current line)
self.paint_fold_icons(
&mut prepaint.fold_icon_layout,
prepaint.current_row,
prepaint.active_row,
window,
cx,
);
Expand Down Expand Up @@ -4694,7 +4703,7 @@ mod tests {
use crate::input::{
Editor, EditorMode, EditorState, FoldPlaceholderRenderer, FoldRange, GutterDecoration,
GutterDecorationCollection, OverviewRulerDecoration, OverviewRulerDecorationCollection,
RangeDecoration, Redo, Undo,
Position, RangeDecoration, Redo, Undo,
};
use gpui::{
AppContext as _, Context, EntityInputHandler as _, ListAlignment, ListOffset, ListState,
Expand Down Expand Up @@ -5463,6 +5472,96 @@ mod tests {
});
}

#[derive(Clone, Default)]
struct ActiveLineNumbers(Rc<std::cell::RefCell<Vec<usize>>>);

impl EditorLineNumberRenderer for ActiveLineNumbers {
fn measure_line_numbers(
&self,
digit_count: usize,
body_style: &TextStyle,
window: &Window,
) -> (Pixels, Pixels) {
().measure_line_numbers(digit_count, body_style, window)
}

fn shape_line_number(
&self,
number: usize,
is_active: bool,
body_style: &TextStyle,
window: &Window,
cx: &App,
) -> ShapedLine {
if is_active {
self.0.borrow_mut().push(number);
}
().shape_line_number(number, is_active, body_style, window, cx)
}
}

struct ActiveLineHarness(Entity<EditorState>, ActiveLineNumbers);

impl Render for ActiveLineHarness {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.size_full()
.child(Editor::new(&self.0).with_line_number_renderer(self.1.clone()))
}
}

#[gpui::test]
fn active_line_follows_the_input_and_its_menu_focus(cx: &mut TestAppContext) {
cx.update(crate::init);
for only_when_focused in [false, true] {
let numbers = ActiveLineNumbers::default();
let mut editor = None;
let window = cx.open_window(size(px(240.), px(140.)), |window, cx| {
let state = cx.new(|cx| {
EditorState::new(window, cx)
.active_line_only_when_focused(only_when_focused)
.default_value("one\ntwo\nthree")
});
editor = Some(state.clone());
ActiveLineHarness(state, numbers.clone())
});
let editor = editor.unwrap();
let mut cx = VisualTestContext::from_window(window.into(), cx);
cx.update(|window, cx| {
let menu_focus = cx.focus_handle();
let other_focus = cx.focus_handle();
let active = |window: &mut Window, cx: &mut App| {
window.draw(cx).clear(cx);
numbers.0.borrow_mut().clear();
window.draw(cx).clear(cx);
numbers.0.borrow().clone()
};

editor.update(cx, |state, cx| {
state.set_cursor_position(Position::new(1, 0), window, cx);
state.focus(window, cx);
});
assert_eq!(active(window, cx), vec![2]);

other_focus.focus(window, cx);
let unfocused: Vec<usize> = if only_when_focused { vec![] } else { vec![2] };
assert_eq!(active(window, cx), unfocused, "{only_when_focused}");

editor.update(cx, |state, cx| {
state.set_selection_focus(Some(menu_focus.clone()), cx);
});
menu_focus.focus(window, cx);
assert_eq!(active(window, cx), vec![2]);

other_focus.focus(window, cx);
editor.update(cx, |state, cx| {
state.set_active_line_only_when_focused(false, window, cx);
});
assert_eq!(active(window, cx), vec![2]);
});
}
}

/// Paints an input moved by its parent, then counts the views a draw with
/// nothing else to do builds: a notify during a draw only marks a view
/// dirty, for the next draw. Only GPUI Fast reuses a clean view.
Expand Down
15 changes: 15 additions & 0 deletions crates/base/src/input/base/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub(crate) enum LayoutMode {
language: Box<EditorLanguage>,
indent_guides: bool,
folding: bool,
active_line_only_when_focused: bool,
highlighter: Rc<RefCell<Option<Box<dyn InputHighlighter>>>>,
highlighter_factory: Option<InputHighlighterFactory>,
diagnostics: DiagnosticSet,
Expand Down Expand Up @@ -86,6 +87,7 @@ impl LayoutMode {
line_number: true,
indent_guides: true,
folding: true,
active_line_only_when_focused: false,
diagnostics: DiagnosticSet::new(&Rope::new()),
auto_closed_pairs: Default::default(),
auto_close: true,
Expand Down Expand Up @@ -150,6 +152,16 @@ impl LayoutMode {
)
}

pub(super) fn is_active_line_only_when_focused(&self) -> bool {
matches!(
self,
Self::CodeEditor {
active_line_only_when_focused: true,
..
}
)
}

pub(super) fn set_auto_close(&mut self, enabled: bool) {
if let Self::CodeEditor { auto_close, .. } = self {
*auto_close = enabled;
Expand Down Expand Up @@ -450,11 +462,13 @@ mod tests {
assert_eq!(mode.max_rows(), usize::MAX);
assert_eq!(mode.min_rows(), 1);
assert_eq!(mode.is_folding(), true);
assert_eq!(mode.is_active_line_only_when_focused(), false);

let mode = LayoutMode::CodeEditor {
line_number: false,
indent_guides: false,
folding: false,
active_line_only_when_focused: true,
rows: 0,
tab: Default::default(),
language: Box::default(),
Expand All @@ -469,6 +483,7 @@ mod tests {
assert_eq!(mode.has_indent_guides(), false);
assert_eq!(mode.min_rows(), 1);
assert_eq!(mode.is_folding(), false);
assert_eq!(mode.is_active_line_only_when_focused(), true);
assert_eq!(mode.is_auto_close(), false);
assert_eq!(mode.is_smart_indent(), false);
}
Expand Down
35 changes: 35 additions & 0 deletions crates/base/src/input/base/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13997,6 +13997,41 @@ impl InputBaseState<crate::input::EditorMode> {
cx.notify();
}

/// Set enable/disable showing the active line only while focused.
///
/// Default: false
#[doc(hidden)]
pub fn active_line_only_when_focused(mut self, active_line_only_when_focused: bool) -> Self {
if let LayoutMode::CodeEditor {
active_line_only_when_focused: a,
..
} = &mut self.mode
{
*a = active_line_only_when_focused;
}
self
}

/// Set showing the active line only while focused.
pub fn set_active_line_only_when_focused(
&mut self,
active_line_only_when_focused: bool,
_: &mut Window,
cx: &mut Context<Self>,
) {
if let LayoutMode::CodeEditor {
active_line_only_when_focused: a,
..
} = &mut self.mode
{
if *a == active_line_only_when_focused {
return;
}
*a = active_line_only_when_focused;
}
cx.notify();
}

/// Set enable/disable automatic closing brackets and quotes.
///
/// When enabled, typing an opener from [`LanguageConfig::auto_closing_pairs`](crate::input::language_config::LanguageConfig::auto_closing_pairs) inserts the
Expand Down
6 changes: 6 additions & 0 deletions website/component/editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ let editor = cx.new(|cx| {
.folding(true)
.show_whitespaces(true)
.scroll_bounce(true)
.active_line_only_when_focused(true)
.default_value(source)
});
```
Expand All @@ -141,6 +142,11 @@ bottom and spring back with precise trackpad or Magic Mouse scrolling, as in the
[Textarea](./textarea.md#scroll-bounce). The gutter moves with the code; the
frame and scrollbar stay put. Ordinary mouse-wheel line scrolling does not bounce.

`active_line_only_when_focused(true)` shows the active line number, the
active-line background and the caret's fold control only while the editor or
its context menu has focus, so that of several editors on screen only the
focused one shows an active line.

## Manual completions

`ShowCompletions` requests suggestions without editing text. It has **no default
Expand Down
3 changes: 3 additions & 0 deletions website/zh-CN/component/editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,15 @@ let editor = cx.new(|cx| {
.folding(true)
.show_whitespaces(true)
.scroll_bounce(true)
.active_line_only_when_focused(true)
.default_value(source)
});
```

`scroll_bounce(true)` 让超出编辑器高度的代码在触控板或 Magic Mouse 的精细像素滚动到达顶部或底部时越过边缘并弹回,与 [Textarea](./textarea.md#滚动回弹) 相同。行号栏随代码移动,外框和滚动条保持不动;普通鼠标滚轮的按行滚动不触发回弹。

`active_line_only_when_focused(true)` 让当前行的行号、当前行背景和光标所在行的折叠按钮只在编辑器或其右键菜单获得焦点时显示,这样界面上有多个编辑器时,只有获得焦点的那个显示当前行。

## 主动补全

`ShowCompletions` 可以在不修改文本的情况下请求补全,**没有默认快捷键**。应用应结合平台习惯选择绑定,避开 Ctrl+Space 等输入法快捷键。Editor 的绑定可以使用 `Input && mode == editor` 条件;现有 `Input` 绑定仍然有效。
Expand Down
Loading