diff --git a/java/java-impl/src/com/intellij/psi/formatter/java/AbstractJavaBlock.java b/java/java-impl/src/com/intellij/psi/formatter/java/AbstractJavaBlock.java index bff7c05d00e6..161fa89399c6 100644 --- a/java/java-impl/src/com/intellij/psi/formatter/java/AbstractJavaBlock.java +++ b/java/java-impl/src/com/intellij/psi/formatter/java/AbstractJavaBlock.java @@ -1284,11 +1284,14 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo @Nullable @Override - public ExtraReformatRanges getExtraRangesToFormat() { + public ExtraReformatRanges getExtraRangesToFormat(FormatTextRanges ranges) { if (!Registry.is("smart.reformat.vcs.changes")) return null; - if (myNode instanceof PsiForStatement || myNode instanceof PsiIfStatement) { - return new ExtraReformatRanges(myNode.getTextRange()); + int startOffset = getTextRange().getStartOffset(); + if (ranges.isOnInsertedLine(startOffset)) { + if (myNode instanceof PsiForStatement || myNode instanceof PsiIfStatement) { + return new ExtraReformatRanges(myNode.getTextRange()); + } } return null; 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 c203240abd30..c5bc370dcf4b 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 @@ -34,7 +34,6 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.*; import com.intellij.testFramework.LightIdeaTestCase; import com.intellij.util.IncorrectOperationException; -import com.intellij.util.containers.ContainerUtil; import com.intellij.util.text.LineReader; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -83,7 +82,7 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase { return result.toString(); } - protected enum Action {REFORMAT, INDENT, REFORMAT_WITH_CONTEXT} + protected enum Action {REFORMAT, INDENT, REFORMAT_WITH_CONTEXT, REFORMAT_WITH_INSERTED_LINE_CONTEXT} public static JavaCodeStyleSettings getJavaSettings() { return getSettings().getRootSettings().getCustomSettings(JavaCodeStyleSettings.class); @@ -110,8 +109,20 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase { ACTIONS.put(Action.REFORMAT_WITH_CONTEXT, new TestFormatAction() { @Override public void run(PsiFile psiFile, int startOffset, int endOffset) { - List ranges = ContainerUtil.newArrayList(new TextRange(startOffset, endOffset)); - CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, ranges); + SimpleFormatRangesInfo info = new SimpleFormatRangesInfo(new TextRange(startOffset, endOffset)); + CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, info); + } + }); + ACTIONS.put(Action.REFORMAT_WITH_INSERTED_LINE_CONTEXT, new TestFormatAction() { + @Override + public void run(PsiFile psiFile, int startOffset, int endOffset) { + SimpleFormatRangesInfo info = new SimpleFormatRangesInfo(new TextRange(startOffset, endOffset)) { + @Override + public boolean isOnInsertedLine(int offset) { + return startOffset <= offset && offset < endOffset; + } + }; + CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, info); } }); } diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/java/FormatWithContextTest.kt b/java/java-tests/testSrc/com/intellij/psi/formatter/java/FormatWithContextTest.kt index 29d74a852f51..97689ed4a453 100644 --- a/java/java-tests/testSrc/com/intellij/psi/formatter/java/FormatWithContextTest.kt +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/java/FormatWithContextTest.kt @@ -22,7 +22,7 @@ import com.intellij.openapi.util.registry.Registry class FormatWithContextTest : AbstractJavaFormatterTest() { fun check(before: String, after: String) { - doTextTest(Action.REFORMAT_WITH_CONTEXT, before, after) + doTextTest(Action.REFORMAT_WITH_INSERTED_LINE_CONTEXT, before, after) } override fun setUp() { 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 9f93742199a5..4bfd890cf850 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,6 +22,7 @@ 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; @@ -72,7 +73,7 @@ public class JavaSmartReformatPerformanceTest extends AbstractJavaFormatterTest return () -> CommandProcessor.getInstance().executeCommand( getProject(), () -> ApplicationManager.getApplication().runWriteAction( - () -> codeStyleManager.reformatTextWithContext(file, ranges) + () -> codeStyleManager.reformatTextWithContext(file, new SimpleFormatRangesInfo(ranges)) ), 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 ef0a4b812406..b7700789ad9d 100644 --- a/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java +++ b/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java @@ -152,8 +152,15 @@ public abstract class CodeStyleManager { * @param ranges * @throws IncorrectOperationException */ - public abstract void reformatTextWithContext(@NotNull PsiFile file, @NotNull Collection ranges) throws IncorrectOperationException; + public abstract void reformatTextWithContext(@NotNull PsiFile file, @NotNull FormatRangesInfo ranges) throws IncorrectOperationException; + /** + * @deprecated use {@link #reformatTextWithContext(PsiFile, FormatRangesInfo)} + */ + public void reformatTextWithContext(@NotNull PsiFile file, @NotNull Collection ranges) throws IncorrectOperationException { + reformatTextWithContext(file, new SimpleFormatRangesInfo(ranges)); + } + /** * Re-formats the specified range of a file, modifying only line indents and leaving * all other whitespace intact. diff --git a/platform/core-api/src/com/intellij/psi/codeStyle/FormatRangesInfo.java b/platform/core-api/src/com/intellij/psi/codeStyle/FormatRangesInfo.java new file mode 100644 index 000000000000..4157d406a89a --- /dev/null +++ b/platform/core-api/src/com/intellij/psi/codeStyle/FormatRangesInfo.java @@ -0,0 +1,32 @@ +/* + * 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; + +import com.intellij.openapi.util.TextRange; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public abstract class FormatRangesInfo { + + @NotNull + public abstract List getRangesToFormat(); + + public boolean isOnInsertedLine(int offset) { + return false; + } + +} diff --git a/platform/core-api/src/com/intellij/psi/codeStyle/SimpleFormatRangesInfo.java b/platform/core-api/src/com/intellij/psi/codeStyle/SimpleFormatRangesInfo.java new file mode 100644 index 000000000000..c57a2bbdd22d --- /dev/null +++ b/platform/core-api/src/com/intellij/psi/codeStyle/SimpleFormatRangesInfo.java @@ -0,0 +1,43 @@ +/* + * 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; + +import com.intellij.openapi.util.TextRange; +import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; +import java.util.List; + +public class SimpleFormatRangesInfo extends FormatRangesInfo { + + private final List myRanges; + + public SimpleFormatRangesInfo(@NotNull Collection ranges) { + myRanges = ContainerUtil.newArrayList(ranges); + } + + public SimpleFormatRangesInfo(TextRange range) { + myRanges = ContainerUtil.newArrayList(range); + } + + @NotNull + @Override + public List getRangesToFormat() { + return myRanges; + } + +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/actions/FormatChangedTextUtil.java b/platform/lang-impl/src/com/intellij/codeInsight/actions/FormatChangedTextUtil.java index 9444468f9024..a4156947d282 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/actions/FormatChangedTextUtil.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/actions/FormatChangedTextUtil.java @@ -33,10 +33,12 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; +import com.intellij.psi.codeStyle.FormatRangesInfo; import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.diff.FilesTooBigForDiffException; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Collection; import java.util.List; @@ -154,4 +156,11 @@ public class FormatChangedTextUtil { public boolean isChangeNotTrackedForFile(@NotNull Project project, @NotNull PsiFile file) { return false; } + + + @Nullable + public FormatRangesInfo getChangedTextHelper(@NotNull PsiFile file) throws FilesTooBigForDiffException { + return null; + } + } 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 758f129ee004..6cde4d45241e 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeProcessor.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeProcessor.java @@ -30,6 +30,7 @@ import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleManager; +import com.intellij.psi.codeStyle.FormatRangesInfo; import com.intellij.util.IncorrectOperationException; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.diff.FilesTooBigForDiffException; @@ -41,7 +42,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; public class ReformatCodeProcessor extends AbstractLayoutCodeProcessor { @@ -51,7 +51,7 @@ public class ReformatCodeProcessor extends AbstractLayoutCodeProcessor { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.actions.ReformatCodeProcessor"); private static final String PROGRESS_TEXT = CodeInsightBundle.message("reformat.progress.common.text"); - private final Collection myRanges = new ArrayList(); + private final Collection myRanges = new ArrayList<>(); private SelectionModel mySelectionModel; public ReformatCodeProcessor(Project project, boolean processChangedTextOnly) { @@ -110,11 +110,9 @@ public class ReformatCodeProcessor extends AbstractLayoutCodeProcessor { protected FutureTask prepareTask(@NotNull final PsiFile file, final boolean processChangedTextOnly) throws IncorrectOperationException { - return new FutureTask(() -> { + return new FutureTask<>(() -> { FormattingProgressTask.FORMATTING_CANCELLED_FLAG.set(false); try { - Collection ranges = getRangesToFormat(processChangedTextOnly, file); - CharSequence before = null; Document document = PsiDocumentManager.getInstance(myProject).getDocument(file); if (getInfoCollector() != null) { @@ -125,9 +123,13 @@ public class ReformatCodeProcessor extends AbstractLayoutCodeProcessor { CaretVisualPositionKeeper caretPositionKeeper = new CaretVisualPositionKeeper(document); if (processChangedTextOnly) { - CodeStyleManager.getInstance(myProject).reformatTextWithContext(file, ranges); + FormatRangesInfo helper = FormatChangedTextUtil.getInstance().getChangedTextHelper(file); + if (helper != null) { + CodeStyleManager.getInstance(myProject).reformatTextWithContext(file, helper); + } } else { + Collection ranges = getRangesToFormat(file); CodeStyleManager.getInstance(myProject).reformatText(file, ranges); } @@ -163,20 +165,16 @@ public class ReformatCodeProcessor extends AbstractLayoutCodeProcessor { } @NotNull - private Collection getRangesToFormat(boolean processChangedTextOnly, PsiFile file) throws FilesTooBigForDiffException { + private Collection getRangesToFormat(PsiFile file) { if (mySelectionModel != null) { return getSelectedRanges(mySelectionModel); } - - if (processChangedTextOnly) { - return FormatChangedTextUtil.getInstance().getChangedTextRanges(myProject, file); - } - + return !myRanges.isEmpty() ? myRanges : ContainerUtil.newArrayList(file.getTextRange()); } private static class CaretVisualPositionKeeper { - private final Map myCaretRelativeVerticalPositions = new HashMap(); + private final Map myCaretRelativeVerticalPositions = new HashMap<>(); private CaretVisualPositionKeeper(@Nullable Document document) { if (document == null) return; diff --git a/platform/lang-impl/src/com/intellij/formatting/AdjustFormatRangesState.kt b/platform/lang-impl/src/com/intellij/formatting/AdjustFormatRangesState.kt index 8f06dac54a93..a700c34f5bec 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.FormatRangesInfo import com.intellij.psi.formatter.common.AbstractBlock import com.intellij.util.containers.Stack @@ -55,14 +56,26 @@ interface BlockProcessor { fun processCompositeBlock(block: Block) } -class AdditionalRangesExtractor : BlockProcessor { +class VcsAwareFormatRangesInfo(val formattingRanges: List, + private val insertedRanges: List): FormatRangesInfo() +{ + constructor(changedTextRange: TextRange): this(listOf(changedTextRange), emptyList()) + + override fun getRangesToFormat() = formattingRanges + + override fun isOnInsertedLine(offset: Int) = insertedRanges.find { it.contains(offset) } != null + +} + + +class AdditionalRangesExtractor(private val formatRanges: FormatTextRanges) : BlockProcessor { val extraRanges = mutableListOf() override fun processLeafBlock(block: Block) = Unit override fun processCompositeBlock(block: Block) { if (block is AbstractBlock) { - block.extraRangesToFormat?.let { extraRanges.add(it) } + block.getExtraRangesToFormat(formatRanges)?.let { extraRanges.add(it) } } } } @@ -79,7 +92,7 @@ fun FormatTextRanges.mergeWith(extraRanges: ExtraReformatRanges) { class AdjustFormatRangesState(var currentRoot: Block, val formatRanges: FormatTextRanges) : State() { - private val extractor = AdditionalRangesExtractor() + private val extractor = AdditionalRangesExtractor(formatRanges) private val state = Stack(currentRoot) init { diff --git a/platform/lang-impl/src/com/intellij/formatting/FormatTextRanges.java b/platform/lang-impl/src/com/intellij/formatting/FormatTextRanges.java index 3c6bcedfacaf..aff4a41f0d18 100644 --- a/platform/lang-impl/src/com/intellij/formatting/FormatTextRanges.java +++ b/platform/lang-impl/src/com/intellij/formatting/FormatTextRanges.java @@ -17,6 +17,7 @@ package com.intellij.formatting; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.StringUtil; +import com.intellij.psi.codeStyle.FormatRangesInfo; import java.util.ArrayList; import java.util.List; @@ -24,13 +25,20 @@ import java.util.List; public class FormatTextRanges { private final List myRanges = new ArrayList<>(); + private final FormatRangesInfo myHelper; public FormatTextRanges() { + myHelper = null; } public FormatTextRanges(TextRange range, boolean processHeadingWhitespace) { + myHelper = null; add(range, processHeadingWhitespace); } + + public FormatTextRanges(FormatRangesInfo helper) { + myHelper = helper; + } public void add(TextRange range, boolean processHeadingWhitespace) { myRanges.add(new FormatTextRange(range, processHeadingWhitespace)); @@ -75,4 +83,9 @@ public class FormatTextRanges { public String toString() { return "FormatTextRanges{" + StringUtil.join(myRanges, StringUtil.createToStringFunction(FormatTextRange.class), ","); } + + public boolean isOnInsertedLine(int offset) { + return myHelper != null && myHelper.isOnInsertedLine(offset); + } + } \ 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 e54aa509453f..91853339741a 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 @@ -183,7 +183,7 @@ public abstract class AbstractBlock implements ASTBlock { * @return additional range to reformat, when this block if formatted */ @Nullable - public ExtraReformatRanges getExtraRangesToFormat() { + public ExtraReformatRanges getExtraRangesToFormat(FormatTextRanges ranges) { 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 0855aef076ce..9b1bb43cb0da 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,15 +165,24 @@ public class CodeStyleManagerImpl extends CodeStyleManager { } @Override - public void reformatTextWithContext(@NotNull PsiFile file, @NotNull Collection ranges) throws IncorrectOperationException { - reformatText(file, ranges, null, true); + public void reformatTextWithContext(@NotNull PsiFile file, @NotNull FormatRangesInfo helper) throws IncorrectOperationException { + reformatText(file, helper, null, true); } public void reformatText(@NotNull PsiFile file, @NotNull Collection ranges, @Nullable Editor editor) throws IncorrectOperationException { reformatText(file, ranges, 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, + @Nullable Editor editor, + boolean reformatContext) throws IncorrectOperationException + { + List ranges = rangesInfo.getRangesToFormat(); if (ranges.isEmpty()) { return; } @@ -228,7 +237,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager { )); } - FormatTextRanges formatRanges = new FormatTextRanges(); + FormatTextRanges formatRanges = new FormatTextRanges(rangesInfo); 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 84e218118c61..5dbf53754d9b 100644 --- a/platform/platform-tests/testSrc/com/intellij/codeInsight/actions/MockCodeStyleManager.java +++ b/platform/platform-tests/testSrc/com/intellij/codeInsight/actions/MockCodeStyleManager.java @@ -25,6 +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.Indent; import com.intellij.util.IncorrectOperationException; import com.intellij.util.ThrowableRunnable; @@ -72,8 +73,8 @@ public class MockCodeStyleManager extends CodeStyleManager { } @Override - public void reformatTextWithContext(@NotNull PsiFile file, @NotNull Collection ranges) throws IncorrectOperationException { - reformatText(file, ranges); + public void reformatTextWithContext(@NotNull PsiFile file, @NotNull FormatRangesInfo helper) throws IncorrectOperationException { + reformatText(file, helper.getRangesToFormat()); } @NotNull diff --git a/platform/vcs-impl/src/com/intellij/codeInsight/actions/VcsAwareFormatChangedTextUtil.java b/platform/vcs-impl/src/com/intellij/codeInsight/actions/VcsAwareFormatChangedTextUtil.java index ddee7bf5b6b7..d2773062064d 100644 --- a/platform/vcs-impl/src/com/intellij/codeInsight/actions/VcsAwareFormatChangedTextUtil.java +++ b/platform/vcs-impl/src/com/intellij/codeInsight/actions/VcsAwareFormatChangedTextUtil.java @@ -15,6 +15,7 @@ */ package com.intellij.codeInsight.actions; +import com.intellij.formatting.VcsAwareFormatRangesInfo; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.EditorFactory; @@ -33,8 +34,8 @@ import com.intellij.openapi.vcs.impl.LineStatusTrackerManager; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; +import com.intellij.psi.codeStyle.FormatRangesInfo; import com.intellij.util.containers.ContainerUtil; -import com.intellij.util.containers.ContainerUtilRt; import com.intellij.util.diff.FilesTooBigForDiffException; import com.intellij.vcsUtil.VcsUtil; import org.jetbrains.annotations.NotNull; @@ -43,35 +44,43 @@ import org.jetbrains.annotations.Nullable; import java.util.List; class VcsAwareFormatChangedTextUtil extends FormatChangedTextUtil { + @Override @NotNull public List getChangedTextRanges(@NotNull Project project, @NotNull PsiFile file) throws FilesTooBigForDiffException { - Document document = PsiDocumentManager.getInstance(project).getDocument(file); - if (document == null) return ContainerUtil.emptyList(); + FormatRangesInfo helper = getChangedTextHelper(file); + return helper != null ? helper.getRangesToFormat() : ContainerUtil.newArrayList(); + } - List cachedChangedLines = getCachedChangedLines(project, document); - if (cachedChangedLines != null) { - return cachedChangedLines; + @Override + @Nullable + public FormatRangesInfo getChangedTextHelper(@NotNull PsiFile file) throws FilesTooBigForDiffException { + Project project = file.getProject(); + Document document = PsiDocumentManager.getInstance(project).getDocument(file); + if (document == null) return null; + + FormatRangesInfo cachedChangedTextHelper = getCachedChangedLines(project, document); + if (cachedChangedTextHelper != null) { + return cachedChangedTextHelper; } if (ApplicationManager.getApplication().isUnitTestMode()) { CharSequence testContent = file.getUserData(TEST_REVISION_CONTENT); if (testContent != null) { - return calculateChangedTextRanges(document, testContent); + return calculateContextReformatHelper(document, testContent); } } Change change = ChangeListManager.getInstance(project).getChange(file.getVirtualFile()); if (change == null) { - return ContainerUtilRt.emptyList(); + return null; } if (change.getType() == Change.Type.NEW) { - return ContainerUtil.newArrayList(file.getTextRange()); + return new VcsAwareFormatRangesInfo(file.getTextRange()); } String contentFromVcs = getRevisionedContentFrom(change); - return contentFromVcs != null ? calculateChangedTextRanges(document, contentFromVcs) - : ContainerUtil.emptyList(); + return contentFromVcs != null ? calculateContextReformatHelper(document, contentFromVcs) : null; } @Nullable @@ -91,7 +100,7 @@ class VcsAwareFormatChangedTextUtil extends FormatChangedTextUtil { } @Nullable - private static List getCachedChangedLines(@NotNull Project project, @NotNull Document document) { + private static FormatRangesInfo getCachedChangedLines(@NotNull Project project, @NotNull Document document) { LineStatusTracker tracker = LineStatusTrackerManager.getInstance(project).getLineStatusTracker(document); if (tracker != null) { List ranges = tracker.getRanges(); @@ -103,8 +112,8 @@ class VcsAwareFormatChangedTextUtil extends FormatChangedTextUtil { } @NotNull - protected static List calculateChangedTextRanges(@NotNull Document document, - @NotNull CharSequence contentFromVcs) throws FilesTooBigForDiffException + protected static FormatRangesInfo calculateContextReformatHelper(@NotNull Document document, + @NotNull CharSequence contentFromVcs) throws FilesTooBigForDiffException { return getChangedTextRanges(document, getRanges(document, contentFromVcs)); } @@ -150,8 +159,10 @@ class VcsAwareFormatChangedTextUtil extends FormatChangedTextUtil { } @NotNull - private static List getChangedTextRanges(@NotNull Document document, @NotNull List changedRanges) { + private static FormatRangesInfo getChangedTextRanges(@NotNull Document document, @NotNull List changedRanges) { List ranges = ContainerUtil.newArrayList(); + List insertedRanges = ContainerUtil.newArrayList(); + for (Range range : changedRanges) { if (range.getType() != Range.DELETED) { int changeStartLine = range.getLine1(); @@ -160,10 +171,14 @@ class VcsAwareFormatChangedTextUtil extends FormatChangedTextUtil { int lineStartOffset = document.getLineStartOffset(changeStartLine); int lineEndOffset = document.getLineEndOffset(changeEndLine - 1); - ranges.add(new TextRange(lineStartOffset, lineEndOffset)); + TextRange changedTextRange = new TextRange(lineStartOffset, lineEndOffset); + ranges.add(changedTextRange); + if (range.getType() == Range.INSERTED) { + insertedRanges.add(changedTextRange); + } } } - return ranges; + return new VcsAwareFormatRangesInfo(ranges, insertedRanges); } @Override @@ -179,4 +194,5 @@ class VcsAwareFormatChangedTextUtil extends FormatChangedTextUtil { return false; } + }