Skip to content

Commit

Permalink
Fixes eclipse-che#2437: Apply multi-edit formatting without messing u…
Browse files Browse the repository at this point in the history
…p the document (eclipse-che#2719)

* Fixes eclipse-che#2437: Shift remaining ranges when applying multi-edit formatting

A formatting request to a language server may result in a list of
multiple text edits. The ranges of all these text edits are based on the
yet unmodified document. Che applies the edits to the document one by
one. If the inserted text has a different length than the replaced text
then the ranges of the remaining edits become invalid and they must be
shifted appropriately.

This patch shifts the ranges as necessary. To achieve this, it first
converts the ranges based on lines and characters to linear ranges,
which are easier for shifting.

Signed-off-by: Kaloyan Raev <kaloyan.r@zend.com>

* Simplify the shifting logic

Signed-off-by: Kaloyan Raev <kaloyan.r@zend.com>

* Reworked: just applying the text edits backwards is enough
  • Loading branch information
kaloyan-raev authored and Vitalii Parfonov committed Oct 6, 2016
1 parent 054f4c4 commit 0051c43
Showing 1 changed file with 4 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.che.ide.util.loging.Log;
import org.eclipse.che.plugin.languageserver.ide.service.TextDocumentServiceClient;

import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -156,6 +157,9 @@ private void applyEdits(List<TextEditDTO> edits, Document document) {
if (undoRedo != null) {
undoRedo.beginCompoundChange();
}

// #2437: apply the text edits from last to first to avoid messing up the document
Collections.reverse(edits);
for (TextEditDTO change : edits) {
RangeDTO range = change.getRange();
document.replace(range.getStart().getLine(), range.getStart().getCharacter(),
Expand Down

0 comments on commit 0051c43

Please sign in to comment.