Skip to content

Commit a2fae5b

Browse files
committed
- Added folding for heredoc and nowdoc
- Fixed test FoldingTest - Added new tests in FoldingTest
1 parent af1a89d commit a2fae5b

File tree

6 files changed

+94
-4
lines changed

6 files changed

+94
-4
lines changed

php/php.editor/src/org/netbeans/modules/php/editor/csl/FoldingScanner.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.netbeans.modules.php.editor.parser.astnodes.IfStatement;
6464
import org.netbeans.modules.php.editor.parser.astnodes.MatchExpression;
6565
import org.netbeans.modules.php.editor.parser.astnodes.Program;
66+
import org.netbeans.modules.php.editor.parser.astnodes.Quote;
6667
import org.netbeans.modules.php.editor.parser.astnodes.Statement;
6768
import org.netbeans.modules.php.editor.parser.astnodes.SwitchCase;
6869
import org.netbeans.modules.php.editor.parser.astnodes.SwitchStatement;
@@ -139,6 +140,12 @@ public final class FoldingScanner {
139140
new FoldTemplate(0, 0, "#[...]") // NOI18N
140141
);
141142

143+
@NbBundle.Messages("FT_HEREDOC_NOWDOC=Heredoc/Nowdoc")
144+
public static final FoldType TYPE_HEREDOC_NOWDOC = FoldType.MEMBER.derive(
145+
"heredoc/nowdoc", // NOI18N
146+
Bundle.FT_HEREDOC_NOWDOC(),
147+
new FoldTemplate(0, 0, "<<<...") // NOI18N
148+
);
142149
private static final String LAST_CORRECT_FOLDING_PROPERTY = "LAST_CORRECT_FOLDING_PROPERY"; //NOI18N
143150
private static final boolean FOLD_PHPTAG = !Boolean.getBoolean("nb.php.editor.doNotFoldPhptag"); // NOI18N NETBEANS-5480
144151

@@ -183,6 +190,7 @@ public Map<String, List<OffsetRange>> folds(ParserResult info) {
183190
if (FOLD_PHPTAG) {
184191
processPHPTags(folds, doc);
185192
}
193+
processNowdoc(folds, doc);
186194
setFoldingProperty(doc, folds);
187195
return folds;
188196
}
@@ -271,6 +279,42 @@ private void processPHPTags(Map<String, List<OffsetRange>> folds, Document docum
271279
}
272280
}
273281

282+
private void processNowdoc(Map<String, List<OffsetRange>> folds, Document document) {
283+
if (document instanceof BaseDocument) {
284+
BaseDocument doc = (BaseDocument) document;
285+
doc.readLock();
286+
try {
287+
TokenSequence<PHPTokenId> ts = LexUtilities.getPHPTokenSequence(doc, 0);
288+
if (ts == null) {
289+
return;
290+
}
291+
ts.move(0);
292+
int startOffset = -1;
293+
int endOffset = -1;
294+
while (ts.moveNext()) {
295+
Token<PHPTokenId> token = ts.token();
296+
if (token != null) {
297+
PHPTokenId id = token.id();
298+
switch (id) {
299+
case PHP_NOWDOC_TAG_START:
300+
startOffset = ts.offset();
301+
break;
302+
case PHP_NOWDOC_TAG_END:
303+
assert startOffset != -1;
304+
endOffset = ts.offset() + token.length();
305+
getRanges(folds, TYPE_HEREDOC_NOWDOC).add(new OffsetRange(startOffset, endOffset));
306+
break;
307+
default:
308+
break;
309+
}
310+
}
311+
}
312+
} finally {
313+
doc.readUnlock();
314+
}
315+
}
316+
}
317+
274318
private void processScopes(Map<String, List<OffsetRange>> folds, List<Scope> scopes) {
275319
processUseScopes(folds, scopes);
276320
processTypeAndFunctionScopes(folds, scopes);
@@ -541,6 +585,15 @@ public void visit(Attribute node) {
541585
super.visit(node);
542586
}
543587

588+
public void visit(Quote node) {
589+
if (CancelSupport.getDefault().isCancelled()) {
590+
return;
591+
}
592+
if (node.getQuoteType().equals(Quote.Type.HEREDOC)) {
593+
addFold(node, TYPE_HEREDOC_NOWDOC);
594+
}
595+
}
596+
544597
private void addFold(final ASTNode node) {
545598
if (!(node instanceof ASTError) && !(node instanceof EmptyStatement)) {
546599
addFold(createOffsetRange(node));

php/php.editor/src/org/netbeans/modules/php/editor/csl/PHPFoldingProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class PHPFoldingProvider implements FoldTypeProvider {
4444
TYPES.add(FoldingScanner.TYPE_USE);
4545
TYPES.add(FoldingScanner.TYPE_PHPTAG);
4646
TYPES.add(FoldingScanner.TYPE_ATTRIBUTES);
47+
TYPES.add(FoldingScanner.TYPE_HEREDOC_NOWDOC);
4748
}
4849

4950
@Override

php/php.editor/test/unit/data/testfiles/enumerations_01.php.folds

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@
4646
| case C = "C";
4747
| case D = "D";
4848
| case E = "E" . "E";
49-
| case F = <<<F
49+
+ case F = <<<F
5050
| Test
5151
| Test
5252
| Test
53-
| F;
54-
| case G = <<<'G'
53+
- F;
54+
+ case G = <<<'G'
5555
| Test
5656
| Test
5757
| Test
58-
| G;
58+
- G;
5959
- }
6060

6161
+ enum Impl implements Iface1, Iface2 {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
+ echo <<<HEREDOC
4+
| Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
5+
| Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
6+
| Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
7+
| Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
8+
- HEREDOC;
9+
10+
11+
+ echo <<<'NOWDOC'
12+
| Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
13+
| Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
14+
| Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
15+
| Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
16+
- NOWDOC;
17+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
echo <<<HEREDOC
4+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
5+
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
6+
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
7+
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
8+
HEREDOC;
9+
10+
11+
echo <<<'NOWDOC'
12+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
13+
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
14+
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
15+
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
16+
NOWDOC;

php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/FoldingTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,7 @@ public void testEnumerations_01() throws Exception {
121121
checkFolds("testfiles/parser/php81/enumerations_01.php");
122122
}
123123

124+
public void testFoldingHeredocNowdoc() throws Exception {
125+
checkFolds("testfiles/parser/foldingHeredocNowdoc.php");
126+
}
124127
}

0 commit comments

Comments
 (0)