diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/java/AbstractJavaFormatterTest.java b/java/java-tests/testSrc/com/intellij/psi/formatter/java/AbstractJavaFormatterTest.java index c5bc370dcf4b..62c3233e0196 100644 --- a/java/java-tests/testSrc/com/intellij/psi/formatter/java/AbstractJavaFormatterTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/java/AbstractJavaFormatterTest.java @@ -110,7 +110,8 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase { @Override public void run(PsiFile psiFile, int startOffset, int endOffset) { SimpleFormatRangesInfo info = new SimpleFormatRangesInfo(new TextRange(startOffset, endOffset)); - CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, info); + List ranges = info.getRangesToFormat(); + CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, ranges, null); } }); ACTIONS.put(Action.REFORMAT_WITH_INSERTED_LINE_CONTEXT, new TestFormatAction() { @@ -122,7 +123,8 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase { return startOffset <= offset && offset < endOffset; } }; - CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, info); + List ranges = info.getRangesToFormat(); + CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, ranges, info); } }); } diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/performance/JavaSmartReformatPerformanceTest.java b/java/java-tests/testSrc/com/intellij/psi/formatter/performance/JavaSmartReformatPerformanceTest.java index 4bfd890cf850..f9e630823f78 100644 --- a/java/java-tests/testSrc/com/intellij/psi/formatter/performance/JavaSmartReformatPerformanceTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/performance/JavaSmartReformatPerformanceTest.java @@ -22,7 +22,6 @@ import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleManager; -import com.intellij.psi.codeStyle.SimpleFormatRangesInfo; import com.intellij.psi.formatter.java.AbstractJavaFormatterTest; import com.intellij.testFramework.PlatformTestUtil; import com.intellij.util.ThrowableRunnable; @@ -73,7 +72,7 @@ public class JavaSmartReformatPerformanceTest extends AbstractJavaFormatterTest return () -> CommandProcessor.getInstance().executeCommand( getProject(), () -> ApplicationManager.getApplication().runWriteAction( - () -> codeStyleManager.reformatTextWithContext(file, new SimpleFormatRangesInfo(ranges)) + () -> codeStyleManager.reformatTextWithContext(file, ranges, null) ), null, null); diff --git a/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java b/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java index b7700789ad9d..2260899a6616 100644 --- a/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java +++ b/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java @@ -152,13 +152,16 @@ public abstract class CodeStyleManager { * @param ranges * @throws IncorrectOperationException */ - public abstract void reformatTextWithContext(@NotNull PsiFile file, @NotNull FormatRangesInfo ranges) throws IncorrectOperationException; + public abstract void reformatTextWithContext(@NotNull PsiFile file, + @NotNull Collection ranges, + @Nullable DiffInfo diffInfo) throws IncorrectOperationException; + /** * @deprecated use {@link #reformatTextWithContext(PsiFile, FormatRangesInfo)} */ public void reformatTextWithContext(@NotNull PsiFile file, @NotNull Collection ranges) throws IncorrectOperationException { - reformatTextWithContext(file, new SimpleFormatRangesInfo(ranges)); + reformatTextWithContext(file, ranges, null); } /** diff --git a/platform/core-api/src/com/intellij/psi/codeStyle/DiffInfo.java b/platform/core-api/src/com/intellij/psi/codeStyle/DiffInfo.java new file mode 100644 index 000000000000..a80c2db3b501 --- /dev/null +++ b/platform/core-api/src/com/intellij/psi/codeStyle/DiffInfo.java @@ -0,0 +1,20 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.psi.codeStyle; + +public interface DiffInfo { + boolean isOnInsertedLine(int offset); +} diff --git a/platform/core-api/src/com/intellij/psi/codeStyle/FormatRangesInfo.java b/platform/core-api/src/com/intellij/psi/codeStyle/FormatRangesInfo.java index 4157d406a89a..5d1e891c57db 100644 --- a/platform/core-api/src/com/intellij/psi/codeStyle/FormatRangesInfo.java +++ b/platform/core-api/src/com/intellij/psi/codeStyle/FormatRangesInfo.java @@ -20,11 +20,12 @@ import org.jetbrains.annotations.NotNull; import java.util.List; -public abstract class FormatRangesInfo { +public abstract class FormatRangesInfo implements DiffInfo { @NotNull public abstract List getRangesToFormat(); + @Override public boolean isOnInsertedLine(int offset) { return false; } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeProcessor.java b/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeProcessor.java index 6cde4d45241e..b57b0963be29 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeProcessor.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeProcessor.java @@ -38,10 +38,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.awt.*; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; +import java.util.*; +import java.util.List; import java.util.concurrent.FutureTask; public class ReformatCodeProcessor extends AbstractLayoutCodeProcessor { @@ -125,7 +123,8 @@ public class ReformatCodeProcessor extends AbstractLayoutCodeProcessor { if (processChangedTextOnly) { FormatRangesInfo helper = FormatChangedTextUtil.getInstance().getChangedTextHelper(file); if (helper != null) { - CodeStyleManager.getInstance(myProject).reformatTextWithContext(file, helper); + List ranges = helper.getRangesToFormat(); + CodeStyleManager.getInstance(myProject).reformatTextWithContext(file, ranges, helper); } } else { diff --git a/platform/lang-impl/src/com/intellij/formatting/AdjustFormatRangesState.kt b/platform/lang-impl/src/com/intellij/formatting/AdjustFormatRangesState.kt index 1b79993ce555..4d5ef7d52323 100644 --- a/platform/lang-impl/src/com/intellij/formatting/AdjustFormatRangesState.kt +++ b/platform/lang-impl/src/com/intellij/formatting/AdjustFormatRangesState.kt @@ -47,6 +47,7 @@ package com.intellij.formatting import com.intellij.formatting.engine.State import com.intellij.openapi.util.TextRange +import com.intellij.psi.codeStyle.DiffInfo import com.intellij.psi.codeStyle.FormatRangesInfo import com.intellij.psi.formatter.common.AbstractBlock import com.intellij.util.containers.Stack @@ -68,35 +69,40 @@ class VcsAwareFormatRangesInfo(val formattingRanges: List, } -class AdditionalRangesExtractor(private val formatRanges: FormatTextRanges) : BlockProcessor { - val extraRanges = mutableListOf() +class AdditionalRangesExtractor(private val diffInfo: DiffInfo?) : BlockProcessor { + + val totalNewRanges = mutableListOf() override fun processLeafBlock(block: Block) = Unit override fun processCompositeBlock(block: Block) { if (block is AbstractBlock) { - block.getExtraRangesToFormat(formatRanges)?.let { - extraRanges.addAll(it) + val newRanges = block.getExtraRangesToFormat(diffInfo) + if (newRanges != null) { + totalNewRanges.addAll(newRanges) } } } + } class AdjustFormatRangesState(var currentRoot: Block, - val formatRanges: FormatTextRanges) : State() { - - private val extractor = AdditionalRangesExtractor(formatRanges) + val formatRanges: FormatTextRanges, + diffInfo: DiffInfo?) : State() { + + private val extractor = AdditionalRangesExtractor(diffInfo) private val state = Stack(currentRoot) - + init { setOnDone({ - extractor.extraRanges.forEach { - formatRanges.add(it, false) + val newRangesToAdd = extractor.totalNewRanges + newRangesToAdd.forEach { + formatRanges.add(it, false) } }) } - + override fun doIteration() { val currentBlock = state.pop() processBlock(currentBlock) diff --git a/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java b/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java index cbe4459396db..918efc57c692 100644 --- a/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java +++ b/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java @@ -71,7 +71,7 @@ public class FormatProcessor { FormatTextRanges ranges = options.myAffectedRanges; if (ranges != null && options.myReformatContext && Registry.is("smart.reformat.vcs.changes")) { - AdjustFormatRangesState adjustRangesState = new AdjustFormatRangesState(block, ranges); + AdjustFormatRangesState adjustRangesState = new AdjustFormatRangesState(block, ranges, ranges.getDiffInfo()); myStateProcessor = new StateProcessor(adjustRangesState); myStateProcessor.setNextState(myWrapState); } diff --git a/platform/lang-impl/src/com/intellij/formatting/FormatTextRanges.java b/platform/lang-impl/src/com/intellij/formatting/FormatTextRanges.java index 19027c1bb121..afd7628e2228 100644 --- a/platform/lang-impl/src/com/intellij/formatting/FormatTextRanges.java +++ b/platform/lang-impl/src/com/intellij/formatting/FormatTextRanges.java @@ -17,7 +17,8 @@ package com.intellij.formatting; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.psi.codeStyle.FormatRangesInfo; +import com.intellij.psi.codeStyle.DiffInfo; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; @@ -25,19 +26,19 @@ import java.util.List; public class FormatTextRanges { private final List myRanges = new ArrayList<>(); - private final FormatRangesInfo myHelper; + private final DiffInfo myDiffInfo; public FormatTextRanges() { - myHelper = null; + myDiffInfo = null; } public FormatTextRanges(TextRange range, boolean processHeadingWhitespace) { - myHelper = null; + myDiffInfo = null; add(range, processHeadingWhitespace); } - public FormatTextRanges(FormatRangesInfo helper) { - myHelper = helper; + public FormatTextRanges(DiffInfo info) { + myDiffInfo = info; } public void add(TextRange range, boolean processHeadingWhitespace) { @@ -84,9 +85,9 @@ public class FormatTextRanges { return "FormatTextRanges{" + StringUtil.join(myRanges, StringUtil.createToStringFunction(FormatTextRange.class), ","); } - public boolean isInsertedBlock(Block block) { - int offset = block.getTextRange().getStartOffset(); - return myHelper != null && myHelper.isOnInsertedLine(offset); + @Nullable + protected DiffInfo getDiffInfo() { + return myDiffInfo; } } \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/psi/formatter/common/AbstractBlock.java b/platform/lang-impl/src/com/intellij/psi/formatter/common/AbstractBlock.java index b793a2e384e8..24311e926c7c 100644 --- a/platform/lang-impl/src/com/intellij/psi/formatter/common/AbstractBlock.java +++ b/platform/lang-impl/src/com/intellij/psi/formatter/common/AbstractBlock.java @@ -25,6 +25,7 @@ import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; +import com.intellij.psi.codeStyle.DiffInfo; import com.intellij.psi.formatter.FormatterUtil; import com.intellij.psi.formatter.IndentRangesCalculator; import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; @@ -187,8 +188,9 @@ public abstract class AbstractBlock implements ASTBlock { * @return additional range to reformat, when this block if formatted */ @Nullable - public List getExtraRangesToFormat(FormatTextRanges ranges) { - if (ranges.isInsertedBlock(this) && myNode.textContains('\n')) { + public List getExtraRangesToFormat(@Nullable DiffInfo info) { + int startOffset = getTextRange().getStartOffset(); + if (info != null && info.isOnInsertedLine(startOffset) && myNode.textContains('\n')) { return calculateExtraRanges(myNode); } return null; diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java index 9b1bb43cb0da..1f31add3e2a1 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java @@ -165,24 +165,20 @@ public class CodeStyleManagerImpl extends CodeStyleManager { } @Override - public void reformatTextWithContext(@NotNull PsiFile file, @NotNull FormatRangesInfo helper) throws IncorrectOperationException { - reformatText(file, helper, null, true); + public void reformatTextWithContext(@NotNull PsiFile file, @NotNull Collection ranges, @Nullable DiffInfo info) throws IncorrectOperationException { + reformatText(file, ranges, info, null, true); } public void reformatText(@NotNull PsiFile file, @NotNull Collection ranges, @Nullable Editor editor) throws IncorrectOperationException { - reformatText(file, ranges, editor, false); + reformatText(file, ranges, null, editor, false); } - public void reformatText(@NotNull PsiFile file, @NotNull Collection ranges, @Nullable Editor editor, boolean reformatContext) throws IncorrectOperationException { - reformatText(file, new SimpleFormatRangesInfo(ranges), editor, reformatContext); - } - private void reformatText(@NotNull PsiFile file, - @NotNull FormatRangesInfo rangesInfo, + @NotNull Collection ranges, + @Nullable DiffInfo diffInfo, @Nullable Editor editor, boolean reformatContext) throws IncorrectOperationException { - List ranges = rangesInfo.getRangesToFormat(); if (ranges.isEmpty()) { return; } @@ -237,7 +233,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager { )); } - FormatTextRanges formatRanges = new FormatTextRanges(rangesInfo); + FormatTextRanges formatRanges = new FormatTextRanges(diffInfo); for (TextRange range : correctedRanges) { formatRanges.add(range, true); } diff --git a/platform/platform-tests/testSrc/com/intellij/codeInsight/actions/MockCodeStyleManager.java b/platform/platform-tests/testSrc/com/intellij/codeInsight/actions/MockCodeStyleManager.java index 5dbf53754d9b..b094414857ca 100644 --- a/platform/platform-tests/testSrc/com/intellij/codeInsight/actions/MockCodeStyleManager.java +++ b/platform/platform-tests/testSrc/com/intellij/codeInsight/actions/MockCodeStyleManager.java @@ -25,7 +25,7 @@ import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleManager; -import com.intellij.psi.codeStyle.FormatRangesInfo; +import com.intellij.psi.codeStyle.DiffInfo; import com.intellij.psi.codeStyle.Indent; import com.intellij.util.IncorrectOperationException; import com.intellij.util.ThrowableRunnable; @@ -73,8 +73,10 @@ public class MockCodeStyleManager extends CodeStyleManager { } @Override - public void reformatTextWithContext(@NotNull PsiFile file, @NotNull FormatRangesInfo helper) throws IncorrectOperationException { - reformatText(file, helper.getRangesToFormat()); + public void reformatTextWithContext(@NotNull PsiFile file, + @NotNull Collection ranges, + @Nullable DiffInfo diffInfo) throws IncorrectOperationException { + reformatText(file, ranges); } @NotNull