title: Inserting block/braces in If-else statements using JDT
body: <p>I'm creating an Eclipse plugin that should insert block to one line if-else statements.</p>
  
  <p>[Just like Eclipse facilitates via setting a preference for editor on Save-action]</p>
  
  
  
  <p>for e.g.</p>
  
  <pre class="lang-java prettyprint-override"><code>if (isFormed)
      if (i == 1)
          System.out.println("i is 1");
      else
          System.out.println("i is undefined");
  </code></pre>
  
  <p>should be replaced with </p>
  
  <pre class="lang-java prettyprint-override"><code>if (isFormed)
  {
      if (i == 1)
          {
             System.out.println("i is 1");
          }
      else
          {
          System.out.println("i is undefined");
          }
  }
  </code></pre>
  
  <p>Here is how I'm visiting &amp; replacing the statement inside AST</p>
  
  <pre class="lang-java prettyprint-override"><code>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);
                  }
              });
  </code></pre>
  
  <p>&amp; once it's whole visited I commit the working copy.
  This adds block to the outer if-else, &amp; not to the inner ones.</p>
  
  <p>I also tried replacing the document &amp; committing it while visit like below:</p>
  
  <pre class="lang-java prettyprint-override"><code>IDocument document = new org.eclipse.jface.text.Document(iCompilationUnit.getSource());
  TextEdit edits = mCompilationUnit.rewrite(document, null);
  document.replace(ifStatement.getStartPosition(), ifStatement.getLength(), codeToReplace);
  edits.apply(document);
  iCompilationUnit.getBuffer().setContents(document.get());
  iCompilationUnit.commitWorkingCopy(true, new NullProgressMonitor());
  </code></pre>
  
  <p>But this adds braces at wrong places for inner if-else &amp; whole code gets messed up as <strong>IT DOES NOT HAVE UPDATED "offset" &amp; "length" FOR THE CODE TO BE REPLACED</strong> &amp; hence it keeps replacing at wrong places &amp; messes up.</p>
  
  <p>//void org.eclipse.jface.text.IDocument.replace(int offset, int length, String textTobeReplaced)</p>
  
  <p>I also tried getting how eclipse is doing it. But couldn't reach that point.
  <strong>Can anyone help solving this?</strong> Or any sort of plugin code I should refer? Even if I can get <strong>which eclipse plugin does this</strong> I can try de-compiling it.</p>