mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-119366 Reformatting only VCS changed code produces unwanted side-effect [CR-IC-4175]
This commit is contained in:
@@ -3016,4 +3016,36 @@ public void testSCR260() throws Exception {
|
||||
settings.FORMATTER_ON_TAG = "end.*fragment";
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testDoNotIndentNotSelectedStatement_AfterSelectedOne() {
|
||||
myTextRange = new TextRange(0, 73);
|
||||
doTextTest(
|
||||
"class Test {\n" +
|
||||
" public static void main(String[] args) {\n" +
|
||||
" int a = 3;\n" +
|
||||
" System.out.println(\"AAA\");\n" +
|
||||
" }\n" +
|
||||
"}",
|
||||
"class Test {\n" +
|
||||
" public static void main(String[] args) {\n" +
|
||||
" int a = 3;\n" +
|
||||
" System.out.println(\"AAA\");\n" +
|
||||
" }\n" +
|
||||
"}"
|
||||
);
|
||||
|
||||
myTextRange = new TextRange(0, 67);
|
||||
doTextTest(
|
||||
" import java.lang.Override;\n" +
|
||||
" import java.lang.Exception;\n" +
|
||||
" \n" +
|
||||
" class Foo {\n" +
|
||||
"}",
|
||||
"import java.lang.Override;\n" +
|
||||
"import java.lang.Exception;\n" +
|
||||
" \n" +
|
||||
" class Foo {\n" +
|
||||
"}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+31
-2
@@ -34,6 +34,7 @@ import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.codeStyle.Indent;
|
||||
import com.intellij.psi.formatter.FormatterUtil;
|
||||
import com.intellij.psi.impl.CheckUtil;
|
||||
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
|
||||
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
|
||||
@@ -191,9 +192,13 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
|
||||
caretKeeper = new CaretPositionKeeper(editor);
|
||||
}
|
||||
|
||||
Collection<TextRange> correctedRanges = FormatterUtil.isFormatterCalledExplicitly()
|
||||
? removeEndingWhiteSpaceFromEachRange(file, ranges)
|
||||
: ranges;
|
||||
|
||||
final SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(getProject());
|
||||
List<RangeFormatInfo> infos = new ArrayList<RangeFormatInfo>();
|
||||
for (TextRange range : ranges) {
|
||||
for (TextRange range : correctedRanges) {
|
||||
final PsiElement start = findElementInTreeWithFormatterEnabled(file, range.getStartOffset());
|
||||
final PsiElement end = findElementInTreeWithFormatterEnabled(file, range.getEndOffset());
|
||||
if (start != null && !start.isValid()) {
|
||||
@@ -213,7 +218,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
|
||||
}
|
||||
|
||||
FormatTextRanges formatRanges = new FormatTextRanges();
|
||||
for (TextRange range : ranges) {
|
||||
for (TextRange range : correctedRanges) {
|
||||
formatRanges.add(range, true);
|
||||
}
|
||||
codeFormatter.processText(file, formatRanges, true);
|
||||
@@ -233,6 +238,30 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Collection<TextRange> removeEndingWhiteSpaceFromEachRange(@NotNull PsiFile file, @NotNull Collection<TextRange> ranges) {
|
||||
Collection<TextRange> result = new ArrayList<TextRange>();
|
||||
|
||||
for (TextRange range : ranges) {
|
||||
int rangeStart = range.getStartOffset();
|
||||
int rangeEnd = range.getEndOffset();
|
||||
|
||||
PsiElement lastElementInRange = findElementInTreeWithFormatterEnabled(file, range.getEndOffset());
|
||||
if (lastElementInRange instanceof PsiWhiteSpace
|
||||
&& rangeStart < lastElementInRange.getTextRange().getStartOffset())
|
||||
{
|
||||
PsiElement prev = lastElementInRange.getPrevSibling();
|
||||
if (prev != null) {
|
||||
rangeEnd = prev.getTextRange().getEndOffset();
|
||||
}
|
||||
}
|
||||
|
||||
result.add(new TextRange(rangeStart, rangeEnd));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private PsiElement reformatRangeImpl(final PsiElement element,
|
||||
final int startOffset,
|
||||
final int endOffset,
|
||||
|
||||
Reference in New Issue
Block a user