int (final PsiFile file, int openingBraceOffset) { PsiElement element = file.findElementAt(openingBraceOffset); if (element == null || element instanceof PsiFile) return openingBraceOffset; PsiElement parent = element.getParent(); if(parent==null) return openingBraceOffset; ASTNode parentNode = parent.getNode(); if (BasicJavaAstTreeUtil.is(parentNode, BASIC_CODE_BLOCK)) { parentNode = parentNode.getTreeParent(); if (BasicJavaAstTreeUtil.is(parentNode, BASIC_METHOD) || BasicJavaAstTreeUtil.is(parentNode, BASIC_CLASS_INITIALIZER)) { TextRange range = DeclarationRangeUtil.getPossibleDeclarationAtRange(parentNode.getPsi()); if (range == null) { return parentNode.getTextRange().getStartOffset(); } return range.getStartOffset(); } else if (BasicJavaAstTreeUtil.is(parentNode, BASIC_JAVA_STATEMENT_BIT_SET)) { if (BasicJavaAstTreeUtil.is(parentNode, BASIC_BLOCK_STATEMENT) && BasicJavaAstTreeUtil.is(parentNode.getTreeParent(), BASIC_JAVA_STATEMENT_BIT_SET)) { parentNode = parentNode.getTreeParent(); } return parentNode.getTextRange().getStartOffset(); } } else if (BasicJavaAstTreeUtil.is(parentNode, BASIC_CLASS_KEYWORD_BIT_SET)) { TextRange range = DeclarationRangeUtil.getPossibleDeclarationAtRange(parent); if (range == null) { return parentNode.getTextRange().getStartOffset(); } return range.getStartOffset(); } return openingBraceOffset; }