diff: unified - keep caret position on content update

This commit is contained in:
Aleksey Pivovarov
2016-04-20 19:39:15 +03:00
parent 4cc4c52e37
commit f76130cfdd
2 changed files with 21 additions and 1 deletions
@@ -65,6 +65,14 @@ public class LineCol {
}
public static int toOffset(@NotNull Document document, @NotNull LineCol linecol) {
return document.getLineStartOffset(linecol.line) + linecol.column;
return linecol.toOffset(document);
}
public static int toOffset(@NotNull Document document, int line, int col) {
return new LineCol(line, col).toOffset(document);
}
public int toOffset(@NotNull Document document) {
return document.getLineStartOffset(line) + column;
}
}
@@ -384,7 +384,13 @@ public class UnifiedDiffViewer extends ListenerDiffViewerBase {
public void run() {
myFoldingModel.updateContext(myRequest, getFoldingModelSettings());
LineCol oldCaretPosition = LineCol.fromOffset(myDocument, myEditor.getCaretModel().getPrimaryCaret().getOffset());
Pair<int[], Side> oldCaretLineTwoside = transferLineFromOneside(oldCaretPosition.line);
clearDiffPresentation();
if (isContentsEqual) {
boolean equalCharsets = TextDiffViewerUtil.areEqualCharsets(getContents());
boolean equalSeparators = TextDiffViewerUtil.areEqualLineSeparators(getContents());
@@ -431,8 +437,14 @@ public class UnifiedDiffViewer extends ListenerDiffViewerBase {
guarderRangeBlocks.add(createGuardedBlock(textLength, textLength));
}
myChangedBlockData = new ChangedBlockData(diffChanges, guarderRangeBlocks, convertor, isContentsEqual);
int newCaretLine = transferLineToOneside(oldCaretLineTwoside.second,
oldCaretLineTwoside.second.select(oldCaretLineTwoside.first));
myEditor.getCaretModel().moveToOffset(LineCol.toOffset(myDocument, newCaretLine, oldCaretPosition.column));
myFoldingModel.install(changedLines, myRequest, getFoldingModelSettings());
myInitialScrollHelper.onRediff();