Eclipse plug in developmentement (JDT): surround statement with curly braces

You can define an if-else statement on one line.

if (isFormed)
        if (i == 1)
            System.out.println("i is 1");
        else
            System.out.println("i is undefined");

I need to surround a single line if curly bracket operators use my eclipse plugin.

Here is what I tried

node.accept(new ASTVisitor() {
                    @Override
                    public boolean visit(IfStatement ifStatement) {
                        //Add Block in case of IfStatemnet if it is not there.
                        if(ifStatement != null){
                            Statement thenStatement = ifStatement.getThenStatement();
                            Statement elseStatement = ifStatement.getElseStatement();
                            String codeToReplace = "if("+ifStatement.getExpression()+")";
                            if(thenStatement instanceof Block)
                                codeToReplace += "\n"+ thenStatement + "";
                            else
                                codeToReplace += "{\n"+ thenStatement + "\n}";
                            if(elseStatement != null){
                                if(elseStatement instanceof Block)
                                    codeToReplace += "else" + elseStatement +"\n";
                                else
                                    codeToReplace += "else{\n" + elseStatement +"\n}";
                            }
                            replaceStatment(rewriter, getBlockInstence(ifStatement), codeToReplace , ifStatement);
                        }
                        return super.visit(ifStatement);
                    }
                });

This works for an external if, but does not work for an internal if / else.

PS: There is a way to do this using formatting in the settings. I have to do this part of my plugin.

+4
source share
1 answer

. Eclipse, Java → → . , . " ", " ".

, , , .

enter image description here

+1

All Articles