Skip to content

Commit bb217aa

Browse files
committed
Fixed folding of named characters in comments
1 parent 2a3dea3 commit bb217aa

3 files changed

Lines changed: 36 additions & 10 deletions

File tree

src/de/halirutan/mathematica/actions/CollapseExpandNamedCharactersHandler.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.intellij.psi.PsiDocumentManager;
3333
import com.intellij.psi.PsiElement;
3434
import com.intellij.psi.PsiFile;
35+
import de.halirutan.mathematica.codeinsight.folding.MathematicaFoldingGroups;
3536
import de.halirutan.mathematica.lang.parsing.MathematicaElementTypes;
3637
import org.jetbrains.annotations.NotNull;
3738

@@ -43,7 +44,7 @@ class CollapseExpandNamedCharactersHandler implements CodeInsightActionHandler {
4344

4445
private final boolean myExpand;
4546

46-
public CollapseExpandNamedCharactersHandler(final boolean myExpand) {
47+
CollapseExpandNamedCharactersHandler(final boolean myExpand) {
4748
this.myExpand = myExpand;
4849
}
4950

@@ -57,10 +58,7 @@ public void invoke(@NotNull final Project project, @NotNull final Editor editor,
5758
Runnable processor = () -> {
5859
for (FoldRegion region : allFoldRegions) {
5960

60-
PsiElement element = EditorFoldingInfo.get(editor).getPsiElement(region);
61-
final ASTNode node = element != null ? element.getNode() : null;
62-
if (node != null &&
63-
(node.getElementType() == MathematicaElementTypes.IDENTIFIER || node.getElementType() == MathematicaElementTypes.STRING_NAMED_CHARACTER)) {
61+
if (region.getGroup() == MathematicaFoldingGroups.NAMED_CHARACTER_GROUP) {
6462
region.setExpanded(myExpand);
6563
}
6664
}

src/de/halirutan/mathematica/codeinsight/folding/MathematicaExpressionFoldingBuilder.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import java.util.regex.Matcher;
5151
import java.util.regex.Pattern;
5252

53+
import static de.halirutan.mathematica.codeinsight.folding.MathematicaFoldingGroups.NAMED_CHARACTER_GROUP;
54+
5355
/**
5456
* Creates fold-regions from particular nodes of the AST tree. Currently, we support the folding of functions, lists,
5557
* and special "sectioning comments".
@@ -98,15 +100,15 @@ private void collectRegionsRecursively(@NotNull final ASTNode node,
98100
final TextRange nodeRange = node.getTextRange();
99101
final TextRange range = TextRange.create(nodeRange.getStartOffset() + matcher.start(), nodeRange.getStartOffset() + matcher.end());
100102
if (ourNamedCharacters.containsKey(key)) {
101-
descriptors.add(new MathematicaNamedFoldingDescriptor(node, range, null, ourNamedCharacters.get(key), false));
103+
descriptors.add(new MathematicaNamedFoldingDescriptor(node, range, NAMED_CHARACTER_GROUP, ourNamedCharacters.get(key), false));
102104
}
103105
}
104106
}
105107
} else if (foldCharacters && elementType == MathematicaElementTypes.STRING_NAMED_CHARACTER) {
106108
final String name = node.getText();
107109
final String key = name.substring(2, name.length() - 1);
108110
if (ourNamedCharacters.containsKey(key)) {
109-
descriptors.add(new MathematicaNamedFoldingDescriptor(node, node.getTextRange(), null, ourNamedCharacters.get(key), false));
111+
descriptors.add(new MathematicaNamedFoldingDescriptor(node, node.getTextRange(), NAMED_CHARACTER_GROUP, ourNamedCharacters.get(key), false));
110112
}
111113
} else if (elementType == MathematicaElementTypes.LIST_EXPRESSION) {
112114
// Well, we count the number of elements by counting the commas and adding one. Not bullet-proof, but will do.
@@ -120,7 +122,7 @@ private void collectRegionsRecursively(@NotNull final ASTNode node,
120122
final PsiElement psi = node.getPsi();
121123
if (psi instanceof FunctionCall) {
122124
final FunctionCall functionCall = (FunctionCall) psi;
123-
if (functionCall.getScopingConstruct() != MScope.NULL) {
125+
if (functionCall.getScopingConstruct() != MScope.NULL_SCOPE) {
124126
descriptors.add(new NamedFoldingDescriptor(
125127
node,
126128
node.getTextRange(),
@@ -130,6 +132,9 @@ private void collectRegionsRecursively(@NotNull final ASTNode node,
130132
}
131133
}
132134
} else if (node instanceof PsiComment) {
135+
if (foldCharacters) {
136+
foldNamedCharacters(node, descriptors);
137+
}
133138
collectCommentRegion(node, document, descriptors);
134139
}
135140

@@ -140,8 +145,6 @@ private void collectRegionsRecursively(@NotNull final ASTNode node,
140145
}
141146

142147
/**
143-
* Entry method that only check if we have a valid section-comment. The work is done in
144-
* {@link MathematicaExpressionFoldingBuilder#collectCommentRegion(PsiComment, Document, List)}
145148
*
146149
* @param node node to a PsiComment
147150
* @param document the document of the code
@@ -233,6 +236,21 @@ private void collectCommentRegion(@NotNull ASTNode node,
233236

234237
}
235238

239+
private void foldNamedCharacters(final ASTNode node, @NotNull List<FoldingDescriptor> descriptors ) {
240+
final String text = node.getText();
241+
final Matcher matcher = namedCharacterPattern.matcher(text);
242+
while (matcher.find()) {
243+
if (matcher.end() - matcher.start() > 3) {
244+
final String key = text.substring(matcher.start() + 2, matcher.end() - 1);
245+
final TextRange nodeRange = node.getTextRange();
246+
final TextRange range = TextRange.create(nodeRange.getStartOffset() + matcher.start(), nodeRange.getStartOffset() + matcher.end());
247+
if (ourNamedCharacters.containsKey(key)) {
248+
descriptors.add(new MathematicaNamedFoldingDescriptor(node, range, NAMED_CHARACTER_GROUP, ourNamedCharacters.get(key), false));
249+
}
250+
}
251+
}
252+
}
253+
236254
@Nullable
237255
@Override
238256
public String getPlaceholderText(@NotNull final ASTNode node) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package de.halirutan.mathematica.codeinsight.folding;
2+
3+
import com.intellij.openapi.editor.FoldingGroup;
4+
5+
/**
6+
* @author patrick (17.09.17).
7+
*/
8+
public interface MathematicaFoldingGroups {
9+
FoldingGroup NAMED_CHARACTER_GROUP = FoldingGroup.newGroup("NamedCharactersGroup");
10+
}

0 commit comments

Comments
 (0)