5050import java .util .regex .Matcher ;
5151import 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 ) {
0 commit comments