[formatter] simplification: removed unnecessary classes

This commit is contained in:
Yaroslav Lepenkin
2016-07-15 16:15:01 +03:00
parent 1c25166cee
commit f21e71bd56
9 changed files with 36 additions and 63 deletions
@@ -43,7 +43,6 @@ import org.jetbrains.annotations.Nullable;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
@@ -111,16 +110,15 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase {
ACTIONS.put(Action.REFORMAT_WITH_CONTEXT, new TestFormatAction() {
@Override
public void run(PsiFile psiFile, int startOffset, int endOffset) {
Collection<TextRange> ranges = ContainerUtil.newArrayList(new TextRange(startOffset, endOffset));
CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, ranges, null);
List<TextRange> ranges = ContainerUtil.newArrayList(new TextRange(startOffset, endOffset));
CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, ranges);
}
});
ACTIONS.put(Action.REFORMAT_WITH_INSERTED_LINE_CONTEXT, new TestFormatAction() {
@Override
public void run(PsiFile psiFile, int startOffset, int endOffset) {
TextRange range = new TextRange(startOffset, endOffset);
List<TextRange> ranges = ContainerUtil.newArrayList(range);
CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, ranges, ranges);
List<TextRange> ranges = ContainerUtil.newArrayList(new TextRange(startOffset, endOffset));
CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, new ChangedRangesInfo(ranges, ranges));
}
});
}
@@ -72,7 +72,7 @@ public class JavaSmartReformatPerformanceTest extends AbstractJavaFormatterTest
return () -> CommandProcessor.getInstance().executeCommand(
getProject(),
() -> ApplicationManager.getApplication().runWriteAction(
() -> codeStyleManager.reformatTextWithContext(file, ranges, null)
() -> codeStyleManager.reformatTextWithContext(file, ranges)
),
null,
null);
@@ -27,10 +27,12 @@ import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ThrowableRunnable;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.List;
/**
* Service for reformatting code fragments, getting names for elements
@@ -145,20 +147,11 @@ public abstract class CodeStyleManager {
*/
public abstract void reformatText(@NotNull PsiFile file, @NotNull Collection<TextRange> ranges) throws IncorrectOperationException;
/**
* Works as #reformatText, but reformats not only specified ranges, but also some context around to make code look consistent
* @param file
* @param ranges
* @throws IncorrectOperationException
*/
public abstract void reformatTextWithContext(@NotNull PsiFile file,
@NotNull Collection<TextRange> ranges,
@Nullable Collection<TextRange> insertedRanges) throws IncorrectOperationException;
public abstract void reformatTextWithContext(@NotNull PsiFile file, @NotNull ChangedRangesInfo info) throws IncorrectOperationException;
public void reformatTextWithContext(@NotNull PsiFile file, @NotNull Collection<TextRange> ranges) throws IncorrectOperationException {
reformatTextWithContext(file, ranges, null);
List<TextRange> rangesList = ContainerUtil.newArrayList(ranges);
reformatTextWithContext(file, new ChangedRangesInfo(rangesList, null));
}
/**
@@ -1,20 +0,0 @@
/*
* 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);
}
@@ -125,7 +125,7 @@ public class ReformatCodeProcessor extends AbstractLayoutCodeProcessor {
if (processChangedTextOnly) {
ChangedRangesInfo info = FormatChangedTextUtil.getInstance().getChangedRangesInfo(file);
if (info != null) {
CodeStyleManager.getInstance(myProject).reformatTextWithContext(file, info.allChangedRanges, info.insertedRanges);
CodeStyleManager.getInstance(myProject).reformatTextWithContext(file, info);
}
}
else {
@@ -47,7 +47,6 @@ 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.formatter.common.AbstractBlock
import com.intellij.util.containers.Stack
@@ -55,11 +54,7 @@ interface BlockProcessor {
fun processLeafBlock(block: Block)
fun processCompositeBlock(block: Block)
}
class DiffInfoImpl(private val insertedRanges: Collection<TextRange>): DiffInfo {
override fun isOnInsertedLine(offset: Int) = insertedRanges.find { it.contains(offset) } != null
}
class AdditionalRangesExtractor(private val info: FormattingRangesInfo) : BlockProcessor {
val totalNewRanges = mutableListOf<TextRange>()
@@ -18,27 +18,30 @@ package com.intellij.formatting;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.DiffInfo;
import com.intellij.psi.codeStyle.ChangedRangesInfo;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class FormatTextRanges implements FormattingRangesInfo {
private final List<TextRange> myInsertedRanges;
private final List<FormatTextRange> myRanges = new ArrayList<>();
private final DiffInfo myDiffInfo;
public FormatTextRanges() {
myDiffInfo = null;
myInsertedRanges = null;
}
public FormatTextRanges(TextRange range, boolean processHeadingWhitespace) {
myDiffInfo = null;
myInsertedRanges = null;
add(range, processHeadingWhitespace);
}
public FormatTextRanges(DiffInfo info) {
myDiffInfo = info;
public FormatTextRanges(@NotNull ChangedRangesInfo changedRangesInfo) {
changedRangesInfo.allChangedRanges.forEach((range) -> add(range, true));
myInsertedRanges = changedRangesInfo.insertedRanges;
}
public void add(TextRange range, boolean processHeadingWhitespace) {
@@ -67,7 +70,13 @@ public class FormatTextRanges implements FormattingRangesInfo {
@Override
public boolean isOnInsertedLine(int offset) {
return myDiffInfo != null && myDiffInfo.isOnInsertedLine(offset);
if (myInsertedRanges == null) return false;
Optional<TextRange> enclosingRange = myInsertedRanges.stream()
.filter((range) -> range.contains(offset))
.findAny();
return enclosingRange.isPresent();
}
public List<FormatTextRange> getRanges() {
@@ -103,4 +112,5 @@ public class FormatTextRanges implements FormattingRangesInfo {
public List<TextRange> getTextRanges() {
return myRanges.stream().map(FormatTextRange::getTextRange).collect(Collectors.toList());
}
}
@@ -165,17 +165,14 @@ public class CodeStyleManagerImpl extends CodeStyleManager {
@Override
public void reformatTextWithContext(@NotNull PsiFile file,
@NotNull Collection<TextRange> ranges,
@Nullable Collection<TextRange> insertedRanges) throws IncorrectOperationException
@NotNull ChangedRangesInfo info) throws IncorrectOperationException
{
DiffInfo diffInfo = insertedRanges != null ? new DiffInfoImpl(insertedRanges) : null;
FormatTextRanges formatRanges = new FormatTextRanges(diffInfo);
ranges.forEach((range) -> formatRanges.add(range, true));
FormatTextRanges formatRanges = new FormatTextRanges(info);
reformatText(file, formatRanges, null, true);
}
public void reformatText(@NotNull PsiFile file, @NotNull Collection<TextRange> ranges, @Nullable Editor editor) throws IncorrectOperationException {
FormatTextRanges formatRanges = new FormatTextRanges(null);
FormatTextRanges formatRanges = new FormatTextRanges();
ranges.forEach((range) -> formatRanges.add(range, true));
reformatText(file, formatRanges, editor, false);
}
@@ -24,6 +24,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.ChangedRangesInfo;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.Indent;
import com.intellij.util.IncorrectOperationException;
@@ -73,9 +74,8 @@ public class MockCodeStyleManager extends CodeStyleManager {
@Override
public void reformatTextWithContext(@NotNull PsiFile file,
@NotNull Collection<TextRange> ranges,
@Nullable Collection<TextRange> insertedRanges) throws IncorrectOperationException {
reformatText(file, ranges);
@NotNull ChangedRangesInfo ranges) throws IncorrectOperationException {
reformatText(file, ranges.allChangedRanges);
}
@NotNull