[vcs changes formatting] merge changed text ranges, in order to boost performance [IDEA-173139]

This commit is contained in:
Yaroslav Lepenkin
2017-05-22 10:58:08 +03:00
parent 5c36c9ba22
commit c100694fd5
4 changed files with 34 additions and 6 deletions
@@ -15,7 +15,9 @@
*/
package com.intellij.psi.codeStyle;
import com.intellij.openapi.util.Segment;
import com.intellij.openapi.util.TextRange;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -23,12 +25,38 @@ import java.util.List;
public class ChangedRangesInfo {
public final List<TextRange> insertedRanges;
public final List<TextRange> allChangedRanges;
private final List<TextRange> insertedRanges;
private final List<TextRange> allChangedRanges;
public ChangedRangesInfo(@NotNull List<TextRange> allChangedRanges, @Nullable List<TextRange> insertedRanges) {
this.insertedRanges = insertedRanges;
this.allChangedRanges = allChangedRanges;
}
public List<TextRange> insertedRanges() {
return insertedRanges;
}
public List<TextRange> optimizedChangedRanges() {
if (allChangedRanges.isEmpty()) return allChangedRanges;
allChangedRanges.sort(Segment.BY_START_OFFSET_THEN_END_OFFSET);
List<TextRange> result = ContainerUtil.newSmartList();
TextRange prev = allChangedRanges.get(0);
for (TextRange next : allChangedRanges) {
if (next.getStartOffset() <= prev.getEndOffset() + 5) {
int newEndOffset = Math.max(prev.getEndOffset(), next.getEndOffset());
prev = new TextRange(prev.getStartOffset(), newEndOffset);
}
else {
result.add(prev);
prev = next;
}
}
result.add(prev);
return result;
}
}
@@ -39,8 +39,8 @@ public class FormatTextRanges implements FormattingRangesInfo {
}
public FormatTextRanges(@NotNull ChangedRangesInfo changedRangesInfo) {
changedRangesInfo.allChangedRanges.forEach((range) -> add(range, true));
myInsertedRanges = changedRangesInfo.insertedRanges;
changedRangesInfo.optimizedChangedRanges().forEach((range) -> add(range, true));
myInsertedRanges = changedRangesInfo.insertedRanges();
}
public void add(TextRange range, boolean processHeadingWhitespace) {
@@ -75,7 +75,7 @@ public class MockCodeStyleManager extends CodeStyleManager {
@Override
public void reformatTextWithContext(@NotNull PsiFile file,
@NotNull ChangedRangesInfo ranges) throws IncorrectOperationException {
reformatText(file, ranges.allChangedRanges);
reformatText(file, ranges.optimizedChangedRanges());
}
@NotNull
@@ -48,7 +48,7 @@ class VcsAwareFormatChangedTextUtil extends FormatChangedTextUtil {
@NotNull
public List<TextRange> getChangedTextRanges(@NotNull Project project, @NotNull PsiFile file) throws FilesTooBigForDiffException {
ChangedRangesInfo helper = getChangedRangesInfo(file);
return helper != null ? helper.allChangedRanges : ContainerUtil.newArrayList();
return helper != null ? helper.optimizedChangedRanges() : ContainerUtil.newArrayList();
}
@Override