git: Hard wrap in editor (#26507)
This adds the ability for the editor to implement hard wrap (similar to "textwidth" in vim). If you are typing and your line extends beyond the limit, a newline is inserted before the most recent space on the line. If you are otherwise editing the line, pasting, etc. then you will need to manually rewrap. Release Notes: - git: Commit messages are now wrapped "as you type" to 72 characters.
This commit is contained in:
parent
7bca15704b
commit
c8b782d870
4 changed files with 61 additions and 17 deletions
|
@ -608,12 +608,6 @@ pub trait Addon: 'static {
|
||||||
fn to_any(&self) -> &dyn std::any::Any;
|
fn to_any(&self) -> &dyn std::any::Any;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
||||||
pub enum IsVimMode {
|
|
||||||
Yes,
|
|
||||||
No,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Zed's primary implementation of text input, allowing users to edit a [`MultiBuffer`].
|
/// Zed's primary implementation of text input, allowing users to edit a [`MultiBuffer`].
|
||||||
///
|
///
|
||||||
/// See the [module level documentation](self) for more information.
|
/// See the [module level documentation](self) for more information.
|
||||||
|
@ -645,6 +639,7 @@ pub struct Editor {
|
||||||
inline_diagnostics_enabled: bool,
|
inline_diagnostics_enabled: bool,
|
||||||
inline_diagnostics: Vec<(Anchor, InlineDiagnostic)>,
|
inline_diagnostics: Vec<(Anchor, InlineDiagnostic)>,
|
||||||
soft_wrap_mode_override: Option<language_settings::SoftWrap>,
|
soft_wrap_mode_override: Option<language_settings::SoftWrap>,
|
||||||
|
hard_wrap: Option<usize>,
|
||||||
|
|
||||||
// TODO: make this a access method
|
// TODO: make this a access method
|
||||||
pub project: Option<Entity<Project>>,
|
pub project: Option<Entity<Project>>,
|
||||||
|
@ -1356,6 +1351,7 @@ impl Editor {
|
||||||
inline_diagnostics_update: Task::ready(()),
|
inline_diagnostics_update: Task::ready(()),
|
||||||
inline_diagnostics: Vec::new(),
|
inline_diagnostics: Vec::new(),
|
||||||
soft_wrap_mode_override,
|
soft_wrap_mode_override,
|
||||||
|
hard_wrap: None,
|
||||||
completion_provider: project.clone().map(|project| Box::new(project) as _),
|
completion_provider: project.clone().map(|project| Box::new(project) as _),
|
||||||
semantics_provider: project.clone().map(|project| Rc::new(project) as _),
|
semantics_provider: project.clone().map(|project| Rc::new(project) as _),
|
||||||
collaboration_hub: project.clone().map(|project| Box::new(project) as _),
|
collaboration_hub: project.clone().map(|project| Box::new(project) as _),
|
||||||
|
@ -3193,6 +3189,19 @@ impl Editor {
|
||||||
|
|
||||||
let trigger_in_words =
|
let trigger_in_words =
|
||||||
this.show_edit_predictions_in_menu() || !had_active_inline_completion;
|
this.show_edit_predictions_in_menu() || !had_active_inline_completion;
|
||||||
|
if this.hard_wrap.is_some() {
|
||||||
|
let latest: Range<Point> = this.selections.newest(cx).range();
|
||||||
|
if latest.is_empty()
|
||||||
|
&& this
|
||||||
|
.buffer()
|
||||||
|
.read(cx)
|
||||||
|
.snapshot(cx)
|
||||||
|
.line_len(MultiBufferRow(latest.start.row))
|
||||||
|
== latest.start.column
|
||||||
|
{
|
||||||
|
this.rewrap_impl(true, cx)
|
||||||
|
}
|
||||||
|
}
|
||||||
this.trigger_completion_on_input(&text, trigger_in_words, window, cx);
|
this.trigger_completion_on_input(&text, trigger_in_words, window, cx);
|
||||||
linked_editing_ranges::refresh_linked_ranges(this, window, cx);
|
linked_editing_ranges::refresh_linked_ranges(this, window, cx);
|
||||||
this.refresh_inline_completion(true, false, window, cx);
|
this.refresh_inline_completion(true, false, window, cx);
|
||||||
|
@ -8597,10 +8606,10 @@ impl Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rewrap(&mut self, _: &Rewrap, _: &mut Window, cx: &mut Context<Self>) {
|
pub fn rewrap(&mut self, _: &Rewrap, _: &mut Window, cx: &mut Context<Self>) {
|
||||||
self.rewrap_impl(IsVimMode::No, cx)
|
self.rewrap_impl(false, cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rewrap_impl(&mut self, is_vim_mode: IsVimMode, cx: &mut Context<Self>) {
|
pub fn rewrap_impl(&mut self, override_language_settings: bool, cx: &mut Context<Self>) {
|
||||||
let buffer = self.buffer.read(cx).snapshot(cx);
|
let buffer = self.buffer.read(cx).snapshot(cx);
|
||||||
let selections = self.selections.all::<Point>(cx);
|
let selections = self.selections.all::<Point>(cx);
|
||||||
let mut selections = selections.iter().peekable();
|
let mut selections = selections.iter().peekable();
|
||||||
|
@ -8674,7 +8683,9 @@ impl Editor {
|
||||||
RewrapBehavior::Anywhere => true,
|
RewrapBehavior::Anywhere => true,
|
||||||
};
|
};
|
||||||
|
|
||||||
let should_rewrap = is_vim_mode == IsVimMode::Yes || allow_rewrap_based_on_language;
|
let should_rewrap = override_language_settings
|
||||||
|
|| allow_rewrap_based_on_language
|
||||||
|
|| self.hard_wrap.is_some();
|
||||||
if !should_rewrap {
|
if !should_rewrap {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -8722,9 +8733,11 @@ impl Editor {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
let wrap_column = buffer
|
let wrap_column = self.hard_wrap.unwrap_or_else(|| {
|
||||||
.language_settings_at(Point::new(start_row, 0), cx)
|
buffer
|
||||||
.preferred_line_length as usize;
|
.language_settings_at(Point::new(start_row, 0), cx)
|
||||||
|
.preferred_line_length as usize
|
||||||
|
});
|
||||||
let wrapped_text = wrap_with_prefix(
|
let wrapped_text = wrap_with_prefix(
|
||||||
line_prefix,
|
line_prefix,
|
||||||
lines_without_prefixes.join(" "),
|
lines_without_prefixes.join(" "),
|
||||||
|
@ -8735,7 +8748,7 @@ impl Editor {
|
||||||
// TODO: should always use char-based diff while still supporting cursor behavior that
|
// TODO: should always use char-based diff while still supporting cursor behavior that
|
||||||
// matches vim.
|
// matches vim.
|
||||||
let mut diff_options = DiffOptions::default();
|
let mut diff_options = DiffOptions::default();
|
||||||
if is_vim_mode == IsVimMode::Yes {
|
if override_language_settings {
|
||||||
diff_options.max_word_diff_len = 0;
|
diff_options.max_word_diff_len = 0;
|
||||||
diff_options.max_word_diff_line_count = 0;
|
diff_options.max_word_diff_line_count = 0;
|
||||||
} else {
|
} else {
|
||||||
|
@ -14305,6 +14318,11 @@ impl Editor {
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_hard_wrap(&mut self, hard_wrap: Option<usize>, cx: &mut Context<Self>) {
|
||||||
|
self.hard_wrap = hard_wrap;
|
||||||
|
cx.notify();
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_text_style_refinement(&mut self, style: TextStyleRefinement) {
|
pub fn set_text_style_refinement(&mut self, style: TextStyleRefinement) {
|
||||||
self.text_style_refinement = Some(style);
|
self.text_style_refinement = Some(style);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4738,6 +4738,31 @@ async fn test_rewrap(cx: &mut TestAppContext) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[gpui::test]
|
||||||
|
async fn test_hard_wrap(cx: &mut TestAppContext) {
|
||||||
|
init_test(cx, |_| {});
|
||||||
|
let mut cx = EditorTestContext::new(cx).await;
|
||||||
|
|
||||||
|
cx.update_editor(|editor, _, cx| {
|
||||||
|
editor.set_hard_wrap(Some(14), cx);
|
||||||
|
});
|
||||||
|
|
||||||
|
cx.set_state(indoc!(
|
||||||
|
"
|
||||||
|
one two three ˇ
|
||||||
|
"
|
||||||
|
));
|
||||||
|
cx.simulate_input("four");
|
||||||
|
cx.run_until_parked();
|
||||||
|
|
||||||
|
cx.assert_editor_state(indoc!(
|
||||||
|
"
|
||||||
|
one two three
|
||||||
|
fourˇ
|
||||||
|
"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
#[gpui::test]
|
#[gpui::test]
|
||||||
async fn test_clipboard(cx: &mut TestAppContext) {
|
async fn test_clipboard(cx: &mut TestAppContext) {
|
||||||
init_test(cx, |_| {});
|
init_test(cx, |_| {});
|
||||||
|
|
|
@ -367,6 +367,7 @@ pub(crate) fn commit_message_editor(
|
||||||
commit_editor.set_show_gutter(false, cx);
|
commit_editor.set_show_gutter(false, cx);
|
||||||
commit_editor.set_show_wrap_guides(false, cx);
|
commit_editor.set_show_wrap_guides(false, cx);
|
||||||
commit_editor.set_show_indent_guides(false, cx);
|
commit_editor.set_show_indent_guides(false, cx);
|
||||||
|
commit_editor.set_hard_wrap(Some(72), cx);
|
||||||
let placeholder = placeholder.unwrap_or("Enter commit message");
|
let placeholder = placeholder.unwrap_or("Enter commit message");
|
||||||
commit_editor.set_placeholder_text(placeholder, cx);
|
commit_editor.set_placeholder_text(placeholder, cx);
|
||||||
commit_editor
|
commit_editor
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{motion::Motion, object::Object, state::Mode, Vim};
|
use crate::{motion::Motion, object::Object, state::Mode, Vim};
|
||||||
use collections::HashMap;
|
use collections::HashMap;
|
||||||
use editor::{display_map::ToDisplayPoint, scroll::Autoscroll, Bias, Editor, IsVimMode};
|
use editor::{display_map::ToDisplayPoint, scroll::Autoscroll, Bias, Editor};
|
||||||
use gpui::{actions, Context, Window};
|
use gpui::{actions, Context, Window};
|
||||||
use language::SelectionGoal;
|
use language::SelectionGoal;
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ pub(crate) fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
|
||||||
vim.update_editor(window, cx, |vim, editor, window, cx| {
|
vim.update_editor(window, cx, |vim, editor, window, cx| {
|
||||||
editor.transact(window, cx, |editor, window, cx| {
|
editor.transact(window, cx, |editor, window, cx| {
|
||||||
let mut positions = vim.save_selection_starts(editor, cx);
|
let mut positions = vim.save_selection_starts(editor, cx);
|
||||||
editor.rewrap_impl(IsVimMode::Yes, cx);
|
editor.rewrap_impl(true, cx);
|
||||||
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
|
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
|
||||||
s.move_with(|map, selection| {
|
s.move_with(|map, selection| {
|
||||||
if let Some(anchor) = positions.remove(&selection.id) {
|
if let Some(anchor) = positions.remove(&selection.id) {
|
||||||
|
@ -52,7 +52,7 @@ impl Vim {
|
||||||
motion.expand_selection(map, selection, times, false, &text_layout_details);
|
motion.expand_selection(map, selection, times, false, &text_layout_details);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
editor.rewrap_impl(IsVimMode::Yes, cx);
|
editor.rewrap_impl(true, cx);
|
||||||
editor.change_selections(None, window, cx, |s| {
|
editor.change_selections(None, window, cx, |s| {
|
||||||
s.move_with(|map, selection| {
|
s.move_with(|map, selection| {
|
||||||
let anchor = selection_starts.remove(&selection.id).unwrap();
|
let anchor = selection_starts.remove(&selection.id).unwrap();
|
||||||
|
@ -83,7 +83,7 @@ impl Vim {
|
||||||
object.expand_selection(map, selection, around);
|
object.expand_selection(map, selection, around);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
editor.rewrap_impl(IsVimMode::Yes, cx);
|
editor.rewrap_impl(true, cx);
|
||||||
editor.change_selections(None, window, cx, |s| {
|
editor.change_selections(None, window, cx, |s| {
|
||||||
s.move_with(|map, selection| {
|
s.move_with(|map, selection| {
|
||||||
let anchor = original_positions.remove(&selection.id).unwrap();
|
let anchor = original_positions.remove(&selection.id).unwrap();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue