Skip to content

Commit 5cb6387

Browse files
committed
Added some more advanced formatting rules
1 parent d6a50f0 commit 5cb6387

File tree

4 files changed

+104
-14
lines changed

4 files changed

+104
-14
lines changed

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/formatter/CypherBlock.java

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@
88
import com.intellij.formatting.Spacing;
99
import com.intellij.formatting.SpacingBuilder;
1010
import com.intellij.formatting.Wrap;
11+
import com.intellij.formatting.WrapType;
1112
import com.intellij.lang.ASTNode;
1213
import com.intellij.openapi.util.TextRange;
1314
import com.intellij.psi.TokenType;
1415
import com.intellij.psi.codeStyle.CodeStyleSettings;
16+
import com.intellij.psi.formatter.FormatterUtil;
17+
import com.intellij.psi.impl.source.tree.LeafPsiElement;
18+
import com.intellij.psi.impl.source.tree.TreeUtil;
19+
import com.neueda.jetbrains.plugin.graphdb.language.cypher.psi.CypherTypes;
1520
import org.jetbrains.annotations.NotNull;
1621
import org.jetbrains.annotations.Nullable;
1722

1823
import java.util.List;
24+
import java.util.Objects;
25+
import java.util.function.Predicate;
1926
import java.util.stream.Stream;
2027

2128
import static java.util.stream.Collectors.toList;
@@ -35,14 +42,13 @@ public class CypherBlock implements ASTBlock {
3542
@NotNull CodeStyleSettings settings,
3643
@Nullable Wrap wrap,
3744
@Nullable Indent indent,
38-
@Nullable Alignment alignment,
39-
SpacingBuilder spacingBuilder) {
45+
@Nullable Alignment alignment) {
4046
this.node = node;
4147
this.codeStyleSettings = settings;
4248
this.wrap = wrap;
4349
this.indent = indent;
4450
this.alignment = alignment;
45-
this.spacingBuilder = spacingBuilder;
51+
this.spacingBuilder = CypherFormattingModelBuilder.createSpacingBuilder(settings);
4652
}
4753

4854
@Override
@@ -60,16 +66,46 @@ public TextRange getTextRange() {
6066
@Override
6167
public List<Block> getSubBlocks() {
6268
if (subBlocks == null) {
69+
Predicate<ASTNode> isWhitespaceOrEmpty = CypherBlock::isWhitespaceOrEmpty;
6370
subBlocks = Stream.of(node.getChildren(null))
64-
.filter(node -> !CypherBlock.isWhitespaceOrEmpty(node))
71+
.filter(isWhitespaceOrEmpty.negate())
6572
.map(this::makeSubBlock)
73+
.filter(Objects::nonNull)
6674
.collect(toList());
6775
}
6876
return subBlocks;
6977
}
7078

7179
private Block makeSubBlock(@NotNull ASTNode node) {
72-
return new CypherBlock(node, codeStyleSettings, null, Indent.getNoneIndent(), null, spacingBuilder);
80+
Wrap wrap = null;
81+
Indent indent = Indent.getNoneIndent();
82+
83+
if (isTopLevel(node)) {
84+
wrap = Wrap.createWrap(WrapType.ALWAYS, true);
85+
indent = Indent.getNoneIndent();
86+
}
87+
88+
if (node.getElementType() == CypherTypes.K_ON) {
89+
if (TreeUtil.findParent(node, CypherTypes.MERGE) != null) {
90+
wrap = Wrap.createWrap(WrapType.ALWAYS, true);
91+
indent = Indent.getNormalIndent(true);
92+
}
93+
}
94+
95+
if (isReturnBodyKeywords(node)) {
96+
wrap = Wrap.createWrap(WrapType.ALWAYS, true);
97+
indent = Indent.getNormalIndent(false);
98+
}
99+
100+
if (isTopLevel(node)) {
101+
String original = node.getPsi().getText();
102+
String text = original.toUpperCase();
103+
if (!original.equals(text)) {
104+
node.getPsi(LeafPsiElement.class).rawReplaceWithText(text);
105+
}
106+
}
107+
108+
return new CypherBlock(node, codeStyleSettings, wrap, indent, null);
73109
}
74110

75111
@Nullable
@@ -93,13 +129,18 @@ public Alignment getAlignment() {
93129
@Nullable
94130
@Override
95131
public Spacing getSpacing(@Nullable Block child1, @NotNull Block child2) {
132+
//todo Add more complex login on spacing, if needed
133+
96134
return spacingBuilder.getSpacing(this, child1, child2);
97135
}
98136

99137
@NotNull
100138
@Override
101139
public ChildAttributes getChildAttributes(int newChildIndex) {
102-
return new ChildAttributes(null, null);
140+
Indent indent = Indent.getNoneIndent();
141+
//todo add child indentation rules
142+
143+
return new ChildAttributes(indent, null);
103144
}
104145

105146
@Override
@@ -115,4 +156,22 @@ public boolean isLeaf() {
115156
private static boolean isWhitespaceOrEmpty(ASTNode node) {
116157
return node.getElementType() == TokenType.WHITE_SPACE || node.getTextLength() == 0;
117158
}
159+
160+
private static boolean isTopLevel(ASTNode node) {
161+
return FormatterUtil.isOneOf(node,
162+
CypherTypes.CREATE,
163+
CypherTypes.MERGE,
164+
CypherTypes.MATCH,
165+
CypherTypes.WHERE,
166+
CypherTypes.WITH,
167+
CypherTypes.UNION,
168+
CypherTypes.RETURN);
169+
}
170+
171+
private static boolean isReturnBodyKeywords(ASTNode node) {
172+
return FormatterUtil.isOneOf(node,
173+
CypherTypes.LIMIT,
174+
CypherTypes.SKIP,
175+
CypherTypes.ORDER);
176+
}
118177
}

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/formatter/CypherCodeStyleSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public class CypherCodeStyleSettings extends CustomCodeStyleSettings {
77

8-
public boolean SPACE_AFTER_COLON = true;
8+
public boolean SPACE_AFTER_COLON = false;
99
public boolean SPACE_BEFORE_COLON = false;
1010

1111
CypherCodeStyleSettings(CodeStyleSettings container) {

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/formatter/CypherFormattingModelBuilder.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.intellij.psi.codeStyle.CodeStyleSettings;
1616
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
1717
import com.neueda.jetbrains.plugin.graphdb.language.cypher.CypherLanguage;
18+
import com.neueda.jetbrains.plugin.graphdb.language.cypher.psi.CypherTypes;
1819
import org.jetbrains.annotations.NotNull;
1920
import org.jetbrains.annotations.Nullable;
2021

@@ -26,8 +27,8 @@ public class CypherFormattingModelBuilder implements FormattingModelBuilder {
2627
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
2728
CypherBlock block = new CypherBlock(element.getNode(), settings, Wrap.createWrap(WrapType.NONE, false),
2829
Indent.getNoneIndent(),
29-
Alignment.createAlignment(),
30-
createSpacingBuilder(settings));
30+
Alignment.createAlignment()
31+
);
3132
return FormattingModelProvider.createFormattingModelForPsiFile(element.getContainingFile(), block, settings);
3233
}
3334

@@ -37,7 +38,7 @@ public TextRange getRangeAffectingIndent(PsiFile file, int offset, ASTNode eleme
3738
return null;
3839
}
3940

40-
private static SpacingBuilder createSpacingBuilder(CodeStyleSettings settings) {
41+
static SpacingBuilder createSpacingBuilder(CodeStyleSettings settings) {
4142
CypherCodeStyleSettings customSettings = settings.getCustomSettings(CypherCodeStyleSettings.class);
4243
CommonCodeStyleSettings commonSettings = settings.getCommonSettings(CypherLanguage.INSTANCE);
4344

@@ -48,11 +49,34 @@ private static SpacingBuilder createSpacingBuilder(CodeStyleSettings settings) {
4849
return new SpacingBuilder(settings, CypherLanguage.INSTANCE)
4950
.before(OP_COLON).spacing(spacesBeforeColon, spacesBeforeColon, 0, false, 0)
5051
.after(OP_COLON).spacing(spacesAfterColon, spacesAfterColon, 0, false, 0)
52+
53+
.beforeInside(CypherTypes.BRACKET_CURLYOPEN, CypherTypes.NODE_LABEL)
54+
.spacing(1, 1, 0, false, 0)
55+
5156
.withinPair(PARENTHESE_OPEN, PARENTHESE_CLOSE)
52-
.spaceIf(commonSettings.SPACE_WITHIN_BRACKETS, true)
57+
.spaceIf(commonSettings.SPACE_WITHIN_BRACKETS, true)
58+
5359
.withinPair(BRACKET_CURLYOPEN, BRACKET_CURLYCLOSE)
54-
.spaceIf(commonSettings.SPACE_WITHIN_BRACES, true)
60+
.spaceIf(commonSettings.SPACE_WITHIN_BRACES, true)
61+
5562
.before(OP_COMMA).spacing(spacesBeforeComma, spacesBeforeComma, 0, false, 0)
56-
.after(OP_COMMA).spaceIf(commonSettings.SPACE_AFTER_COMMA);
63+
.after(OP_COMMA).spaceIf(commonSettings.SPACE_AFTER_COMMA)
64+
65+
.between(NODE_LABEL, BRACKET_CURLYOPEN).spaces(1)
66+
.between(PARENTHESIZED_EXPRESSION, PATTERN).spaceIf(false)
67+
.around(OP_PLUS).spaces(1)
68+
.around(OP_MINUS).spaces(1)
69+
.around(OP_MUL).spaces(1)
70+
.around(OP_DIVIDE).spaces(1)
71+
.around(OP_MODULO).spaces(1)
72+
.around(OP_POW).spaces(1)
73+
.around(OP_EQUAL).spaces(1)
74+
.around(OP_NOTEQUALS).spaces(1)
75+
.around(OP_LESSTHEN).spaces(1)
76+
.around(OP_GREATHERTHEN).spaces(1)
77+
.around(OP_LESSTHANEQUALS).spaces(1)
78+
.around(OP_GREATERTHANEQUALS).spaces(1)
79+
.between(STATEMENT_ITEM, STATEMENT_ITEM).blankLines(1)
80+
;
5781
}
5882
}

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/formatter/CypherLanguageCodeStyleSettingsProvider.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ public IndentOptionsEditor getIndentOptionsEditor() {
5454
@Nullable
5555
@Override
5656
public CommonCodeStyleSettings getDefaultCommonSettings() {
57-
return new CommonCodeStyleSettings(CypherLanguage.INSTANCE);
57+
CommonCodeStyleSettings settings = new CommonCodeStyleSettings(getLanguage());
58+
CommonCodeStyleSettings.IndentOptions indentOptions = settings.initIndentOptions();
59+
indentOptions.INDENT_SIZE = 2;
60+
indentOptions.CONTINUATION_INDENT_SIZE = 2;
61+
indentOptions.TAB_SIZE = 2;
62+
indentOptions.USE_TAB_CHARACTER = true;
63+
64+
return settings;
5865
}
5966

6067
@Override

0 commit comments

Comments
 (0)