update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / editorActions / JavaBackspaceHandler.java
blob44e4985af417efafc6dcc0845000afd8fda0f409
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;
18 import com.intellij.openapi.editor.Editor;
19 import com.intellij.openapi.editor.ex.EditorEx;
20 import com.intellij.openapi.editor.highlighter.HighlighterIterator;
21 import com.intellij.psi.JavaTokenType;
22 import com.intellij.psi.PsiFile;
23 import com.intellij.psi.PsiJavaFile;
24 import com.intellij.psi.tree.IElementType;
25 import com.intellij.psi.util.PsiUtil;
27 public class JavaBackspaceHandler extends BackspaceHandlerDelegate {
28 private boolean myToDeleteGt;
30 public void beforeCharDeleted(char c, PsiFile file, Editor editor) {
31 int offset = editor.getCaretModel().getOffset() - 1;
32 myToDeleteGt = c =='<' &&
33 file instanceof PsiJavaFile &&
34 PsiUtil.isLanguageLevel5OrHigher(file)
35 && JavaTypedHandler.isAfterClassLikeIdentifierOrDot(offset, editor);
38 public boolean charDeleted(final char c, final PsiFile file, final Editor editor) {
39 int offset = editor.getCaretModel().getOffset();
40 final CharSequence chars = editor.getDocument().getCharsSequence();
41 if (editor.getDocument().getTextLength() <= offset) return false; //virtual space after end of file
43 char c1 = chars.charAt(offset);
44 if (c == '<' && myToDeleteGt) {
45 if (c1 != '>') return true;
46 handleLTDeletion(editor, offset);
47 return true;
49 return false;
52 private static void handleLTDeletion(final Editor editor, final int offset) {
53 HighlighterIterator iterator = ((EditorEx)editor).getHighlighter().createIterator(offset);
54 while (iterator.getStart() > 0 && !JavaTypedHandlerUtil.isTokenInvalidInsideReference(iterator.getTokenType())) {
55 iterator.retreat();
58 if (JavaTypedHandlerUtil.isTokenInvalidInsideReference(iterator.getTokenType())) iterator.advance();
60 int balance = 0;
61 while (!iterator.atEnd() && balance >= 0) {
62 final IElementType tokenType = iterator.getTokenType();
63 if (tokenType == JavaTokenType.LT) {
64 balance++;
66 else if (tokenType == JavaTokenType.GT) {
67 balance--;
69 else if (JavaTypedHandlerUtil.isTokenInvalidInsideReference(tokenType)) {
70 break;
73 iterator.advance();
76 if (balance < 0) {
77 editor.getDocument().deleteString(offset, offset + 1);