update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / codeInsight / editorActions / smartEnter / SmartEnterAction.java
blob4fdf4dfad206b58d353c9cd9cf8bcd2da7700da9
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.
17 package com.intellij.codeInsight.editorActions.smartEnter;
19 import com.intellij.codeInsight.actions.BaseCodeInsightAction;
20 import com.intellij.codeInsight.editorActions.enter.EnterAfterUnmatchedBraceHandler;
21 import com.intellij.lang.Language;
22 import com.intellij.openapi.actionSystem.DataContext;
23 import com.intellij.openapi.actionSystem.IdeActions;
24 import com.intellij.openapi.actionSystem.PlatformDataKeys;
25 import com.intellij.openapi.editor.Document;
26 import com.intellij.openapi.editor.Editor;
27 import com.intellij.openapi.editor.actionSystem.EditorAction;
28 import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
29 import com.intellij.openapi.editor.actionSystem.EditorActionManager;
30 import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler;
31 import com.intellij.openapi.project.Project;
32 import com.intellij.psi.PsiFile;
33 import com.intellij.psi.util.PsiUtilBase;
34 import com.intellij.util.text.CharArrayUtil;
36 import java.util.List;
38 /**
39 * @author max
41 public class SmartEnterAction extends EditorAction {
42 public SmartEnterAction() {
43 super(new Handler());
46 @Override
47 protected Editor getEditor(final DataContext dataContext) {
48 final Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
49 if (editor == null) return null;
50 Project project = editor.getProject();
51 if (project == null) project = PlatformDataKeys.PROJECT.getData(dataContext);
52 return project == null ? null : BaseCodeInsightAction.getInjectedEditor(project, editor);
55 private static class Handler extends EditorWriteActionHandler {
56 public boolean isEnabled(Editor editor, DataContext dataContext) {
57 return getEnterHandler().isEnabled(editor, dataContext);
60 public void executeWriteAction(Editor editor, DataContext dataContext) {
61 final Document doc = editor.getDocument();
62 Project project = PlatformDataKeys.PROJECT.getData(dataContext);
63 if (project == null || editor.isOneLineMode()) {
64 plainEnter(editor, dataContext);
65 return;
67 final int caretOffset = editor.getCaretModel().getOffset();
68 if (isInPreceedingBlanks(editor)) {
69 final int caretLine = doc.getLineNumber(caretOffset);
70 if (caretLine > 0) {
71 int prevLineEnd = doc.getLineEndOffset(caretLine - 1);
72 editor.getCaretModel().moveToOffset(prevLineEnd);
74 EditorActionHandler enterHandler = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_ENTER);
75 enterHandler.execute(editor, dataContext);
76 return;
79 PsiFile psiFile = PsiUtilBase.getPsiFileInEditor(editor, project);
80 if (psiFile == null) {
81 plainEnter(editor, dataContext);
82 return;
85 if (EnterAfterUnmatchedBraceHandler.isAfterUnmatchedLBrace(editor, caretOffset, psiFile.getFileType())) {
86 EditorActionHandler enterHandler = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_ENTER);
87 enterHandler.execute(editor, dataContext);
88 return;
91 final Language language = PsiUtilBase.getLanguageInEditor(editor, project);
92 boolean processed = false;
93 if (language != null) {
94 final List<SmartEnterProcessor> processors = SmartEnterProcessors.INSTANCE.forKey(language);
95 if (!processors.isEmpty()) {
96 for (SmartEnterProcessor processor : processors) {
97 if (processor.process(project, editor, psiFile)) {
98 processed = true;
99 break;
104 if (!processed) {
105 plainEnter(editor, dataContext);
109 private static boolean isInPreceedingBlanks(Editor editor) {
110 int offset = editor.getCaretModel().getOffset();
111 final Document doc = editor.getDocument();
112 CharSequence chars = doc.getCharsSequence();
113 if (offset == doc.getTextLength() || chars.charAt(offset) == '\n') return false;
115 int newLineOffset = CharArrayUtil.shiftBackward(chars, offset - 1, " \t");
116 return newLineOffset < 0 || chars.charAt(newLineOffset) == '\n';
120 public static void plainEnter(Editor editor, DataContext dataContext) {
121 getEnterHandler().execute(editor, dataContext);
124 private static EditorActionHandler getEnterHandler() {
125 return EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_START_NEW_LINE);