blob: fa4d8b5bb20c906ef26d0637c9e9b0b2e7bb389d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/*******************************************************************************
* Copyright (c) 2006 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* QNX - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.indexview;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.resources.FileStorage;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMName;
import org.eclipse.cdt.internal.ui.util.EditorUtility;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.texteditor.ITextEditor;
/**
* @author Doug Schaefer
*
*/
public class OpenDefinitionAction extends IndexAction {
public OpenDefinitionAction(TreeViewer viewer) {
super(viewer, CUIPlugin.getResourceString("IndexView.openDefinition.name"));//$NON-NLS-1$
}
public void run() {
ISelection selection = viewer.getSelection();
if (!(selection instanceof IStructuredSelection))
return;
Object[] objs = ((IStructuredSelection)selection).toArray();
for (int i = 0; i < objs.length; ++i) {
if (!(objs[i] instanceof PDOMBinding))
continue;
try {
PDOMBinding binding = (PDOMBinding)objs[i];
PDOMName name = binding.getFirstDefinition();
if (name == null)
name = binding.getFirstDeclaration();
if (name == null)
continue;
IASTFileLocation location = name.getFileLocation();
IPath path = new Path(location.getFileName());
Object input = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
if (input == null)
input = new FileStorage(path);
IEditorPart editor = EditorUtility.openInEditor(input);
if (editor != null && editor instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor)editor;
int nodeOffset = location.getNodeOffset();
int nodeLength = location.getNodeLength();
int offset;
int length;
if (nodeLength == -1) {
// This means the offset is actually a line number
try {
IDocument document = textEditor.getDocumentProvider().getDocument(editor.getEditorInput());
offset = document.getLineOffset(nodeOffset);
length = document.getLineLength(nodeOffset);
} catch (BadLocationException e) {
CUIPlugin.getDefault().log(e);
return;
}
} else {
offset = nodeOffset;
length = nodeLength;
}
textEditor.selectAndReveal(offset, length);
}
} catch (CoreException e) {
CUIPlugin.getDefault().log(e);
}
}
}
public boolean valid() {
ISelection selection = viewer.getSelection();
if (!(selection instanceof IStructuredSelection))
return false;
Object[] objs = ((IStructuredSelection)selection).toArray();
for (int i = 0; i < objs.length; ++i)
if (objs[i] instanceof PDOMBinding)
return true;
return false;
}
}
|