mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-27 16:57:49 +08:00
simple language: SimpleFoldingBuilder use visitor, cleanup
This commit is contained in:
parent
e52fcb44e6
commit
f483e89218
@ -11,16 +11,16 @@ import com.intellij.openapi.project.DumbAware;
|
|||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import com.intellij.openapi.util.TextRange;
|
import com.intellij.openapi.util.TextRange;
|
||||||
import com.intellij.openapi.util.text.StringUtil;
|
import com.intellij.openapi.util.text.StringUtil;
|
||||||
|
import com.intellij.psi.JavaRecursiveElementWalkingVisitor;
|
||||||
import com.intellij.psi.PsiElement;
|
import com.intellij.psi.PsiElement;
|
||||||
import com.intellij.psi.PsiLiteralExpression;
|
import com.intellij.psi.PsiLiteralExpression;
|
||||||
import com.intellij.psi.util.PsiTreeUtil;
|
import com.intellij.psi.util.PsiLiteralUtil;
|
||||||
import com.intellij.util.containers.ContainerUtil;
|
import com.intellij.util.containers.ContainerUtil;
|
||||||
import org.intellij.sdk.language.psi.SimpleProperty;
|
import org.intellij.sdk.language.psi.SimpleProperty;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -32,28 +32,32 @@ public class SimpleFoldingBuilder extends FoldingBuilderEx implements DumbAware
|
|||||||
FoldingGroup group = FoldingGroup.newGroup(SimpleAnnotator.SIMPLE_PREFIX_STR);
|
FoldingGroup group = FoldingGroup.newGroup(SimpleAnnotator.SIMPLE_PREFIX_STR);
|
||||||
// Initialize the list of folding regions
|
// Initialize the list of folding regions
|
||||||
List<FoldingDescriptor> descriptors = new ArrayList<>();
|
List<FoldingDescriptor> descriptors = new ArrayList<>();
|
||||||
// Get a collection of the literal expressions in the document below root
|
|
||||||
Collection<PsiLiteralExpression> literalExpressions =
|
root.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||||
PsiTreeUtil.findChildrenOfType(root, PsiLiteralExpression.class);
|
|
||||||
// Evaluate the collection
|
@Override
|
||||||
for (final PsiLiteralExpression literalExpression : literalExpressions) {
|
public void visitLiteralExpression(@NotNull PsiLiteralExpression literalExpression) {
|
||||||
String value = literalExpression.getValue() instanceof String ? (String) literalExpression.getValue() : null;
|
super.visitLiteralExpression(literalExpression);
|
||||||
if (value != null && value.startsWith(SimpleAnnotator.SIMPLE_PREFIX_STR + SimpleAnnotator.SIMPLE_SEPARATOR_STR)) {
|
|
||||||
Project project = literalExpression.getProject();
|
String value = PsiLiteralUtil.getStringLiteralContent(literalExpression);
|
||||||
String key = value.substring(
|
if (value != null && value.startsWith(SimpleAnnotator.SIMPLE_PREFIX_STR + SimpleAnnotator.SIMPLE_SEPARATOR_STR)) {
|
||||||
SimpleAnnotator.SIMPLE_PREFIX_STR.length() + SimpleAnnotator.SIMPLE_SEPARATOR_STR.length()
|
Project project = literalExpression.getProject();
|
||||||
);
|
String key = value.substring(
|
||||||
// find SimpleProperty for the given key in the project
|
SimpleAnnotator.SIMPLE_PREFIX_STR.length() + SimpleAnnotator.SIMPLE_SEPARATOR_STR.length()
|
||||||
SimpleProperty simpleProperty = ContainerUtil.getOnlyItem(SimpleUtil.findProperties(project, key));
|
);
|
||||||
if (simpleProperty != null) {
|
// find SimpleProperty for the given key in the project
|
||||||
// Add a folding descriptor for the literal expression at this node.
|
SimpleProperty simpleProperty = ContainerUtil.getOnlyItem(SimpleUtil.findProperties(project, key));
|
||||||
descriptors.add(new FoldingDescriptor(literalExpression.getNode(),
|
if (simpleProperty != null) {
|
||||||
new TextRange(literalExpression.getTextRange().getStartOffset() + 1,
|
// Add a folding descriptor for the literal expression at this node.
|
||||||
literalExpression.getTextRange().getEndOffset() - 1),
|
descriptors.add(new FoldingDescriptor(literalExpression.getNode(),
|
||||||
group, Collections.singleton(simpleProperty)));
|
new TextRange(literalExpression.getTextRange().getStartOffset() + 1,
|
||||||
|
literalExpression.getTextRange().getEndOffset() - 1),
|
||||||
|
group, Collections.singleton(simpleProperty)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
return descriptors.toArray(FoldingDescriptor.EMPTY);
|
return descriptors.toArray(FoldingDescriptor.EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,22 +71,34 @@ public class SimpleFoldingBuilder extends FoldingBuilderEx implements DumbAware
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public String getPlaceholderText(@NotNull ASTNode node) {
|
public String getPlaceholderText(@NotNull ASTNode node) {
|
||||||
if (!(node.getPsi() instanceof PsiLiteralExpression nodeElement)) {
|
if (node.getPsi() instanceof PsiLiteralExpression psiLiteralExpression) {
|
||||||
return null ;
|
String text = PsiLiteralUtil.getStringLiteralContent(psiLiteralExpression);
|
||||||
|
if (text == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String key = text.substring(SimpleAnnotator.SIMPLE_PREFIX_STR.length() +
|
||||||
|
SimpleAnnotator.SIMPLE_SEPARATOR_STR.length());
|
||||||
|
|
||||||
|
SimpleProperty simpleProperty = ContainerUtil.getOnlyItem(
|
||||||
|
SimpleUtil.findProperties(psiLiteralExpression.getProject(), key)
|
||||||
|
);
|
||||||
|
if (simpleProperty == null) {
|
||||||
|
return StringUtil.THREE_DOTS;
|
||||||
|
}
|
||||||
|
|
||||||
|
String propertyValue = simpleProperty.getValue();
|
||||||
|
// IMPORTANT: keys can come with no values, so a test for null is needed
|
||||||
|
// IMPORTANT: Convert embedded \n to backslash n, so that the string will look
|
||||||
|
// like it has LF embedded in it and embedded " to escaped "
|
||||||
|
if (propertyValue == null) {
|
||||||
|
return StringUtil.THREE_DOTS;
|
||||||
|
}
|
||||||
|
|
||||||
|
return propertyValue.replaceAll("\n", "\\n").replaceAll("\"", "\\\\\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
String key = ((String) nodeElement.getValue()).substring(
|
return null;
|
||||||
SimpleAnnotator.SIMPLE_PREFIX_STR.length() + SimpleAnnotator.SIMPLE_SEPARATOR_STR.length()
|
|
||||||
);
|
|
||||||
|
|
||||||
SimpleProperty simpleProperty = ContainerUtil.getOnlyItem(SimpleUtil.findProperties(nodeElement.getProject(), key));
|
|
||||||
if (simpleProperty == null) return StringUtil.THREE_DOTS;
|
|
||||||
|
|
||||||
String propertyValue = simpleProperty.getValue();
|
|
||||||
// IMPORTANT: keys can come with no values, so a test for null is needed
|
|
||||||
// IMPORTANT: Convert embedded \n to backslash n, so that the string will look
|
|
||||||
// like it has LF embedded in it and embedded " to escaped "
|
|
||||||
return propertyValue == null ? StringUtil.THREE_DOTS : propertyValue.replaceAll("\n", "\\n").replaceAll("\"", "\\\\\"");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user