mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-10-06 06:21:16 +07:00
Settings/Code Style/JavaDoc: move range correction logic inside diff calculator
This commit is contained in:
committed by
Aleksey Pivovarov
parent
c87a2f72c5
commit
207ba262b6
@@ -34,6 +34,7 @@ import com.intellij.openapi.editor.markup.GutterIconRenderer;
|
||||
import com.intellij.openapi.editor.markup.SeparatorPlacement;
|
||||
import com.intellij.openapi.fileEditor.FileEditor;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.util.diff.FilesTooBigForDiffException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -75,7 +76,52 @@ public class ChangesDiffCalculator implements Disposable {
|
||||
fragment.highlight(fragmentHighlighter);
|
||||
}
|
||||
|
||||
return new ArrayList<TextRange>(myNewMarkup.ranges);
|
||||
List<TextRange> ranges = new ArrayList<>(myNewMarkup.ranges);
|
||||
for (TextRange range : myNewMarkup.ranges) {
|
||||
if (range.getStartOffset() >= currentDocument.getTextLength()) {
|
||||
continue;
|
||||
}
|
||||
ranges.add(calculateChangeHighlightRange(currentDocument, range));
|
||||
}
|
||||
|
||||
return ranges;
|
||||
}
|
||||
|
||||
/**
|
||||
* We want to highlight document formatting changes introduced by particular formatting property value change.
|
||||
* However, there is a possible effect that white space region is removed. We still want to highlight that, hence, it's necessary
|
||||
* to highlight neighbour region.
|
||||
* <p/>
|
||||
* This method encapsulates logic of adjusting preview highlight change if necessary.
|
||||
*
|
||||
* @param range initial range to highlight
|
||||
* @return resulting range to highlight
|
||||
*/
|
||||
private static TextRange calculateChangeHighlightRange(Document currentDocument, TextRange range) {
|
||||
CharSequence text = currentDocument.getCharsSequence();
|
||||
|
||||
if (range.getLength() <= 0) {
|
||||
int offset = range.getStartOffset();
|
||||
while (offset < text.length() && text.charAt(offset) == ' ') {
|
||||
offset++;
|
||||
}
|
||||
return offset > range.getStartOffset() ? new TextRange(offset, offset) : range;
|
||||
}
|
||||
|
||||
int startOffset = range.getStartOffset() + 1;
|
||||
int endOffset = range.getEndOffset() + 1;
|
||||
boolean useSameRange = true;
|
||||
while (endOffset <= text.length()
|
||||
&& StringUtil.equals(text.subSequence(range.getStartOffset(), range.getEndOffset()), text.subSequence(startOffset, endOffset)))
|
||||
{
|
||||
useSameRange = false;
|
||||
startOffset++;
|
||||
endOffset++;
|
||||
}
|
||||
startOffset--;
|
||||
endOffset--;
|
||||
|
||||
return useSameRange ? range : new TextRange(startOffset, endOffset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+6
-52
@@ -37,7 +37,6 @@ import com.intellij.openapi.project.ProjectUtil;
|
||||
import com.intellij.openapi.ui.OnePixelDivider;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiFileFactory;
|
||||
@@ -59,8 +58,10 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.LineNumberReader;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public abstract class CodeStyleAbstractPanel implements Disposable {
|
||||
@@ -289,23 +290,13 @@ public abstract class CodeStyleAbstractPanel implements Disposable {
|
||||
}
|
||||
|
||||
private void highlightChanges(Document beforeReformat) {
|
||||
|
||||
myPreviewRangesToHighlight.clear();
|
||||
MarkupModel markupModel = myEditor.getMarkupModel();
|
||||
markupModel.removeAllHighlighters();
|
||||
int textLength = myEditor.getDocument().getTextLength();
|
||||
boolean highlightPreview = false;
|
||||
Collection<TextRange> ranges = myDiffCalculator.calculateDiff(beforeReformat, myEditor.getDocument());
|
||||
for (TextRange range : ranges) {
|
||||
if (range.getStartOffset() >= textLength) {
|
||||
continue;
|
||||
}
|
||||
highlightPreview = true;
|
||||
TextRange rangeToUse = calculateChangeHighlightRange(range);
|
||||
myPreviewRangesToHighlight.add(rangeToUse);
|
||||
}
|
||||
|
||||
if (highlightPreview) {
|
||||
myPreviewRangesToHighlight.addAll(myDiffCalculator.calculateDiff(beforeReformat, myEditor.getDocument()));
|
||||
|
||||
if (!myPreviewRangesToHighlight.isEmpty()) {
|
||||
myEndHighlightPreviewChangesTimeMillis = System.currentTimeMillis() + TIME_TO_HIGHLIGHT_PREVIEW_CHANGES_IN_MILLIS;
|
||||
myShowsPreviewHighlighters = true;
|
||||
}
|
||||
@@ -326,43 +317,6 @@ public abstract class CodeStyleAbstractPanel implements Disposable {
|
||||
&& targetPosition.column >= startPosition.column && targetPosition.column <= endPosition.column;
|
||||
}
|
||||
|
||||
/**
|
||||
* We want to highlight document formatting changes introduced by particular formatting property value change.
|
||||
* However, there is a possible effect that white space region is removed. We still want to highlight that, hence, it's necessary
|
||||
* to highlight neighbour region.
|
||||
* <p/>
|
||||
* This method encapsulates logic of adjusting preview highlight change if necessary.
|
||||
*
|
||||
* @param range initial range to highlight
|
||||
* @return resulting range to highlight
|
||||
*/
|
||||
private TextRange calculateChangeHighlightRange(TextRange range) {
|
||||
CharSequence text = myEditor.getDocument().getCharsSequence();
|
||||
|
||||
if (range.getLength() <= 0) {
|
||||
int offset = range.getStartOffset();
|
||||
while (offset < text.length() && text.charAt(offset) == ' ') {
|
||||
offset++;
|
||||
}
|
||||
return offset > range.getStartOffset() ? new TextRange(offset, offset) : range;
|
||||
}
|
||||
|
||||
int startOffset = range.getStartOffset() + 1;
|
||||
int endOffset = range.getEndOffset() + 1;
|
||||
boolean useSameRange = true;
|
||||
while (endOffset <= text.length()
|
||||
&& StringUtil.equals(text.subSequence(range.getStartOffset(), range.getEndOffset()), text.subSequence(startOffset, endOffset)))
|
||||
{
|
||||
useSameRange = false;
|
||||
startOffset++;
|
||||
endOffset++;
|
||||
}
|
||||
startOffset--;
|
||||
endOffset--;
|
||||
|
||||
return useSameRange ? range : new TextRange(startOffset, endOffset);
|
||||
}
|
||||
|
||||
private void updatePreviewHighlighter(final EditorEx editor) {
|
||||
EditorColorsScheme scheme = editor.getColorsScheme();
|
||||
editor.getSettings().setCaretRowShown(false);
|
||||
|
||||
Reference in New Issue
Block a user