Skip to main content
summaryrefslogtreecommitdiffstats
blob: e58ba1012d06e409a28f5335871f1c28d58465a1 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
 * Copyright (c) 2009 Red Hat, Inc.
 * 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:
 *     Red Hat - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.systemtap.local.core;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexFileLocation;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.linuxtools.systemtap.local.core.SystemTapUIErrorMessages;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.texteditor.ITextEditor;
/**
 * Helper class that finds and opens files. Finds based on function names,
 * opens based on path and in the current default editor. 
 *
 */
public class FileFinderOpener {
	
	private static HashMap<String, Integer> offset = new HashMap<String, Integer>();
	private static HashMap<String, Integer> length = new HashMap<String, Integer>();

	
	/**
	 * @param project : C Project Type
	 * @param functionName : name of a function 
	 * @return an ArrayList of String paths (relative to current workspace) of
	 * files with specified function name
	 */
	private static ArrayList<String> findFunctionsInProject(ICProject project, 
			String functionName)  {
		  ArrayList<String> files = new ArrayList<String>() ;

		  IIndexManager manager = CCorePlugin.getIndexManager();
		  IIndex index = null;
		    try {
				index = manager.getIndex(project);
				index.acquireReadLock();
				IBinding[] bindings = index.findBindings(functionName.toCharArray(), IndexFilter.ALL, null);
				for (IBinding bind : bindings) {
					if (bind instanceof IFunction) {
						IFunction ifunction = (IFunction) bind;
						IIndexName[] names = index.findNames(ifunction,
								IIndex.FIND_DEFINITIONS);
						for (IIndexName iname : names) {
							IIndexFile file = iname.getFile();
							if (file != null) {
								IIndexFileLocation filelocation = file.getLocation();
								String loc = filelocation.getURI().getPath();
								files.add(loc);
								offset.put(loc, iname.getNodeOffset());
								length.put(loc, iname.getNodeLength());
							}
						}
					}
				}
				
			} catch (CoreException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		   index.releaseReadLock();
		   return files;
	}
	
	
	public static String findAndOpen(ICProject project, String functionName) {
		offset.clear();
		length.clear();

		ArrayList<String> files = findFunctionsInProject(project, functionName);
		
		if (files == null || files.size() < 1)
			return null;
		StringBuilder output = new StringBuilder();

		if (files.size() == 1) {
			open(files.get(0), offset.get(files.get(0)), length.get(files.get(0)));
		} else {
			ElementListSelectionDialog d = new ElementListSelectionDialog(
					new Shell(), new LabelProvider());
			d.setTitle(Messages.getString("FileFinderOpener.MultipleFilesDialog"));  //$NON-NLS-1$
			d.setMessage(Messages.getString("FileFinderOpener.MultFilesDialogM1") + functionName + Messages.getString("FileFinderOpener.MultFilesDialogM2") +   //$NON-NLS-1$ //$NON-NLS-2$
					Messages.getString("FileFinderOpener.MultFilesDialogM3"));  //$NON-NLS-1$
			d.setElements(files.toArray());
			d.open();
			for (Object o : d.getResult()) {
				if (o instanceof String) {
					String s = (String) o;
					output.append(open(s, offset.get(s), length.get(s)));
				}
			}
		}	
		
		return output.toString();
	}
	
	
	public static String open(String path, int offset, int length) {
		if (path == null)
			return null;
		File fileToOpen = new File(path);
		 
		if (fileToOpen.exists() && fileToOpen.isFile()) {
		    IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
		    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		 
		    try {
		        IEditorPart ed = IDE.openEditorOnFileStore( page, fileStore );
		        if (ed instanceof ITextEditor && offset > 0) {
		        	ITextEditor text = (ITextEditor) ed;
		        	text.selectAndReveal(offset, length);
		        	return text.getTitle();
		        }
		    } catch ( PartInitException e ) {
			}
		} else {
			SystemTapUIErrorMessages mess = new SystemTapUIErrorMessages(Messages.getString("FileFinderOpener.FileNotFound"),  //$NON-NLS-1$
					Messages.getString("FileFinderOpener.FileNotFound1"), Messages.getString("FileFinderOpener.FileNotFound2") + path);   //$NON-NLS-1$ //$NON-NLS-2$
			mess.schedule();
		}
		return null;
	}
}

Back to the top