Make context extension a property of FormatTextRanges

GitOrigin-RevId: 2f17e9430d190f52bc996dfbfa478e8b12f9a079
This commit is contained in:
Rustam Vishnyakov
2019-09-10 15:02:35 +00:00
committed by intellij-monorepo-bot
parent d71531efae
commit 1d3e269789
8 changed files with 29 additions and 40 deletions
@@ -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();
}
}
}
@@ -32,6 +32,8 @@ public class FormatTextRanges implements FormattingRangesInfo {
private final List<FormatTextRange> 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;
}
}
@@ -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,
@@ -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
);
@@ -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;
}
@@ -521,6 +521,7 @@ public class PostprocessReformattingAspect implements PomModelAspect {
List<PostponedAction> 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);
}
}
@@ -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());
@@ -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<? extends TextRange> 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);
});