1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Use node-selector for Open Call Hierarchy and Open Type Hierarchy.

This commit is contained in:
Markus Schorn 2008-03-06 13:27:49 +00:00
parent 6be8db253c
commit 9d9607ee05
6 changed files with 18 additions and 128 deletions

View file

@ -298,7 +298,8 @@ public class BaseUITestCase extends BaseTestCase {
final protected TreeItem checkTreeNode(Tree tree, int i0, String label) { final protected TreeItem checkTreeNode(Tree tree, int i0, String label) {
TreeItem root= null; TreeItem root= null;
for (int i=0; i<400; i++) { for (int millis=0; millis < 5000; millis= millis==0 ? 1 : millis*2) {
runEventQueue(millis);
try { try {
root= tree.getItem(i0); root= tree.getItem(i0);
if (label.equals(root.getText())) { if (label.equals(root.getText())) {
@ -311,7 +312,6 @@ public class BaseUITestCase extends BaseTestCase {
catch (IllegalArgumentException e) { catch (IllegalArgumentException e) {
// item does not yet exist. // item does not yet exist.
} }
runEventQueue(10);
} }
assertNotNull("Tree node " + label + "{" + i0 + "} does not exist!", root); assertNotNull("Tree node " + label + "{" + i0 + "} does not exist!", root);
assertEquals(label, root.getText()); assertEquals(label, root.getText());
@ -321,7 +321,8 @@ public class BaseUITestCase extends BaseTestCase {
final protected TreeItem checkTreeNode(Tree tree, int i0, int i1, String label) { final protected TreeItem checkTreeNode(Tree tree, int i0, int i1, String label) {
TreeItem item= null; TreeItem item= null;
String itemText= null; String itemText= null;
for (int i=0; i<400; i++) { for (int millis=0; millis < 5000; millis= millis==0 ? 1 : millis*2) {
runEventQueue(millis);
TreeItem root= tree.getItem(i0); TreeItem root= tree.getItem(i0);
try { try {
TreeItem firstItem= root.getItem(0); TreeItem firstItem= root.getItem(0);
@ -333,7 +334,7 @@ public class BaseUITestCase extends BaseTestCase {
if (label.equals(itemText)) { if (label.equals(itemText)) {
return item; return item;
} }
if (i > 100) { if (millis > 2000) {
assertEquals(label, itemText); assertEquals(label, itemText);
return item; return item;
} }
@ -348,7 +349,6 @@ public class BaseUITestCase extends BaseTestCase {
catch (SWTException e) { catch (SWTException e) {
// widget was disposed, try again. // widget was disposed, try again.
} }
runEventQueue(10);
} }
assertEquals("Timeout expired waiting for tree node {" + i0 + "," + i1 + "}", label, itemText); assertEquals("Timeout expired waiting for tree node {" + i0 + "," + i1 + "}", label, itemText);
return null; return null;

View file

@ -82,7 +82,7 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
checkTreeNode(tree, 0, "proto()"); checkTreeNode(tree, 0, "proto()");
checkTreeNode(tree, 0, 0, "main()"); checkTreeNode(tree, 0, 0, "main()");
editor.selectAndReveal(content.indexOf("func(); //ref"), 7); editor.selectAndReveal(content.indexOf("func(); //ref"), 4);
openCallHierarchy(editor); openCallHierarchy(editor);
checkTreeNode(tree, 0, "func()"); checkTreeNode(tree, 0, "func()");
checkTreeNode(tree, 0, 0, "main()"); checkTreeNode(tree, 0, 0, "main()");
@ -737,7 +737,7 @@ public class BasicCallHierarchyTest extends CallHierarchyBaseTest {
checkTreeNode(tree, 0, "proto(int)"); checkTreeNode(tree, 0, "proto(int)");
checkTreeNode(tree, 0, 0, "main(int)"); checkTreeNode(tree, 0, 0, "main(int)");
editor.selectAndReveal(content.indexOf("func(1); //ref"), 7); editor.selectAndReveal(content.indexOf("func(1); //ref"), 4);
openCallHierarchy(editor); openCallHierarchy(editor);
checkTreeNode(tree, 0, "func(int)"); checkTreeNode(tree, 0, "func(int)");
checkTreeNode(tree, 0, 0, "main(int)"); checkTreeNode(tree, 0, 0, "main(int)");

View file

@ -37,7 +37,7 @@ public class InitializersInCallHierarchyTest extends CallHierarchyBaseTest {
waitForIndexer(fIndex, file, INDEXER_WAIT_TIME); waitForIndexer(fIndex, file, INDEXER_WAIT_TIME);
CEditor editor = openFile(file); CEditor editor = openFile(file);
editor.selectAndReveal(content.indexOf("a"), 5); editor.selectAndReveal(content.indexOf("a"), 1);
openCallHierarchy(editor); openCallHierarchy(editor);
Tree tree = getCHTreeViewer().getTree(); Tree tree = getCHTreeViewer().getTree();
checkTreeNode(tree, 0, "a"); checkTreeNode(tree, 0, "a");

View file

@ -28,7 +28,6 @@ import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IEnumeration; import org.eclipse.cdt.core.dom.ast.IEnumeration;
@ -44,13 +43,10 @@ import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IFunctionDeclaration; import org.eclipse.cdt.core.model.IFunctionDeclaration;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.ui.util.ExceptionHandler; import org.eclipse.cdt.internal.ui.util.ExceptionHandler;
import org.eclipse.cdt.internal.ui.util.StatusLineHandler; import org.eclipse.cdt.internal.ui.util.StatusLineHandler;
import org.eclipse.cdt.internal.ui.viewsupport.FindNameForSelectionVisitor;
import org.eclipse.cdt.internal.ui.viewsupport.IndexUI; import org.eclipse.cdt.internal.ui.viewsupport.IndexUI;
public class TypeHierarchyUI { public class TypeHierarchyUI {
@ -114,6 +110,7 @@ public class TypeHierarchyUI {
final Display display= Display.getCurrent(); final Display display= Display.getCurrent();
Job job= new Job(Messages.TypeHierarchyUI_OpenTypeHierarchy) { Job job= new Job(Messages.TypeHierarchyUI_OpenTypeHierarchy) {
@Override
protected IStatus run(IProgressMonitor monitor) { protected IStatus run(IProgressMonitor monitor) {
try { try {
StatusLineHandler.clearStatusLine(editor.getSite()); StatusLineHandler.clearStatusLine(editor.getSite());
@ -147,7 +144,7 @@ public class TypeHierarchyUI {
index.acquireReadLock(); index.acquireReadLock();
try { try {
IASTName name= getSelectedName(index, editorInput, sel); IASTName name= IndexUI.getSelectedName(editorInput, sel);
if (name != null) { if (name != null) {
IBinding binding= name.resolveBinding(); IBinding binding= name.resolveBinding();
if (!isValidInput(binding)) { if (!isValidInput(binding)) {
@ -260,21 +257,6 @@ public class TypeHierarchyUI {
return IndexUI.findAnyDeclaration(index, project, binding); return IndexUI.findAnyDeclaration(index, project, binding);
} }
private static IASTName getSelectedName(IIndex index, IEditorInput editorInput, IRegion sel) throws CoreException {
int selectionStart = sel.getOffset();
int selectionLength = sel.getLength();
IWorkingCopy workingCopy = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editorInput);
if (workingCopy == null)
return null;
int options= ITranslationUnit.AST_SKIP_INDEXED_HEADERS;
IASTTranslationUnit ast = workingCopy.getAST(index, options);
FindNameForSelectionVisitor finder= new FindNameForSelectionVisitor(ast.getFilePath(), selectionStart, selectionLength);
ast.accept(finder);
return finder.getSelectedName();
}
public static boolean isValidInput(IBinding binding) { public static boolean isValidInput(IBinding binding) {
if (isValidTypeInput(binding) if (isValidTypeInput(binding)
|| binding instanceof ICPPMember || binding instanceof ICPPMember

View file

@ -1,95 +0,0 @@
/*******************************************************************************
* Copyright (c) 2006 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.viewsupport;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
/**
* Searches for a name related to the given selection. The first choice will be the
* largest name inside the selection. If it does not exist the smallest name
* surounding the selection is taken.
* @see IASTNode#accept(ASTVisitor)
* @since 4.0
*/
public class FindNameForSelectionVisitor extends ASTVisitor {
private String fFilePath;
private int fOffset;
private int fEndOffset;
private IASTName fSelectedName;
public FindNameForSelectionVisitor(String filePath, int selectionStart, int selectionLength) {
fFilePath= filePath;
fOffset= selectionStart;
fEndOffset= selectionStart+selectionLength;
shouldVisitDeclarations= true;
shouldVisitNames= true;
}
/**
* After the visitor was accepted by an ast-node you can query the
* selected name.
* @return the name found for the selection, or <code>null</code>.
* @since 4.0
*/
public IASTName getSelectedName() {
return fSelectedName;
}
public int visit(IASTDeclaration declaration) {
IASTFileLocation loc= declaration.getFileLocation();
if (loc == null || !loc.getFileName().equals(fFilePath)) {
return PROCESS_SKIP;
}
int offset= loc.getNodeOffset();
int endoffset= offset + loc.getNodeLength();
if (endoffset < fOffset || fEndOffset < offset) {
return PROCESS_SKIP;
}
return PROCESS_CONTINUE;
}
public int visit(IASTName name) {
IASTFileLocation loc= name.getFileLocation();
if (loc == null) {
return PROCESS_CONTINUE;
}
if (!loc.getFileName().equals(fFilePath)) {
return PROCESS_SKIP;
}
int offset= loc.getNodeOffset();
int endoffset= offset + loc.getNodeLength();
// check if name is inside of selection
if (fOffset <= offset && endoffset <= fEndOffset) {
fSelectedName= name;
return PROCESS_ABORT;
}
// check if name surrounds selection
if (offset <= fOffset && fEndOffset <= endoffset) {
fSelectedName= name;
// continue as we might find a name inside of the selection,
// which is preferred.
return PROCESS_CONTINUE;
}
return PROCESS_CONTINUE;
}
}

View file

@ -368,9 +368,14 @@ public class IndexUI {
} }
public static IASTName getSelectedName(IEditorInput editorInput, ITextSelection selection) throws CoreException { public static IASTName getSelectedName(IEditorInput editorInput, ITextSelection selection) throws CoreException {
final int selectionStart = selection.getOffset(); return getSelectedName(editorInput, selection.getOffset(), selection.getLength());
final int selectionLength = selection.getLength(); }
public static IASTName getSelectedName(IEditorInput editorInput, IRegion selection) throws CoreException {
return getSelectedName(editorInput, selection.getOffset(), selection.getLength());
}
private static IASTName getSelectedName(IEditorInput editorInput, final int offset, final int length) {
IWorkingCopy workingCopy = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editorInput); IWorkingCopy workingCopy = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editorInput);
if (workingCopy == null) if (workingCopy == null)
return null; return null;
@ -379,9 +384,7 @@ public class IndexUI {
ASTProvider.getASTProvider().runOnAST(workingCopy, ASTProvider.WAIT_YES, null, new ASTRunnable() { ASTProvider.getASTProvider().runOnAST(workingCopy, ASTProvider.WAIT_YES, null, new ASTRunnable() {
public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) { public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) {
if (ast != null) { if (ast != null) {
FindNameForSelectionVisitor finder= new FindNameForSelectionVisitor(ast.getFilePath(), selectionStart, selectionLength); result[0]= ast.getNodeSelector(null).findEnclosingName(offset, length);
ast.accept(finder);
result[0]= finder.getSelectedName();
} }
return Status.OK_STATUS; return Status.OK_STATUS;
} }