Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> supportedMethods = new HashSet<String>() {
{
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -1809,46 +1804,23 @@ 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;
}
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
Expand Down Expand Up @@ -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()) {
Expand All @@ -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;
}
Expand All @@ -1971,4 +1942,5 @@ public boolean isCollapsedByDefault(@NotNull ASTNode astNode) {
}
return false;
}

}
4 changes: 2 additions & 2 deletions src/com/intellij/advancedExpressionFolding/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ protected static String subscript(String str) {
}

private static String map(String str, Map<Character, Character> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] = "";
Expand Down
33 changes: 33 additions & 0 deletions src/com/intellij/advancedExpressionFolding/PropertyUtil.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
21 changes: 7 additions & 14 deletions src/com/intellij/advancedExpressionFolding/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<FoldingDescriptor> descriptors = new ArrayList<>();
descriptors.add(new FoldingDescriptor(element.getNode(),
TextRange.create(getOperand().getTextRange().getEndOffset(),
Expand Down
20 changes: 20 additions & 0 deletions test/com/intellij/advancedExpressionFolding/PropertyUtilTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}