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 270934
Collapse All | Expand All

(-)a/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java (-7 / +58 lines)
Lines 2306-2314 Link Here
2306
            }
2306
            }
2307
            return true;
2307
            return true;
2308
        }
2308
        }
2309
        
2310
        /**
2311
         * Finds the end of the line (for brace insertion) after the statement Tree, provided
2312
         * the statement is followed nu whitespace only.
2313
         * @param statement
2314
         * @return 
2315
         */
2316
        private int findNewlineAfterStatement(Tree statement) {
2317
            int pos = (int)sp.getEndPosition(root, statement);
2318
            if (pos < 0) {
2319
                return pos;
2320
            }
2321
            int index = tokens.index();
2322
            try {
2323
                tokens.move(pos);
2324
                while (tokens.moveNext()) {
2325
                    Token<JavaTokenId> tukac = tokens.token();
2326
                    switch (tukac.id()) {
2327
                        case WHITESPACE: {
2328
                            int nl = tukac.text().toString().indexOf('\n');
2329
                            if (nl != -1) {
2330
                                return tokens.offset() + nl + 1;
2331
                            }
2332
                            break;
2333
                        }
2334
                        case LINE_COMMENT:
2335
                            // up to and including EOL:
2336
                            return tokens.offset() + tukac.length();
2337
                        case BLOCK_COMMENT:
2338
                            break;
2339
                        default:
2340
                            return pos;
2341
                    }
2342
                }
2343
            } finally {
2344
                tokens.moveIndex(index);
2345
                tokens.moveNext();
2346
            }
2347
            return pos;
2348
        }
2309
2349
2310
        @Override
2350
        @Override
2311
        public Boolean visitIf(IfTree node, Void p) {
2351
        public Boolean visitIf(final IfTree node, Void p) {
2312
            accept(IF);
2352
            accept(IF);
2313
            boolean old = continuationIndent;
2353
            boolean old = continuationIndent;
2314
            try {
2354
            try {
Lines 2320-2328 Link Here
2320
            }
2360
            }
2321
            StatementTree elseStat = node.getElseStatement();
2361
            StatementTree elseStat = node.getElseStatement();
2322
            CodeStyle.BracesGenerationStyle redundantIfBraces = cs.redundantIfBraces();
2362
            CodeStyle.BracesGenerationStyle redundantIfBraces = cs.redundantIfBraces();
2363
            int eoln = findNewlineAfterStatement(node);
2323
            if ((elseStat != null && redundantIfBraces == CodeStyle.BracesGenerationStyle.ELIMINATE && danglingElseChecker.hasDanglingElse(node.getThenStatement())) ||
2364
            if ((elseStat != null && redundantIfBraces == CodeStyle.BracesGenerationStyle.ELIMINATE && danglingElseChecker.hasDanglingElse(node.getThenStatement())) ||
2324
                    (redundantIfBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < sp.getEndPosition(root, node) || node.getCondition().getKind() == Tree.Kind.ERRONEOUS)))
2365
                    (redundantIfBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < eoln || node.getCondition().getKind() == Tree.Kind.ERRONEOUS))) {
2325
                redundantIfBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE;
2366
                redundantIfBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE;
2367
            }
2326
            lastIndent = indent;
2368
            lastIndent = indent;
2327
            boolean prevblock = wrapStatement(cs.wrapIfStatement(), redundantIfBraces, cs.spaceBeforeIfLeftBrace() ? 1 : 0, node.getThenStatement());
2369
            boolean prevblock = wrapStatement(cs.wrapIfStatement(), redundantIfBraces, cs.spaceBeforeIfLeftBrace() ? 1 : 0, node.getThenStatement());
2328
            if (elseStat != null) {
2370
            if (elseStat != null) {
Lines 2351-2358 Link Here
2351
                    lastIndent -= indentSize;
2393
                    lastIndent -= indentSize;
2352
                } else {
2394
                } else {
2353
                    redundantIfBraces = cs.redundantIfBraces();
2395
                    redundantIfBraces = cs.redundantIfBraces();
2354
                    if (redundantIfBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < sp.getEndPosition(root, node)))
2396
                    if (redundantIfBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < eoln)) {
2355
                        redundantIfBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE;
2397
                        redundantIfBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE;
2398
                    }
2356
                    wrapElse = cs.wrapIfStatement();
2399
                    wrapElse = cs.wrapIfStatement();
2357
                }
2400
                }
2358
                wrapStatement(wrapElse, redundantIfBraces, cs.spaceBeforeElseLeftBrace() ? 1 : 0, preserveNewLine, elseStat);
2401
                wrapStatement(wrapElse, redundantIfBraces, cs.spaceBeforeElseLeftBrace() ? 1 : 0, preserveNewLine, elseStat);
Lines 2366-2374 Link Here
2366
            lastIndent = indent;
2409
            lastIndent = indent;
2367
            boolean old = continuationIndent;
2410
            boolean old = continuationIndent;
2368
            try {
2411
            try {
2412
                int eoln = findNewlineAfterStatement(node);
2369
                CodeStyle.BracesGenerationStyle redundantDoWhileBraces = cs.redundantDoWhileBraces();
2413
                CodeStyle.BracesGenerationStyle redundantDoWhileBraces = cs.redundantDoWhileBraces();
2370
                if (redundantDoWhileBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < sp.getEndPosition(root, node) || node.getCondition().getKind() == Tree.Kind.ERRONEOUS))
2414
                if (redundantDoWhileBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset <  eoln || node.getCondition().getKind() == Tree.Kind.ERRONEOUS)) {
2371
                    redundantDoWhileBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE;
2415
                    redundantDoWhileBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE;
2416
                }
2372
                boolean isBlock = node.getStatement().getKind() == Tree.Kind.BLOCK || redundantDoWhileBraces == CodeStyle.BracesGenerationStyle.GENERATE;
2417
                boolean isBlock = node.getStatement().getKind() == Tree.Kind.BLOCK || redundantDoWhileBraces == CodeStyle.BracesGenerationStyle.GENERATE;
2373
                if (isBlock && redundantDoWhileBraces == CodeStyle.BracesGenerationStyle.ELIMINATE) {
2418
                if (isBlock && redundantDoWhileBraces == CodeStyle.BracesGenerationStyle.ELIMINATE) {
2374
                    Iterator<? extends StatementTree> stats = ((BlockTree)node.getStatement()).getStatements().iterator();
2419
                    Iterator<? extends StatementTree> stats = ((BlockTree)node.getStatement()).getStatements().iterator();
Lines 2408-2416 Link Here
2408
                continuationIndent = old;
2453
                continuationIndent = old;
2409
            }
2454
            }
2410
            lastIndent = indent;
2455
            lastIndent = indent;
2456
            int eoln = findNewlineAfterStatement(node);
2411
            CodeStyle.BracesGenerationStyle redundantWhileBraces = cs.redundantWhileBraces();
2457
            CodeStyle.BracesGenerationStyle redundantWhileBraces = cs.redundantWhileBraces();
2412
            if (redundantWhileBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < sp.getEndPosition(root, node) || node.getCondition().getKind() == Tree.Kind.ERRONEOUS))
2458
            if (redundantWhileBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < eoln || node.getCondition().getKind() == Tree.Kind.ERRONEOUS)) {
2413
                redundantWhileBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE;
2459
                redundantWhileBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE;
2460
            }
2414
            wrapStatement(cs.wrapWhileStatement(), redundantWhileBraces, cs.spaceBeforeWhileLeftBrace() ? 1 : 0, node.getStatement());
2461
            wrapStatement(cs.wrapWhileStatement(), redundantWhileBraces, cs.spaceBeforeWhileLeftBrace() ? 1 : 0, node.getStatement());
2415
            return true;
2462
            return true;
2416
        }
2463
        }
Lines 2471-2478 Link Here
2471
            }
2518
            }
2472
            lastIndent = indent;
2519
            lastIndent = indent;
2473
            CodeStyle.BracesGenerationStyle redundantForBraces = cs.redundantForBraces();
2520
            CodeStyle.BracesGenerationStyle redundantForBraces = cs.redundantForBraces();
2474
            if (redundantForBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < sp.getEndPosition(root, node)))
2521
            int eoln = findNewlineAfterStatement(node);
2522
            if (redundantForBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < eoln || node.getCondition().getKind() == Tree.Kind.ERRONEOUS)) {
2475
                redundantForBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE;
2523
                redundantForBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE;
2524
            }
2476
            wrapStatement(cs.wrapForStatement(), redundantForBraces, cs.spaceBeforeForLeftBrace() ? 1 : 0, node.getStatement());
2525
            wrapStatement(cs.wrapForStatement(), redundantForBraces, cs.spaceBeforeForLeftBrace() ? 1 : 0, node.getStatement());
2477
            return true;            
2526
            return true;            
2478
        }
2527
        }
Lines 2496-2503 Link Here
2496
            }
2545
            }
2497
            lastIndent = indent;
2546
            lastIndent = indent;
2498
            CodeStyle.BracesGenerationStyle redundantForBraces = cs.redundantForBraces();
2547
            CodeStyle.BracesGenerationStyle redundantForBraces = cs.redundantForBraces();
2499
            if (redundantForBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < sp.getEndPosition(root, node)))
2548
            int eoln = findNewlineAfterStatement(node);
2549
            if (redundantForBraces == CodeStyle.BracesGenerationStyle.GENERATE && (startOffset > sp.getStartPosition(root, node) || endOffset < eoln)) {
2500
                redundantForBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE;
2550
                redundantForBraces = CodeStyle.BracesGenerationStyle.LEAVE_ALONE;
2551
            }
2501
            wrapStatement(cs.wrapForStatement(), redundantForBraces, cs.spaceBeforeForLeftBrace() ? 1 : 0, node.getStatement());
2552
            wrapStatement(cs.wrapForStatement(), redundantForBraces, cs.spaceBeforeForLeftBrace() ? 1 : 0, node.getStatement());
2502
            return true;
2553
            return true;
2503
        }
2554
        }
(-)a/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java (-3 / +25 lines)
Lines 4205-4212 Link Here
4205
                + "            i++;\n"
4205
                + "            i++;\n"
4206
                + "    }\n"
4206
                + "    }\n"
4207
                + "}\n";
4207
                + "}\n";
4208
        String golden2 =
4209
                "package hierbas.del.litoral;\n"
4210
                + "public class Test{\n"
4211
                + "    public void test() {\n"
4212
                + "        int i = 5;\n"
4213
                + "        if (i > 0)\n"
4214
                + "            i++;\n"
4215
                + "        if (i > 0)\n"
4216
                + "            i++;\n"
4217
                + "    }\n"
4218
                + "}\n";
4208
        reformat(doc, content, golden, 92, 128);
4219
        reformat(doc, content, golden, 92, 128);
4209
        reformat(doc, content, golden, 92, 127);
4220
        reformat(doc, content, golden2, 92, 127);
4210
4221
4211
        golden =
4222
        golden =
4212
                "package hierbas.del.litoral;\n"
4223
                "package hierbas.del.litoral;\n"
Lines 4220-4227 Link Here
4220
                + "        }\n"
4231
                + "        }\n"
4221
                + "    }\n"
4232
                + "    }\n"
4222
                + "}\n";
4233
                + "}\n";
4223
        reformat(doc, content, golden, 128, 163);
4234
        golden2 =
4224
        reformat(doc, content, golden, 127, 163);
4235
                "package hierbas.del.litoral;\n"
4236
                + "public class Test{\n"
4237
                + "    public void test() {\n"
4238
                + "        int i = 5;\n"
4239
                + "        if (i > 0)\n"
4240
                + "            i++;\n"
4241
                + "        if (i > 0)\n"
4242
                + "            i++;\n"
4243
                + "    }\n"
4244
                + "}\n";
4245
        reformat(doc, content, golden, 128, 164);
4246
        reformat(doc, content, golden2, 127, 163);
4225
    }
4247
    }
4226
4248
4227
    public void test177858() throws Exception {
4249
    public void test177858() throws Exception {

Return to bug 270934