2 * Copyright 2000-2015 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.ide.actions;
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;
56 import java.awt.event.InputEvent;
57 import java.util.ArrayList;
58 import java.util.List;
60 public class GotoClassAction extends GotoActionBase implements DumbAware {
62 public void actionPerformed(@NotNull AnActionEvent e) {
63 Project project = e.getProject();
64 if (project == null) return;
66 if (!DumbService.getInstance(project).isDumb()) {
67 super.actionPerformed(e);
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);
79 public void gotoActionPerformed(@NotNull AnActionEvent e) {
80 final Project project = e.getProject();
81 if (project == null) return;
83 FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.class");
85 PsiDocumentManager.getInstance(project).commitAllDocuments();
87 final GotoClassModel2 model = new GotoClassModel2(project);
88 showNavigationPopup(e, model, new GotoActionCallback<Language>() {
90 protected ChooseByNameFilter<Language> createFilter(@NotNull ChooseByNamePopup popup) {
91 return new ChooseByNameLanguageFilter(popup, model, GotoClassSymbolConfiguration.getInstance(project), project);
95 public void elementChosen(ChooseByNamePopup popup, Object element) {
96 AccessToken token = ReadAction.start();
98 if (element instanceof PsiElement) {
99 final PsiElement psiElement = getElement(((PsiElement)element), popup);
100 final VirtualFile file = PsiUtilCore.getVirtualFile(psiElement);
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()) {
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);
119 NavigationUtil.activateFileWithPsiElement(psiElement, !popup.isOpenInCurrentWindowRequested());
122 EditSourceUtil.navigate(((NavigationItem)element), true, popup.isOpenInCurrentWindowRequested());
129 }, IdeBundle.message("go.to.class.toolwindow.title"), true);
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) {
141 final StructureView view = builder.createStructureView(editors[0], psiElement.getProject());
143 final StructureViewTreeElement element = findElement(view.getTreeModel().getRoot(), psiElement, 4);
144 if (element == null) {
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);
158 target = ((StructureViewTreeElement)treeElement).getValue();
163 return target instanceof Navigatable ? (Navigatable)target : null;
166 Disposer.dispose(view);
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;
176 for (TreeElement child : node.getChildren()) {
177 if (child instanceof StructureViewTreeElement) {
178 final StructureViewTreeElement e = findElement((StructureViewTreeElement)child, element, hopes - 1);
189 private static PsiElement getElement(@NotNull PsiElement element, ChooseByNamePopup popup) {
190 final String path = popup.getPathToAnonymous();
192 final String[] classes = path.split("\\$");
193 List<Integer> indexes = new ArrayList<Integer>();
194 for (String cls : classes) {
195 if (cls.isEmpty()) continue;
197 indexes.add(Integer.parseInt(cls) - 1);
199 catch (Exception e) {
203 PsiElement current = element;
204 for (int index : indexes) {
205 final PsiElement[] anonymousClasses = getAnonymousClasses(current);
206 if (anonymousClasses.length > index) {
207 current = anonymousClasses[index];
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) {
226 return PsiElement.EMPTY_ARRAY;
230 protected boolean hasContributors(DataContext dataContext) {
231 return ChooseByNameRegistry.getInstance().getClassModelContributors().length > 0;