Skip to main content
summaryrefslogtreecommitdiffstats
blob: ebd66e2050b81a2a5eab4ebfb1f8b8d01f7dc2c4 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*******************************************************************************
 * Copyright (c) 2004, 2005 IBM Corporation 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:
 *     IBM Corp. - Rational Software - initial implementation
 *******************************************************************************/

package org.eclipse.cdt.internal.ui.search.actions;

import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICLogConstants;
import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.dom.IASTServiceProvider;
import org.eclipse.cdt.core.dom.IASTServiceProvider.UnsupportedDialectException;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.parser.ParseError;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.search.DOMSearchUtil;
import org.eclipse.cdt.core.search.ICSearchScope;
import org.eclipse.cdt.core.search.ICSearchConstants.LimitTo;
import org.eclipse.cdt.core.search.ICSearchConstants.SearchFor;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.search.CSearchQuery;
import org.eclipse.cdt.internal.ui.search.CSearchUtil;
import org.eclipse.cdt.internal.ui.search.DOMQuery;
import org.eclipse.cdt.internal.ui.util.ExternalEditorInput;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.ui.IWorkbenchSite;


public abstract class FindAction extends SelectionParseAction {
	public FindAction(CEditor editor){
		super( editor );
	}
	
	public FindAction(IWorkbenchSite site){
		super( site );
	}
	
    /**
     * This is used to create a DOMSearchQuery based on an IASTName, LimitTo, ICSearchScope and an ICSearchCollector if
     * one should be used.
     * 
     * This is a convenience method to setup the title of the DOMSearchQuery as the rest of the parameters are passed directly
     * to the DOMSearchQuery constructor:
     * 
     * return new DOMQuery(buffer.toString(), name, limitTo, scope, collector);
     * 
     * @param name
     * @param limitTo
     * @param scope
     * @param collector
     * @return
     */
	 public CSearchQuery createDOMSearchQueryForName( IASTName name, LimitTo limitTo, ICSearchScope scope, String searchPattern){
		 return new DOMQuery(DOMSearchUtil.getSearchPattern(name), name, limitTo, scope, searchPattern);
	 }
	 
	 
     /**
      * This is a convenience method and is the same as invoking:
      * createDOMSearchQueryForName( name, limitTo, scope, null );
      * 
      * @param name
      * @param limitTo
      * @param scope
      * @return
      */
	 public CSearchQuery createSearchQueryForName( IASTName name, LimitTo limitTo, ICSearchScope scope, String searchPattern ){
		 return createDOMSearchQueryForName( name, limitTo, scope, searchPattern );
	 }
	 
	public void run() {
		ISelection sel = getSelection();
		 
	 	if (sel instanceof IStructuredSelection) {
			run((IStructuredSelection) sel);
		} else if (sel instanceof ITextSelection) {
			run((ITextSelection) sel);
		} 
	}
	
	public void run(IStructuredSelection sel){
		Object obj = sel.getFirstElement();
	 	if( obj == null || !(obj instanceof ICElement ) ){
	 		operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
	 		return;
	 	}

        clearStatusLine();

		ICElement element = (ICElement) obj;
		
		CSearchQuery job = createSearchQuery( getFullyQualifiedName(element), CSearchUtil.getSearchForFromElement(element));
		
		if (job == null)
			return;
		
		NewSearchUI.activateSearchResultView();
		
		NewSearchUI.runQueryInBackground(job);
	}
	
	 /**
	 * @param node
	 */
	 protected CSearchQuery createSearchQuery(String pattern, SearchFor searchFor) {
	 	CSearchQuery op = null;
		ICSearchScope scope = getScope();
		
		if (scope == null)
			return null;
		
		String scopeDescription = getScopeDescription();
	
		//Create a case sensitive search operation - limited by the node
		List search = new LinkedList();
		search.add(searchFor);
		
		LimitTo limitTo = getLimitTo();
		
		op = new CSearchQuery(CCorePlugin.getWorkspace(), pattern,true,search,limitTo,scope,scopeDescription);
		return op;
		
	}
	
	public void run(ITextSelection sel){
		if(sel == null) {
			return;
		}
		
		SelSearchNode selNode = getSelection( sel );
		
        IASTNode foundNode = null;
        IASTTranslationUnit tu = null;
        ParserLanguage lang = null;
        String file = null;
        
        ParseWithProgress runnable = new ParseWithProgress();
        ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(fEditor.getSite().getShell());
        try {
            progressMonitor.run(true, true, runnable);
            tu = runnable.getTu();
            file = runnable.getFile();
            lang = runnable.getLang();
        } catch (InvocationTargetException e1) {
            operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
            return;
        } catch (InterruptedException e1) {
            operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
            return;
        }
        		
		try{
			foundNode = tu.selectNodeForLocation(file, selNode.selStart, selNode.selEnd - selNode.selStart);
		} 
		catch (ParseError er){}
		catch (Exception ex){}
		catch ( VirtualMachineError vmErr){
			if (vmErr instanceof OutOfMemoryError){
				org.eclipse.cdt.internal.core.model.Util.log(null, "Selection Search Out Of Memory error: " + vmErr.getMessage() + " on File: " + file, ICLogConstants.CDT); //$NON-NLS-1$ //$NON-NLS-2$
			}
		}
		
		IASTName foundName = null;
		if (foundNode instanceof IASTName) {
			foundName = (IASTName)foundNode;
		} else {
			ASTVisitor collector = null;
			if (lang == ParserLanguage.CPP) {
				collector = new DOMSearchUtil.CPPNameCollector();
			} else {
				collector = new DOMSearchUtil.CNameCollector();
			}

			if (foundNode != null) { // nothing found
				foundNode.accept( collector );
				
				List names = null;
				if (collector instanceof DOMSearchUtil.CPPNameCollector) {
					names = ((DOMSearchUtil.CPPNameCollector)collector).nameList;
				} else {
					names = ((DOMSearchUtil.CNameCollector)collector).nameList;
				}
				
				if (names.size() == 1) { // just right
					clearStatusLine();
				} else if (names.size() == 0) { // no names selected
					operationNotAvailable(CSEARCH_OPERATION_NO_NAMES_SELECTED_MESSAGE);
					return;
				}
				
				foundName = (IASTName)names.get(0);
			}
		}
		
		LimitTo limitTo = getLimitTo();
		ICSearchScope searchScope = null;
		searchScope = getScope();

		if (searchScope == null)
			return;
		
		CSearchQuery job = createSearchQueryForName(foundName, limitTo, searchScope, selNode.selText);
		
		if (job == null)
			return; 
		
		NewSearchUI.activateSearchResultView();
		
		NewSearchUI.runQueryInBackground(job);
	}
    
    private class ParseWithProgress implements IRunnableWithProgress 
    {
        private IASTTranslationUnit tu = null;
        private String file=null;
        private ParserLanguage lang=null;
        
        public ParseWithProgress() {}

        public void run(IProgressMonitor monitor) {
            if (fEditor.getEditorInput() instanceof ExternalEditorInput) {
                ExternalEditorInput input = (ExternalEditorInput)fEditor.getEditorInput();
                try {
                    // get the project for the external editor input's translation unit
                    ICElement project = input.getTranslationUnit();
                    while (!(project instanceof ICProject) && project != null) {
                        project = project.getParent();
                    }
                    
                    if (project instanceof ICProject) {
                        tu = CDOM.getInstance().getASTService().getTranslationUnit(input.getStorage(), ((ICProject)project).getProject());
                        lang = DOMSearchUtil.getLanguage(input.getStorage().getFullPath(), ((ICProject)project).getProject());
                    }
                } catch (UnsupportedDialectException e) {
                    operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
                    return;
                }
                
                file = input.getStorage().getFullPath().toOSString();
            } else {
                IFile resourceFile = null;
                resourceFile = fEditor.getInputFile();
                
                try {
                    tu = CDOM.getInstance().getASTService().getTranslationUnit(
                            resourceFile,
                            CDOM.getInstance().getCodeReaderFactory(
                                    CDOM.PARSE_WORKING_COPY_WHENEVER_POSSIBLE));
                } catch (IASTServiceProvider.UnsupportedDialectException e) {
                    operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
                    return;
                }
                
                file = resourceFile.getLocation().toOSString();
                lang = DOMSearchUtil.getLanguageFromFile(resourceFile);
            }
        }

        public IASTTranslationUnit getTu() {
            return tu;
        }

        public String getFile() {
            return file;
        }

        public ParserLanguage getLang() {
            return lang;
        }
    };
	
    
    private String getFullyQualifiedName(ICElement element){
      
    	StringBuffer fullName = new StringBuffer(element.getElementName());
    	
    	while (element.getElementType() != 0){
    		element = element.getParent();
    		//Keep going up CModel until we hit Translation Unit
    		//or Working Copy (both represented by C_UNIT) or hit a null
    		if (element.getElementType() == ICElement.C_UNIT ||
    			element == null){
    			fullName.insert(0,"::"); //$NON-NLS-1$
    			break;
    		}
    		else if (element.getElementType() != ICElement.C_ENUMERATION){
    			//get the parent name as long as it is not an enumeration - enumerators
    			//don't use the enumeration name as part of the fully qualified name
    			fullName.insert(0,"::"); //$NON-NLS-1$
    			fullName.insert(0,element.getElementName());
    		}
    	}
    	
    	return fullName.toString();
    }
    
    abstract protected String getScopeDescription(); 

	abstract protected ICSearchScope getScope();
	
	abstract protected LimitTo getLimitTo();
    
}

Back to the top