IDEA-118590 Rule matcher for arrangement of Java code does not use the most specific rule

This commit is contained in:
Svetlana.Zemlyanskay
2014-03-26 16:04:52 +04:00
parent 190c324cae
commit c3e242f03f
2 changed files with 44 additions and 1 deletions
@@ -15,6 +15,7 @@
*/
package com.intellij.psi.codeStyle.arrangement
import static com.intellij.psi.codeStyle.arrangement.std.StdArrangementTokens.EntryType.*
import static com.intellij.psi.codeStyle.arrangement.std.StdArrangementTokens.Modifier.*
import static com.intellij.psi.codeStyle.arrangement.std.StdArrangementTokens.Order.BY_NAME
@@ -112,4 +113,23 @@ class Test {
rules: [ruleWithOrder(BY_NAME, rule(PUBLIC)), ruleWithOrder(BY_NAME, rule(PUBLIC, STATIC))]
)
}
void "test different entries type with modifier conditions"() {
doTest(
initial: '''\
class Test {
public static void b() {}
public void a() {}
}
''',
expected: '''\
class Test {
public void a() {}
public static void b() {}
}
''',
rules: [rule(FIELD, PUBLIC), rule(FIELD), rule(METHOD, PUBLIC), rule(METHOD), rule(METHOD, PUBLIC, ABSTRACT), rule(METHOD, ABSTRACT),
rule(FIELD, PUBLIC, STATIC), rule(FIELD, STATIC), rule(METHOD, PUBLIC, STATIC), rule(METHOD, STATIC)]
)
}
}
@@ -15,9 +15,12 @@
*/
package com.intellij.psi.codeStyle.arrangement.match;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.codeStyle.arrangement.ArrangementUtil;
import com.intellij.psi.codeStyle.arrangement.std.ArrangementSettingsToken;
import com.intellij.psi.codeStyle.arrangement.std.StdArrangementTokenType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Set;
@@ -58,7 +61,27 @@ public class StdArrangementMatchRule extends ArrangementMatchRule implements Clo
return tokens.containsAll(tokens1) ? 0 : 1;
}
else {
return tokens.containsAll(tokens1) ? -1 : 0;
if (tokens.containsAll(tokens1)) {
return -1;
}
final String entryType = getEntryType(tokens);
final String entryType1 = getEntryType(tokens1);
final int compare = StringUtil.compare(entryType, entryType1, false);
if (compare != 0 || tokens.size() == tokens1.size()) {
return compare;
}
return tokens.size() < tokens1.size() ? 1 : -1;
}
}
@Nullable
private static String getEntryType(@NotNull Set<ArrangementSettingsToken> tokens) {
for (ArrangementSettingsToken token : tokens) {
if (StdArrangementTokenType.ENTRY_TYPE.is(token)) {
return token.getId();
}
}
return null;
}
}