AbstractJavaBlock, refacroting: extracted SyntheticBlockBuilder

This commit is contained in:
Yaroslav Lepenkin
2014-11-28 13:10:43 +02:00
parent 678336b73e
commit d56a4cc970
2 changed files with 77 additions and 28 deletions
@@ -709,33 +709,6 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
return new ChainMethodCallsBlockBuilder(alignment, blockWrap, indent).build(nodes);
}
@NotNull
private Block createSyntheticBlock(@NotNull final List<ASTNode> subNodes, final Wrap wrap, @Nullable final Alignment alignment) {
final ArrayList<Block> subBlocks = new ArrayList<Block>();
final ASTNode firstNode = subNodes.get(0);
if (firstNode.getElementType() == JavaTokenType.DOT) {
subBlocks.add(createJavaBlock(firstNode, getSettings(), myJavaSettings, Indent.getNoneIndent(), null, AlignmentStrategy.getNullStrategy()));
subNodes.remove(0);
if (!subNodes.isEmpty()) {
subBlocks.add(createSyntheticBlock(subNodes, wrap, null));
}
return new SyntheticCodeBlock(subBlocks, alignment, mySettings,
myJavaSettings, Indent.getContinuationIndent(myIndentSettings.USE_RELATIVE_INDENTS), wrap);
}
return new SyntheticCodeBlock(createJavaBlocks(subNodes), alignment, mySettings,
myJavaSettings, Indent.getContinuationWithoutFirstIndent(myIndentSettings.USE_RELATIVE_INDENTS), null);
}
@NotNull
private List<Block> createJavaBlocks(@NotNull final List<ASTNode> subNodes) {
final ArrayList<Block> result = new ArrayList<Block>();
for (ASTNode node : subNodes) {
result.add(createJavaBlock(node, getSettings(), myJavaSettings, Indent.getContinuationWithoutFirstIndent(myIndentSettings.USE_RELATIVE_INDENTS), null,
AlignmentStrategy.getNullStrategy()));
}
return result;
}
private static void collectNodes(@NotNull List<ASTNode> nodes, @NotNull ASTNode node) {
ASTNode child = node.getFirstChildNode();
while (child != null) {
@@ -1384,7 +1357,8 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
myWrap = getNewWrap();
}
blocks.add(createSyntheticBlock(currentCallChunk.nodes, wrapToUse, alignmentToUse));
SyntheticBlockBuilder builder = new SyntheticBlockBuilder(mySettings, myJavaSettings);
blocks.add(builder.create(currentCallChunk.nodes, wrapToUse, alignmentToUse));
}
return blocks;
@@ -0,0 +1,75 @@
/*
* Copyright 2000-2014 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.formatter.java;
import com.intellij.formatting.Alignment;
import com.intellij.formatting.Block;
import com.intellij.formatting.Indent;
import com.intellij.formatting.Wrap;
import com.intellij.formatting.alignment.AlignmentStrategy;
import com.intellij.lang.ASTNode;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
import com.intellij.psi.formatter.java.SyntheticCodeBlock;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import static com.intellij.psi.formatter.java.AbstractJavaBlock.*;
public class SyntheticBlockBuilder {
private final CommonCodeStyleSettings mySettings;
private final CommonCodeStyleSettings.IndentOptions myIndentSettings;
private final JavaCodeStyleSettings myJavaSettings;
public SyntheticBlockBuilder(@NotNull CommonCodeStyleSettings settings, @NotNull JavaCodeStyleSettings javaSettings) {
mySettings = settings;
myIndentSettings = settings.getIndentOptions();
myJavaSettings = javaSettings;
}
@NotNull
public Block create(@NotNull final List<ASTNode> subNodes, final Wrap wrap, @Nullable final Alignment alignment) {
final ArrayList<Block> subBlocks = new ArrayList<Block>();
final ASTNode firstNode = subNodes.get(0);
if (firstNode.getElementType() == JavaTokenType.DOT) {
AlignmentStrategy strategy = AlignmentStrategy.getNullStrategy();
Block block = createJavaBlock(firstNode, mySettings, myJavaSettings, Indent.getNoneIndent(), null, strategy);
subBlocks.add(block);
subNodes.remove(0);
if (!subNodes.isEmpty()) {
subBlocks.add(create(subNodes, wrap, null));
}
return new SyntheticCodeBlock(subBlocks, alignment, mySettings, myJavaSettings, Indent.getContinuationIndent(myIndentSettings.USE_RELATIVE_INDENTS), wrap);
}
return new SyntheticCodeBlock(createJavaBlocks(subNodes), alignment, mySettings, myJavaSettings, Indent.getContinuationWithoutFirstIndent(myIndentSettings.USE_RELATIVE_INDENTS), null);
}
@NotNull
private List<Block> createJavaBlocks(@NotNull final List<ASTNode> subNodes) {
final ArrayList<Block> result = new ArrayList<Block>();
for (ASTNode node : subNodes) {
Indent indent = Indent.getContinuationWithoutFirstIndent(myIndentSettings.USE_RELATIVE_INDENTS);
result.add(createJavaBlock(node, mySettings, myJavaSettings, indent, null, AlignmentStrategy.getNullStrategy()));
}
return result;
}
}