refactoring
[fedora-idea.git] / platform / lang-impl / src / com / intellij / codeInsight / AutoPopupController.java
blobcb4e3f7eb271676250712c527e4535ca61103feb
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;
19 import com.intellij.codeInsight.completion.CompletionProgressIndicator;
20 import com.intellij.codeInsight.completion.DotAutoLookupHandler;
21 import com.intellij.codeInsight.completion.impl.CompletionServiceImpl;
22 import com.intellij.codeInsight.hint.ShowParameterInfoHandler;
23 import com.intellij.ide.IdeEventQueue;
24 import com.intellij.openapi.Disposable;
25 import com.intellij.openapi.actionSystem.AnAction;
26 import com.intellij.openapi.actionSystem.DataContext;
27 import com.intellij.openapi.actionSystem.AnActionEvent;
28 import com.intellij.openapi.actionSystem.ex.ActionManagerEx;
29 import com.intellij.openapi.actionSystem.ex.AnActionListener;
30 import com.intellij.openapi.application.ApplicationManager;
31 import com.intellij.openapi.components.ServiceManager;
32 import com.intellij.openapi.editor.Editor;
33 import com.intellij.openapi.project.Project;
34 import com.intellij.openapi.util.Condition;
35 import com.intellij.psi.PsiDocumentManager;
36 import com.intellij.psi.PsiElement;
37 import com.intellij.psi.PsiFile;
38 import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
39 import com.intellij.psi.util.PsiUtilBase;
40 import com.intellij.util.Alarm;
41 import org.jetbrains.annotations.Nullable;
43 /**
46 public class AutoPopupController implements Disposable {
47 private final Project myProject;
48 private final Alarm myAlarm = new Alarm();
50 public static AutoPopupController getInstance(Project project){
51 return ServiceManager.getService(project, AutoPopupController.class);
54 public AutoPopupController(Project project) {
55 myProject = project;
56 setupListeners();
59 private void setupListeners() {
60 ActionManagerEx.getInstanceEx().addAnActionListener(new AnActionListener() {
61 public void beforeActionPerformed(AnAction action, DataContext dataContext, AnActionEvent event) {
62 myAlarm.cancelAllRequests();
65 public void beforeEditorTyping(char c, DataContext dataContext) {
66 myAlarm.cancelAllRequests();
70 public void afterActionPerformed(final AnAction action, final DataContext dataContext, AnActionEvent event) {
72 }, this);
74 IdeEventQueue.getInstance().addActivityListener(new Runnable() {
75 public void run() {
76 myAlarm.cancelAllRequests();
78 }, this);
81 public void autoPopupMemberLookup(final Editor editor, @Nullable final Condition<Editor> condition){
82 if (ApplicationManager.getApplication().isUnitTestMode()) return;
84 final CodeInsightSettings settings = CodeInsightSettings.getInstance();
85 if (settings.AUTO_POPUP_MEMBER_LOOKUP) {
86 final PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, myProject);
87 if (file == null) return;
88 final Runnable request = new Runnable(){
89 public void run(){
90 if (myProject.isDisposed()) return;
91 if (editor.isDisposed()) return;
93 PsiDocumentManager.getInstance(myProject).commitAllDocuments();
94 if (condition != null && !condition.value(editor)) return;
95 new DotAutoLookupHandler().invoke(myProject, editor, file);
98 // invoke later prevents cancelling request by keyPressed from the same action
99 ApplicationManager.getApplication().invokeLater(new Runnable() {
100 public void run() {
101 myAlarm.addRequest(request, settings.MEMBER_LOOKUP_DELAY);
107 public void invokeAutoPopupRunnable(final Runnable request, final int delay) {
108 if (ApplicationManager.getApplication().isUnitTestMode()) return;
109 final CompletionProgressIndicator currentCompletion = CompletionServiceImpl.getCompletionService().getCurrentCompletion();
110 if (currentCompletion != null) {
111 currentCompletion.closeAndFinish();
114 // invoke later prevents cancelling request by keyPressed from the same action
115 ApplicationManager.getApplication().invokeLater(new Runnable() {
116 public void run() {
117 myAlarm.addRequest(request, delay);
122 public void autoPopupParameterInfo(final Editor editor, final PsiElement highlightedMethod){
123 if (ApplicationManager.getApplication().isUnitTestMode()) return;
125 ApplicationManager.getApplication().assertIsDispatchThread();
126 final CodeInsightSettings settings = CodeInsightSettings.getInstance();
127 if (settings.AUTO_POPUP_PARAMETER_INFO) {
128 final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
129 PsiFile file = documentManager.getPsiFile(editor.getDocument());
130 if (file == null) return;
132 if (!documentManager.isUncommited(editor.getDocument())) {
133 file = documentManager.getPsiFile(InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(editor, file).getDocument());
134 if (file == null) return;
137 final PsiFile file1 = file;
138 final Runnable request = new Runnable(){
139 public void run(){
140 if (!editor.isDisposed()) {
141 documentManager.commitAllDocuments();
142 int lbraceOffset = editor.getCaretModel().getOffset() - 1;
143 new ShowParameterInfoHandler().invoke(myProject, editor, file1, lbraceOffset, highlightedMethod);
147 // invoke later prevents cancelling request by keyPressed from the same action
148 ApplicationManager.getApplication().invokeLater(new Runnable() {
149 public void run() {
150 myAlarm.addRequest(request, settings.PARAMETER_INFO_DELAY);
156 public void dispose() {