diff --git a/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java b/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java index 82ae92ac1a30..0e4128ead0c4 100644 --- a/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java +++ b/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java @@ -48,7 +48,7 @@ public class FormatProcessor { @Nullable FormatTextRanges affectedRanges, @NotNull FormattingProgressCallback progressCallback) { - this(docModel, rootBlock, new FormatOptions(settings, indentOptions, affectedRanges, false), progressCallback); + this(docModel, rootBlock, new FormatOptions(settings, indentOptions, affectedRanges), progressCallback); } public FormatProcessor(final FormattingDocumentModel model, @@ -63,7 +63,7 @@ public class FormatProcessor { BlockIndentOptions blockIndentOptions = new BlockIndentOptions(settings, defaultIndentOption, block); myDocument = model.getDocument(); - myReformatContext = options.myReformatContext; + myReformatContext = options.isReformatWithContext(); final InitialInfoBuilder builder = prepareToBuildBlocksSequentially(block, model, options, settings, defaultIndentOption, myProgressCallback); myWrapState = new WrapBlocksState(builder, blockIndentOptions); @@ -391,27 +391,27 @@ public class FormatProcessor { public CommonCodeStyleSettings.IndentOptions myIndentOptions; public FormatTextRanges myAffectedRanges; - public boolean myReformatContext; public int myInterestingOffset; public FormatOptions(CodeStyleSettings settings, CommonCodeStyleSettings.IndentOptions options, - FormatTextRanges ranges, - boolean reformatContext) { - this(settings, options, ranges, reformatContext, -1); + FormatTextRanges ranges) { + this(settings, options, ranges, -1); } public FormatOptions(CodeStyleSettings settings, CommonCodeStyleSettings.IndentOptions options, FormatTextRanges ranges, - boolean reformatContext, int interestingOffset) { mySettings = settings; myIndentOptions = options; myAffectedRanges = ranges; - myReformatContext = reformatContext; myInterestingOffset = interestingOffset; } + + public boolean isReformatWithContext() { + return myAffectedRanges != null && myAffectedRanges.isExtendToContext(); + } } } diff --git a/platform/lang-impl/src/com/intellij/formatting/FormatTextRanges.java b/platform/lang-impl/src/com/intellij/formatting/FormatTextRanges.java index 95427615b17d..73919f887957 100644 --- a/platform/lang-impl/src/com/intellij/formatting/FormatTextRanges.java +++ b/platform/lang-impl/src/com/intellij/formatting/FormatTextRanges.java @@ -32,6 +32,8 @@ public class FormatTextRanges implements FormattingRangesInfo { private final List myRanges = new ArrayList<>(); private @Nullable FormattingRangesExtender myRangesExtender; + private boolean myExtendToContext; + public FormatTextRanges() { myInsertedRanges = null; } @@ -144,5 +146,12 @@ public class FormatTextRanges implements FormattingRangesInfo { new TextRange(ranges.get(0).getStartOffset(), ranges.get(ranges.size() - 1).getEndOffset()) : null; } - + + public boolean isExtendToContext() { + return myExtendToContext; + } + + public void setExtendToContext(boolean extendToContext) { + myExtendToContext = extendToContext; + } } \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/formatting/FormatterEx.java b/platform/lang-impl/src/com/intellij/formatting/FormatterEx.java index 1b52c349fafb..24810a5a43a9 100644 --- a/platform/lang-impl/src/com/intellij/formatting/FormatterEx.java +++ b/platform/lang-impl/src/com/intellij/formatting/FormatterEx.java @@ -36,12 +36,6 @@ public abstract class FormatterEx implements Formatter { CommonCodeStyleSettings.IndentOptions indentOptions, FormatTextRanges affectedRanges) throws IncorrectOperationException; - public abstract void format(final FormattingModel model, - final CodeStyleSettings settings, - final CommonCodeStyleSettings.IndentOptions indentOptions, - final FormatTextRanges affectedRanges, - final boolean formatContextAroundRanges) throws IncorrectOperationException; - public abstract int adjustLineIndent(final FormattingModel psiBasedFormattingModel, final CodeStyleSettings settings, final CommonCodeStyleSettings.IndentOptions indentOptions, diff --git a/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java b/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java index ea6fcfa36150..ec3f9e96145e 100644 --- a/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java +++ b/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java @@ -222,22 +222,13 @@ public class FormatterImpl extends FormatterEx final CodeStyleSettings settings, final CommonCodeStyleSettings.IndentOptions indentOptions, final FormatTextRanges affectedRanges) throws IncorrectOperationException { - format(model, settings, indentOptions, affectedRanges, false); - } - - @Override - public void format(final FormattingModel model, - final CodeStyleSettings settings, - final CommonCodeStyleSettings.IndentOptions indentOptions, - final FormatTextRanges affectedRanges, - final boolean formatContextAroundRanges) throws IncorrectOperationException { - try { + try { validateModel(model); SequentialTask task = new MyFormattingTask() { @NotNull @Override protected FormatProcessor buildProcessor() { - FormatOptions options = new FormatOptions(settings, indentOptions, affectedRanges, formatContextAroundRanges); + FormatOptions options = new FormatOptions(settings, indentOptions, affectedRanges); FormatProcessor processor = new FormatProcessor( model.getDocumentModel(), model.getRootBlock(), options, getProgressCallback() ); @@ -429,7 +420,7 @@ public class FormatterImpl extends FormatterEx @Nullable FormatTextRanges affectedRanges, int interestingOffset) { - FormatOptions options = new FormatOptions(settings, indentOptions, affectedRanges, false, interestingOffset); + FormatOptions options = new FormatOptions(settings, indentOptions, affectedRanges, interestingOffset); FormatProcessor processor = new FormatProcessor( docModel, rootBlock, options, FormattingProgressCallback.EMPTY ); diff --git a/platform/lang-impl/src/com/intellij/formatting/InitialInfoBuilder.java b/platform/lang-impl/src/com/intellij/formatting/InitialInfoBuilder.java index 61035f97519d..84f14bdbf00d 100644 --- a/platform/lang-impl/src/com/intellij/formatting/InitialInfoBuilder.java +++ b/platform/lang-impl/src/com/intellij/formatting/InitialInfoBuilder.java @@ -85,7 +85,7 @@ public class InitialInfoBuilder { CommonCodeStyleSettings.IndentOptions options, @NotNull FormattingProgressCallback progressCallback) { InitialInfoBuilder builder = new InitialInfoBuilder(root, model, formatOptions.myAffectedRanges, settings, options, formatOptions.myInterestingOffset, progressCallback); - builder.setCollectAlignmentsInsideFormattingRange(formatOptions.myReformatContext); + builder.setCollectAlignmentsInsideFormattingRange(formatOptions.isReformatWithContext()); builder.buildFrom(root, 0, null, null, null); return builder; } diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/PostprocessReformattingAspect.java b/platform/lang-impl/src/com/intellij/psi/impl/source/PostprocessReformattingAspect.java index a9fb63eb18bb..a532114eb99e 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/PostprocessReformattingAspect.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/PostprocessReformattingAspect.java @@ -521,6 +521,7 @@ public class PostprocessReformattingAspect implements PomModelAspect { List result = new ArrayList<>(); if (!freeFormattingActions.isEmpty()) { FormatTextRanges ranges = new FormatTextRanges(); + ranges.setExtendToContext(true); for (PostprocessFormattingTask action : freeFormattingActions) { TextRange range = TextRange.create(action); ranges.add(range, action instanceof ReformatWithHeadingWhitespaceTask); @@ -775,12 +776,12 @@ public class PostprocessReformattingAspect implements PomModelAspect { public void execute(@NotNull FileViewProvider viewProvider) { final PsiFile file = viewProvider.getPsi(viewProvider.getBaseLanguage()); final FormatTextRanges textRanges = myRanges.ensureNonEmpty(); + textRanges.setExtendToContext(true); if (ExternalFormatProcessor.useExternalFormatter(file)) { CodeStyleManagerImpl.formatRanges(file, myRanges, null); } else { final CodeFormatterFacade codeFormatter = getFormatterFacade(viewProvider); - codeFormatter.setReformatContext(true); codeFormatter.processText(file, textRanges, false); } } diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeFormatterFacade.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeFormatterFacade.java index fcadde23914f..8d9c9f1c9357 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeFormatterFacade.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeFormatterFacade.java @@ -67,7 +67,6 @@ public class CodeFormatterFacade { private final FormatterTagHandler myTagHandler; private final int myRightMargin; private final boolean myCanChangeWhitespaceOnly; - private boolean myReformatContext; public CodeFormatterFacade(CodeStyleSettings settings, @Nullable Language language) { this(settings, language, false); @@ -82,10 +81,6 @@ public class CodeFormatterFacade { myCanChangeWhitespaceOnly = canChangeWhitespaceOnly; } - public void setReformatContext(boolean value) { - myReformatContext = value; - } - public ASTNode processElement(ASTNode element) { TextRange range = element.getTextRange(); return processRange(element, range.getStartOffset(), range.getEndOffset()); @@ -207,7 +202,7 @@ public class CodeFormatterFacade { CommonCodeStyleSettings.IndentOptions indentOptions = mySettings.getIndentOptionsByFile(file, textRanges.size() == 1 ? textRanges.get(0).getTextRange() : null); - formatter.format(model, mySettings, indentOptions, ranges, myReformatContext); + formatter.format(model, mySettings, indentOptions, ranges); for (FormatTextRange range : textRanges) { TextRange textRange = range.getTextRange(); wrapLongLinesIfNecessary(file, document, textRange.getStartOffset(), textRange.getEndOffset()); 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 a8a36e4287a1..98d65cf1feff 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,19 +165,19 @@ public class CodeStyleManagerImpl extends CodeStyleManager implements Formatting @NotNull ChangedRangesInfo info) throws IncorrectOperationException { FormatTextRanges formatRanges = new FormatTextRanges(info); - reformatText(file, formatRanges, null, true); + formatRanges.setExtendToContext(true); + reformatText(file, formatRanges, null); } public void reformatText(@NotNull PsiFile file, @NotNull Collection ranges, @Nullable Editor editor) throws IncorrectOperationException { FormatTextRanges formatRanges = new FormatTextRanges(); ranges.forEach((range) -> formatRanges.add(range, true)); - reformatText(file, formatRanges, editor, false); + reformatText(file, formatRanges, editor); } private void reformatText(@NotNull PsiFile file, @NotNull FormatTextRanges ranges, - @Nullable Editor editor, - boolean reformatContext) throws IncorrectOperationException + @Nullable Editor editor) throws IncorrectOperationException { if (ranges.isEmpty()) { return; @@ -214,7 +214,6 @@ public class CodeStyleManagerImpl extends CodeStyleManager implements Formatting ? null // do nothing, delegate the external formatting activity to post-processor : () -> { final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings(file), file.getLanguage()); - codeFormatter.setReformatContext(reformatContext); codeFormatter.processText(file, ranges, true); });