simplified, added filtering for subsequent parameter pairs: "key, value", "first, second"

This commit is contained in:
Yaroslav Lepenkin
2014-07-22 19:00:09 +04:00
parent d477f91dba
commit 0aa3890084
2 changed files with 49 additions and 19 deletions
@@ -17,6 +17,7 @@ package com.intellij.codeInsight.folding.impl;
import com.intellij.lang.folding.FoldingDescriptor;
import com.intellij.lang.folding.NamedFoldingDescriptor;
import com.intellij.openapi.util.Couple;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
@@ -32,12 +33,14 @@ public class ParameterNameFoldingManager {
private static final int MIN_NAME_LENGTH_THRESHOLD = 3;
private static final int MIN_ARGS_TO_FOLD = 2;
private static final String[] RANGE_START_WORDS = {
"begin", "start", "from", "first"
};
private static final String[] RANGE_END_WORDS = {
"end", "to", "last"
};
private static final List<Couple<String>> COMMONLY_USED_PARAMETER_PAIR = ContainerUtil.newArrayList(
Couple.of("begin", "end"),
Couple.of("start", "end"),
Couple.of("first", "last"),
Couple.of("first", "second"),
Couple.of("from", "to"),
Couple.of("key", "value")
);
private final PsiCallExpression myCallExpression;
@@ -119,20 +122,16 @@ public class ParameterNameFoldingManager {
String secondParamName = myParameters[second].getName();
if (firstParamName == null || secondParamName == null) return false;
if (containsAnyWord(firstParamName, RANGE_START_WORDS) && containsAnyWord(secondParamName, RANGE_END_WORDS)) {
for (Couple<String> knownPair : COMMONLY_USED_PARAMETER_PAIR) {
if (StringUtil.containsIgnoreCase(firstParamName, knownPair.first)
&& StringUtil.containsIgnoreCase(secondParamName, knownPair.second)) {
return true;
}
}
return false;
}
private static boolean containsAnyWord(@NotNull String str, @NotNull String[] words) {
for (String word : words) {
if (StringUtil.containsIgnoreCase(str, word)) return true;
}
return false;
}
private boolean shouldInlineParameterName(int paramIndex) {
PsiExpression argument = myCallArguments[paramIndex];
if (isLiteralExpression(argument) && argument.getType() != null) {
@@ -692,6 +692,37 @@ public class CharSymbol {
assert regions.size() == 2
}
public void "test do not inline known subsequent parameter names"() {
def text = """
public class Test {
public void main() {
test1(1, 2);
test2(1, 2);
test3(1, 2);
doTest("first", "second");
}
public void test1(int first, int second) {
int start = first;
int end = second;
}
public void test2(int key, int value) {
int start = key;
int end = value;
}
public void test3(int key, int value) {
int start = key;
int end = value;
}
}
"""
configure text
def regions = myFixture.editor.foldingModel.allFoldRegions
assert regions.size() == 4
}
public void "test do not inline paired ranged names"() {
def text = """
public class CharSymbol {
@@ -712,18 +743,18 @@ public class CharSymbol {
}
public void substring2(int startIndex, int endIndex) {
int start = beginIndex;
int start = startIndex;
int end = endIndex;
}
public void substring3(int from, int to) {
int start = beginIndex;
int end = endIndex;
int start = from;
int end = to;
}
public void substring4(int first, int last) {
int start = beginIndex;
int end = endIndex;
int start = first;
int end = last;
}
}
"""