Skip to content

Commit 14d9498

Browse files
committed
Add support for indentation of fields with leading commas (#45)
1 parent 28484bd commit 14d9498

File tree

7 files changed

+71
-8
lines changed

7 files changed

+71
-8
lines changed

src/main/com/intellij/lang/jsgraphql/ide/formatter/JSGraphQLBlock.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ public Indent getIndent() {
107107
if (myNode.getElementType() == JSGraphQLTokenTypes.RBRACE || myNode.getElementType() == JSGraphQLTokenTypes.LBRACE) {
108108
return Indent.getNoneIndent();
109109
}
110+
if(myNode.getElementType() == JSGraphQLTokenTypes.PUNCTUATION) {
111+
if(",".equals(myNode.getText())) {
112+
return Indent.getNormalIndent();
113+
}
114+
}
110115

111116
if(parent != null) {
112117
JSGraphQLElementType astNode = getAstNode(parent);

src/main/com/intellij/lang/jsgraphql/languageservice/api/Token.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ public void setKind(String kind) {
5050
this.kind = kind;
5151
}
5252

53+
public Token withTextAndOffset(String text, int offset) {
54+
final Token token = new Token();
55+
token.type = type;
56+
token.text = text;
57+
token.kind = kind;
58+
token.start = start + offset;
59+
token.end = token.start + text.length();
60+
return token;
61+
}
62+
5363
@Override
5464
public String toString() {
5565
return "Token{" +

src/main/com/intellij/lang/jsgraphql/lexer/JSGraphQLCodeMirrorTokenMapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ public class JSGraphQLCodeMirrorTokenMapper {
1818

1919
private final static Map<String, IElementType> mappings = Maps.newConcurrentMap();
2020

21+
public static final String CODEMIRROR_WHITESPACE = "ws";
22+
2123
static {
2224
for (IElementType tokenType : JSGraphQLTokenTypes.ALL_TOKEN_TYPES) {
2325
if(tokenType instanceof JSGraphQLTokenType) {
2426
mappings.put(((JSGraphQLTokenType)tokenType).getLexerTokenType(), tokenType);
2527
} else if(tokenType.equals(JSGraphQLTokenTypes.WHITESPACE)) {
26-
mappings.put("ws", tokenType); // CodeMirror uses 'ws' for whitespace
28+
mappings.put(CODEMIRROR_WHITESPACE, tokenType); // CodeMirror uses 'ws' for whitespace
2729
} else if(tokenType.equals(JSGraphQLTokenTypes.INVALIDCHAR)) {
2830
mappings.put("invalidchar", tokenType); // CodeMirror uses 'invalidchar' for whitespace
2931
}

src/main/com/intellij/lang/jsgraphql/lexer/JSGraphQLLexer.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import com.intellij.openapi.diagnostic.Logger;
1919
import com.intellij.openapi.project.Project;
2020
import com.intellij.psi.tree.IElementType;
21+
22+
import org.apache.commons.lang.StringUtils;
2123
import org.jetbrains.annotations.NotNull;
2224
import org.jetbrains.annotations.Nullable;
2325

@@ -99,6 +101,21 @@ private void fetchTokensFromLanguageService() {
99101
final IElementType punctuationTokenType = getPunctuationTokenType(text);
100102
if (punctuationTokenType != null) {
101103
tokenType = punctuationTokenType;
104+
} else if(text.contains(",")) {
105+
// separate out commas from surrounding whitespace to support indentation on ", field" lines
106+
int offset = 0;
107+
final String[] parts = StringUtils.splitByCharacterType(text);
108+
for (String part : parts) {
109+
final Token partSourceToken = token.withTextAndOffset(part, offset);
110+
if(part.equals(",")) {
111+
tokens.add(new JSGraphQLToken(tokenType, partSourceToken));
112+
} else {
113+
partSourceToken.setKind(JSGraphQLCodeMirrorTokenMapper.CODEMIRROR_WHITESPACE);
114+
tokens.add(new JSGraphQLToken(JSGraphQLTokenTypes.WHITESPACE, partSourceToken));
115+
}
116+
offset += part.length();
117+
}
118+
continue; // already added the required tokens
102119
} else if (JSGraphQLKeywords.FRAGMENT_DOTS.equals(text)) {
103120
// consider the "..." spread operator a keyword for highlighting
104121
tokenType = JSGraphQLTokenTypes.KEYWORD;

test-resources/testData/FormatterExpectedResult.graphql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ query queryName($foo: TestInput, $site: TestEnum = RED) {
126126
}
127127
}
128128

129+
130+
### comma before fields ###
131+
132+
query Commas {
133+
foo {
134+
bar
135+
, baz
136+
, baz2
137+
}
138+
}
139+
129140
### --------- annotations.graphql --------- ###
130141

131142
type AnnotatedObject @foo(foo: {test: true}) {

test-resources/testData/FormatterTestData.graphql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ __typename
126126
}
127127
}
128128

129+
130+
### comma before fields ###
131+
132+
query Commas {
133+
foo {
134+
bar
135+
, baz
136+
, baz2
137+
}
138+
}
139+
129140
### --------- annotations.graphql --------- ###
130141

131142
type AnnotatedObject @foo(foo: {test: true}) {

test-resources/testData/ParsingTestData.txt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,8 @@ GraphQL File(0,2428)
493493
PsiElement(OPEN_QUOTE)('"')(1471,1472)
494494
PsiElement(STRING)('foo')(1472,1475)
495495
PsiElement(CLOSE_QUOTE)('"')(1475,1476)
496-
PsiElement(PUNCTUATION)(', ')(1476,1478)
496+
PsiElement(PUNCTUATION)(',')(1476,1477)
497+
PsiWhiteSpace(' ')(1477,1478)
497498
PsiElement(ATTRIBUTE)('option')(1478,1484)
498499
PsiElement(PUNCTUATION)(':')(1484,1485)
499500
PsiWhiteSpace(' ')(1485,1486)
@@ -577,7 +578,8 @@ GraphQL File(0,2428)
577578
PsiWhiteSpace(' ')(1734,1735)
578579
JSGraphQLNamedTypePsiElement(Atom)(1735,1744)
579580
PsiElement(ATOM)('TestInput')(1735,1744)
580-
PsiElement(PUNCTUATION)(', ')(1744,1746)
581+
PsiElement(PUNCTUATION)(',')(1744,1745)
582+
PsiWhiteSpace(' ')(1745,1746)
581583
PsiElement(VARIABLE)('$')(1746,1747)
582584
PsiElement(VARIABLE)('site')(1747,1751)
583585
PsiElement(PUNCTUATION)(':')(1751,1752)
@@ -631,9 +633,11 @@ GraphQL File(0,2428)
631633
PsiWhiteSpace(' ')(1868,1869)
632634
PsiElement(LBRACKET)('[')(1869,1870)
633635
PsiElement(<UNKNOWN>)('RED')(1870,1873)
634-
PsiElement(PUNCTUATION)(', ')(1873,1875)
636+
PsiElement(PUNCTUATION)(',')(1873,1874)
637+
PsiWhiteSpace(' ')(1874,1875)
635638
PsiElement(<UNKNOWN>)('GREEN')(1875,1880)
636-
PsiElement(PUNCTUATION)(', ')(1880,1882)
639+
PsiElement(PUNCTUATION)(',')(1880,1881)
640+
PsiWhiteSpace(' ')(1881,1882)
637641
PsiElement(<UNKNOWN>)('BLUE')(1882,1886)
638642
PsiElement(RBRACKET)(']')(1886,1887)
639643
PsiWhiteSpace('\n')(1887,1888)
@@ -649,9 +653,11 @@ GraphQL File(0,2428)
649653
PsiWhiteSpace(' ')(1921,1922)
650654
PsiElement(LBRACKET)('[')(1922,1923)
651655
PsiElement(NUMBER)('1.23')(1923,1927)
652-
PsiElement(PUNCTUATION)(', ')(1927,1929)
656+
PsiElement(PUNCTUATION)(',')(1927,1928)
657+
PsiWhiteSpace(' ')(1928,1929)
653658
PsiElement(NUMBER)('1.3e-1')(1929,1935)
654-
PsiElement(PUNCTUATION)(', ')(1935,1937)
659+
PsiElement(PUNCTUATION)(',')(1935,1936)
660+
PsiWhiteSpace(' ')(1936,1937)
655661
PsiElement(NUMBER)('-1.35384e+3')(1937,1948)
656662
PsiElement(RBRACKET)(']')(1948,1949)
657663
PsiWhiteSpace('\n')(1949,1950)
@@ -810,7 +816,8 @@ GraphQL File(0,2428)
810816
PsiElement(OPEN_QUOTE)('"')(2340,2341)
811817
PsiElement(STRING)('test')(2341,2345)
812818
PsiElement(CLOSE_QUOTE)('"')(2345,2346)
813-
PsiElement(PUNCTUATION)(', ')(2346,2348)
819+
PsiElement(PUNCTUATION)(',')(2346,2347)
820+
PsiWhiteSpace(' ')(2347,2348)
814821
PsiElement(ATTRIBUTE)('foo')(2348,2351)
815822
PsiElement(PUNCTUATION)(':')(2351,2352)
816823
PsiWhiteSpace(' ')(2352,2353)

0 commit comments

Comments
 (0)