package com.intellij.psi.impl.source.tree.java;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.CharTable;
import com.intellij.psi.impl.source.tree.*;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.JavaTokenType;
import com.intellij.lang.ASTNode;

public class MethodElement extends RepositoryTreeElement{
  private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.tree.java.MethodElement");

  public MethodElement() {
    super(METHOD);
  }

  protected MethodElement(IElementType type) {
    super(type);
  }

  public int getTextOffset() {
    return findChildByRole(ChildRole.NAME).getStartOffset();
  }

  public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) {
    if (first == last && first.getElementType() == ElementType.CODE_BLOCK){
      ASTNode semicolon = findChildByRole(ChildRole.CLOSING_SEMICOLON);
      if (semicolon != null){
        deleteChildInternal(semicolon);
      }
    }
    return super.addInternal(first, last, anchor, before);
  }

  public void deleteChildInternal(ASTNode child) {
    super.deleteChildInternal(child);
    if (child.getElementType() == CODE_BLOCK){
      final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
      LeafElement semicolon = Factory.createSingleLeafElement(SEMICOLON, new char[]{';'}, 0, 1, treeCharTab, getManager());
      this.addInternal(semicolon, semicolon, null, Boolean.TRUE);
    }
  }

  public ASTNode findChildByRole(int role){
    LOG.assertTrue(ChildRole.isUnique(role));
    switch(role){
      default:
        return null;

      case ChildRole.DOC_COMMENT:
        return PsiImplUtil.findDocComment(this);

      case ChildRole.MODIFIER_LIST:
        return TreeUtil.findChild(this, MODIFIER_LIST);

      case ChildRole.TYPE_PARAMETER_LIST:
        return TreeUtil.findChild(this, TYPE_PARAMETER_LIST);

      case ChildRole.NAME:
        return TreeUtil.findChild(this, IDENTIFIER);

      case ChildRole.TYPE:
        return TreeUtil.findChild(this, TYPE);

      case ChildRole.METHOD_BODY:
        return TreeUtil.findChild(this, CODE_BLOCK);

      case ChildRole.PARAMETER_LIST:
        return TreeUtil.findChild(this, PARAMETER_LIST);

      case ChildRole.THROWS_LIST:
        return TreeUtil.findChild(this, THROWS_LIST);

      case ChildRole.CLOSING_SEMICOLON:
        return TreeUtil.findChildBackward(this, SEMICOLON);
    }
  }

  public int getChildRole(ASTNode child) {
    LOG.assertTrue(child.getTreeParent() == this);
    IElementType i = child.getElementType();
    if (i == JavaTokenType.DOC_COMMENT || i == JavaDocElementType.DOC_COMMENT) {
      return getChildRole(child, ChildRole.DOC_COMMENT);
    }
    else if (i == C_STYLE_COMMENT || i == END_OF_LINE_COMMENT) {
      {
        return ChildRole.NONE;
      }
    }
    else if (i == MODIFIER_LIST) {
      return ChildRole.MODIFIER_LIST;
    }
    else if (i == TYPE_PARAMETER_LIST) {
      return ChildRole.TYPE_PARAMETER_LIST;
    }
    else if (i == CODE_BLOCK) {
      return ChildRole.METHOD_BODY;
    }
    else if (i == PARAMETER_LIST) {
      return ChildRole.PARAMETER_LIST;
    }
    else if (i == THROWS_LIST) {
      return ChildRole.THROWS_LIST;
    }
    else if (i == TYPE) {
      return getChildRole(child, ChildRole.TYPE);
    }
    else if (i == IDENTIFIER) {
      return getChildRole(child, ChildRole.NAME);
    }
    else if (i == SEMICOLON) {
      return getChildRole(child, ChildRole.CLOSING_SEMICOLON);
    }
    else {
      return ChildRole.NONE;
    }
  }
}

@@ -42,16 +42,50 @@
 .java;%0A%0A
+import com.intellij.lang.ASTNode;%0A
 import c
@@ -147,22 +147,67 @@
 lij.
-util.CharTable
+psi.JavaTokenType;%0Aimport com.intellij.psi.impl.PsiImplUtil
 ;%0Aim
@@ -252,50 +252,8 @@
 .*;%0A
-import com.intellij.psi.impl.PsiImplUtil;%0A
 impo
@@ -315,59 +315,64 @@
 lij.
-psi.JavaTokenType;%0Aimport com.intellij.lang.ASTNode
+util.CharTable;%0Aimport org.jetbrains.annotations.NotNull
 ;%0A%0Ap
@@ -1189,16 +1189,25 @@
 nternal(
+@NotNull 
 ASTNode 
@@ -1223,33 +1223,97 @@
     
-super.deleteChildInternal
+if (child.getElementType() == CODE_BLOCK)%7B%0A      final ASTNode prevWS = TreeUtil.prevLeaf
 (chi
@@ -1325,50 +1325,162 @@
     
+  
 if (
-child.getElementType() == CODE_BLOCK)%7B
+prevWS != null && prevWS.getElementType() == ElementType.WHITE_SPACE) %7B%0A        removeChild(prevWS);%0A      %7D%0A      super.deleteChildInternal(child);
 %0A   
@@ -1688,13 +1688,8 @@
     
-this.
 addI
@@ -1737,32 +1737,89 @@
 an.TRUE);%0A    %7D%0A
+    else %7B%0A      super.deleteChildInternal(child);%0A    %7D%0A
   %7D%0A%0A  public AS

Correct 'create method from usage' formatting for method braces placement set to 'next line'.