[java] squeezes comments out of for-update statements

This commit is contained in:
Roman Shevchenko
2017-12-08 13:08:41 +01:00
parent fd2467aa1b
commit 362a9fe2a3
4 changed files with 100 additions and 31 deletions
@@ -1,17 +1,5 @@
/*
* Copyright 2000-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package com.intellij.lang.java.parser;
@@ -384,22 +372,23 @@ public class StatementParser {
return token;
}
private void parseExpressionOrExpressionList(final PsiBuilder builder) {
final PsiBuilder.Marker expr = myParser.getExpressionParser().parse(builder);
private void parseExpressionOrExpressionList(PsiBuilder builder) {
PsiBuilder.Marker expr = myParser.getExpressionParser().parse(builder);
if (expr == null) return;
final PsiBuilder.Marker expressionStatement;
PsiBuilder.Marker expressionStatement;
if (builder.getTokenType() != JavaTokenType.COMMA) {
expressionStatement = expr.precede();
done(expressionStatement, JavaElementType.EXPRESSION_STATEMENT);
expressionStatement.setCustomEdgeTokenBinders(null, WhitespacesBinders.DEFAULT_RIGHT_BINDER);
}
else {
final PsiBuilder.Marker expressionList = expr.precede();
PsiBuilder.Marker expressionList = expr.precede();
expressionStatement = expressionList.precede();
do {
builder.advanceLexer();
final PsiBuilder.Marker nextExpression = myParser.getExpressionParser().parse(builder);
PsiBuilder.Marker nextExpression = myParser.getExpressionParser().parse(builder);
if (nextExpression == null) {
error(builder, JavaErrorMessages.message("expected.expression"));
}
@@ -408,6 +397,7 @@ public class StatementParser {
done(expressionList, JavaElementType.EXPRESSION_LIST);
done(expressionStatement, JavaElementType.EXPRESSION_LIST_STATEMENT);
expressionStatement.setCustomEdgeTokenBinders(null, WhitespacesBinders.DEFAULT_RIGHT_BINDER);
}
}
@@ -0,0 +1,40 @@
PsiJavaFile:ForComments0.java
PsiForStatement
PsiKeyword:for('for')
PsiJavaToken:LPARENTH('(')
PsiDeclarationStatement
PsiLocalVariable:i
PsiModifierList:
<empty list>
PsiTypeElement:int
PsiKeyword:int('int')
PsiWhiteSpace(' ')
PsiIdentifier:i('i')
PsiJavaToken:EQ('=')
PsiLiteralExpression:0
PsiJavaToken:INTEGER_LITERAL('0')
PsiJavaToken:SEMICOLON(';')
PsiWhiteSpace(' ')
PsiBinaryExpression:i<1
PsiReferenceExpression:i
PsiReferenceParameterList
<empty list>
PsiIdentifier:i('i')
PsiJavaToken:LT('<')
PsiLiteralExpression:1
PsiJavaToken:INTEGER_LITERAL('1')
PsiJavaToken:SEMICOLON(';')
PsiWhiteSpace(' ')
PsiExpressionStatement
PsiPostfixExpression:i++
PsiReferenceExpression:i
PsiReferenceParameterList
<empty list>
PsiIdentifier:i('i')
PsiJavaToken:PLUSPLUS('++')
PsiWhiteSpace(' ')
PsiComment(C_STYLE_COMMENT)('/**/')
PsiJavaToken:RPARENTH(')')
PsiWhiteSpace(' ')
PsiEmptyStatement
PsiJavaToken:SEMICOLON(';')
@@ -0,0 +1,49 @@
PsiJavaFile:ForComments1.java
PsiForStatement
PsiKeyword:for('for')
PsiJavaToken:LPARENTH('(')
PsiDeclarationStatement
PsiLocalVariable:i
PsiModifierList:
<empty list>
PsiTypeElement:int
PsiKeyword:int('int')
PsiWhiteSpace(' ')
PsiIdentifier:i('i')
PsiJavaToken:EQ('=')
PsiLiteralExpression:0
PsiJavaToken:INTEGER_LITERAL('0')
PsiJavaToken:SEMICOLON(';')
PsiWhiteSpace(' ')
PsiBinaryExpression:i<1
PsiReferenceExpression:i
PsiReferenceParameterList
<empty list>
PsiIdentifier:i('i')
PsiJavaToken:LT('<')
PsiLiteralExpression:1
PsiJavaToken:INTEGER_LITERAL('1')
PsiJavaToken:SEMICOLON(';')
PsiWhiteSpace(' ')
PsiExpressionListStatement
PsiExpressionList
PsiPostfixExpression:i++
PsiReferenceExpression:i
PsiReferenceParameterList
<empty list>
PsiIdentifier:i('i')
PsiJavaToken:PLUSPLUS('++')
PsiJavaToken:COMMA(',')
PsiWhiteSpace(' ')
PsiPostfixExpression:j++
PsiReferenceExpression:j
PsiReferenceParameterList
<empty list>
PsiIdentifier:j('j')
PsiJavaToken:PLUSPLUS('++')
PsiWhiteSpace(' ')
PsiComment(C_STYLE_COMMENT)('/**/')
PsiJavaToken:RPARENTH(')')
PsiWhiteSpace(' ')
PsiEmptyStatement
PsiJavaToken:SEMICOLON(';')
@@ -1,17 +1,5 @@
/*
* Copyright 2000-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package com.intellij.java.parser.partial;
@@ -81,6 +69,8 @@ public class StatementParserTest extends JavaParsingTestCase {
public void testForIncomplete10() { doParserTest("for(var x"); }
public void testForInvalid0() { doParserTest("for(if (i<0) i++; ;) ;"); }
public void testForInvalid1() { doParserTest("for(class C { }; ;) ;"); }
public void testForComments0() { doParserTest("for(int i=0; i<1; i++ /**/) ;"); }
public void testForComments1() { doParserTest("for(int i=0; i<1; i++, j++ /**/) ;"); }
public void testForEach() { doParserTest("for(Object o : map.entrySet()) ;"); }
public void testForEachIncomplete0() { doParserTest("for(Object : list) ;"); }