Apply external command formatting if buffer has changed while computing it
This commit is contained in:
parent
368d2a73ea
commit
70cb2fa8d7
3 changed files with 18 additions and 32 deletions
|
@ -569,6 +569,7 @@ impl Buffer {
|
||||||
.read_with(&cx, |this, cx| this.diff(new_text, cx))
|
.read_with(&cx, |this, cx| this.diff(new_text, cx))
|
||||||
.await;
|
.await;
|
||||||
this.update(&mut cx, |this, cx| {
|
this.update(&mut cx, |this, cx| {
|
||||||
|
if this.version() == diff.base_version {
|
||||||
this.finalize_last_transaction();
|
this.finalize_last_transaction();
|
||||||
this.apply_diff(diff, cx);
|
this.apply_diff(diff, cx);
|
||||||
if let Some(transaction) = this.finalize_last_transaction().cloned() {
|
if let Some(transaction) = this.finalize_last_transaction().cloned() {
|
||||||
|
@ -579,10 +580,10 @@ impl Buffer {
|
||||||
new_mtime,
|
new_mtime,
|
||||||
cx,
|
cx,
|
||||||
);
|
);
|
||||||
Ok(Some(transaction))
|
return Ok(Some(transaction));
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
Ok(None)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
|
@ -1197,25 +1198,10 @@ impl Buffer {
|
||||||
self.edit([(offset..len, "\n")], None, cx);
|
self.edit([(offset..len, "\n")], None, cx);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
|
|
||||||
if self.version == diff.base_version {
|
|
||||||
self.start_transaction();
|
|
||||||
self.text.set_line_ending(diff.line_ending);
|
|
||||||
self.edit(diff.edits, None, cx);
|
|
||||||
self.end_transaction(cx)
|
|
||||||
} else {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Apply a diff to the buffer. If the buffer has changed since the given diff was
|
/// Apply a diff to the buffer. If the buffer has changed since the given diff was
|
||||||
/// calculated, then adjust the diff to account for those changes, and discard any
|
/// calculated, then adjust the diff to account for those changes, and discard any
|
||||||
/// parts of the diff that conflict with those changes.
|
/// parts of the diff that conflict with those changes.
|
||||||
pub fn apply_non_conflicting_portion_of_diff(
|
pub fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> Option<TransactionId> {
|
||||||
&mut self,
|
|
||||||
diff: Diff,
|
|
||||||
cx: &mut ModelContext<Self>,
|
|
||||||
) -> Option<TransactionId> {
|
|
||||||
// Check for any edits to the buffer that have occurred since this diff
|
// Check for any edits to the buffer that have occurred since this diff
|
||||||
// was computed.
|
// was computed.
|
||||||
let snapshot = self.snapshot();
|
let snapshot = self.snapshot();
|
||||||
|
@ -1223,7 +1209,7 @@ impl Buffer {
|
||||||
let mut delta = 0;
|
let mut delta = 0;
|
||||||
let adjusted_edits = diff.edits.into_iter().filter_map(|(range, new_text)| {
|
let adjusted_edits = diff.edits.into_iter().filter_map(|(range, new_text)| {
|
||||||
while let Some(edit_since) = edits_since.peek() {
|
while let Some(edit_since) = edits_since.peek() {
|
||||||
// If the edit occurs after a diff hunk, then it can does not
|
// If the edit occurs after a diff hunk, then it does not
|
||||||
// affect that hunk.
|
// affect that hunk.
|
||||||
if edit_since.old.start > range.end {
|
if edit_since.old.start > range.end {
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -254,7 +254,7 @@ async fn test_normalize_whitespace(cx: &mut gpui::TestAppContext) {
|
||||||
let format_diff = format.await;
|
let format_diff = format.await;
|
||||||
buffer.update(cx, |buffer, cx| {
|
buffer.update(cx, |buffer, cx| {
|
||||||
let version_before_format = format_diff.base_version.clone();
|
let version_before_format = format_diff.base_version.clone();
|
||||||
buffer.apply_non_conflicting_portion_of_diff(format_diff, cx);
|
buffer.apply_diff(format_diff, cx);
|
||||||
|
|
||||||
// The outcome depends on the order of concurrent taks.
|
// The outcome depends on the order of concurrent taks.
|
||||||
//
|
//
|
||||||
|
|
|
@ -2921,7 +2921,7 @@ impl Project {
|
||||||
buffer.finalize_last_transaction();
|
buffer.finalize_last_transaction();
|
||||||
buffer.start_transaction();
|
buffer.start_transaction();
|
||||||
if let Some(diff) = trailing_whitespace_diff {
|
if let Some(diff) = trailing_whitespace_diff {
|
||||||
buffer.apply_non_conflicting_portion_of_diff(diff, cx);
|
buffer.apply_diff(diff, cx);
|
||||||
}
|
}
|
||||||
if ensure_final_newline {
|
if ensure_final_newline {
|
||||||
buffer.ensure_final_newline(cx);
|
buffer.ensure_final_newline(cx);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue