From 055859888e1ea14aec4c1595b8f585ea3010d986 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Mon, 21 Apr 2014 20:55:38 +0400 Subject: [PATCH] Refactoring: class rename with minor code enhancements --- .../jet/plugin/formatter/JetBlock.java | 46 +++++++++---------- .../formatter/JetFormattingModelBuilder.java | 4 +- ...rategy.java => NodeAlignmentStrategy.java} | 12 ++--- 3 files changed, 30 insertions(+), 32 deletions(-) rename idea/src/org/jetbrains/jet/plugin/formatter/{ASTAlignmentStrategy.java => NodeAlignmentStrategy.java} (80%) diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java index cf34748b948..cb29ea2cd94 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetBlock.java @@ -44,7 +44,7 @@ import static org.jetbrains.jet.lexer.JetTokens.*; */ public class JetBlock extends AbstractBlock { private static final int KDOC_COMMENT_INDENT = 1; - private final ASTAlignmentStrategy myAlignmentStrategy; + private final NodeAlignmentStrategy myAlignmentStrategy; private final Indent myIndent; private final CodeStyleSettings mySettings; private final KotlinSpacingBuilder mySpacingBuilder; @@ -58,14 +58,14 @@ public class JetBlock extends AbstractBlock { // private static final List - public JetBlock(@NotNull ASTNode node, - ASTAlignmentStrategy alignmentStrategy, + public JetBlock( + @NotNull ASTNode node, + NodeAlignmentStrategy alignmentStrategy, Indent indent, Wrap wrap, CodeStyleSettings settings, KotlinSpacingBuilder spacingBuilder ) { - super(node, wrap, alignmentStrategy.getAlignment(node)); myAlignmentStrategy = alignmentStrategy; myIndent = indent; @@ -89,7 +89,7 @@ public class JetBlock extends AbstractBlock { private List buildSubBlocks() { List blocks = new ArrayList(); - ASTAlignmentStrategy childrenAlignmentStrategy = getChildrenAlignmentStrategy(); + NodeAlignmentStrategy childrenAlignmentStrategy = getChildrenAlignmentStrategy(); for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) { IElementType childType = child.getElementType(); @@ -106,18 +106,16 @@ public class JetBlock extends AbstractBlock { } @NotNull - private Block buildSubBlock(@NotNull ASTNode child, ASTAlignmentStrategy alignmentStrategy) { - Wrap wrap = null; - - // Affects to spaces around operators... + private Block buildSubBlock(@NotNull ASTNode child, NodeAlignmentStrategy alignmentStrategy) { + // Skip one sub-level for operators, so type of block node is an element type of operator if (child.getElementType() == OPERATION_REFERENCE) { ASTNode operationNode = child.getFirstChildNode(); 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) { @@ -185,12 +183,12 @@ public class JetBlock extends AbstractBlock { return myNode.getFirstChildNode() == null; } - private ASTAlignmentStrategy getChildrenAlignmentStrategy() { + private NodeAlignmentStrategy getChildrenAlignmentStrategy() { CommonCodeStyleSettings jetCommonSettings = mySettings.getCommonSettings(JetLanguage.INSTANCE); JetCodeStyleSettings jetSettings = mySettings.getCustomSettings(JetCodeStyleSettings.class); // Prepare default null strategy - ASTAlignmentStrategy strategy = myAlignmentStrategy; + NodeAlignmentStrategy strategy = myAlignmentStrategy; // Redefine list of strategies for some special elements IElementType parentType = myNode.getElementType(); @@ -210,26 +208,26 @@ public class JetBlock extends AbstractBlock { return strategy; } - private static ASTAlignmentStrategy getAlignmentForChildInParenthesis( + private static NodeAlignmentStrategy getAlignmentForChildInParenthesis( 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 parenthesisAlignment = shouldAlignParenthesis ? Alignment.createAlignment() : null; + final Alignment bracketsAlignment = shouldAlignParenthesis ? Alignment.createAlignment() : null; - return new ASTAlignmentStrategy() { + return new NodeAlignmentStrategy() { @Override public Alignment getAlignment(@NotNull ASTNode node) { IElementType childNodeType = node.getElementType(); ASTNode prev = getPrevWithoutWhitespace(node); 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; } - if (childNodeType == openParenth || childNodeType == closeParenth) { - return parenthesisAlignment; + if (childNodeType == openBracket || childNodeType == closeBracket) { + return bracketsAlignment; } 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) { - return ASTAlignmentStrategy - .fromTypes(AlignmentStrategy.createAlignmentPerTypeStrategy(Arrays.asList((IElementType) ARROW), WHEN_ENTRY, true)); + return NodeAlignmentStrategy.fromTypes( + AlignmentStrategy.createAlignmentPerTypeStrategy(Arrays.asList((IElementType) ARROW), WHEN_ENTRY, true)); } else { - return ASTAlignmentStrategy.getNullStrategy(); + return NodeAlignmentStrategy.getNullStrategy(); } } diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java b/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java index 923143b0039..eea72e4464c 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/JetFormattingModelBuilder.java @@ -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"); * 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) { PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(JetLanguage.INSTANCE); JetBlock block = new JetBlock( - containingFile.getNode(), ASTAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings, + containingFile.getNode(), NodeAlignmentStrategy.getNullStrategy(), Indent.getNoneIndent(), null, settings, FormatterPackage.createSpacingBuilder(settings)); return FormattingModelProvider.createFormattingModelForPsiFile( diff --git a/idea/src/org/jetbrains/jet/plugin/formatter/ASTAlignmentStrategy.java b/idea/src/org/jetbrains/jet/plugin/formatter/NodeAlignmentStrategy.java similarity index 80% rename from idea/src/org/jetbrains/jet/plugin/formatter/ASTAlignmentStrategy.java rename to idea/src/org/jetbrains/jet/plugin/formatter/NodeAlignmentStrategy.java index 4cc94bc448d..d53a0115906 100644 --- a/idea/src/org/jetbrains/jet/plugin/formatter/ASTAlignmentStrategy.java +++ b/idea/src/org/jetbrains/jet/plugin/formatter/NodeAlignmentStrategy.java @@ -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"); * 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.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 null all the time */ - public static ASTAlignmentStrategy getNullStrategy() { + public static NodeAlignmentStrategy getNullStrategy() { return NULL_STRATEGY; } - public static ASTAlignmentStrategy fromTypes(AlignmentStrategy strategy) { + public static NodeAlignmentStrategy fromTypes(AlignmentStrategy strategy) { return new AlignmentStrategyWrapper(strategy); } @Nullable public abstract Alignment getAlignment(@NotNull ASTNode node); - public static class AlignmentStrategyWrapper extends ASTAlignmentStrategy { + public static class AlignmentStrategyWrapper extends NodeAlignmentStrategy { private final AlignmentStrategy internalStrategy; public AlignmentStrategyWrapper(@NotNull AlignmentStrategy internalStrategy) {