mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 09:19:13 +07:00
[java] fixes formatting around soft keywords
This commit is contained in:
+12
-30
@@ -11,6 +11,7 @@ import com.intellij.lang.java.JavaParserDefinition;
|
||||
import com.intellij.lexer.Lexer;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
@@ -1764,41 +1765,22 @@ public class JavaSpacePropertyProcessor extends JavaElementVisitor {
|
||||
|
||||
if (result == null) {
|
||||
Lexer lexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST);
|
||||
|
||||
TokenCheckResult res1 = checkToken(token1, type1, lexer), res2 = checkToken(token2, type2, lexer);
|
||||
if (res1 == TokenCheckResult.INCORRECT || res2 == TokenCheckResult.INCORRECT) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (res1 == TokenCheckResult.RESTRICTED_KEYWORD || type1 == JavaTokenType.IDENTIFIER && res2 == TokenCheckResult.RESTRICTED_KEYWORD) {
|
||||
result = false;
|
||||
}
|
||||
else {
|
||||
lexer.start(token1.getText() + token2.getText());
|
||||
boolean canMerge = lexer.getTokenType() == type1;
|
||||
lexer.advance();
|
||||
canMerge &= lexer.getTokenType() == type2;
|
||||
result = canMerge;
|
||||
}
|
||||
|
||||
String text1 = token1.getText(), text2 = token2.getText();
|
||||
lexer.start(text1 + text2);
|
||||
IElementType reparsedType1 = lexer.getTokenType();
|
||||
String reparsedText1 = lexer.getTokenText();
|
||||
lexer.advance();
|
||||
IElementType reparsedType2 = lexer.getTokenType();
|
||||
String reparsedText2 = lexer.getTokenText();
|
||||
result = sameTokens(type1, text1, reparsedType1, reparsedText1) && sameTokens(type2, text2, reparsedType2, reparsedText2);
|
||||
ourTokenStickingMatrix.put(key, result);
|
||||
}
|
||||
|
||||
return result.booleanValue();
|
||||
}
|
||||
|
||||
private enum TokenCheckResult {OK, INCORRECT, RESTRICTED_KEYWORD}
|
||||
|
||||
private static TokenCheckResult checkToken(ASTNode token, IElementType tokenType, Lexer lexer) {
|
||||
lexer.start(token.getText());
|
||||
|
||||
IElementType first = lexer.getTokenType();
|
||||
if (first != tokenType) {
|
||||
boolean kw = first == JavaTokenType.IDENTIFIER && ElementType.KEYWORD_BIT_SET.contains(tokenType);
|
||||
return kw ? TokenCheckResult.RESTRICTED_KEYWORD : TokenCheckResult.INCORRECT;
|
||||
}
|
||||
|
||||
lexer.advance();
|
||||
return lexer.getTokenType() == null ? TokenCheckResult.OK : TokenCheckResult.INCORRECT;
|
||||
private static boolean sameTokens(IElementType type, String text, IElementType reparsedType, String reparsedText) {
|
||||
return reparsedType == type ||
|
||||
reparsedType == JavaTokenType.IDENTIFIER && ElementType.KEYWORD_BIT_SET.contains(type) && Comparing.equal(text, reparsedText);
|
||||
}
|
||||
}
|
||||
+2
-4
@@ -1,8 +1,6 @@
|
||||
// "Replace explicit type with 'var'" "true"
|
||||
class Main {
|
||||
void m(String[] args){
|
||||
for ( var arg : args) {
|
||||
|
||||
void m(String[] args) {
|
||||
for (var arg : args) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
// "Replace explicit type with 'var'" "true"
|
||||
import java.io.*;
|
||||
class Main {
|
||||
void m(InputStream s){
|
||||
try ( var in = s) {}
|
||||
}
|
||||
void m(InputStream s) {
|
||||
try (var in = s) {}
|
||||
}
|
||||
}
|
||||
+2
-4
@@ -1,8 +1,6 @@
|
||||
// "Replace explicit type with 'var'" "true"
|
||||
class Main {
|
||||
void m(String[] args){
|
||||
for (<caret>String arg : args) {
|
||||
|
||||
void m(String[] args) {
|
||||
for (<caret>String arg : args) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
// "Replace explicit type with 'var'" "true"
|
||||
import java.io.*;
|
||||
class Main {
|
||||
void m(InputStream s){
|
||||
try (<caret>InputStream in = s) {}
|
||||
}
|
||||
void m(InputStream s) {
|
||||
try (<caret>InputStream in = s) {}
|
||||
}
|
||||
}
|
||||
+15
-1
@@ -1,15 +1,24 @@
|
||||
// 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.psi.formatter.java;
|
||||
|
||||
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.testFramework.LightPlatformTestCase;
|
||||
|
||||
/**
|
||||
* Is intended to hold specific java formatting tests for 'spacing' settings.
|
||||
*
|
||||
* @author Denis Zhdanov
|
||||
* @since Apr 29, 2010 5:50:34 PM
|
||||
* @since Apr 29, 2010
|
||||
*/
|
||||
public class JavaFormatterSpaceTest extends AbstractJavaFormatterTest {
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
LanguageLevelProjectExtension.getInstance(LightPlatformTestCase.getProject()).setLanguageLevel(LanguageLevel.JDK_X);
|
||||
}
|
||||
|
||||
public void testSpacingBetweenTypeParameters() {
|
||||
// Implied by IDEADEV-3666
|
||||
getSettings().SPACE_AFTER_COMMA = true;
|
||||
@@ -670,4 +679,9 @@ public class JavaFormatterSpaceTest extends AbstractJavaFormatterTest {
|
||||
" foo(100 , 200);\n" +
|
||||
"}");
|
||||
}
|
||||
|
||||
public void testSpacingAroundVarKeyword() {
|
||||
doMethodTest("for ( var path : paths) ;", "for (var path : paths) ;");
|
||||
doMethodTest("try ( @A var r = open()) { }", "try (@A var r = open()) {\n}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user