mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Patch for Devin Steffler.
Fixed Bug 101287 'find decl/defn' navigation not working using ctags indexer & no ScannerInfo
This commit is contained in:
parent
d2dce4a297
commit
295e397a5a
9 changed files with 202 additions and 39 deletions
|
@ -93,6 +93,30 @@ public class DOMSearchUtil {
|
|||
|
||||
return results.getSearchResults();
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a convenience method that uses the SearchEngine to find everything in the index
|
||||
* that matches to a String.
|
||||
*
|
||||
* @param scope the scope to search within the index
|
||||
* @param searchPattern the string used to search the index with
|
||||
* @return
|
||||
*/
|
||||
public static Set getMatchesFromSearchEngine(ICSearchScope scope, String searchPattern, LimitTo limitTo) {
|
||||
SearchEngine engine = new SearchEngine();
|
||||
BasicSearchResultCollector results = new BasicSearchResultCollector();
|
||||
|
||||
ICSearchPattern pattern = SearchEngine.createSearchPattern( searchPattern, ICSearchConstants.UNKNOWN_SEARCH_FOR, limitTo, true );
|
||||
|
||||
try {
|
||||
engine.setWaitingPolicy(ICSearchConstants.FORCE_IMMEDIATE_SEARCH);
|
||||
engine.search(CCorePlugin.getWorkspace(), pattern, scope, results, false);
|
||||
} catch (InterruptedException e) {
|
||||
return EMPTY_MATCHES;
|
||||
}
|
||||
|
||||
return results.getSearchResults();
|
||||
}
|
||||
|
||||
private static CSearchPattern createPattern( IASTName searchName, LimitTo limitTo, boolean caseSensitive) {
|
||||
IBinding binding = searchName.resolveBinding();
|
||||
|
|
|
@ -43,6 +43,7 @@ import org.eclipse.cdt.core.search.IMatchLocatable;
|
|||
import org.eclipse.cdt.core.search.LineLocatable;
|
||||
import org.eclipse.cdt.core.search.OffsetLocatable;
|
||||
import org.eclipse.cdt.core.search.OrPattern;
|
||||
import org.eclipse.cdt.core.search.SearchEngine;
|
||||
import org.eclipse.cdt.internal.core.CharOperation;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ISourceCodeParser;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.ANSICParserExtensionConfiguration;
|
||||
|
@ -75,6 +76,8 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
|
|||
public static final int ACCURATE_MATCH = 2;
|
||||
public static final int INACCURATE_MATCH = 3;
|
||||
|
||||
private static SearchFor[] fSearchForValues = { CLASS_STRUCT, FUNCTION, VAR, UNION, METHOD, FIELD, ENUM, ENUMTOR, NAMESPACE };
|
||||
|
||||
protected static class Requestor extends NullSourceElementRequestor
|
||||
{
|
||||
public int badCharacterOffset = -1;
|
||||
|
@ -141,6 +144,12 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
|
|||
pattern = createMacroPattern( patternString, limitTo, matchMode, caseSensitive );
|
||||
} else if ( searchFor == INCLUDE){
|
||||
pattern = createIncludePattern( patternString, limitTo, matchMode, caseSensitive);
|
||||
} else if ( searchFor == UNKNOWN_SEARCH_FOR ) {
|
||||
OrPattern orPattern = new OrPattern();
|
||||
for( int i=0; i<fSearchForValues.length; i++) {
|
||||
orPattern.addPattern( SearchEngine.createSearchPattern( patternString, fSearchForValues[i], limitTo, caseSensitive ) );
|
||||
}
|
||||
pattern = orPattern;
|
||||
}
|
||||
|
||||
return pattern;
|
||||
|
|
|
@ -37,6 +37,8 @@ import org.eclipse.core.resources.ResourcesPlugin;
|
|||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IPath;
|
||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||
import org.eclipse.jface.text.TextSelection;
|
||||
import org.eclipse.jface.viewers.ISelection;
|
||||
|
||||
/**
|
||||
* Test F2/F3 with the CTags Indexer for a CPP project.
|
||||
|
@ -124,7 +126,8 @@ public class CPPSelectionTestsCTagsIndexer extends BaseSelectionTestsIndexer
|
|||
suite.addTest(new CPPSelectionTestsCTagsIndexer("testCPPSpecDeclsDefs")); //$NON-NLS-1$
|
||||
suite.addTest(new CPPSelectionTestsCTagsIndexer("testNoDefinitions")); //$NON-NLS-1$
|
||||
suite.addTest(new CPPSelectionTestsCTagsIndexer("testOpenFileDiffDir")); //$NON-NLS-1$
|
||||
|
||||
suite.addTest(new CPPSelectionTestsCTagsIndexer("testBug101287")); //$NON-NLS-1$
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
|
@ -639,6 +642,25 @@ public class CPPSelectionTestsCTagsIndexer extends BaseSelectionTestsIndexer
|
|||
assertEquals(((ASTNode)decl).getLength(), "y".length());
|
||||
|
||||
}
|
||||
|
||||
public void testBug101287() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("int abc;\n"); //$NON-NLS-1$
|
||||
buffer.append("int main(int argc, char **argv) {\n"); //$NON-NLS-1$
|
||||
buffer.append("abc\n"); //$NON-NLS-1$
|
||||
buffer.append("}\n"); //$NON-NLS-1$
|
||||
|
||||
String code = buffer.toString();
|
||||
IFile file = importFile("testBug101287.c", code); //$NON-NLS-1$
|
||||
|
||||
int offset = code.indexOf("abc\n"); //$NON-NLS-1$
|
||||
|
||||
ISelection decl = testF3Selection(file, offset);
|
||||
if (decl instanceof TextSelection) {
|
||||
assertEquals(((TextSelection)decl).getOffset(), code.indexOf("int abc;\n"));
|
||||
assertEquals(((TextSelection)decl).getLength(), "int abc;\n".length());
|
||||
}
|
||||
}
|
||||
|
||||
// REMINDER: see CSelectionTestsCTagsIndexer#suite() when appending new tests to this suite
|
||||
|
||||
|
|
|
@ -124,6 +124,7 @@ public class CPPSelectionTestsDOMIndexer extends BaseSelectionTestsIndexer imple
|
|||
suite.addTest(new CPPSelectionTestsDOMIndexer("testNoDefinitions")); //$NON-NLS-1$
|
||||
suite.addTest(new CPPSelectionTestsDOMIndexer("testBug95202")); //$NON-NLS-1$
|
||||
suite.addTest(new CPPSelectionTestsDOMIndexer("testBug95229")); //$NON-NLS-1$
|
||||
suite.addTest(new CPPSelectionTestsDOMIndexer("testBug101287")); //$NON-NLS-1$
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
@ -992,6 +993,29 @@ public class CPPSelectionTestsDOMIndexer extends BaseSelectionTestsIndexer imple
|
|||
assertEquals(((ASTNode)decl).getOffset(), 11);
|
||||
assertEquals(((ASTNode)decl).getLength(), 14);
|
||||
}
|
||||
|
||||
public void testBug101287() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("int abc;\n"); //$NON-NLS-1$
|
||||
buffer.append("int main(int argc, char **argv) {\n"); //$NON-NLS-1$
|
||||
buffer.append("abc\n"); //$NON-NLS-1$
|
||||
buffer.append("}\n"); //$NON-NLS-1$
|
||||
|
||||
String code = buffer.toString();
|
||||
IFile file = importFile("testBug101287.c", code); //$NON-NLS-1$
|
||||
|
||||
int offset = code.indexOf("abc\n"); //$NON-NLS-1$
|
||||
IASTNode def = testF2(file, offset);
|
||||
IASTNode decl = testF3(file, offset);
|
||||
assertTrue(decl instanceof IASTName);
|
||||
assertEquals(((IASTName)decl).toString(), "abc"); //$NON-NLS-1$
|
||||
assertEquals(((ASTNode)decl).getOffset(), 4);
|
||||
assertEquals(((ASTNode)decl).getLength(), 3);
|
||||
assertTrue(decl instanceof IASTName);
|
||||
assertEquals(((IASTName)decl).toString(), "abc"); //$NON-NLS-1$
|
||||
assertEquals(((ASTNode)decl).getOffset(), 4);
|
||||
assertEquals(((ASTNode)decl).getLength(), 3);
|
||||
}
|
||||
|
||||
// REMINDER: see CPPSelectionTestsDomIndexer#suite() when appending new tests to this suite
|
||||
}
|
||||
|
|
|
@ -12,7 +12,6 @@ package org.eclipse.cdt.ui.tests.text.selectiontests;
|
|||
|
||||
import java.io.File;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
|
@ -32,7 +31,6 @@ import org.eclipse.cdt.make.internal.core.scannerconfig2.PerProjectSICollector;
|
|||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.IProjectDescription;
|
||||
import org.eclipse.core.resources.IResource;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IPath;
|
||||
|
@ -121,6 +119,7 @@ public class CSelectionTestsCTagsIndexer extends BaseSelectionTestsIndexer
|
|||
suite.addTest(new CSelectionTestsCTagsIndexer("testCPPSpecDeclsDefs")); //$NON-NLS-1$
|
||||
suite.addTest(new CSelectionTestsCTagsIndexer("testNoDefinitions")); //$NON-NLS-1$
|
||||
suite.addTest(new CSelectionTestsCTagsIndexer("testOpenFileDiffDir")); //$NON-NLS-1$
|
||||
suite.addTest(new CSelectionTestsCTagsIndexer("testBug101287")); //$NON-NLS-1$
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
@ -630,6 +629,25 @@ public class CSelectionTestsCTagsIndexer extends BaseSelectionTestsIndexer
|
|||
}
|
||||
}
|
||||
|
||||
public void testBug101287() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("int abc;\n"); //$NON-NLS-1$
|
||||
buffer.append("int main(int argc, char **argv) {\n"); //$NON-NLS-1$
|
||||
buffer.append("abc\n"); //$NON-NLS-1$
|
||||
buffer.append("}\n"); //$NON-NLS-1$
|
||||
|
||||
String code = buffer.toString();
|
||||
IFile file = importFile("testBug101287.c", code); //$NON-NLS-1$
|
||||
|
||||
int offset = code.indexOf("abc\n"); //$NON-NLS-1$
|
||||
|
||||
ISelection decl = testF3Selection(file, offset);
|
||||
if (decl instanceof TextSelection) {
|
||||
assertEquals(((TextSelection)decl).getOffset(), code.indexOf("int abc;\n"));
|
||||
assertEquals(((TextSelection)decl).getLength(), "int abc;\n".length());
|
||||
}
|
||||
}
|
||||
|
||||
// REMINDER: see CSelectionTestsCTagsIndexer#suite() when appending new tests to this suite
|
||||
|
||||
}
|
||||
|
|
|
@ -12,7 +12,6 @@ package org.eclipse.cdt.ui.tests.text.selectiontests;
|
|||
|
||||
import java.io.File;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
|
@ -115,7 +114,8 @@ public class CSelectionTestsDOMIndexer extends BaseSelectionTestsIndexer impleme
|
|||
suite.addTest(new CSelectionTestsDOMIndexer("testCPPSpecDeclsDefs")); //$NON-NLS-1$
|
||||
suite.addTest(new CSelectionTestsDOMIndexer("testNoDefinitions")); //$NON-NLS-1$
|
||||
suite.addTest(new CSelectionTestsDOMIndexer("testOpenFileDiffDir")); //$NON-NLS-1$
|
||||
|
||||
suite.addTest(new CSelectionTestsDOMIndexer("testBug101287")); //$NON-NLS-1$
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
|
@ -620,6 +620,29 @@ public class CSelectionTestsDOMIndexer extends BaseSelectionTestsIndexer impleme
|
|||
assertEquals(((ASTNode)decl).getLength(), "y".length()); //$NON-NLS-1$
|
||||
|
||||
}
|
||||
|
||||
public void testBug101287() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("int abc;\n"); //$NON-NLS-1$
|
||||
buffer.append("int main(int argc, char **argv) {\n"); //$NON-NLS-1$
|
||||
buffer.append("abc\n"); //$NON-NLS-1$
|
||||
buffer.append("}\n"); //$NON-NLS-1$
|
||||
|
||||
String code = buffer.toString();
|
||||
IFile file = importFile("testBug101287.c", code); //$NON-NLS-1$
|
||||
|
||||
int offset = code.indexOf("abc\n"); //$NON-NLS-1$
|
||||
IASTNode def = testF2(file, offset);
|
||||
IASTNode decl = testF3(file, offset);
|
||||
assertTrue(decl instanceof IASTName);
|
||||
assertEquals(((IASTName)decl).toString(), "abc"); //$NON-NLS-1$
|
||||
assertEquals(((ASTNode)decl).getOffset(), 4);
|
||||
assertEquals(((ASTNode)decl).getLength(), 3);
|
||||
assertTrue(decl instanceof IASTName);
|
||||
assertEquals(((IASTName)decl).toString(), "abc"); //$NON-NLS-1$
|
||||
assertEquals(((ASTNode)decl).getOffset(), 4);
|
||||
assertEquals(((ASTNode)decl).getLength(), 3);
|
||||
}
|
||||
|
||||
// REMINDER: see CSelectionTestsDomIndexer#suite() when appending new tests to this suite
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@ import org.eclipse.cdt.core.model.ICProject;
|
|||
import org.eclipse.cdt.core.parser.ParseError;
|
||||
import org.eclipse.cdt.core.parser.ParserLanguage;
|
||||
import org.eclipse.cdt.core.search.DOMSearchUtil;
|
||||
import org.eclipse.cdt.core.search.ICSearchConstants;
|
||||
import org.eclipse.cdt.core.search.ICSearchScope;
|
||||
import org.eclipse.cdt.core.search.ICSearchConstants.LimitTo;
|
||||
import org.eclipse.cdt.core.search.ICSearchConstants.SearchFor;
|
||||
|
@ -72,8 +73,15 @@ public abstract class FindAction extends SelectionParseAction {
|
|||
* @param collector
|
||||
* @return
|
||||
*/
|
||||
public static CSearchQuery createDOMSearchQueryForName( IASTName name, LimitTo limitTo, ICSearchScope scope){
|
||||
return new DOMQuery(DOMSearchUtil.getSearchPattern(name), name, limitTo, scope);
|
||||
public CSearchQuery createDOMSearchQueryForName( IASTName name, LimitTo limitTo, ICSearchScope scope, String searchPattern){
|
||||
if (name != null) {
|
||||
return new DOMQuery(DOMSearchUtil.getSearchPattern(name), name, limitTo, scope);
|
||||
} else {
|
||||
if (searchPattern != null)
|
||||
return createSearchQuery(searchPattern, ICSearchConstants.UNKNOWN_SEARCH_FOR);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -86,8 +94,8 @@ public abstract class FindAction extends SelectionParseAction {
|
|||
* @param scope
|
||||
* @return
|
||||
*/
|
||||
public static CSearchQuery createSearchQueryForName( IASTName name, LimitTo limitTo, ICSearchScope scope ){
|
||||
return createDOMSearchQueryForName( name, limitTo, scope);
|
||||
public CSearchQuery createSearchQueryForName( IASTName name, LimitTo limitTo, ICSearchScope scope, String searchPattern ){
|
||||
return createDOMSearchQueryForName( name, limitTo, scope, searchPattern );
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
@ -188,32 +196,29 @@ public abstract class FindAction extends SelectionParseAction {
|
|||
} else {
|
||||
collector = new DOMSearchUtil.CNameCollector();
|
||||
}
|
||||
|
||||
if (foundNode == null) { // nothing found
|
||||
operationNotAvailable(CSEARCH_OPERATION_NO_NAMES_SELECTED_MESSAGE);
|
||||
return;
|
||||
|
||||
if (foundNode != null) { // nothing found
|
||||
foundNode.accept( collector );
|
||||
|
||||
List names = null;
|
||||
if (collector instanceof DOMSearchUtil.CPPNameCollector) {
|
||||
names = ((DOMSearchUtil.CPPNameCollector)collector).nameList;
|
||||
} else {
|
||||
names = ((DOMSearchUtil.CNameCollector)collector).nameList;
|
||||
}
|
||||
|
||||
if (names.size() == 1) { // just right
|
||||
clearStatusLine();
|
||||
} else if (names.size() == 0) { // no names selected
|
||||
operationNotAvailable(CSEARCH_OPERATION_NO_NAMES_SELECTED_MESSAGE);
|
||||
return;
|
||||
} else if (names.size() > 1) { // too many names selected
|
||||
operationNotAvailable(CSEARCH_OPERATION_TOO_MANY_NAMES_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
foundName = (IASTName)names.get(0);
|
||||
}
|
||||
|
||||
foundNode.accept( collector );
|
||||
|
||||
List names = null;
|
||||
if (collector instanceof DOMSearchUtil.CPPNameCollector) {
|
||||
names = ((DOMSearchUtil.CPPNameCollector)collector).nameList;
|
||||
} else {
|
||||
names = ((DOMSearchUtil.CNameCollector)collector).nameList;
|
||||
}
|
||||
|
||||
if (names.size() == 1) { // just right
|
||||
clearStatusLine();
|
||||
} else if (names.size() == 0) { // no names selected
|
||||
operationNotAvailable(CSEARCH_OPERATION_NO_NAMES_SELECTED_MESSAGE);
|
||||
return;
|
||||
} else if (names.size() > 1) { // too many names selected
|
||||
operationNotAvailable(CSEARCH_OPERATION_TOO_MANY_NAMES_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
foundName = (IASTName)names.get(0);
|
||||
}
|
||||
|
||||
LimitTo limitTo = getLimitTo();
|
||||
|
@ -223,7 +228,7 @@ public abstract class FindAction extends SelectionParseAction {
|
|||
if (searchScope == null)
|
||||
return;
|
||||
|
||||
CSearchQuery job = FindAction.createSearchQueryForName(foundName, limitTo, searchScope);
|
||||
CSearchQuery job = createSearchQueryForName(foundName, limitTo, searchScope, selNode.selText);
|
||||
|
||||
if (job == null)
|
||||
return;
|
||||
|
|
|
@ -185,8 +185,27 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
|
|||
}
|
||||
}
|
||||
} else if (selectedNames.length == 0){
|
||||
operationNotAvailable(CSEARCH_OPERATION_NO_NAMES_SELECTED_MESSAGE);
|
||||
return;
|
||||
// last try: search the index for the selected string, even if no name was found for that selection
|
||||
ICElement[] scope = new ICElement[1];
|
||||
scope[0] = project;
|
||||
Set matches = DOMSearchUtil.getMatchesFromSearchEngine( SearchEngine.createCSearchScope(scope), selNode.selText, ICSearchConstants.DECLARATIONS_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.setLocatable(theMatch.getLocatable());
|
||||
storage.setResource(ParserUtil.getResourceForFilename(theMatch.getLocation().toOSString()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
operationNotAvailable(CSEARCH_OPERATION_NO_DECLARATION_MESSAGE);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
operationNotAvailable(CSEARCH_OPERATION_TOO_MANY_NAMES_MESSAGE);
|
||||
return;
|
||||
|
|
|
@ -199,8 +199,27 @@ public class OpenDefinitionAction extends SelectionParseAction implements
|
|||
}
|
||||
}
|
||||
} else if (selectedNames.length == 0){
|
||||
operationNotAvailable(CSEARCH_OPERATION_NO_NAMES_SELECTED_MESSAGE);
|
||||
return;
|
||||
// last try: search the index for the selected string, even if no name was found for that selection
|
||||
ICElement[] scope = new ICElement[1];
|
||||
scope[0] = project;
|
||||
Set matches = DOMSearchUtil.getMatchesFromSearchEngine( SearchEngine.createCSearchScope(scope), selNode.selText, 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.setLocatable(theMatch.getLocatable());
|
||||
storage.setResource(ParserUtil.getResourceForFilename(theMatch.getLocation().toOSString()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
operationNotAvailable(CSEARCH_OPERATION_NO_DEFINITION_MESSAGE);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
operationNotAvailable(CSEARCH_OPERATION_TOO_MANY_NAMES_MESSAGE);
|
||||
return;
|
||||
|
|
Loading…
Add table
Reference in a new issue