Skip to content

Commit 2224150

Browse files
committed
Restore whitespace tokens for top level fragment placeholders in Apollo to preserve them during format lines (#162)
1 parent 230ba7e commit 2224150

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,27 @@ private void fetchTokensFromLanguageService() {
102102
}
103103
final String text = token.getText();
104104
IElementType tokenType = JSGraphQLCodeMirrorTokenMapper.getTokenType(token.getType());
105-
if (tokenType.equals(JSGraphQLTokenTypes.PUNCTUATION)) {
105+
if(tokenType.equals(JSGraphQLTokenTypes.WHITESPACE)) {
106+
String originalText = bufferAsString.substring(token.getStart(), token.getEnd());
107+
if(originalText.trim().length() > 0) {
108+
// whitespace token with visible text, e.g. due to placeholders being removed at top level
109+
// by the language service and there's nothing valid to replace it with within the GraphQL grammar
110+
// so split the token into traditional ws tokens and the placeholder contents
111+
int offset = 0;
112+
final String[] parts = StringUtils.splitByCharacterType(originalText);
113+
for (String part : parts) {
114+
final Token partSourceToken = token.withTextAndOffset(part, offset);
115+
if(part.trim().length() > 0) {
116+
tokens.add(new JSGraphQLToken(JSGraphQLTokenTypes.TEMPLATE_FRAGMENT, partSourceToken));
117+
} else {
118+
partSourceToken.setKind(JSGraphQLCodeMirrorTokenMapper.CODEMIRROR_WHITESPACE);
119+
tokens.add(new JSGraphQLToken(JSGraphQLTokenTypes.WHITESPACE, partSourceToken));
120+
}
121+
offset += part.length();
122+
}
123+
continue; // already added the required tokens
124+
}
125+
} else if (tokenType.equals(JSGraphQLTokenTypes.PUNCTUATION)) {
106126
final IElementType punctuationTokenType = getPunctuationTokenType(text);
107127
if (punctuationTokenType != null) {
108128
tokenType = punctuationTokenType;

0 commit comments

Comments
 (0)