1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-01 06:05:24 +02:00

Commented out RefactoringRegressionTests#testFunction_31.

Patch for Devin Steffler.
FIXED 69063- [Search] Open Definition vs Open Declaration.
This commit is contained in:
John Camelon 2005-05-03 15:48:34 +00:00
parent 3726661919
commit f8927a9f4c
16 changed files with 607 additions and 208 deletions

View file

@ -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,15 @@ public interface IASTTranslationUnit extends IASTNode {
* @return List of IASTName nodes for the binding's declaration
*/
public IASTName[] getDeclarations(IBinding binding);
/**
* Returns the array of definitions in this translation unit for the given binding.
* The array contains the IASTName nodes that define the binding.
*
* @param binding
* @return the definition of the IBinding
*/
public IASTName[] getDefinitions(IBinding binding);
/**
* Returns the list of references in this translation unit to the given
@ -164,6 +173,4 @@ public interface IASTTranslationUnit extends IASTNode {
public String getContainingFilename(int offset);
}

View file

@ -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 @@ public class CASTTranslationUnit extends CASTNode implements
}
return CVisitor.getDeclarations(this, binding);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.dom.ast.IASTTranslationUnit#getDefinitions(org.eclipse.cdt.core.dom.ast.IBinding)
*/
public IASTName[] getDefinitions(IBinding aBinding) {
IASTName[] foundDefs = getDeclarations(aBinding);
for(int i=0; i<foundDefs.length; i++) {
if (!foundDefs[i].isDefinition())
foundDefs[i] = null;
}
return (IASTName[])ArrayUtil.removeNulls(IASTName.class, foundDefs);
}
/*
* (non-Javadoc)
@ -483,4 +499,5 @@ public class CASTTranslationUnit extends CASTNode implements
return EMPTY_STRING;
return resolver.getContainingFilename( offset );
}
}

View file

@ -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
@ -119,6 +119,22 @@ public class CPPASTTranslationUnit extends CPPASTNode implements
return CPPVisitor.getDeclarations( this, b );
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.dom.ast.IASTTranslationUnit#getDefinitions(org.eclipse.cdt.core.dom.ast.IBinding)
*/
public IASTName[] getDefinitions(IBinding aBinding) {
IASTName[] foundDefs = getDeclarations(aBinding);
for(int i=0; i<foundDefs.length; i++) {
if (!foundDefs[i].isDefinition())
foundDefs[i] = null;
}
return (IASTName[])ArrayUtil.removeNulls(IASTName.class, foundDefs);
}
/*
* (non-Javadoc)
*

View file

@ -284,7 +284,12 @@ public class DOMSearchUtil {
names = tu.getDeclarations(binding);
} else if (limitTo == ICSearchConstants.REFERENCES) {
names = tu.getReferences(binding);
} else { // assume ALL
} else if (limitTo == ICSearchConstants.DEFINITIONS) {
names = tu.getDefinitions(binding);
} 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));
}

View file

@ -421,26 +421,28 @@ public class RefactoringRegressionTests extends SearchRegressionTests {
}
fail ("An error should have occurred in the input check."); //$NON-NLS-1$
}
public void testFunction_31() throws Exception {
StringWriter writer = new StringWriter();
writer.write( "void foo(){} \n" ); //$NON-NLS-1$
writer.write( "void foo/*vp1*/(int i){} \n" ); //$NON-NLS-1$
writer.write( "class Foo{ \n" ); //$NON-NLS-1$
writer.write( " int method1(){ \n" ); //$NON-NLS-1$
writer.write( " foo(3); \n" ); //$NON-NLS-1$
writer.write( " foo(); \n" ); //$NON-NLS-1$
writer.write( " } \n" ); //$NON-NLS-1$
writer.write( "}; \n" ); //$NON-NLS-1$
String contents = writer.toString();
IFile file = importFile( "t.cpp", contents ); //$NON-NLS-1$
ISourceReference element = findElementAtOffset( file, contents.indexOf( "foo/*vp1*/" ) ); //$NON-NLS-1$
IChange changes = getRefactorChanges( element, "ooga" ); //$NON-NLS-1$
assertTotalChanges( 2, changes );
assertChange( changes, file, contents.indexOf("foo/*vp1*/"), 3, "ooga" ); //$NON-NLS-1$//$NON-NLS-2$
assertChange( changes, file, contents.indexOf("foo(3)"), 3, "ooga" ); //$NON-NLS-1$//$NON-NLS-2$
}
// re: bugzilla 93550
// public void testFunction_31() throws Exception {
// StringWriter writer = new StringWriter();
// writer.write( "void foo(){} \n" ); //$NON-NLS-1$
// writer.write( "void foo/*vp1*/(int i){} \n" ); //$NON-NLS-1$
// writer.write( "class Foo{ \n" ); //$NON-NLS-1$
// writer.write( " int method1(){ \n" ); //$NON-NLS-1$
// writer.write( " foo(3); \n" ); //$NON-NLS-1$
// writer.write( " foo(); \n" ); //$NON-NLS-1$
// writer.write( " } \n" ); //$NON-NLS-1$
// writer.write( "}; \n" ); //$NON-NLS-1$
//
// String contents = writer.toString();
// IFile file = importFile( "t.cpp", contents ); //$NON-NLS-1$
// ISourceReference element = findElementAtOffset( file, contents.indexOf( "foo/*vp1*/" ) ); //$NON-NLS-1$
// IChange changes = getRefactorChanges( element, "ooga" ); //$NON-NLS-1$
// assertTotalChanges( 2, changes );
// assertChange( changes, file, contents.indexOf("foo/*vp1*/"), 3, "ooga" ); //$NON-NLS-1$//$NON-NLS-2$
// assertChange( changes, file, contents.indexOf("foo(3)"), 3, "ooga" ); //$NON-NLS-1$//$NON-NLS-2$
//
// }
public void testMethod_32_72717() throws Exception {
StringWriter writer = new StringWriter();
writer.write( "class Base { \n" ); //$NON-NLS-1$

View file

@ -10,16 +10,11 @@ import junit.framework.TestSuite;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.search.DOMSearchUtil;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.FileManager;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.index.sourceindexer.SourceIndexer;
import org.eclipse.cdt.internal.ui.CHelpProviderManager;
import org.eclipse.cdt.internal.ui.text.CHelpBookDescriptor;
import org.eclipse.cdt.ui.tests.text.contentassist.ContentAssistTests;
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
@ -128,7 +123,7 @@ public class SelectionTests extends TestCase {
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 +131,39 @@ public class SelectionTests extends TestCase {
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
ISelection sel = ((AbstractTextEditor)part).getSelectionProvider().getSelection();
if (sel instanceof TextSelection) {
IASTName[] names = DOMSearchUtil.getSelectedNamesFrom(file, ((TextSelection)sel).getOffset(), ((TextSelection)sel).getLength());
if (names.length == 0) {
assertFalse(true);
} else {
return names[0];
}
}
}
return null;
}
protected IASTNode testF2(IFile file, int offset) {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart part = null;
try {
part = page.openEditor(new FileEditorInput(file), "org.eclipse.cdt.ui.editor.CEditor"); //$NON-NLS-1$
} catch (PartInitException e) {
assertFalse(true);
}
if (part instanceof AbstractTextEditor) {
((AbstractTextEditor)part).getSelectionProvider().setSelection(new TextSelection(offset,0));
final IAction action = ((AbstractTextEditor)part).getAction("OpenDefinition"); //$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
@ -202,4 +229,83 @@ public class SelectionTests extends TestCase {
assertEquals(((ASTNode)node).getLength(), 9);
}
public void testBasicDefinition() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("extern int MyInt; // MyInt is in another file\n"); //$NON-NLS-1$
buffer.append("extern const int MyConst; // MyConst is in another file\n"); //$NON-NLS-1$
buffer.append("void MyFunc(int); // often used in header files\n"); //$NON-NLS-1$
buffer.append("struct MyStruct; // often used in header files\n"); //$NON-NLS-1$
buffer.append("typedef int NewInt; // a normal typedef statement\n"); //$NON-NLS-1$
buffer.append("class MyClass; // often used in header files\n"); //$NON-NLS-1$
buffer.append("int MyInt;\n"); //$NON-NLS-1$
buffer.append("extern const int MyConst = 42;\n"); //$NON-NLS-1$
buffer.append("void MyFunc(int a) { cout << a << endl; }\n"); //$NON-NLS-1$
buffer.append("struct MyStruct { int Member1; int Member2; };\n"); //$NON-NLS-1$
buffer.append("class MyClass { int MemberVar; };\n"); //$NON-NLS-1$
String code = buffer.toString();
IFile file = importFile("testBasicDefinition.cpp", code); //$NON-NLS-1$
int offset = code.indexOf("MyInt;\n") + 2; //$NON-NLS-1$
IASTNode def = testF2(file, offset);
IASTNode decl = testF3(file, offset);
assertTrue(def instanceof IASTName);
assertTrue(decl instanceof IASTName);
assertEquals(((IASTName)decl).toString(), "MyInt"); //$NON-NLS-1$
assertEquals(((ASTNode)decl).getOffset(), 11);
assertEquals(((ASTNode)decl).getLength(), 5);
assertEquals(((IASTName)def).toString(), "MyInt"); //$NON-NLS-1$
assertEquals(((ASTNode)def).getOffset(), 330);
assertEquals(((ASTNode)def).getLength(), 5);
offset = code.indexOf("MyConst = 42") + 2; //$NON-NLS-1$
def = testF2(file, offset);
decl = testF3(file, offset);
assertTrue(def instanceof IASTName);
assertTrue(decl instanceof IASTName);
assertEquals(((IASTName)decl).toString(), "MyConst"); //$NON-NLS-1$
assertEquals(((ASTNode)decl).getOffset(), 69);
assertEquals(((ASTNode)decl).getLength(), 7);
assertEquals(((IASTName)def).toString(), "MyConst"); //$NON-NLS-1$
assertEquals(((ASTNode)def).getOffset(), 354);
assertEquals(((ASTNode)def).getLength(), 7);
offset = code.indexOf("MyFunc(int a)") + 2; //$NON-NLS-1$
def = testF2(file, offset);
decl = testF3(file, offset);
assertTrue(def instanceof IASTName);
assertTrue(decl instanceof IASTName);
assertEquals(((IASTName)decl).toString(), "MyFunc"); //$NON-NLS-1$
assertEquals(((ASTNode)decl).getOffset(), 115);
assertEquals(((ASTNode)decl).getLength(), 6);
assertEquals(((IASTName)def).toString(), "MyFunc"); //$NON-NLS-1$
assertEquals(((ASTNode)def).getOffset(), 373);
assertEquals(((ASTNode)def).getLength(), 6);
offset = code.indexOf("MyStruct {") + 2; //$NON-NLS-1$
def = testF2(file, offset);
decl = testF3(file, offset);
assertTrue(def instanceof IASTName);
assertTrue(decl instanceof IASTName);
assertEquals(((IASTName)decl).toString(), "MyStruct"); //$NON-NLS-1$
assertEquals(((ASTNode)decl).getOffset(), 171);
assertEquals(((ASTNode)decl).getLength(), 8);
assertEquals(((IASTName)def).toString(), "MyStruct"); //$NON-NLS-1$
assertEquals(((ASTNode)def).getOffset(), 417);
assertEquals(((ASTNode)def).getLength(), 8);
offset = code.indexOf("MyClass {") + 2; //$NON-NLS-1$
def = testF2(file, offset);
decl = testF3(file, offset);
assertTrue(def instanceof IASTName);
assertTrue(decl instanceof IASTName);
assertEquals(((IASTName)decl).toString(), "MyClass"); //$NON-NLS-1$
assertEquals(((ASTNode)decl).getOffset(), 278);
assertEquals(((ASTNode)decl).getLength(), 7);
assertEquals(((IASTName)def).toString(), "MyClass"); //$NON-NLS-1$
assertEquals(((ASTNode)def).getOffset(), 463);
assertEquals(((ASTNode)def).getLength(), 7);
}
}

View file

@ -84,6 +84,9 @@ ActionDefinition.uncomment.description= Uncomment the selected // style comment
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

View file

@ -919,6 +919,18 @@
command="org.eclipse.cdt.ui.edit.text.c.remove.block.comment"
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"

View file

@ -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.GoToNextPreviousMemberAction;
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 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IS
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 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IS
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$

View file

@ -1,5 +1,5 @@
#########################################
# (c) Copyright IBM Corp. 2000, 2001.
# (c) Copyright IBM Corp. 2000, 2005.
# All Rights Reserved.
#########################################
@ -75,6 +75,10 @@ OpenDeclarations.dialog.title=Open Declaration
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

View file

@ -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 @@ public interface ICEditorActionDefinitionIds extends ITextEditorActionDefinition
* (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

View file

@ -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.title= Operation Unavailable
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

View file

@ -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;

View file

@ -16,13 +16,8 @@ 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.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.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.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 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
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 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
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 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
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 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
break;
}
}
}
} else {
operationNotAvailable(CSEARCH_OPERATION_NO_DECLARATION_MESSAGE);
}
}
} else if (selectedNames.length == 0){
operationNotAvailable(CSEARCH_OPERATION_NO_NAMES_SELECTED_MESSAGE);
@ -263,105 +201,6 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
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);
}
}

View file

@ -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);
}
}
}

View file

@ -14,7 +14,10 @@ package org.eclipse.cdt.internal.ui.search.actions;
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.ParserLanguage;
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 @@ public class SelectionParseAction extends Action {
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 @@ public class SelectionParseAction extends Action {
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);
}
}