Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commit undo checkpoints before each :write #11062

Merged
merged 1 commit into from
Jul 13, 2024
Merged
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
16 changes: 11 additions & 5 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,12 @@ fn write_impl(
let path = path.map(AsRef::as_ref);

if config.insert_final_newline {
insert_final_newline(doc, view);
insert_final_newline(doc, view.id);
}

// Save an undo checkpoint for any outstanding changes.
doc.append_changes_to_history(view);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally Document::save_impl would be in charge of this but currently Document::append_changes_to_history needs a &mut View. Maybe we can refactor that later and move this call into document.


let fmt = if config.auto_format {
doc.auto_format().map(|fmt| {
let callback = make_format_callback(
Expand All @@ -366,13 +369,12 @@ fn write_impl(
Ok(())
}

fn insert_final_newline(doc: &mut Document, view: &mut View) {
fn insert_final_newline(doc: &mut Document, view_id: ViewId) {
let text = doc.text();
if line_ending::get_line_ending(&text.slice(..)).is_none() {
let eof = Selection::point(text.len_chars());
let insert = Transaction::insert(text, &eof, doc.line_ending.as_str().into());
doc.apply(&insert, view.id);
doc.append_changes_to_history(view);
doc.apply(&insert, view_id);
}
}

Expand Down Expand Up @@ -703,11 +705,15 @@ pub fn write_all_impl(

for (doc_id, target_view) in saves {
let doc = doc_mut!(cx.editor, &doc_id);
let view = view_mut!(cx.editor, target_view);

if config.insert_final_newline {
insert_final_newline(doc, view_mut!(cx.editor, target_view));
insert_final_newline(doc, target_view);
}

// Save an undo checkpoint for any outstanding changes.
doc.append_changes_to_history(view);

let fmt = if config.auto_format {
doc.auto_format().map(|fmt| {
let callback = make_format_callback(
Expand Down
Loading