Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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,11 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AdvancedExpressionFoldingBuilder extends FoldingBuilderEx {
import static com.intellij.advancedExpressionFolding.PropertyUtil.guessPropertyName;

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 @@ -1297,12 +1299,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 @@ -1828,29 +1828,6 @@ 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();
}

@Nullable
private static NumberLiteral getSlicePosition(@NotNull PsiElement parent, @NotNull Expression qualifierExpression,
@NotNull PsiBinaryExpression a2b, @NotNull Document document) {
Expand Down Expand Up @@ -1928,8 +1905,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 +1916,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 +1947,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 @@ -102,12 +102,12 @@ public String getPlaceholderText() {
? operands.get(i + 1).getTextRange().getStartOffset() + 1
: operands.get(i + 1).getTextRange().getStartOffset();
int fI = i;
StringBuilder sI = new StringBuilder().append(buf[0]);
StringBuilder sI = new StringBuilder(buf[0].length() + 2).append(buf[0]);
if (!(operands.get(fI + 1) instanceof CharSequenceLiteral)) {
sI.append("$");
sI.append('$');
}
if (!(operands.get(fI + 1) instanceof Variable) && !(operands.get(fI + 1) instanceof CharSequenceLiteral)) {
sI.append("{");
sI.append('{');
buf[0] = "}";
} else {
buf[0] = "";
Expand Down
29 changes: 29 additions & 0 deletions src/com/intellij/advancedExpressionFolding/PropertyUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.intellij.advancedExpressionFolding;

import org.jetbrains.annotations.NotNull;

public class PropertyUtil {

@NotNull
public static String guessPropertyName(@NotNull String text) {
StringBuilder sb = new StringBuilder();
if (text.startsWith("get")) {
sb.append(text.substring(3));
Comment thread
stokito marked this conversation as resolved.
Outdated
} 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();
}
}
21 changes: 7 additions & 14 deletions src/com/intellij/advancedExpressionFolding/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,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"));
}
}