Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] FIXED 69063- [Search] Open Definition vs Open Declaration.


FIXED 69063- [Search] Open Definition vs Open Declaration.

Devin Steffler
IBM's Eclipse CDT
Ottawa (Palladium), Ontario, Canada


Index: ui/org/eclipse/cdt/ui/tests/text/selectiontests/SelectionTests.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/SelectionTests.java,v
retrieving revision 1.1
diff -u -r1.1 SelectionTests.java
--- ui/org/eclipse/cdt/ui/tests/text/selectiontests/SelectionTests.java	2 May 2005 17:40:47 -0000	1.1
+++ ui/org/eclipse/cdt/ui/tests/text/selectiontests/SelectionTests.java	3 May 2005 13:45:10 -0000
@@ -128,7 +128,7 @@
         IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
         IEditorPart part = null;
         try {
-            part = page.openEditor(new FileEditorInput(file), "org.eclipse.cdt.ui.editor.CEditor"); // TODO Devin testing
+            part = page.openEditor(new FileEditorInput(file), "org.eclipse.cdt.ui.editor.CEditor"); //$NON-NLS-1$
         } catch (PartInitException e) {
             assertFalse(true);
         }
@@ -136,7 +136,7 @@
         if (part instanceof AbstractTextEditor) {
             ((AbstractTextEditor)part).getSelectionProvider().setSelection(new TextSelection(offset,0));
             
-            final IAction action = ((AbstractTextEditor)part).getAction("OpenDeclarations");
+            final IAction action = ((AbstractTextEditor)part).getAction("OpenDeclarations"); //$NON-NLS-1$
             action.run();
         
             // the action above should highlight the declaration, so now retrieve it and use that selection to get the IASTName selected on the TU
Index: plugin.properties
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/plugin.properties,v
retrieving revision 1.82
diff -u -r1.82 plugin.properties
--- plugin.properties	27 Apr 2005 19:18:11 -0000	1.82
+++ plugin.properties	3 May 2005 13:44:59 -0000
@@ -84,6 +84,9 @@
 ActionDefinition.opendecl.name= Open Declaration
 ActionDefinition.opendecl.description= Open an editor on the selected element's declaration(s)
 
+ActionDefinition.opendef.name= Open Definition
+ActionDefinition.opendef.description= Open an editor on the selected element's definition 
+
 ActionDefinition.opencview.name= Show in C/C++ Project view
 ActionDefinition.opencview.description= Show the selected resource in the C/C++ Project view
 
Index: plugin.xml
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/plugin.xml,v
retrieving revision 1.158
diff -u -r1.158 plugin.xml
--- plugin.xml	27 Apr 2005 19:18:11 -0000	1.158
+++ plugin.xml	3 May 2005 13:44:59 -0000
@@ -920,6 +920,18 @@
             configuration="org.eclipse.ui.defaultAcceleratorConfiguration">
       </keyBinding>
       <command
+            name="%ActionDefinition.opendef.name"
+            category="org.eclipse.cdt.ui.category.source"
+            description="%ActionDefinition.opendef.description"
+            id="org.eclipse.cdt.ui.edit.opendef">
+      </command>
+      <keyBinding
+            string="F2"
+            scope="org.eclipse.cdt.ui.cEditorScope"
+            command="org.eclipse.cdt.ui.edit.opendef"
+            configuration="org.eclipse.ui.defaultAcceleratorConfiguration">
+      </keyBinding>
+      <command
             name="%ActionDefinition.opendecl.name"
             category="org.eclipse.cdt.ui.category.source"
             description="%ActionDefinition.opendecl.description"
Index: src/org/eclipse/cdt/internal/ui/editor/CEditor.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java,v
retrieving revision 1.86
diff -u -r1.86 CEditor.java
--- src/org/eclipse/cdt/internal/ui/editor/CEditor.java	12 Apr 2005 22:58:55 -0000	1.86
+++ src/org/eclipse/cdt/internal/ui/editor/CEditor.java	3 May 2005 13:44:59 -0000
@@ -1,5 +1,5 @@
 /*
- * (c) Copyright IBM Corp. 2000, 2001.
+ * (c) Copyright IBM Corp. 2000, 2005.
  * All Rights Reserved.
  */
 package org.eclipse.cdt.internal.ui.editor;
@@ -26,6 +26,7 @@
 import org.eclipse.cdt.internal.ui.actions.RemoveBlockCommentAction;
 import org.eclipse.cdt.internal.ui.browser.typehierarchy.OpenTypeHierarchyAction;
 import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction;
+import org.eclipse.cdt.internal.ui.search.actions.OpenDefinitionAction;
 import org.eclipse.cdt.internal.ui.search.actions.SelectionSearchGroup;
 import org.eclipse.cdt.internal.ui.text.CPairMatcher;
 import org.eclipse.cdt.internal.ui.text.CSourceViewerConfiguration;
@@ -686,6 +687,10 @@
 		action.setActionDefinitionId(ICEditorActionDefinitionIds.OPEN_DECL);
 		setAction("OpenDeclarations", action); //$NON-NLS-1$
 
+        action = new OpenDefinitionAction(this);
+        action.setActionDefinitionId(ICEditorActionDefinitionIds.OPEN_DEF);
+        setAction("OpenDefinition", action); //$NON-NLS-1$
+        
 		action = new OpenTypeHierarchyAction(this);
 		action.setActionDefinitionId(ICEditorActionDefinitionIds.OPEN_TYPE_HIERARCHY);
 		setAction("OpenTypeHierarchy", action); //$NON-NLS-1$
@@ -729,6 +734,7 @@
 		addAction(menu, ITextEditorActionConstants.GROUP_EDIT, "RemoveBlockComment"); //$NON-NLS-1$
 
 		addAction(menu, ITextEditorActionConstants.GROUP_FIND, "OpenDeclarations"); //$NON-NLS-1$
+        addAction(menu, ITextEditorActionConstants.GROUP_FIND, "OpenDefinition"); //$NON-NLS-1$
 
 		addAction(menu, ITextEditorActionConstants.GROUP_FIND, "OpenTypeHierarchy"); //$NON-NLS-1$
         addAction(menu, ITextEditorActionConstants.GROUP_FIND, "GotoNextMember"); //$NON-NLS-1$
Index: src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties,v
retrieving revision 1.19
diff -u -r1.19 CEditorMessages.properties
--- src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties	12 Apr 2005 22:58:55 -0000	1.19
+++ src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties	3 May 2005 13:45:00 -0000
@@ -1,5 +1,5 @@
 #########################################
-# (c) Copyright IBM Corp. 2000, 2001.
+# (c) Copyright IBM Corp. 2000, 2005.
 # All Rights Reserved.
 #########################################
 
@@ -75,6 +75,10 @@
 OpenDeclarations.label=&Open Declaration@F3
 OpenDeclarations.tooltip=Open an editor on the selected element's declaration
 
+OpenDefinition.description=Open an editor on the selected element's definition
+OpenDefinition.label=Open &Definition@F2
+OpenDefinition.tooltip=Open an editor on the selected element's definition
+
 OpenOutline.description=Shows outline
 OpenOutline.dialog.title=Show outline
 OpenOutline.label=&Show outline@Ctrl+O
Index: src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java,v
retrieving revision 1.13
diff -u -r1.13 ICEditorActionDefinitionIds.java
--- src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java	26 Feb 2005 01:42:42 -0000	1.13
+++ src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java	3 May 2005 13:45:00 -0000
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2004 QNX Software Systems and others.
+ * Copyright (c) 2000, 2005 QNX Software Systems and others.
  * All rights reserved. This program and the accompanying materials 
  * are made available under the terms of the Common Public License v1.0
  * which accompanies this distribution, and is available at
@@ -68,6 +68,12 @@
 	 * (value <code>"org.eclipse.cdt.ui.edit.opendecl"</code>).
 	 */
 	public static final String OPEN_DECL= "org.eclipse.cdt.ui.edit.opendecl"; //$NON-NLS-1$
+    
+    /**
+     * Action definition ID of the open definition action
+     * (value <code>"org.eclipse.cdt.ui.edit.opendef"</code>).
+     */
+    public static final String OPEN_DEF= "org.eclipse.cdt.ui.edit.opendef"; //$NON-NLS-1$
 
 	/**
 	 * Action definition ID of the show in C/C++ Projects View action
Index: src/org/eclipse/cdt/internal/ui/search/CSearchMessages.properties
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchMessages.properties,v
retrieving revision 1.21
diff -u -r1.21 CSearchMessages.properties
--- src/org/eclipse/cdt/internal/ui/search/CSearchMessages.properties	27 Apr 2005 17:43:28 -0000	1.21
+++ src/org/eclipse/cdt/internal/ui/search/CSearchMessages.properties	3 May 2005 13:45:00 -0000
@@ -1,5 +1,5 @@
 ###############################################################################
-# Copyright (c) 2000, 2004 IBM Corporation and others.
+# Copyright (c) 2000, 2005 IBM Corporation and others.
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Common Public License v1.0
 # which accompanies this distribution, and is available at
@@ -90,6 +90,8 @@
 CSearchOperation.operationUnavailable.message= The operation is unavailable on the current selection.
 CSearchOperation.tooManyNames.message= The operation is unavailable on the current selection (too many different names selected).
 CSearchOperation.noNamesSelected.message= The operation is unavailable on the current selection (no name selected).
+CSearchOperation.noDefinitionFound.message= No definition was found.
+CSearchOperation.noDeclarationFound.message= No declaration was found.
 
 
 WorkspaceScope= Workspace
Index: src/org/eclipse/cdt/internal/ui/search/actions/FindDeclarationsAction.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindDeclarationsAction.java,v
retrieving revision 1.4
diff -u -r1.4 FindDeclarationsAction.java
--- src/org/eclipse/cdt/internal/ui/search/actions/FindDeclarationsAction.java	29 Apr 2005 00:56:11 -0000	1.4
+++ src/org/eclipse/cdt/internal/ui/search/actions/FindDeclarationsAction.java	3 May 2005 13:45:00 -0000
@@ -11,15 +11,12 @@
 
 package org.eclipse.cdt.internal.ui.search.actions;
 
-import org.eclipse.cdt.core.model.ICElement;
 import org.eclipse.cdt.core.search.ICSearchConstants;
 import org.eclipse.cdt.core.search.ICSearchScope;
 import org.eclipse.cdt.core.search.SearchEngine;
 import org.eclipse.cdt.core.search.ICSearchConstants.LimitTo;
-import org.eclipse.cdt.internal.core.model.CProject;
 import org.eclipse.cdt.internal.ui.editor.CEditor;
 import org.eclipse.cdt.internal.ui.search.CSearchMessages;
-import org.eclipse.core.resources.IProject;
 import org.eclipse.ui.IWorkbenchSite;
 
 
Index: src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java,v
retrieving revision 1.13
diff -u -r1.13 OpenDeclarationsAction.java
--- src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java	27 Apr 2005 17:43:28 -0000	1.13
+++ src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java	3 May 2005 13:45:00 -0000
@@ -16,13 +16,8 @@
 
 import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
 import org.eclipse.cdt.core.dom.ast.IASTName;
-import org.eclipse.cdt.core.model.CModelException;
-import org.eclipse.cdt.core.model.CoreModel;
 import org.eclipse.cdt.core.model.ICElement;
-import org.eclipse.cdt.core.model.ICProject;
-import org.eclipse.cdt.core.model.ITranslationUnit;
 import org.eclipse.cdt.core.parser.ParserUtil;
-import org.eclipse.cdt.core.resources.FileStorage;
 import org.eclipse.cdt.core.search.DOMSearchUtil;
 import org.eclipse.cdt.core.search.ICSearchConstants;
 import org.eclipse.cdt.core.search.IMatch;
@@ -31,24 +26,15 @@
 import org.eclipse.cdt.internal.ui.editor.CEditor;
 import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
 import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput;
-import org.eclipse.cdt.internal.ui.util.EditorUtility;
 import org.eclipse.cdt.internal.ui.util.ExternalEditorInput;
 import org.eclipse.cdt.ui.CUIPlugin;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.Path;
 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.swt.widgets.Shell;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.PartInitException;
-import org.eclipse.ui.texteditor.AbstractTextEditor;
 import org.eclipse.ui.texteditor.IUpdate;
 
 public class OpenDeclarationsAction extends SelectionParseAction implements IUpdate {
@@ -71,58 +57,6 @@
 		searchEngine = new SearchEngine();
 	}
 
-//	protected void setDialogTitle(String title) {
-//		fDialogTitle= title;
-//	}
-//	
-//	protected void setDialogMessage(String message) {
-//		fDialogMessage= message;
-//	}
-	
-	protected SelSearchNode getSelectedStringFromEditor() {
-		ISelection selection = getSelection();
-		if( selection == null || !(selection instanceof ITextSelection) ) 
-	 		 return null;
-
- 		return getSelection( (ITextSelection)selection );
-	}
-	String projectName = "";  //$NON-NLS-1$
-	 private static class Storage
-	{
-		private IResource resource;
-		private String fileName;
-		private int offset=0;
-		private int length=0;
-
-		public final String getFileName() {
-			return fileName;
-		}
-		public Storage() {
-		}
-		public final IResource getResource() {
-			return resource;
-		}
-		public int getLength() {
-			return length;
-		}
-		public int getOffset() {
-			return offset;
-		}
-		public void setFileName(String fileName) {
-			this.fileName = fileName;
-		}
-		public void setLength(int length) {
-			this.length = length;
-		}
-		public void setOffset(int offset) {
-			this.offset = offset;
-		}
-		public void setResource(IResource resource) {
-			this.resource = resource;
-		}
-		
-	}
-
 	/* (non-Javadoc)
 	 * @see org.eclipse.jface.action.IAction#run()
 	 */
@@ -195,6 +129,8 @@
                             storage.setLength(end - start);
                             storage.setOffset(start);
                             storage.setResource(ParserUtil.getResourceForFilename( fileName ));
+                        } else {
+                            operationNotAvailable(CSEARCH_OPERATION_NO_DECLARATION_MESSAGE);
                         }
 					} else {
 						// step 3 starts here
@@ -202,7 +138,7 @@
 						scope[0] = new CProject(null, fEditor.getInputFile().getProject());
 						Set matches = DOMSearchUtil.getMatchesFromSearchEngine(SearchEngine.createCSearchScope(scope), searchName, ICSearchConstants.DECLARATIONS);
 
-						if (matches != null) {
+						if (matches != null && matches.size() > 0) {
 							Iterator itr = matches.iterator();
 							while(itr.hasNext()) {
 								Object match = itr.next();
@@ -215,7 +151,9 @@
 									break;
 								}
 							}
-						}
+						} else {
+                            operationNotAvailable(CSEARCH_OPERATION_NO_DECLARATION_MESSAGE);
+                        }
 					}
 				} else if (selectedNames.length == 0){
 					operationNotAvailable(CSEARCH_OPERATION_NO_NAMES_SELECTED_MESSAGE);
@@ -263,105 +201,6 @@
 		 		 CUIPlugin.getDefault().log(x);
 		}
 	}
-
-	/**
-	 * @param string
-	 * @param i
-	 */
-	protected boolean open(String filename, int offset, int length) throws PartInitException, CModelException {
-		IPath path = new Path( filename );
-		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
-		if( file != null )
-		{
-			open( file, offset, length );
-			return true;
-		}
-
-        ICProject cproject = CoreModel.getDefault().getCModel().getCProject( projectName );
-		ITranslationUnit unit = CoreModel.getDefault().createTranslationUnitFrom(cproject, path);
-		if (unit != null) {
-			setSelectionAtOffset( EditorUtility.openInEditor(unit), offset, length );
-			return true;
-		}
-        
-		FileStorage storage = new FileStorage(null, path);
-		IEditorPart part = EditorUtility.openInEditor(storage);
-		setSelectionAtOffset(part, offset, length);
-		return true;
-		
-	}
-	protected Shell getShell() {
-		return fEditor.getSite().getShell();
-	}
-	
-	
-	protected void open( IMatch element ) throws CModelException, PartInitException
-	{
-		open( element.getResource(), element.getStartOffset(), element.getEndOffset() - element.getStartOffset() );
-	}
-	
-	/**
-	 * Opens the editor on the given element and subsequently selects it.
-	 */
-	protected void open( IResource resource, int offset, int length ) throws CModelException, PartInitException {
-		IEditorPart part= EditorUtility.openInEditor(resource);
-		setSelectionAtOffset(part, offset, length);
-	}
 						
-	/**
-	 * @param part
-	 * @param offset
-	 * @param length TODO
-	 */
-	private void setSelectionAtOffset(IEditorPart part, int offset, int length) {
-		if( part instanceof AbstractTextEditor )
-        {
-			try {
-            ((AbstractTextEditor) part).selectAndReveal(offset, length);
-			} catch (Exception e) {}
-        }
-	}
-//	/**
-//	 * Shows a dialog for resolving an ambigous C element.
-//	 * Utility method that can be called by subclassers.
-//	 */
-//	protected IMatch selectCElement(List elements, Shell shell, String title, String message) {
-//		
-//		int nResults= elements.size();
-//		
-//		if (nResults == 0)
-//			return null;
-//		
-//		if (nResults == 1)
-//			return (IMatch) elements.get(0);
-//			
-//
-//		ElementListSelectionDialog dialog= new ElementListSelectionDialog(shell, new CSearchResultLabelProvider(), false, false);
-//		dialog.setTitle(title);
-//		dialog.setMessage(message);
-//		dialog.setElements(elements);
-//		
-//		if (dialog.open() == Window.OK) {
-//			Object[] selection= dialog.getResult();
-//			if (selection != null && selection.length > 0) {
-//				nResults= selection.length;
-//				for (int i= 0; i < nResults; i++) {
-//					Object current= selection[i];
-//					if (current instanceof IMatch)
-//						return (IMatch) current;
-//				}
-//			}
-//		}		
-//		return null;
-//	}	
-	
-
-	/* (non-Javadoc)
-	  * @see org.eclipse.ui.texteditor.IUpdate#update()
-	  */
-	 public void update() {
-	 		 setEnabled(getSelectedStringFromEditor() != null);
-	 }
-
 }
 
Index: src/org/eclipse/cdt/internal/ui/search/actions/SelectionParseAction.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/SelectionParseAction.java,v
retrieving revision 1.9
diff -u -r1.9 SelectionParseAction.java
--- src/org/eclipse/cdt/internal/ui/search/actions/SelectionParseAction.java	2 May 2005 17:40:48 -0000	1.9
+++ src/org/eclipse/cdt/internal/ui/search/actions/SelectionParseAction.java	3 May 2005 13:45:00 -0000
@@ -14,7 +14,10 @@
 import java.io.IOException;
 
 import org.eclipse.cdt.core.CCorePlugin;
+import org.eclipse.cdt.core.model.CModelException;
 import org.eclipse.cdt.core.model.CoreModel;
+import org.eclipse.cdt.core.model.ICProject;
+import org.eclipse.cdt.core.model.ITranslationUnit;
 import org.eclipse.cdt.core.model.IWorkingCopy;
 import org.eclipse.cdt.core.parser.CodeReader;
 import org.eclipse.cdt.core.parser.IParser;
@@ -28,22 +31,33 @@
 import org.eclipse.cdt.core.parser.ParserMode;
 import org.eclipse.cdt.core.parser.ParserUtil;
 import org.eclipse.cdt.core.parser.ScannerInfo;
+import org.eclipse.cdt.core.resources.FileStorage;
+import org.eclipse.cdt.core.search.IMatch;
 import org.eclipse.cdt.internal.ui.editor.CEditor;
 import org.eclipse.cdt.internal.ui.search.CSearchMessages;
+import org.eclipse.cdt.internal.ui.util.EditorUtility;
 import org.eclipse.cdt.ui.CUIPlugin;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+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.action.Action;
 import org.eclipse.jface.action.IStatusLineManager;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.ITextSelection;
 import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.IFileEditorInput;
 import org.eclipse.ui.IViewSite;
 import org.eclipse.ui.IWorkbenchSite;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.texteditor.AbstractTextEditor;
 import org.eclipse.ui.texteditor.IDocumentProvider;
 
 /**
@@ -55,7 +69,9 @@
     protected static final String CSEARCH_OPERATION_TOO_MANY_NAMES_MESSAGE = "CSearchOperation.tooManyNames.message"; //$NON-NLS-1$
 	protected static final String CSEARCH_OPERATION_NO_NAMES_SELECTED_MESSAGE = "CSearchOperation.noNamesSelected.message"; //$NON-NLS-1$
 	protected static final String CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE = "CSearchOperation.operationUnavailable.message"; //$NON-NLS-1$
-	
+    protected static final String CSEARCH_OPERATION_NO_DEFINITION_MESSAGE = "CSearchOperation.noDefinitionFound.message"; //$NON-NLS-1$
+    protected static final String CSEARCH_OPERATION_NO_DECLARATION_MESSAGE = "CSearchOperation.noDeclarationFound.message"; //$NON-NLS-1$
+        
 	protected IWorkbenchSite fSite;
 	protected CEditor fEditor;
 
@@ -522,4 +538,148 @@
 	 	protected int selEnd;
 	}
 
+    protected SelSearchNode getSelectedStringFromEditor() {
+        ISelection selection = getSelection();
+        if( selection == null || !(selection instanceof ITextSelection) ) 
+             return null;
+
+        return getSelection( (ITextSelection)selection );
+    }
+    
+    String projectName = "";  //$NON-NLS-1$
+    protected static class Storage
+    {
+        private IResource resource;
+        private String fileName;
+        private int offset=0;
+        private int length=0;
+
+        public final String getFileName() {
+            return fileName;
+        }
+        public Storage() {
+        }
+        public final IResource getResource() {
+            return resource;
+        }
+        public int getLength() {
+            return length;
+        }
+        public int getOffset() {
+            return offset;
+        }
+        public void setFileName(String fileName) {
+            this.fileName = fileName;
+        }
+        public void setLength(int length) {
+            this.length = length;
+        }
+        public void setOffset(int offset) {
+            this.offset = offset;
+        }
+        public void setResource(IResource resource) {
+            this.resource = resource;
+        }
+        
+    }
+    
+    /**
+     * @param string
+     * @param i
+     */
+    protected boolean open(String filename, int offset, int length) throws PartInitException, CModelException {
+        IPath path = new Path( filename );
+        IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
+        if( file != null )
+        {
+            open( file, offset, length );
+            return true;
+        }
+
+        ICProject cproject = CoreModel.getDefault().getCModel().getCProject( projectName );
+        ITranslationUnit unit = CoreModel.getDefault().createTranslationUnitFrom(cproject, path);
+        if (unit != null) {
+            setSelectionAtOffset( EditorUtility.openInEditor(unit), offset, length );
+            return true;
+        }
+        
+        FileStorage storage = new FileStorage(null, path);
+        IEditorPart part = EditorUtility.openInEditor(storage);
+        setSelectionAtOffset(part, offset, length);
+        return true;
+        
+    }
+    protected Shell getShell() {
+        return fEditor.getSite().getShell();
+    }
+    
+    
+    protected void open( IMatch element ) throws CModelException, PartInitException
+    {
+        open( element.getResource(), element.getStartOffset(), element.getEndOffset() - element.getStartOffset() );
+    }
+    
+    /**
+     * Opens the editor on the given element and subsequently selects it.
+     */
+    protected void open( IResource resource, int offset, int length ) throws CModelException, PartInitException {
+        IEditorPart part= EditorUtility.openInEditor(resource);
+        setSelectionAtOffset(part, offset, length);
+    }
+                        
+    /**
+     * @param part
+     * @param offset
+     * @param length TODO
+     */
+    protected void setSelectionAtOffset(IEditorPart part, int offset, int length) {
+        if( part instanceof AbstractTextEditor )
+        {
+            try {
+            ((AbstractTextEditor) part).selectAndReveal(offset, length);
+            } catch (Exception e) {}
+        }
+    }
+//  /**
+//   * Shows a dialog for resolving an ambigous C element.
+//   * Utility method that can be called by subclassers.
+//   */
+//  protected IMatch selectCElement(List elements, Shell shell, String title, String message) {
+//      
+//      int nResults= elements.size();
+//      
+//      if (nResults == 0)
+//          return null;
+//      
+//      if (nResults == 1)
+//          return (IMatch) elements.get(0);
+//          
+//
+//      ElementListSelectionDialog dialog= new ElementListSelectionDialog(shell, new CSearchResultLabelProvider(), false, false);
+//      dialog.setTitle(title);
+//      dialog.setMessage(message);
+//      dialog.setElements(elements);
+//      
+//      if (dialog.open() == Window.OK) {
+//          Object[] selection= dialog.getResult();
+//          if (selection != null && selection.length > 0) {
+//              nResults= selection.length;
+//              for (int i= 0; i < nResults; i++) {
+//                  Object current= selection[i];
+//                  if (current instanceof IMatch)
+//                      return (IMatch) current;
+//              }
+//          }
+//      }       
+//      return null;
+//  }   
+    
+
+    /* (non-Javadoc)
+      * @see org.eclipse.ui.texteditor.IUpdate#update()
+      */
+     public void update() {
+             setEnabled(getSelectedStringFromEditor() != null);
+     }
+    
 }
Index: src/org/eclipse/cdt/internal/ui/search/actions/OpenDefinitionAction.java
===================================================================
RCS file: src/org/eclipse/cdt/internal/ui/search/actions/OpenDefinitionAction.java
diff -N src/org/eclipse/cdt/internal/ui/search/actions/OpenDefinitionAction.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/cdt/internal/ui/search/actions/OpenDefinitionAction.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,217 @@
+/*******************************************************************************
+ * Copyright (c) 2005 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials 
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ * 
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.internal.ui.search.actions;
+
+import java.util.Iterator;
+import java.util.Set;
+
+import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
+import org.eclipse.cdt.core.dom.ast.IASTName;
+import org.eclipse.cdt.core.model.ICElement;
+import org.eclipse.cdt.core.parser.ParserUtil;
+import org.eclipse.cdt.core.search.DOMSearchUtil;
+import org.eclipse.cdt.core.search.ICSearchConstants;
+import org.eclipse.cdt.core.search.IMatch;
+import org.eclipse.cdt.core.search.SearchEngine;
+import org.eclipse.cdt.internal.core.model.CProject;
+import org.eclipse.cdt.internal.ui.editor.CEditor;
+import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
+import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput;
+import org.eclipse.cdt.internal.ui.util.ExternalEditorInput;
+import org.eclipse.cdt.ui.CUIPlugin;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.ui.texteditor.IUpdate;
+
+/**
+ * Open Definition Action (F2).
+ * 
+ * @author dsteffle
+ */
+public class OpenDefinitionAction extends SelectionParseAction implements
+        IUpdate {
+
+    public static final IASTName[] BLANK_NAME_ARRAY = new IASTName[0];
+    //private String fDialogTitle;
+    //private String fDialogMessage;
+    SearchEngine searchEngine = null;
+
+    /**
+     * Creates a new action with the given editor
+     */
+    public OpenDefinitionAction(CEditor editor) {
+        super( editor );
+        setText(CEditorMessages.getString("OpenDefinition.label")); //$NON-NLS-1$
+        setToolTipText(CEditorMessages.getString("OpenDefinition.tooltip")); //$NON-NLS-1$
+        setDescription(CEditorMessages.getString("OpenDefinition.description")); //$NON-NLS-1$
+
+        searchEngine = new SearchEngine();
+    }
+
+//  protected void setDialogTitle(String title) {
+//      fDialogTitle= title;
+//  }
+//  
+//  protected void setDialogMessage(String message) {
+//      fDialogMessage= message;
+//  }
+    
+    /* (non-Javadoc)
+     * @see org.eclipse.jface.action.IAction#run()
+     */
+    public void run() {
+        final SelSearchNode selNode = getSelectedStringFromEditor();
+        
+        if(selNode == null) {
+            return;
+        }
+        
+        final Storage storage = new Storage();
+
+        IRunnableWithProgress runnable = new IRunnableWithProgress() 
+        {
+            // steps:
+            // 1- parse and get the best selected name based on the offset/length into that TU
+            // 2- based on the IASTName selected, find the best definition of it in the TU
+            // 3- if no IASTName is found for a definition, then search the Index
+            public void run(IProgressMonitor monitor) {
+                int selectionStart = selNode.selStart;
+                int selectionLength = selNode.selEnd - selNode.selStart;
+                
+                IFile resourceFile = null;
+                
+                IASTName[] selectedNames = BLANK_NAME_ARRAY;
+                if (fEditor.getEditorInput() instanceof ExternalEditorInput) {
+                    if( fEditor.getEditorInput() instanceof ITranslationUnitEditorInput )
+                    {
+                        ITranslationUnitEditorInput ip = (ITranslationUnitEditorInput) fEditor.getEditorInput();
+                        IResource r = ip.getTranslationUnit().getUnderlyingResource();
+                        if( r.getType() == IResource.FILE )
+                            resourceFile = (IFile) r;
+                        else
+                        {
+                            operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
+                            return;
+
+                        }
+                    }
+                }
+                else
+                    resourceFile = fEditor.getInputFile();
+                
+
+                if (resourceFile != null) 
+                    projectName = findProjectName(resourceFile);
+                
+                // step 1 starts here
+                if (resourceFile != null)
+                    selectedNames = DOMSearchUtil.getSelectedNamesFrom(resourceFile, selectionStart, selectionLength);
+                                
+                if (selectedNames.length > 0 && selectedNames[0] != null) { // just right, only one name selected
+                    IASTName searchName = selectedNames[0];
+                    // step 2 starts here
+                    IASTName[] domNames = DOMSearchUtil.getNamesFromDOM(searchName, ICSearchConstants.DEFINITIONS);
+
+                    if (domNames != null && domNames.length > 0 && domNames[0] != null) {
+                        String fileName=null;
+                        int start=0;
+                        int end=0;
+                        
+                        if ( domNames[0].getTranslationUnit() != null ) {
+                            IASTFileLocation location = domNames[0].getTranslationUnit().flattenLocationsToFile( domNames[0].getNodeLocations() );
+                            fileName = location.getFileName();
+                            start = location.getNodeOffset();
+                            end = location.getNodeOffset() + location.getNodeLength();
+                        }
+                        
+                        if (fileName != null) {
+                            storage.setFileName(fileName);
+                            storage.setLength(end - start);
+                            storage.setOffset(start);
+                            storage.setResource(ParserUtil.getResourceForFilename( fileName ));
+                        } else {
+                            operationNotAvailable(CSEARCH_OPERATION_NO_DEFINITION_MESSAGE);
+                        }
+                    } else {
+                        // step 3 starts here
+                        ICElement[] scope = new ICElement[1];
+                        scope[0] = new CProject(null, fEditor.getInputFile().getProject());
+                        Set matches = DOMSearchUtil.getMatchesFromSearchEngine(SearchEngine.createCSearchScope(scope), searchName, ICSearchConstants.DEFINITIONS);
+
+                        if (matches != null && matches.size() > 0) {
+                            Iterator itr = matches.iterator();
+                            while(itr.hasNext()) {
+                                Object match = itr.next();
+                                if (match instanceof IMatch) {
+                                    IMatch theMatch = (IMatch)match;
+                                    storage.setFileName(theMatch.getLocation().toOSString());
+                                    storage.setLength(theMatch.getEndOffset() - theMatch.getStartOffset());
+                                    storage.setOffset(theMatch.getStartOffset());
+                                    storage.setResource(ParserUtil.getResourceForFilename(theMatch.getLocation().toOSString()));
+                                    break;
+                                }
+                            }
+                        } else {
+                            operationNotAvailable(CSEARCH_OPERATION_NO_DEFINITION_MESSAGE);
+                        }
+                    }
+                } else if (selectedNames.length == 0){
+                    operationNotAvailable(CSEARCH_OPERATION_NO_NAMES_SELECTED_MESSAGE);
+                    return;
+                } else {
+                    operationNotAvailable(CSEARCH_OPERATION_TOO_MANY_NAMES_MESSAGE);
+                    return;
+                }
+                
+                return;
+            }
+            
+            private String findProjectName(IFile resourceFile) {
+                if( resourceFile == null ) return ""; //$NON-NLS-1$
+                IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
+                for( int i = 0; i < projects.length; ++i )
+                {
+                    if( projects[i].contains(resourceFile) )
+                        return projects[i].getName();
+                }
+                return ""; //$NON-NLS-1$
+            }
+        };
+
+        try {
+            ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(getShell());
+            progressMonitor.run(true, true, runnable);
+            
+            int nameOffset = storage.getOffset();
+            int nameLength = storage.getLength();
+            if( storage.getResource() != null )
+            {
+                clearStatusLine();
+                open( storage.getResource(), nameOffset,  nameLength );
+                return;
+            }
+            String fileName = storage.getFileName();
+            
+            if (fileName != null){
+                clearStatusLine();
+                open( fileName, nameOffset, nameLength);
+            }
+        } catch(Exception x) {
+                 CUIPlugin.getDefault().log(x);
+        }
+    }
+
+}
Index: parser/org/eclipse/cdt/core/dom/ast/IASTTranslationUnit.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTTranslationUnit.java,v
retrieving revision 1.20
diff -u -r1.20 IASTTranslationUnit.java
--- parser/org/eclipse/cdt/core/dom/ast/IASTTranslationUnit.java	13 Apr 2005 17:43:40 -0000	1.20
+++ parser/org/eclipse/cdt/core/dom/ast/IASTTranslationUnit.java	3 May 2005 13:44:41 -0000
@@ -1,5 +1,5 @@
 /**********************************************************************
- * Copyright (c) 2004 IBM Corporation and others.
+ * 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 Common Public License v1.0
  * which accompanies this distribution, and is available at
@@ -67,6 +67,14 @@
 	 * @return List of IASTName nodes for the binding's declaration
 	 */
 	public IASTName[] getDeclarations(IBinding binding);
+    
+    /**
+     * Returns the definition in this translation unit for the given binding.
+     *  
+     * @param binding
+     * @return the definition of the IBinding
+     */
+    public IASTName getDefinition(IBinding binding);
 
 	/**
 	 * Returns the list of references in this translation unit to the given
@@ -164,6 +172,4 @@
 
 	public String getContainingFilename(int offset);
     
-    
-    
 }
\ No newline at end of file
Index: parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTTranslationUnit.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTTranslationUnit.java,v
retrieving revision 1.20
diff -u -r1.20 CASTTranslationUnit.java
--- parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTTranslationUnit.java	28 Apr 2005 15:55:18 -0000	1.20
+++ parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTTranslationUnit.java	3 May 2005 13:44:42 -0000
@@ -1,5 +1,5 @@
 /**********************************************************************
- * Copyright (c) 2002-2004 IBM Canada and others.
+ * Copyright (c) 2002, 2005 IBM Canada and others.
  * All rights reserved.   This program and the accompanying materials
  * are made available under the terms of the Common Public License v0.5
  * which accompanies this distribution, and is available at
@@ -110,6 +110,22 @@
         }
 		return CVisitor.getDeclarations(this, binding);
 	}
+    
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.eclipse.cdt.core.dom.ast.IASTTranslationUnit#getDefinition(org.eclipse.cdt.core.dom.ast.IBinding)
+     */
+    public IASTName getDefinition(IBinding aBinding) {
+        IASTName[] foundDecls = getDeclarations(aBinding);
+        
+        for(int i=0; i<foundDecls.length; i++) {
+            if (foundDecls[i].isDefinition())
+                return foundDecls[i];
+        }
+        
+        return null; // if no definition was found then return null
+    }
 
 	/*
 	 * (non-Javadoc)
@@ -483,4 +499,5 @@
 			return EMPTY_STRING;
 		return resolver.getContainingFilename( offset );
 	}
+
 }
Index: parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTTranslationUnit.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTTranslationUnit.java,v
retrieving revision 1.22
diff -u -r1.22 CPPASTTranslationUnit.java
--- parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTTranslationUnit.java	28 Apr 2005 15:55:18 -0000	1.22
+++ parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTTranslationUnit.java	3 May 2005 13:44:42 -0000
@@ -1,5 +1,5 @@
 /**********************************************************************
- * Copyright (c) 2004 IBM Corporation and others.
+ * 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 Common Public License v1.0
  * which accompanies this distribution, and is available at
@@ -122,6 +122,22 @@
     /*
      * (non-Javadoc)
      * 
+     * @see org.eclipse.cdt.core.dom.ast.IASTTranslationUnit#getDefinition(org.eclipse.cdt.core.dom.ast.IBinding)
+     */
+    public IASTName getDefinition(IBinding aBinding) {
+        IASTName[] foundDecls = getDeclarations(aBinding);
+        
+        for(int i=0; i<foundDecls.length; i++) {
+            if (foundDecls[i].isDefinition())
+                return foundDecls[i];
+        }
+        
+        return null; // if no definition was found then return null
+    }
+    
+    /*
+     * (non-Javadoc)
+     * 
      * @see org.eclipse.cdt.core.dom.ast.IASTTranslationUnit#getReferences(org.eclipse.cdt.core.dom.ast.IBinding)
      */
     public IASTName[] getReferences(IBinding b) {
Index: search/org/eclipse/cdt/core/search/DOMSearchUtil.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/DOMSearchUtil.java,v
retrieving revision 1.3
diff -u -r1.3 DOMSearchUtil.java
--- search/org/eclipse/cdt/core/search/DOMSearchUtil.java	2 May 2005 18:04:24 -0000	1.3
+++ search/org/eclipse/cdt/core/search/DOMSearchUtil.java	3 May 2005 13:44:43 -0000
@@ -284,7 +284,16 @@
              names = tu.getDeclarations(binding);
          } else if (limitTo == ICSearchConstants.REFERENCES) {
              names = tu.getReferences(binding);
-         } else { // assume ALL
+         } else if (limitTo == ICSearchConstants.DEFINITIONS) {
+             IASTName def = tu.getDefinition(binding);
+             if (def != null) {
+                 names = new IASTName[1];
+                 names[0] = def;
+             }
+         } else if (limitTo == ICSearchConstants.ALL_OCCURRENCES){
+             names = tu.getDeclarations(binding);
+             names = (IASTName[])ArrayUtil.addAll(IASTName.class, names, tu.getReferences(binding));
+         } else {  // assume ALL
              names = tu.getDeclarations(binding);
              names = (IASTName[])ArrayUtil.addAll(IASTName.class, names, tu.getReferences(binding));
          }

Back to the top