33163b76381f6277a2688024996bf77a0803df40
[idea/community.git] / platform / lang-impl / src / com / intellij / ide / actions / GotoClassAction.java
1 /*
2  * Copyright 2000-2015 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.ide.actions;
17
18 import com.intellij.codeInsight.navigation.NavigationUtil;
19 import com.intellij.featureStatistics.FeatureUsageTracker;
20 import com.intellij.ide.IdeBundle;
21 import com.intellij.ide.structureView.StructureView;
22 import com.intellij.ide.structureView.StructureViewBuilder;
23 import com.intellij.ide.structureView.StructureViewTreeElement;
24 import com.intellij.ide.util.EditSourceUtil;
25 import com.intellij.ide.util.gotoByName.*;
26 import com.intellij.ide.util.treeView.smartTree.TreeElement;
27 import com.intellij.lang.Language;
28 import com.intellij.lang.LanguageStructureViewBuilder;
29 import com.intellij.lang.PsiStructureViewFactory;
30 import com.intellij.navigation.AnonymousElementProvider;
31 import com.intellij.navigation.ChooseByNameRegistry;
32 import com.intellij.navigation.NavigationItem;
33 import com.intellij.openapi.actionSystem.*;
34 import com.intellij.openapi.application.AccessToken;
35 import com.intellij.openapi.application.ReadAction;
36 import com.intellij.openapi.extensions.Extensions;
37 import com.intellij.openapi.fileEditor.FileEditor;
38 import com.intellij.openapi.fileEditor.FileEditorManager;
39 import com.intellij.openapi.fileEditor.OpenFileDescriptor;
40 import com.intellij.openapi.project.DumbAware;
41 import com.intellij.openapi.project.DumbService;
42 import com.intellij.openapi.project.Project;
43 import com.intellij.openapi.ui.playback.commands.ActionCommand;
44 import com.intellij.openapi.util.Disposer;
45 import com.intellij.openapi.vfs.VirtualFile;
46 import com.intellij.pom.Navigatable;
47 import com.intellij.psi.PsiDocumentManager;
48 import com.intellij.psi.PsiElement;
49 import com.intellij.psi.codeStyle.MinusculeMatcher;
50 import com.intellij.psi.codeStyle.NameUtil;
51 import com.intellij.psi.util.PsiUtilCore;
52 import org.jetbrains.annotations.NotNull;
53 import org.jetbrains.annotations.Nullable;
54
55 import java.awt.*;
56 import java.awt.event.InputEvent;
57 import java.util.ArrayList;
58 import java.util.List;
59
60 public class GotoClassAction extends GotoActionBase implements DumbAware {
61   @Override
62   public void actionPerformed(@NotNull AnActionEvent e) {
63     Project project = e.getProject();
64     if (project == null) return;
65
66     if (!DumbService.getInstance(project).isDumb()) {
67       super.actionPerformed(e);
68     }
69     else {
70       DumbService.getInstance(project).showDumbModeNotification(IdeBundle.message("go.to.class.dumb.mode.message"));
71       AnAction action = ActionManager.getInstance().getAction(GotoFileAction.ID);
72       InputEvent event = ActionCommand.getInputEvent(GotoFileAction.ID);
73       Component component = e.getData(PlatformDataKeys.CONTEXT_COMPONENT);
74       ActionManager.getInstance().tryToExecute(action, event, component, e.getPlace(), true);
75     }
76   }
77
78   @Override
79   public void gotoActionPerformed(@NotNull AnActionEvent e) {
80     final Project project = e.getProject();
81     if (project == null) return;
82
83     FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.class");
84
85     PsiDocumentManager.getInstance(project).commitAllDocuments();
86
87     final GotoClassModel2 model = new GotoClassModel2(project);
88     showNavigationPopup(e, model, new GotoActionCallback<Language>() {
89       @Override
90       protected ChooseByNameFilter<Language> createFilter(@NotNull ChooseByNamePopup popup) {
91         return new ChooseByNameLanguageFilter(popup, model, GotoClassSymbolConfiguration.getInstance(project), project);
92       }
93
94       @Override
95       public void elementChosen(ChooseByNamePopup popup, Object element) {
96         AccessToken token = ReadAction.start();
97         try {
98           if (element instanceof PsiElement) {
99             final PsiElement psiElement = getElement(((PsiElement)element), popup);
100             final VirtualFile file = PsiUtilCore.getVirtualFile(psiElement);
101
102             if (file != null && popup.getLinePosition() != -1) {
103               OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, popup.getLinePosition(), popup.getColumnPosition());
104               Navigatable n = descriptor.setUseCurrentWindow(popup.isOpenInCurrentWindowRequested());
105               if (n.canNavigate()) {
106                 n.navigate(true);
107                 return;
108               }
109             }
110
111             if (psiElement != null && file != null && popup.getMemberPattern() != null) {
112               NavigationUtil.activateFileWithPsiElement(psiElement, !popup.isOpenInCurrentWindowRequested());
113               Navigatable member = findMember(popup.getMemberPattern(), psiElement, file);
114               if (member != null) {
115                 member.navigate(true);
116               }
117             }
118
119             NavigationUtil.activateFileWithPsiElement(psiElement, !popup.isOpenInCurrentWindowRequested());
120           }
121           else {
122             EditSourceUtil.navigate(((NavigationItem)element), true, popup.isOpenInCurrentWindowRequested());
123           }
124         }
125         finally {
126           token.finish();
127         }
128       }
129     }, IdeBundle.message("go.to.class.toolwindow.title"), true);
130   }
131
132   @Nullable
133   private static Navigatable findMember(String pattern, PsiElement psiElement, VirtualFile file) {
134     final PsiStructureViewFactory factory = LanguageStructureViewBuilder.INSTANCE.forLanguage(psiElement.getLanguage());
135     final StructureViewBuilder builder = factory == null ? null : factory.getStructureViewBuilder(psiElement.getContainingFile());
136     final FileEditor[] editors = FileEditorManager.getInstance(psiElement.getProject()).getEditors(file);
137     if (builder == null || editors.length == 0) {
138       return null;
139     }
140
141     final StructureView view = builder.createStructureView(editors[0], psiElement.getProject());
142     try {
143       final StructureViewTreeElement element = findElement(view.getTreeModel().getRoot(), psiElement, 4);
144       if (element == null) {
145         return null;
146       }
147
148       final MinusculeMatcher matcher = new MinusculeMatcher(pattern, NameUtil.MatchingCaseSensitivity.NONE);
149       int max = Integer.MIN_VALUE;
150       Object target = null;
151       for (TreeElement treeElement : element.getChildren()) {
152         if (treeElement instanceof StructureViewTreeElement) {
153           String presentableText = treeElement.getPresentation().getPresentableText();
154           if (presentableText != null) {
155             final int degree = matcher.matchingDegree(presentableText);
156             if (degree > max) {
157               max = degree;
158               target = ((StructureViewTreeElement)treeElement).getValue();
159             }
160           }
161         }
162       }
163       return target instanceof Navigatable ? (Navigatable)target : null;
164     }
165     finally {
166       Disposer.dispose(view);
167     }
168   }
169
170   @Nullable
171   private static StructureViewTreeElement findElement(StructureViewTreeElement node, PsiElement element, int hopes) {
172     final Object value = node.getValue();
173     if (value instanceof PsiElement) {
174       if (((PsiElement)value).isEquivalentTo(element)) return node;
175       if (hopes != 0) {
176         for (TreeElement child : node.getChildren()) {
177           if (child instanceof StructureViewTreeElement) {
178             final StructureViewTreeElement e = findElement((StructureViewTreeElement)child, element, hopes - 1);
179             if (e != null) {
180               return e;
181             }
182           }
183         }
184       }
185     }
186     return null;
187   }
188
189   private static PsiElement getElement(@NotNull PsiElement element, ChooseByNamePopup popup) {
190     final String path = popup.getPathToAnonymous();
191     if (path != null) {
192       final String[] classes = path.split("\\$");
193       List<Integer> indexes = new ArrayList<Integer>();
194       for (String cls : classes) {
195         if (cls.isEmpty()) continue;
196         try {
197           indexes.add(Integer.parseInt(cls) - 1);
198         }
199         catch (Exception e) {
200           return element;
201         }
202       }
203       PsiElement current = element;
204       for (int index : indexes) {
205         final PsiElement[] anonymousClasses = getAnonymousClasses(current);
206         if (anonymousClasses.length > index) {
207           current = anonymousClasses[index];
208         }
209         else {
210           return current;
211         }
212       }
213       return current;
214     }
215     return element;
216   }
217
218   @NotNull
219   private static PsiElement[] getAnonymousClasses(@NotNull PsiElement element) {
220     for (AnonymousElementProvider provider : Extensions.getExtensions(AnonymousElementProvider.EP_NAME)) {
221       final PsiElement[] elements = provider.getAnonymousElements(element);
222       if (elements.length > 0) {
223         return elements;
224       }
225     }
226     return PsiElement.EMPTY_ARRAY;
227   }
228
229   @Override
230   protected boolean hasContributors(DataContext dataContext) {
231     return ChooseByNameRegistry.getInstance().getClassModelContributors().length > 0;
232   }
233 }