update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / editorActions / smartEnter / BlockBraceFixer.java
blob46982ddccd0293151e92a00637b142d2c29f6c0f
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInsight.editorActions.smartEnter;
18 import com.intellij.codeInsight.editorActions.enter.EnterAfterUnmatchedBraceHandler;
19 import com.intellij.openapi.editor.Editor;
20 import com.intellij.openapi.fileTypes.FileType;
21 import com.intellij.psi.PsiCodeBlock;
22 import com.intellij.psi.PsiElement;
23 import com.intellij.psi.PsiStatement;
24 import com.intellij.util.IncorrectOperationException;
26 /**
27 * Created by IntelliJ IDEA.
28 * User: max
29 * Date: Sep 5, 2003
30 * Time: 6:30:10 PM
31 * To change this template use Options | File Templates.
33 public class BlockBraceFixer implements Fixer{
34 public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
35 if (psiElement instanceof PsiCodeBlock && afterUnmatchedBrace(editor,psiElement.getContainingFile().getFileType())) {
36 PsiCodeBlock block = (PsiCodeBlock) psiElement;
37 int stopOffset = block.getTextRange().getEndOffset();
38 final PsiStatement[] statements = block.getStatements();
39 if (statements.length > 0) {
40 stopOffset = statements[0].getTextRange().getEndOffset();
42 editor.getDocument().insertString(stopOffset, "}");
46 private boolean afterUnmatchedBrace(Editor editor, FileType fileType) {
47 return EnterAfterUnmatchedBraceHandler.isAfterUnmatchedLBrace(editor, editor.getCaretModel().getOffset(), fileType);