This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 240157
Collapse All | Expand All

(-)a/php.editor/src/org/netbeans/modules/php/editor/PHPBracesMatcher.java (+50 lines)
Lines 79-84 Link Here
79
            TokenSequence<?extends PHPTokenId> ts = LexUtilities.getPHPTokenSequence(doc, offset);
79
            TokenSequence<?extends PHPTokenId> ts = LexUtilities.getPHPTokenSequence(doc, offset);
80
80
81
            if (ts != null) {
81
            if (ts != null) {
82
                // #240157
83
                if (searchForward(ts, offset)){
84
                    offset--;
85
                    if (offset < 0) {
86
                        return null;
87
                    }
88
                }
89
82
                ts.move(offset);
90
                ts.move(offset);
83
91
84
                if (!ts.moveNext()) {
92
                if (!ts.moveNext()) {
Lines 159-164 Link Here
159
            TokenSequence<?extends PHPTokenId> ts = LexUtilities.getPHPTokenSequence(doc, offset);
167
            TokenSequence<?extends PHPTokenId> ts = LexUtilities.getPHPTokenSequence(doc, offset);
160
168
161
            if (ts != null) {
169
            if (ts != null) {
170
                // #240157
171
                if (searchForward(ts, offset)){
172
                    offset--;
173
                    if (offset < 0) {
174
                        return null;
175
                    }
176
                }
177
162
                ts.move(offset);
178
                ts.move(offset);
163
179
164
                if (!ts.moveNext()) {
180
                if (!ts.moveNext()) {
Lines 212-215 Link Here
212
        }
228
        }
213
    }
229
    }
214
230
231
    private boolean searchForward(TokenSequence<? extends PHPTokenId> ts, int offset) {
232
        // if there is a brace token just before a caret position, search foward
233
        // e.g. if (isSomething()^), if (isSomething())^{
234
        // "^" is the caret
235
        if (context.isSearchingBackward()) {
236
            ts.move(offset);
237
            if (ts.movePrevious()) {
238
                Token<? extends PHPTokenId> previousToken = ts.token();
239
                if (previousToken != null && isBraceToken(previousToken)) {
240
                    return true;
241
                }
242
            }
243
        }
244
        return false;
245
    }
246
247
    private static boolean isBraceToken(Token<? extends PHPTokenId> token) {
248
        PHPTokenId id = token.id();
249
        return LexUtilities.textEquals(token.text(), '(') // NOI18N
250
                || LexUtilities.textEquals(token.text(), ')') // NOI18N
251
                || id == PHPTokenId.PHP_CURLY_OPEN
252
                || id == PHPTokenId.PHP_CURLY_CLOSE
253
                || LexUtilities.textEquals(token.text(), '[') // NOI18N
254
                || LexUtilities.textEquals(token.text(), ']') // NOI18N
255
                || LexUtilities.textEquals(token.text(), '$', '{') // NOI18N
256
                || LexUtilities.textEquals(token.text(), ':') // NOI18N
257
                || id == PHPTokenId.PHP_ENDFOR
258
                || id == PHPTokenId.PHP_ENDFOREACH
259
                || id == PHPTokenId.PHP_ENDIF
260
                || id == PHPTokenId.PHP_ENDSWITCH
261
                || id == PHPTokenId.PHP_ENDWHILE
262
                || id == PHPTokenId.PHP_ELSEIF
263
                || id == PHPTokenId.PHP_ELSE;
264
    }
215
}
265
}
(-)a/php.editor/test/unit/src/org/netbeans/modules/php/editor/PHPBracesMatcherTest.java (+253 lines)
Lines 45-50 Link Here
45
package org.netbeans.modules.php.editor;
45
package org.netbeans.modules.php.editor;
46
46
47
import javax.swing.text.BadLocationException;
47
import javax.swing.text.BadLocationException;
48
import static junit.framework.TestCase.assertEquals;
49
import static junit.framework.TestCase.assertNotNull;
50
import org.netbeans.api.editor.mimelookup.MimeLookup;
51
import org.netbeans.editor.BaseDocument;
52
import org.netbeans.modules.editor.bracesmatching.api.BracesMatchingTestUtils;
53
import org.netbeans.spi.editor.bracesmatching.BracesMatcher;
54
import org.netbeans.spi.editor.bracesmatching.BracesMatcherFactory;
55
import org.netbeans.spi.editor.bracesmatching.MatcherContext;
48
56
49
/**
57
/**
50
 * Test for PHPBracesMatcher
58
 * Test for PHPBracesMatcher
Lines 181-185 Link Here
181
                + "^endswitch;\n");
189
                + "^endswitch;\n");
182
    }
190
    }
183
191
192
    public void testIssue240157_01() throws Exception {
193
        matchesBackward("if (isSomething@(^~)){}");
194
        matchesForward("if (isSomething~(^@)){}");
195
    }
184
196
197
    public void testIssue240157_02() throws Exception {
198
        matchesBackward("if (isSomething~(@)^){}");
199
        matchesForward("if ~(isSomething~()^@){}");
200
    }
201
202
    public void testIssue240157_03() throws Exception {
203
        matchesBackward("if ~(isSomething()@)^{}");
204
        matchesForward("if (isSomething())^@{~}");
205
    }
206
207
    public void testIssue240157_04() throws Exception {
208
        matchesBackward("if (isSomething())@{^~}");
209
        matchesForward("if (isSomething())~{^@}");
210
    }
211
212
    public void testIssue240157_05() throws Exception {
213
        matchesBackward("if (isSomething())~{@}^");
214
    }
215
216
    public void testIssue240157_06() throws Exception {
217
        matchesBackward("if (isSomething^@(~)){}");
218
        matchesForward("if (isSomething^@(~)){}");
219
    }
220
221
    public void testIssue240157_07() throws Exception {
222
        matchesBackward("if @(^isSomething()~){}");
223
    }
224
225
    public void testIssue240157_08() throws Exception {
226
        matchesBackward("if @^(isSomething()~){}");
227
        matchesForward("if @^(isSomething()~){}");
228
    }
229
230
    public void testIssue240157_09() throws Exception {
231
        matchesBackward("if (isSomething()) ^@{~}");
232
        matchesForward("if (isSomething()) ^@{~}");
233
    }
234
235
    public void testIssue240157_10() throws Exception {
236
        matchesBackward("echo \"Some string with braced @${^variables[ $index ]~} in it.\";");
237
    }
238
239
    public void testIssue240157_11() throws Exception {
240
        matchesBackward("echo \"Some string with braced ${variables~[ $index @]^} in it.\";");
241
    }
242
243
    public void testIssue240157_AlternativeSyntax_01() throws Exception {
244
        matchesBackward(
245
                "if ($i == 0) @:^\n"
246
                + "    if ($j == 0) :\n"
247
                + "    endif;\n"
248
                + "~elseif ($i == 1):\n"
249
                + "    if ($j == 2):\n"
250
                + "        $l = 33;\n"
251
                + "    else:\n"
252
                + "        $l = 22;\n"
253
                + "    endif;\n"
254
                + "endif;\n"
255
                + "\n"
256
        );
257
    }
258
259
    public void testIssue240157_AlternativeSyntax_02() throws Exception {
260
        matchesBackward(
261
                "if ($i == 0) ~:\n"
262
                + "    if ($j == 0) :\n"
263
                + "    endif;\n"
264
                + "@elseif^ ($i == 1):\n"
265
                + "    if ($j == 2):\n"
266
                + "        $l = 33;\n"
267
                + "    else:\n"
268
                + "        $l = 22;\n"
269
                + "    endif;\n"
270
                + "endif;\n"
271
                + "\n"
272
        );
273
    }
274
275
    public void testIssue240157_AlternativeSyntax_03() throws Exception {
276
        matchesBackward(
277
                "if ($i == 0) :\n"
278
                + "    if ($j == 0) :\n"
279
                + "    endif;\n"
280
                + "elseif ($i == 1):\n"
281
                + "    if ($j == 2):\n"
282
                + "        $l = 33;\n"
283
                + "    else~:\n"
284
                + "        $l = 22;\n"
285
                + "    @endif^;\n"
286
                + "endif;\n"
287
                + "\n"
288
        );
289
    }
290
291
    public void testIssue240157_AlternativeSyntax_04() throws Exception {
292
        matchesBackward(
293
                "if ($i == 0) ^@:\n"
294
                + "    if ($j == 0) :\n"
295
                + "    endif;\n"
296
                + "~elseif ($i == 1):\n"
297
                + "    if ($j == 2):\n"
298
                + "        $l = 33;\n"
299
                + "    else:\n"
300
                + "        $l = 22;\n"
301
                + "    endif;\n"
302
                + "endif;\n"
303
                + "\n"
304
        );
305
    }
306
307
    public void testIssue240157_AlternativeSyntax_05() throws Exception {
308
        matchesBackward(
309
                "if ($i == 0) :\n"
310
                + "    if ($j == 0) :\n"
311
                + "    endif;\n"
312
                + "elseif ($i == 1)~:\n"
313
                + "    if ($j == 2):\n"
314
                + "        $l = 33;\n"
315
                + "    else:\n"
316
                + "        $l = 22;\n"
317
                + "    endif;\n"
318
                + "@en^dif;\n"
319
                + "\n"
320
        );
321
    }
322
323
    public void testIssue240157_AlternativeSyntax_06() throws Exception {
324
        matchesBackward(
325
                "for ($i = 0; $i < count($array); $i++) ~:\n"
326
                + "    for ($i = 0; $i < count($array); $i++) :\n"
327
                + "    endfor;\n"
328
                + "@endfor^;"
329
                + "\n"
330
        );
331
    }
332
333
    public void testIssue240157_AlternativeSyntax_07() throws Exception {
334
        matchesBackward(
335
                "for ($i = 0; $i < count($array); $i++) :\n"
336
                + "    for ($i = 0; $i < count($array); $i++) ~:\n"
337
                + "    ^@endfor;\n"
338
                + "endfor;"
339
                + "\n"
340
        );
341
    }
342
343
    public void testIssue240157_AlternativeSyntax_08() throws Exception {
344
        matchesBackward(
345
                "while (true)~:\n"
346
                + "    while(false):\n"
347
                + "        if ($a == 1):\n"
348
                + "\n            "
349
                + "        endif;\n"
350
                + "    endwhile;\n"
351
                + "@endwhile^;\n");
352
    }
353
354
    public void testIssue240157_AlternativeSyntax_09() throws Exception {
355
        matchesBackward(
356
                "while (true)~:\n"
357
                + "    while(false):\n"
358
                + "        if ($a == 1):\n"
359
                + "\n            "
360
                + "        endif;\n"
361
                + "    endwhile;\n"
362
                + "^@endwhile;\n"
363
        );
364
    }
365
366
    public void testIssue240157_AlternativeSyntax_10() throws Exception {
367
        matchesBackward(
368
                "switch ($i)~:\n"
369
                + "    case 22:\n"
370
                + "        $i = 44;\n"
371
                + "        break;\n"
372
                + "    case 33:\n"
373
                + "    case 44:\n"
374
                + "        $i = 55;\n"
375
                + "        break;\n"
376
                + "    default:\n"
377
                + "        $i = 66;\n"
378
                + "@endswitch^;\n"
379
        );
380
    }
381
382
    public void testIssue240157_AlternativeSyntax_11() throws Exception {
383
        matchesBackward(
384
                "switch ($i)~:\n"
385
                + "    case 22:\n"
386
                + "        $i = 44;\n"
387
                + "        break;\n"
388
                + "    case 33:\n"
389
                + "    case 44:\n"
390
                + "        $i = 55;\n"
391
                + "        break;\n"
392
                + "    default:\n"
393
                + "        $i = 66;\n"
394
                + "^@endswitch;\n"
395
        );
396
    }
397
398
    private void matchesBackward(String original) throws BadLocationException {
399
        matches(original, true);
400
    }
401
402
    private void matchesForward(String original) throws BadLocationException {
403
        matches(original, false);
404
    }
405
406
    /**
407
     * "^": a caret position, "@": an origin position, "~": a matching position.
408
     *
409
     * @param original code
410
     * @param backward {@code true} if search backward, otherwise {@code false}
411
     * @throws BadLocationException
412
     */
413
    private void matches(final String original, boolean backward) throws BadLocationException {
414
        BracesMatcherFactory factory = MimeLookup.getLookup(getPreferredMimeType()).lookup(BracesMatcherFactory.class);
415
        String wrappedOriginal = wrapAsPhp(original);
416
        int caretPosition = wrappedOriginal.replaceAll("(@|~)", "").indexOf('^');
417
        int originPosition = wrappedOriginal.replaceAll("(\\^|~)", "").indexOf('@');
418
        int matchingPosition = wrappedOriginal.replaceAll("(\\^|@)", "").indexOf('~');
419
        wrappedOriginal = wrappedOriginal.replaceAll("(\\^|@|~)", "");
420
421
        BaseDocument doc = getDocument(wrappedOriginal);
422
423
        MatcherContext context = BracesMatchingTestUtils.createMatcherContext(doc, caretPosition, backward, 1);
424
        BracesMatcher matcher = factory.createMatcher(context);
425
        int[] origin = null, matches = null;
426
        try {
427
            origin = matcher.findOrigin();
428
            matches = matcher.findMatches();
429
        } catch (InterruptedException ex) {
430
        }
431
432
        assertNotNull("Did not find origin for " + " position " + originPosition, origin);
433
        assertNotNull("Did not find matches for " + " position " + matchingPosition, matches);
434
435
        assertEquals("Incorrect origin", originPosition, origin[0]);
436
        assertEquals("Incorrect matches", matchingPosition, matches[0]);
437
    }
185
}
438
}

Return to bug 240157