mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-09 02:36:01 +02:00
Bug 463234 - Infrastructure for testing ambiguous targets for Open
Declaration Change-Id: Ib5b5166b19a85516f7e7b9d930d763a2e280768e Signed-off-by: Nathan Ridge <zeratul976@hotmail.com>
This commit is contained in:
parent
f4cf37f562
commit
c89901c49a
6 changed files with 88 additions and 19 deletions
|
@ -28,6 +28,7 @@ import org.eclipse.ui.part.FileEditorInput;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
|
import org.eclipse.cdt.core.model.ICElement;
|
||||||
import org.eclipse.cdt.core.model.ILanguage;
|
import org.eclipse.cdt.core.model.ILanguage;
|
||||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
import org.eclipse.cdt.ui.testplugin.EditorTestHelper;
|
import org.eclipse.cdt.ui.testplugin.EditorTestHelper;
|
||||||
|
@ -39,6 +40,8 @@ import org.eclipse.cdt.internal.core.parser.ParserException;
|
||||||
import org.eclipse.cdt.internal.ui.editor.ASTProvider;
|
import org.eclipse.cdt.internal.ui.editor.ASTProvider;
|
||||||
import org.eclipse.cdt.internal.ui.editor.CEditor;
|
import org.eclipse.cdt.internal.ui.editor.CEditor;
|
||||||
import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction;
|
import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction;
|
||||||
|
import org.eclipse.cdt.internal.ui.search.actions.SelectionParseAction;
|
||||||
|
import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction.ITargetDisambiguator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for all selection tests, using the indexer or not.
|
* Base class for all selection tests, using the indexer or not.
|
||||||
|
@ -62,10 +65,44 @@ public abstract class BaseSelectionTests extends BaseUITestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected IASTNode testF3(IFile file, int offset) throws ParserException, CoreException {
|
protected IASTNode testF3(IFile file, int offset) throws ParserException, CoreException {
|
||||||
return testF3(file, offset, 0);
|
return testF3(file, offset, 0, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class TargetChooser implements ITargetDisambiguator {
|
||||||
|
private int fIndex;
|
||||||
|
private boolean fDisambiguationRequested = false;
|
||||||
|
|
||||||
|
public TargetChooser(int index) {
|
||||||
|
fIndex = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ICElement disambiguateTargets(ICElement[] targets, SelectionParseAction action) {
|
||||||
|
fDisambiguationRequested = true;
|
||||||
|
return targets[fIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean disambiguationRequested() {
|
||||||
|
return fDisambiguationRequested;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected IASTNode testF3(IFile file, int offset, int length) throws ParserException, CoreException {
|
protected IASTNode testF3(IFile file, int offset, int length) throws ParserException, CoreException {
|
||||||
|
return testF3(file, offset, length, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected IASTNode testF3WithAmbiguity(IFile file, int offset, int targetChoiceIndex)
|
||||||
|
throws ParserException, CoreException {
|
||||||
|
TargetChooser chooser = new TargetChooser(targetChoiceIndex);
|
||||||
|
OpenDeclarationsAction.sDisallowAmbiguousInput = false;
|
||||||
|
IASTNode result = testF3(file, offset, 0, chooser);
|
||||||
|
OpenDeclarationsAction.sDisallowAmbiguousInput = true;
|
||||||
|
assertTrue(chooser.disambiguationRequested()); // Make sure there actually was an ambiguity
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected IASTNode testF3(IFile file, int offset, int length, ITargetDisambiguator disambiguator)
|
||||||
|
throws ParserException, CoreException {
|
||||||
if (offset < 0)
|
if (offset < 0)
|
||||||
throw new ParserException("offset can not be less than 0 and was " + offset); //$NON-NLS-1$
|
throw new ParserException("offset can not be less than 0 and was " + offset); //$NON-NLS-1$
|
||||||
|
|
||||||
|
@ -83,7 +120,11 @@ public abstract class BaseSelectionTests extends BaseUITestCase {
|
||||||
editor.getSelectionProvider().setSelection(new TextSelection(offset,length));
|
editor.getSelectionProvider().setSelection(new TextSelection(offset,length));
|
||||||
|
|
||||||
final OpenDeclarationsAction action = (OpenDeclarationsAction) editor.getAction("OpenDeclarations"); //$NON-NLS-1$
|
final OpenDeclarationsAction action = (OpenDeclarationsAction) editor.getAction("OpenDeclarations"); //$NON-NLS-1$
|
||||||
|
if (disambiguator == null) {
|
||||||
action.runSync();
|
action.runSync();
|
||||||
|
} else {
|
||||||
|
action.runSync(disambiguator);
|
||||||
|
}
|
||||||
|
|
||||||
if (shouldUpdateEditor()) {
|
if (shouldUpdateEditor()) {
|
||||||
// update the file/part to point to the newly opened IFile/IEditorPart
|
// update the file/part to point to the newly opened IFile/IEditorPart
|
||||||
|
|
|
@ -66,7 +66,7 @@ public class BaseSelectionTestsIndexer extends BaseSelectionTests {
|
||||||
@Override
|
@Override
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
OpenDeclarationsAction.sIsJUnitTest= true;
|
OpenDeclarationsAction.sDisallowAmbiguousInput= true;
|
||||||
IWorkbenchPage page= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
|
IWorkbenchPage page= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
|
||||||
IViewReference[] refs= page.getViewReferences();
|
IViewReference[] refs= page.getViewReferences();
|
||||||
for (IViewReference viewReference : refs) {
|
for (IViewReference viewReference : refs) {
|
||||||
|
|
|
@ -130,7 +130,7 @@ public class CPPSelectionTestsNoIndexer extends BaseSelectionTests {
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
initProject();
|
initProject();
|
||||||
OpenDeclarationsAction.sIsJUnitTest= true;
|
OpenDeclarationsAction.sDisallowAmbiguousInput= true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1191,5 +1191,4 @@ public class CPPSelectionTestsNoIndexer extends BaseSelectionTests {
|
||||||
int offset = code.indexOf("obj.waldo") + 4;
|
int offset = code.indexOf("obj.waldo") + 4;
|
||||||
assertTrue(testF3(file, offset) instanceof IASTName);
|
assertTrue(testF3(file, offset) instanceof IASTName);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,7 +100,7 @@ public class CSelectionTestsNoIndexer extends BaseSelectionTests {
|
||||||
@Override
|
@Override
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
OpenDeclarationsAction.sIsJUnitTest= true;
|
OpenDeclarationsAction.sDisallowAmbiguousInput= true;
|
||||||
initProject();
|
initProject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,15 +25,17 @@ import org.eclipse.cdt.core.model.ICElement;
|
||||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
import org.eclipse.cdt.ui.CUIPlugin;
|
import org.eclipse.cdt.ui.CUIPlugin;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.internal.ui.actions.OpenActionUtil;
|
||||||
import org.eclipse.cdt.internal.ui.editor.CEditor;
|
import org.eclipse.cdt.internal.ui.editor.CEditor;
|
||||||
import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
|
import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
|
||||||
import org.eclipse.cdt.internal.ui.text.CWordFinder;
|
import org.eclipse.cdt.internal.ui.text.CWordFinder;
|
||||||
|
import org.eclipse.cdt.internal.ui.viewsupport.CElementLabels;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Navigates to the definition of a name, or to the declaration if invoked on the definition.
|
* Navigates to the definition of a name, or to the declaration if invoked on the definition.
|
||||||
*/
|
*/
|
||||||
public class OpenDeclarationsAction extends SelectionParseAction {
|
public class OpenDeclarationsAction extends SelectionParseAction {
|
||||||
public static boolean sIsJUnitTest = false;
|
public static boolean sDisallowAmbiguousInput = false;
|
||||||
|
|
||||||
ITextSelection fTextSelection;
|
ITextSelection fTextSelection;
|
||||||
|
|
||||||
|
@ -49,7 +51,7 @@ public class OpenDeclarationsAction extends SelectionParseAction {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
OpenDeclarationsJob job = createJob();
|
OpenDeclarationsJob job = createJob(sDefaultDisambiguator);
|
||||||
if (job != null)
|
if (job != null)
|
||||||
job.schedule();
|
job.schedule();
|
||||||
}
|
}
|
||||||
|
@ -58,17 +60,23 @@ public class OpenDeclarationsAction extends SelectionParseAction {
|
||||||
* For the purpose of regression testing.
|
* For the purpose of regression testing.
|
||||||
*/
|
*/
|
||||||
public void runSync() throws CoreException {
|
public void runSync() throws CoreException {
|
||||||
OpenDeclarationsJob job = createJob();
|
OpenDeclarationsJob job = createJob(sDefaultDisambiguator);
|
||||||
|
if (job != null)
|
||||||
|
job.performNavigation(new NullProgressMonitor());
|
||||||
|
}
|
||||||
|
public void runSync(ITargetDisambiguator targetDisambiguator) throws CoreException {
|
||||||
|
OpenDeclarationsJob job = createJob(targetDisambiguator);
|
||||||
if (job != null)
|
if (job != null)
|
||||||
job.performNavigation(new NullProgressMonitor());
|
job.performNavigation(new NullProgressMonitor());
|
||||||
}
|
}
|
||||||
|
|
||||||
private OpenDeclarationsJob createJob() {
|
private OpenDeclarationsJob createJob(ITargetDisambiguator targetDisambiguator) {
|
||||||
String text= computeSelectedWord();
|
String text= computeSelectedWord();
|
||||||
OpenDeclarationsJob job= null;
|
OpenDeclarationsJob job= null;
|
||||||
ICElement elem= fEditor.getInputCElement();
|
ICElement elem= fEditor.getInputCElement();
|
||||||
if (elem instanceof ITranslationUnit && fTextSelection != null) {
|
if (elem instanceof ITranslationUnit && fTextSelection != null) {
|
||||||
job= new OpenDeclarationsJob(this, (ITranslationUnit) elem, fTextSelection, text);
|
job= new OpenDeclarationsJob(this, (ITranslationUnit) elem, fTextSelection, text,
|
||||||
|
targetDisambiguator);
|
||||||
}
|
}
|
||||||
return job;
|
return job;
|
||||||
}
|
}
|
||||||
|
@ -93,4 +101,25 @@ public class OpenDeclarationsAction extends SelectionParseAction {
|
||||||
}
|
}
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to diambiguate between multiple candidate targets for this action.
|
||||||
|
*/
|
||||||
|
public static interface ITargetDisambiguator {
|
||||||
|
ICElement disambiguateTargets(ICElement[] targets, SelectionParseAction action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disambiguates by showing the user a dialog to choose.
|
||||||
|
*/
|
||||||
|
private static class DialogTargetDisambiguator implements ITargetDisambiguator {
|
||||||
|
@Override
|
||||||
|
public ICElement disambiguateTargets(ICElement[] targets, SelectionParseAction action) {
|
||||||
|
return OpenActionUtil.selectCElement(targets, action.getSite().getShell(),
|
||||||
|
CEditorMessages.OpenDeclarationsAction_dialog_title, CEditorMessages.OpenDeclarationsAction_selectMessage,
|
||||||
|
CElementLabels.ALL_DEFAULT | CElementLabels.ALL_FULLY_QUALIFIED | CElementLabels.MF_POST_FILE_QUALIFIED, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final ITargetDisambiguator sDefaultDisambiguator = new DialogTargetDisambiguator();
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,10 +97,9 @@ import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
|
||||||
import org.eclipse.cdt.internal.core.model.ext.CElementHandleFactory;
|
import org.eclipse.cdt.internal.core.model.ext.CElementHandleFactory;
|
||||||
import org.eclipse.cdt.internal.core.model.ext.ICElementHandle;
|
import org.eclipse.cdt.internal.core.model.ext.ICElementHandle;
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.ui.actions.OpenActionUtil;
|
|
||||||
import org.eclipse.cdt.internal.ui.editor.ASTProvider;
|
import org.eclipse.cdt.internal.ui.editor.ASTProvider;
|
||||||
import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
|
import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
|
||||||
import org.eclipse.cdt.internal.ui.viewsupport.CElementLabels;
|
import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction.ITargetDisambiguator;
|
||||||
import org.eclipse.cdt.internal.ui.viewsupport.IndexUI;
|
import org.eclipse.cdt.internal.ui.viewsupport.IndexUI;
|
||||||
|
|
||||||
class OpenDeclarationsJob extends Job implements ASTRunnable {
|
class OpenDeclarationsJob extends Job implements ASTRunnable {
|
||||||
|
@ -112,13 +111,16 @@ class OpenDeclarationsJob extends Job implements ASTRunnable {
|
||||||
private IIndex fIndex;
|
private IIndex fIndex;
|
||||||
private final ITextSelection fTextSelection;
|
private final ITextSelection fTextSelection;
|
||||||
private final String fSelectedText;
|
private final String fSelectedText;
|
||||||
|
private final ITargetDisambiguator fTargetDisambiguator;
|
||||||
|
|
||||||
OpenDeclarationsJob(SelectionParseAction action, ITranslationUnit editorInput, ITextSelection textSelection, String text) {
|
OpenDeclarationsJob(SelectionParseAction action, ITranslationUnit editorInput,
|
||||||
|
ITextSelection textSelection, String text, ITargetDisambiguator targetDisambiguator) {
|
||||||
super(CEditorMessages.OpenDeclarations_dialog_title);
|
super(CEditorMessages.OpenDeclarations_dialog_title);
|
||||||
fAction= action;
|
fAction= action;
|
||||||
fTranslationUnit= editorInput;
|
fTranslationUnit= editorInput;
|
||||||
fTextSelection= textSelection;
|
fTextSelection= textSelection;
|
||||||
fSelectedText= text;
|
fSelectedText= text;
|
||||||
|
fTargetDisambiguator= targetDisambiguator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -533,13 +535,11 @@ class OpenDeclarationsJob extends Job implements ASTRunnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (target == null) {
|
if (target == null) {
|
||||||
if (OpenDeclarationsAction.sIsJUnitTest) {
|
if (OpenDeclarationsAction.sDisallowAmbiguousInput) {
|
||||||
throw new RuntimeException("ambiguous input: " + uniqueElements.size()); //$NON-NLS-1$
|
throw new RuntimeException("ambiguous input: " + uniqueElements.size()); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
ICElement[] elemArray= uniqueElements.toArray(new ICElement[uniqueElements.size()]);
|
ICElement[] elemArray= uniqueElements.toArray(new ICElement[uniqueElements.size()]);
|
||||||
target = (ISourceReference) OpenActionUtil.selectCElement(elemArray, fAction.getSite().getShell(),
|
target = (ISourceReference) fTargetDisambiguator.disambiguateTargets(elemArray, fAction);
|
||||||
CEditorMessages.OpenDeclarationsAction_dialog_title, CEditorMessages.OpenDeclarationsAction_selectMessage,
|
|
||||||
CElementLabels.ALL_DEFAULT | CElementLabels.ALL_FULLY_QUALIFIED | CElementLabels.MF_POST_FILE_QUALIFIED, 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (target != null) {
|
if (target != null) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue