Refactoring: class rename with minor code enhancements
This commit is contained in:
@@ -44,7 +44,7 @@ import static org.jetbrains.jet.lexer.JetTokens.*;
|
|||||||
*/
|
*/
|
||||||
public class JetBlock extends AbstractBlock {
|
public class JetBlock extends AbstractBlock {
|
||||||
private static final int KDOC_COMMENT_INDENT = 1;
|
private static final int KDOC_COMMENT_INDENT = 1;
|
||||||
private final ASTAlignmentStrategy myAlignmentStrategy;
|
private final NodeAlignmentStrategy myAlignmentStrategy;
|
||||||
private final Indent myIndent;
|
private final Indent myIndent;
|
||||||
private final CodeStyleSettings mySettings;
|
private final CodeStyleSettings mySettings;
|
||||||
private final KotlinSpacingBuilder mySpacingBuilder;
|
private final KotlinSpacingBuilder mySpacingBuilder;
|
||||||
@@ -58,14 +58,14 @@ public class JetBlock extends AbstractBlock {
|
|||||||
|
|
||||||
// private static final List<IndentWhitespaceRule>
|
// private static final List<IndentWhitespaceRule>
|
||||||
|
|
||||||
public JetBlock(@NotNull ASTNode node,
|
public JetBlock(
|
||||||
ASTAlignmentStrategy alignmentStrategy,
|
@NotNull ASTNode node,
|
||||||
|
NodeAlignmentStrategy alignmentStrategy,
|
||||||
Indent indent,
|
Indent indent,
|
||||||
Wrap wrap,
|
Wrap wrap,
|
||||||
CodeStyleSettings settings,
|
CodeStyleSettings settings,
|
||||||
KotlinSpacingBuilder spacingBuilder
|
KotlinSpacingBuilder spacingBuilder
|
||||||
) {
|
) {
|
||||||
|
|
||||||
super(node, wrap, alignmentStrategy.getAlignment(node));
|
super(node, wrap, alignmentStrategy.getAlignment(node));
|
||||||
myAlignmentStrategy = alignmentStrategy;
|
myAlignmentStrategy = alignmentStrategy;
|
||||||
myIndent = indent;
|
myIndent = indent;
|
||||||
@@ -89,7 +89,7 @@ public class JetBlock extends AbstractBlock {
|
|||||||
private List<Block> buildSubBlocks() {
|
private List<Block> buildSubBlocks() {
|
||||||
List<Block> blocks = new ArrayList<Block>();
|
List<Block> blocks = new ArrayList<Block>();
|
||||||
|
|
||||||
ASTAlignmentStrategy childrenAlignmentStrategy = getChildrenAlignmentStrategy();
|
NodeAlignmentStrategy childrenAlignmentStrategy = getChildrenAlignmentStrategy();
|
||||||
|
|
||||||
for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) {
|
for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) {
|
||||||
IElementType childType = child.getElementType();
|
IElementType childType = child.getElementType();
|
||||||
@@ -106,18 +106,16 @@ public class JetBlock extends AbstractBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private Block buildSubBlock(@NotNull ASTNode child, ASTAlignmentStrategy alignmentStrategy) {
|
private Block buildSubBlock(@NotNull ASTNode child, NodeAlignmentStrategy alignmentStrategy) {
|
||||||
Wrap wrap = null;
|
// Skip one sub-level for operators, so type of block node is an element type of operator
|
||||||
|
|
||||||
// Affects to spaces around operators...
|
|
||||||
if (child.getElementType() == OPERATION_REFERENCE) {
|
if (child.getElementType() == OPERATION_REFERENCE) {
|
||||||
ASTNode operationNode = child.getFirstChildNode();
|
ASTNode operationNode = child.getFirstChildNode();
|
||||||
if (operationNode != null) {
|
if (operationNode != null) {
|
||||||
return new JetBlock(operationNode, alignmentStrategy, Indent.getNoneIndent(), wrap, mySettings, mySpacingBuilder);
|
return new JetBlock(operationNode, alignmentStrategy, Indent.getNoneIndent(), null, mySettings, mySpacingBuilder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new JetBlock(child, alignmentStrategy, createChildIndent(child), wrap, mySettings, mySpacingBuilder);
|
return new JetBlock(child, alignmentStrategy, createChildIndent(child), null, mySettings, mySpacingBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Indent indentIfNotBrace(@NotNull ASTNode child) {
|
private static Indent indentIfNotBrace(@NotNull ASTNode child) {
|
||||||
@@ -185,12 +183,12 @@ public class JetBlock extends AbstractBlock {
|
|||||||
return myNode.getFirstChildNode() == null;
|
return myNode.getFirstChildNode() == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ASTAlignmentStrategy getChildrenAlignmentStrategy() {
|
private NodeAlignmentStrategy getChildrenAlignmentStrategy() {
|
||||||
CommonCodeStyleSettings jetCommonSettings = mySettings.getCommonSettings(JetLanguage.INSTANCE);
|
CommonCodeStyleSettings jetCommonSettings = mySettings.getCommonSettings(JetLanguage.INSTANCE);
|
||||||
JetCodeStyleSettings jetSettings = mySettings.getCustomSettings(JetCodeStyleSettings.class);
|
JetCodeStyleSettings jetSettings = mySettings.getCustomSettings(JetCodeStyleSettings.class);
|
||||||
|
|
||||||
// Prepare default null strategy
|
// Prepare default null strategy
|
||||||
ASTAlignmentStrategy strategy = myAlignmentStrategy;
|
NodeAlignmentStrategy strategy = myAlignmentStrategy;
|
||||||
|
|
||||||
// Redefine list of strategies for some special elements
|
// Redefine list of strategies for some special elements
|
||||||
IElementType parentType = myNode.getElementType();
|
IElementType parentType = myNode.getElementType();
|
||||||
@@ -210,26 +208,26 @@ public class JetBlock extends AbstractBlock {
|
|||||||
return strategy;
|
return strategy;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ASTAlignmentStrategy getAlignmentForChildInParenthesis(
|
private static NodeAlignmentStrategy getAlignmentForChildInParenthesis(
|
||||||
boolean shouldAlignChild, final IElementType parameter, final IElementType delimiter,
|
boolean shouldAlignChild, final IElementType parameter, final IElementType delimiter,
|
||||||
boolean shouldAlignParenthesis, final IElementType openParenth, final IElementType closeParenth
|
boolean shouldAlignParenthesis, final IElementType openBracket, final IElementType closeBracket
|
||||||
) {
|
) {
|
||||||
// TODO: Check this approach in other situations and refactor
|
|
||||||
final Alignment parameterAlignment = shouldAlignChild ? Alignment.createAlignment() : null;
|
final Alignment parameterAlignment = shouldAlignChild ? Alignment.createAlignment() : null;
|
||||||
final Alignment parenthesisAlignment = shouldAlignParenthesis ? Alignment.createAlignment() : null;
|
final Alignment bracketsAlignment = shouldAlignParenthesis ? Alignment.createAlignment() : null;
|
||||||
|
|
||||||
return new ASTAlignmentStrategy() {
|
return new NodeAlignmentStrategy() {
|
||||||
@Override
|
@Override
|
||||||
public Alignment getAlignment(@NotNull ASTNode node) {
|
public Alignment getAlignment(@NotNull ASTNode node) {
|
||||||
IElementType childNodeType = node.getElementType();
|
IElementType childNodeType = node.getElementType();
|
||||||
|
|
||||||
ASTNode prev = getPrevWithoutWhitespace(node);
|
ASTNode prev = getPrevWithoutWhitespace(node);
|
||||||
if ((prev != null && prev.getElementType() == TokenType.ERROR_ELEMENT) || childNodeType == TokenType.ERROR_ELEMENT) {
|
if ((prev != null && prev.getElementType() == TokenType.ERROR_ELEMENT) || childNodeType == TokenType.ERROR_ELEMENT) {
|
||||||
|
// Prefer align to parameters on incomplete code (case of line break after comma, when next parameters is absent)
|
||||||
return parameterAlignment;
|
return parameterAlignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (childNodeType == openParenth || childNodeType == closeParenth) {
|
if (childNodeType == openBracket || childNodeType == closeBracket) {
|
||||||
return parenthesisAlignment;
|
return bracketsAlignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (childNodeType == parameter || childNodeType == delimiter) {
|
if (childNodeType == parameter || childNodeType == delimiter) {
|
||||||
@@ -241,13 +239,13 @@ public class JetBlock extends AbstractBlock {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ASTAlignmentStrategy getAlignmentForCaseBranch(boolean shouldAlignInColumns) {
|
private static NodeAlignmentStrategy getAlignmentForCaseBranch(boolean shouldAlignInColumns) {
|
||||||
if (shouldAlignInColumns) {
|
if (shouldAlignInColumns) {
|
||||||
return ASTAlignmentStrategy
|
return NodeAlignmentStrategy.fromTypes(
|
||||||
.fromTypes(AlignmentStrategy.createAlignmentPerTypeStrategy(Arrays.asList((IElementType) ARROW), WHEN_ENTRY, true));
|
AlignmentStrategy.createAlignmentPerTypeStrategy(Arrays.asList((IElementType) ARROW), WHEN_ENTRY, true));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return ASTAlignmentStrategy.getNullStrategy();
|
return NodeAlignmentStrategy.getNullStrategy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2013 JetBrains s.r.o.
|
* Copyright 2010-2014 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -34,7 +34,7 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder {
|
|||||||
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
|
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
|
||||||
PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(JetLanguage.INSTANCE);
|
PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(JetLanguage.INSTANCE);
|
||||||
JetBlock block = new JetBlock(
|
JetBlock block = new JetBlock(
|
||||||
containingFile.getNode(), ASTAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings,
|
containingFile.getNode(), NodeAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings,
|
||||||
FormatterPackage.createSpacingBuilder(settings));
|
FormatterPackage.createSpacingBuilder(settings));
|
||||||
|
|
||||||
return FormattingModelProvider.createFormattingModelForPsiFile(
|
return FormattingModelProvider.createFormattingModelForPsiFile(
|
||||||
|
|||||||
+6
-6
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2013 JetBrains s.r.o.
|
* Copyright 2010-2014 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -22,23 +22,23 @@ import com.intellij.lang.ASTNode;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
public abstract class ASTAlignmentStrategy {
|
public abstract class NodeAlignmentStrategy {
|
||||||
|
|
||||||
private static final ASTAlignmentStrategy NULL_STRATEGY = fromTypes(AlignmentStrategy.wrap(null));
|
private static final NodeAlignmentStrategy NULL_STRATEGY = fromTypes(AlignmentStrategy.wrap(null));
|
||||||
|
|
||||||
/** @return shared strategy instance that returns <code>null</code> all the time */
|
/** @return shared strategy instance that returns <code>null</code> all the time */
|
||||||
public static ASTAlignmentStrategy getNullStrategy() {
|
public static NodeAlignmentStrategy getNullStrategy() {
|
||||||
return NULL_STRATEGY;
|
return NULL_STRATEGY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ASTAlignmentStrategy fromTypes(AlignmentStrategy strategy) {
|
public static NodeAlignmentStrategy fromTypes(AlignmentStrategy strategy) {
|
||||||
return new AlignmentStrategyWrapper(strategy);
|
return new AlignmentStrategyWrapper(strategy);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public abstract Alignment getAlignment(@NotNull ASTNode node);
|
public abstract Alignment getAlignment(@NotNull ASTNode node);
|
||||||
|
|
||||||
public static class AlignmentStrategyWrapper extends ASTAlignmentStrategy {
|
public static class AlignmentStrategyWrapper extends NodeAlignmentStrategy {
|
||||||
private final AlignmentStrategy internalStrategy;
|
private final AlignmentStrategy internalStrategy;
|
||||||
|
|
||||||
public AlignmentStrategyWrapper(@NotNull AlignmentStrategy internalStrategy) {
|
public AlignmentStrategyWrapper(@NotNull AlignmentStrategy internalStrategy) {
|
||||||
Reference in New Issue
Block a user