diff --git a/src/com/intellij/advancedExpressionFolding/AdvancedExpressionFoldingBuilder.java b/src/com/intellij/advancedExpressionFolding/AdvancedExpressionFoldingBuilder.java index 66abd2ad..80a138c7 100644 --- a/src/com/intellij/advancedExpressionFolding/AdvancedExpressionFoldingBuilder.java +++ b/src/com/intellij/advancedExpressionFolding/AdvancedExpressionFoldingBuilder.java @@ -23,9 +23,12 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -public class AdvancedExpressionFoldingBuilder extends FoldingBuilderEx { +import static com.intellij.advancedExpressionFolding.PropertyUtil.guessPropertyName; +import static java.lang.Character.isWhitespace; +public class AdvancedExpressionFoldingBuilder extends FoldingBuilderEx { private static final FoldingDescriptor[] NO_DESCRIPTORS = new FoldingDescriptor[0]; + private static final Pattern GENERICS_PATTERN = Pattern.compile("<[^<>]*>"); private static Set supportedMethods = new HashSet() { { @@ -337,15 +340,9 @@ private static boolean isReferenceToReference(@Nullable PsiReferenceExpression r @Contract("_, _, true -> !null") private static Expression getExpression(@NotNull PsiElement element, @NotNull Document document, boolean synthetic) { - if (synthetic) { - return CachedValuesManager.getCachedValue(element, - () -> CachedValueProvider.Result.create(buildExpression(element, document, true), - PsiModificationTracker.MODIFICATION_COUNT)); - } else { - return CachedValuesManager.getCachedValue(element, - () -> CachedValueProvider.Result.create(buildExpression(element, document, false), - PsiModificationTracker.MODIFICATION_COUNT)); - } + return CachedValuesManager.getCachedValue(element, + () -> CachedValueProvider.Result.create(buildExpression(element, document, synthetic), + PsiModificationTracker.MODIFICATION_COUNT)); } @SuppressWarnings("WeakerAccess") @@ -736,7 +733,7 @@ && startsWith(((PsiMethodCallExpression) qualifier).getMethodExpression().getRef && ((PsiReferenceExpression) e).isReferenceTo(r.resolve()) || e instanceof PsiMethodCallExpression && ((PsiMethodCallExpression) e).getMethodExpression().isReferenceTo(r.resolve()) ).toList(); - if (references.size() > 0) { + if (!references.isEmpty()) { return new ElvisExpression(element, element.getTextRange(), getAnyExpression(element.getThenExpression(), document), getAnyExpression(element.getElseExpression(), document), @@ -807,7 +804,7 @@ private static boolean calculateIfFinal(@NotNull PsiVariable element) { && ((PsiPostfixExpression) e).getOperand() instanceof PsiReferenceExpression && ((PsiReferenceExpression) ((PsiPostfixExpression) e).getOperand()).isReferenceTo(element) ).toList(); - if (references.size() == 0) { + if (references.isEmpty()) { isFinal = true; } } @@ -1221,7 +1218,7 @@ private static Expression getNewExpression(PsiNewExpression element, @NotNull Do break; } } - if (flag && arguments.size() > 0) { + if (flag && !arguments.isEmpty()) { if (settings.getState().isGetExpressionsCollapse()) return new SetLiteral(element, element.getTextRange(), TextRange.create(anonymousClass.getLBrace().getTextRange().getStartOffset(), @@ -1297,12 +1294,10 @@ private static Variable getVariableExpression(PsiElement element, boolean copy) @NotNull private static String eraseGenerics(@NotNull String signature) { - String re = "<[^<>]*>"; - Pattern p = Pattern.compile(re); - Matcher m = p.matcher(signature); + Matcher m = GENERICS_PATTERN.matcher(signature); while (m.find()) { signature = m.replaceAll(""); - m = p.matcher(signature); + m = GENERICS_PATTERN.matcher(signature); } return signature; } @@ -1797,10 +1792,10 @@ static int findDot(@NotNull Document document, int position, int i, boolean incl position < document.getText().length()) { position += i; offset += i; - if (charAt(document, position).equals(".")) { + if (charAt(document, position) == '.') { break; } - if (!charAt(document, position).matches("\\s")) { + if (!isWhitespace(charAt(document, position))) { return Integer.MAX_VALUE; } } @@ -1809,14 +1804,14 @@ static int findDot(@NotNull Document document, int position, int i, boolean incl do { position += i; offsetWithNewLine += i; - if (i < 0 && charAt(document, position).equals("\n")) { + if (i < 0 && charAt(document, position) == '\n') { offset = offsetWithNewLine; - } else if (i > 0 && charAt(document, position).matches("\\s")) { + } else if (i > 0 && isWhitespace(charAt(document, position))) { offset = offsetWithNewLine; } } while (Math.abs(offsetWithNewLine) < 100 && position > 0 && position < document.getText().length() && - charAt(document, position).matches("\\s")); + isWhitespace(charAt(document, position))); } if (Math.abs(offset) >= 100) { return Integer.MAX_VALUE; @@ -1824,31 +1819,8 @@ static int findDot(@NotNull Document document, int position, int i, boolean incl return offset; } - private static String charAt(@NotNull Document document, int position) { - return document.getText(TextRange.create(position, position + 1)); - } - - @NotNull - private static String guessPropertyName(@NotNull String text) { - StringBuilder sb = new StringBuilder(); - if (text.startsWith("get")) { - sb.append(text.substring(3)); - } else if (text.startsWith("set")) { - sb.append(text.substring(3)); - } else if (text.startsWith("is")) { - sb.append(text.substring(2)); - } else { - sb.append(text); - } - for (int i = 0; i < sb.length(); i++) { - if (Character.isUpperCase(sb.charAt(i)) && - (i == sb.length() - 1 || Character.isUpperCase(sb.charAt(i + 1)) || i == 0)) { - sb.setCharAt(i, Character.toLowerCase(sb.charAt(i))); - } else if (Character.isLowerCase(sb.charAt(i))) { - break; - } - } - return sb.toString(); + private static char charAt(@NotNull Document document, int position) { + return document.getText(TextRange.create(position, position + 1)).charAt(0); } @Nullable @@ -1928,8 +1900,8 @@ public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement element, @NotNul try { @Nullable Expression expression = getNonSyntheticExpression(element, document); if (expression != null && expression.supportsFoldRegions(document, null)) { - allDescriptors = new ArrayList<>(); FoldingDescriptor[] descriptors = expression.buildFoldRegions(expression.getElement(), document, null); + allDescriptors = new ArrayList<>(); Collections.addAll(allDescriptors, descriptors); } if (expression == null || expression.isNested()) { @@ -1939,12 +1911,11 @@ public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement element, @NotNul if (allDescriptors == null) { allDescriptors = new ArrayList<>(); } - allDescriptors.addAll(Arrays.asList(descriptors)); + Collections.addAll(allDescriptors, descriptors); } } } - } catch (IndexNotReadyException e) { - // ignore + } catch (IndexNotReadyException ignore) { } return allDescriptors != null ? allDescriptors.toArray(NO_DESCRIPTORS) : NO_DESCRIPTORS; } @@ -1971,4 +1942,5 @@ public boolean isCollapsedByDefault(@NotNull ASTNode astNode) { } return false; } + } diff --git a/src/com/intellij/advancedExpressionFolding/Expression.java b/src/com/intellij/advancedExpressionFolding/Expression.java index 956d85ff..dbc7800d 100644 --- a/src/com/intellij/advancedExpressionFolding/Expression.java +++ b/src/com/intellij/advancedExpressionFolding/Expression.java @@ -151,13 +151,13 @@ protected static String subscript(String str) { } private static String map(String str, Map subscriptMapping) { - StringBuilder sb = new StringBuilder(); + StringBuilder sb = new StringBuilder(str.length()); for (int i = 0; i < str.length(); i++) { Character c = subscriptMapping.get(str.charAt(i)); if (c == null) { return null; } else if (!c.equals('❤')) { - sb.append(c); + sb.append((char) c); } } return sb.toString(); diff --git a/src/com/intellij/advancedExpressionFolding/InterpolatedString.java b/src/com/intellij/advancedExpressionFolding/InterpolatedString.java index 9cb1f006..17cdd360 100644 --- a/src/com/intellij/advancedExpressionFolding/InterpolatedString.java +++ b/src/com/intellij/advancedExpressionFolding/InterpolatedString.java @@ -85,10 +85,10 @@ public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement element, @NotNul : operands.get(i + 1).getTextRange().getStartOffset(); StringBuilder sI = new StringBuilder().append(buf[0]); if (!(operands.get(i + 1) instanceof CharSequenceLiteral)) { - sI.append("$"); + sI.append('$'); } if (!(operands.get(i + 1) instanceof Variable) && !(operands.get(i + 1) instanceof CharSequenceLiteral)) { - sI.append("{"); + sI.append('{'); buf[0] = "}"; } else { buf[0] = ""; diff --git a/src/com/intellij/advancedExpressionFolding/PropertyUtil.java b/src/com/intellij/advancedExpressionFolding/PropertyUtil.java new file mode 100644 index 00000000..185f3abd --- /dev/null +++ b/src/com/intellij/advancedExpressionFolding/PropertyUtil.java @@ -0,0 +1,33 @@ +package com.intellij.advancedExpressionFolding; + +import org.jetbrains.annotations.NotNull; + +import static java.lang.Character.isLowerCase; +import static java.lang.Character.isUpperCase; +import static java.lang.Character.toLowerCase; + +public class PropertyUtil { + + @NotNull + public static String guessPropertyName(@NotNull String text) { + StringBuilder sb = new StringBuilder(text.length()); + int startPos; + if (text.startsWith("get") || text.startsWith("set")) { + startPos = 3; + } else if (text.startsWith("is")) { + startPos = 2; + } else { + startPos = 0; + } + sb.append(text, startPos, text.length()); + for (int i = 0; i < sb.length(); i++) { + if (isUpperCase(sb.charAt(i)) && + (i == sb.length() - 1 || isUpperCase(sb.charAt(i + 1)) || i == 0)) { + sb.setCharAt(i, toLowerCase(sb.charAt(i))); + } else if (isLowerCase(sb.charAt(i))) { + break; + } + } + return sb.toString(); + } +} diff --git a/src/com/intellij/advancedExpressionFolding/Range.java b/src/com/intellij/advancedExpressionFolding/Range.java index d601f760..7c493362 100644 --- a/src/com/intellij/advancedExpressionFolding/Range.java +++ b/src/com/intellij/advancedExpressionFolding/Range.java @@ -108,20 +108,13 @@ public boolean supportsFoldRegions(@NotNull Document document, @Override public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement element, @NotNull Document document, @Nullable Expression parent) { FoldingGroup group = FoldingGroup.newGroup(getClass().getName()); - StringBuilder sb1 = new StringBuilder().append(" ").append(separator).append(" "); - if (isStartInclusive()) { - sb1.append("["); - } else { - sb1.append("("); - } - String p1 = sb1.toString(); - StringBuilder sb2 = new StringBuilder(); - if (isEndInclusive()) { - sb2.append("]"); - } else { - sb2.append(")"); - } - String p2 = sb2.toString(); + String p1 = new StringBuilder(separator.length() + 3) + .append(' ') + .append(separator) + .append(' ') + .append(isStartInclusive() ? '[' : '(') + .toString(); + String p2 = isEndInclusive() ? "]" : ")"; ArrayList descriptors = new ArrayList<>(); descriptors.add(new FoldingDescriptor(element.getNode(), TextRange.create(getOperand().getTextRange().getEndOffset(), diff --git a/test/com/intellij/advancedExpressionFolding/PropertyUtilTest.java b/test/com/intellij/advancedExpressionFolding/PropertyUtilTest.java new file mode 100644 index 00000000..2617f58a --- /dev/null +++ b/test/com/intellij/advancedExpressionFolding/PropertyUtilTest.java @@ -0,0 +1,20 @@ +package com.intellij.advancedExpressionFolding; + +import org.junit.Test; + +import static com.intellij.advancedExpressionFolding.PropertyUtil.guessPropertyName; +import static org.junit.Assert.*; + +public class PropertyUtilTest { + @Test + public void testGuessPropertyName() { + assertEquals("", guessPropertyName("")); + assertEquals("length", guessPropertyName("length")); + assertEquals("name", guessPropertyName("getName")); + assertEquals("name", guessPropertyName("setName")); + assertEquals("enabled", guessPropertyName("isEnabled")); + assertEquals("enabledByDefault", guessPropertyName("isEnabledByDefault")); + assertEquals("html", guessPropertyName("getHTML")); + assertEquals("htmlText", guessPropertyName("isHTMLText")); + } +} \ No newline at end of file