|
27 | 27 | import com.intellij.lang.folding.NamedFoldingDescriptor; |
28 | 28 | import com.intellij.openapi.editor.Document; |
29 | 29 | import com.intellij.openapi.util.TextRange; |
| 30 | +import com.intellij.psi.PsiComment; |
30 | 31 | import com.intellij.psi.PsiElement; |
| 32 | +import com.intellij.psi.PsiFile; |
31 | 33 | import com.intellij.psi.tree.IElementType; |
32 | 34 | import com.intellij.psi.tree.TokenSet; |
| 35 | +import com.intellij.psi.util.PsiTreeUtil; |
33 | 36 | import de.halirutan.mathematica.parsing.MathematicaElementTypes; |
| 37 | +import de.halirutan.mathematica.parsing.psi.api.CompoundExpression; |
34 | 38 | import de.halirutan.mathematica.parsing.psi.api.FunctionCall; |
| 39 | +import de.halirutan.mathematica.parsing.psi.util.Comments; |
| 40 | +import de.halirutan.mathematica.parsing.psi.util.Comments.CommentStyle; |
35 | 41 | import de.halirutan.mathematica.parsing.psi.util.LocalizationConstruct.ConstructType; |
36 | 42 | import org.jetbrains.annotations.NotNull; |
37 | 43 | import org.jetbrains.annotations.Nullable; |
|
45 | 51 | import java.util.regex.Pattern; |
46 | 52 |
|
47 | 53 | /** |
| 54 | + * Creates fold-regions from particular nodes of the AST tree. Currently, we support the folding of functions, lists, |
| 55 | + * and special "sectioning comments". |
| 56 | + * |
48 | 57 | * @author patrick (26.07.15) |
49 | 58 | */ |
50 | 59 | public class MathematicaExpressionFoldingBuilder implements FoldingBuilder { |
@@ -87,56 +96,141 @@ private void collectRegionsRecursively(@NotNull final ASTNode node, |
87 | 96 | if (matcher.end() - matcher.start() > 3) { |
88 | 97 | final String key = symbol.substring(matcher.start() + 2, matcher.end() - 1); |
89 | 98 | final TextRange nodeRange = node.getTextRange(); |
90 | | - final TextRange range = TextRange.create(nodeRange.getStartOffset()+matcher.start(), nodeRange.getStartOffset() + matcher.end()); |
| 99 | + final TextRange range = TextRange.create(nodeRange.getStartOffset() + matcher.start(), nodeRange.getStartOffset() + matcher.end()); |
91 | 100 | if (ourNamedCharacters.containsKey(key)) { |
92 | 101 | descriptors.add(new MathematicaNamedFoldingDescriptor(node, range, null, ourNamedCharacters.get(key), false)); |
93 | 102 | } |
94 | 103 | } |
95 | 104 | } |
96 | 105 | } else if (foldCharacters && elementType == MathematicaElementTypes.STRING_NAMED_CHARACTER) { |
97 | 106 | final String name = node.getText(); |
98 | | - final String key = name.substring(2,name.length()-1); |
| 107 | + final String key = name.substring(2, name.length() - 1); |
99 | 108 | if (ourNamedCharacters.containsKey(key)) { |
100 | 109 | descriptors.add(new MathematicaNamedFoldingDescriptor(node, node.getTextRange(), null, ourNamedCharacters.get(key), false)); |
101 | 110 | } |
102 | | - } else if(elementType==MathematicaElementTypes.LIST_EXPRESSION) { |
| 111 | + } else if (elementType == MathematicaElementTypes.LIST_EXPRESSION) { |
103 | 112 | // Well, we count the number of elements by counting the commas and adding one. Not bullet-proof, but will do. |
104 | | - final int numberOfListElements = node.getChildren(TokenSet.create(MathematicaElementTypes.COMMA)).length + 1; |
105 | | - descriptors.add(new NamedFoldingDescriptor( |
106 | | - node, |
107 | | - node.getTextRange(), |
108 | | - null, |
109 | | - "{ <<" + numberOfListElements + ">> }")); |
| 113 | + final int numberOfListElements = node.getChildren(TokenSet.create(MathematicaElementTypes.COMMA)).length + 1; |
| 114 | + descriptors.add(new NamedFoldingDescriptor( |
| 115 | + node, |
| 116 | + node.getTextRange(), |
| 117 | + null, |
| 118 | + "{ <<" + numberOfListElements + ">> }")); |
| 119 | + } else if (elementType == MathematicaElementTypes.FUNCTION_CALL_EXPRESSION) { |
| 120 | + final PsiElement psi = node.getPsi(); |
| 121 | + if (psi instanceof FunctionCall) { |
| 122 | + final FunctionCall functionCall = (FunctionCall) psi; |
| 123 | + if (functionCall.getScopingConstruct() != ConstructType.NULL) { |
| 124 | + descriptors.add(new NamedFoldingDescriptor( |
| 125 | + node, |
| 126 | + node.getTextRange(), |
| 127 | + null, |
| 128 | + functionCall.getHead().getText() + "[...]" |
| 129 | + )); |
| 130 | + } |
| 131 | + } |
| 132 | + } else if (node instanceof PsiComment) { |
| 133 | + collectCommentRegion(node, document, descriptors); |
| 134 | + } |
| 135 | + |
| 136 | + for (ASTNode child : node.getChildren(null)) { |
| 137 | + collectRegionsRecursively(child, document, descriptors); |
| 138 | + } |
| 139 | + |
110 | 140 | } |
111 | 141 |
|
112 | | - else if(elementType==MathematicaElementTypes.FUNCTION_CALL_EXPRESSION) |
113 | | - |
114 | | - { |
115 | | - final PsiElement psi = node.getPsi(); |
116 | | - if (psi instanceof FunctionCall) { |
117 | | - final FunctionCall functionCall = (FunctionCall) psi; |
118 | | - if (functionCall.getScopingConstruct() != ConstructType.NULL) { |
119 | | - descriptors.add(new NamedFoldingDescriptor( |
120 | | - node, |
121 | | - node.getTextRange(), |
122 | | - null, |
123 | | - functionCall.getHead().getText() + "[...]" |
124 | | - )); |
| 142 | + /** |
| 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)} |
| 145 | + * |
| 146 | + * @param node node to a PsiComment |
| 147 | + * @param document the document of the code |
| 148 | + * @param descriptors collects all folding descriptors |
| 149 | + */ |
| 150 | + private void collectCommentRegion(@NotNull final ASTNode node, |
| 151 | + @NotNull final Document document, |
| 152 | + @NotNull List<FoldingDescriptor> descriptors) { |
| 153 | + final PsiComment psi = (PsiComment) node.getPsi(); |
| 154 | + boolean isAtTopLevel = false; |
| 155 | + if (psi.getParent() instanceof PsiFile) { |
| 156 | + isAtTopLevel = true; |
| 157 | + } else { |
| 158 | + final PsiElement parent = psi.getParent(); |
| 159 | + if (parent instanceof CompoundExpression && parent.getParent() instanceof PsiFile) { |
| 160 | + isAtTopLevel = true; |
125 | 161 | } |
126 | 162 | } |
| 163 | + |
| 164 | + if (isAtTopLevel && Comments.isCorrectSectionComment(psi)) { |
| 165 | + final CommentStyle style = Comments.getStyle(psi); |
| 166 | + if (style != null && style.compareTo(CommentStyle.SUBSUBSUBSECTION) < 0) { |
| 167 | + collectCommentRegion(node, psi, style, document, descriptors); |
| 168 | + } |
| 169 | + } |
| 170 | + |
127 | 171 | } |
128 | 172 |
|
| 173 | + /** |
| 174 | + * Here the work is done for finding the correct region to fold for a sectioning-comment. The sectioning comments like |
| 175 | + * <code> |
| 176 | + * (* ::Chapter:: *) |
| 177 | + * (*this is the text of the chapter*) |
| 178 | + * </code> |
| 179 | + * |
| 180 | + * @param node AST node of the comment |
| 181 | + * @param comment the PSI element of node |
| 182 | + * @param style Extracted style of the sectioning comment of node |
| 183 | + * @param document The document that contains the comment |
| 184 | + * @param descriptors Collection for the region descriptors for folding |
| 185 | + */ |
| 186 | + private void collectCommentRegion(@NotNull ASTNode node, |
| 187 | + @NotNull final PsiComment comment, |
| 188 | + @NotNull final CommentStyle style, |
| 189 | + @NotNull final Document document, |
| 190 | + @NotNull List<FoldingDescriptor> descriptors) { |
| 191 | + final PsiFile file = comment.getContainingFile(); |
| 192 | + StringBuilder placeHolderText = new StringBuilder("<< ::"); |
| 193 | + placeHolderText.append(style).append(":: "); |
| 194 | + |
| 195 | + final int currentLine = document.getLineNumber(comment.getTextOffset()); |
| 196 | + int endOffset = document.getTextLength(); |
| 197 | + |
| 198 | + for (int i = currentLine + 1; i < document.getLineCount(); i++) { |
| 199 | + int start = document.getLineStartOffset(i); |
| 200 | + int end = document.getLineEndOffset(i); |
| 201 | + final PsiComment commentsInLine = PsiTreeUtil.findElementOfClassAtRange(file, start, end, PsiComment.class); |
| 202 | + if (commentsInLine != null && Comments.isCorrectSectionComment(commentsInLine)) { |
| 203 | + final CommentStyle commentStyle = Comments.getStyle(commentsInLine); |
| 204 | + if (commentStyle != null && commentStyle.compareTo(style) <= 0) { |
| 205 | + endOffset = start - 1; |
| 206 | + if (i < document.getLineCount() - 1) { |
| 207 | + final PsiComment nextLineComment = PsiTreeUtil.findElementOfClassAtRange( |
| 208 | + file, |
| 209 | + document.getLineStartOffset(i + 1), |
| 210 | + document.getLineEndOffset(i + 1), |
| 211 | + PsiComment.class |
| 212 | + ); |
| 213 | + if (nextLineComment != null && !Comments.isCorrectSectionComment(nextLineComment)) { |
| 214 | + placeHolderText.append(Comments.getStrippedText(nextLineComment)); |
| 215 | + } else { |
| 216 | + placeHolderText.append("No description given"); |
| 217 | + } |
| 218 | + } |
| 219 | + break; |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + placeHolderText.append(">>"); |
| 224 | + descriptors.add(new NamedFoldingDescriptor( |
| 225 | + node, |
| 226 | + TextRange.create(node.getStartOffset(), endOffset), |
| 227 | + null, |
| 228 | + placeHolderText.toString() |
| 229 | + )); |
129 | 230 |
|
130 | | - for( |
131 | | - ASTNode child |
132 | | - :node.getChildren(null)) |
133 | 231 |
|
134 | | - { |
135 | | - collectRegionsRecursively(child, document, descriptors); |
136 | 232 | } |
137 | 233 |
|
138 | | -} |
139 | | - |
140 | 234 | @Nullable |
141 | 235 | @Override |
142 | 236 | public String getPlaceholderText(@NotNull final ASTNode node) { |
|
0 commit comments