mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-04 06:45:43 +02:00
Patch for Devin Steffler: Fix for Bug 102182 ([F3] Open Declaration on macro not working in particular project)
This commit is contained in:
parent
f4b1edba07
commit
c1c91403c4
6 changed files with 297 additions and 321 deletions
|
@ -90,7 +90,6 @@ CSearchResultLabelProvider.potentialMatch= \ (inexact)
|
|||
|
||||
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.
|
||||
|
|
|
@ -71,12 +71,14 @@ public class DOMQuery extends CSearchQuery implements ISearchQuery {
|
|||
private IASTName searchName=null;
|
||||
private LimitTo limitTo=null;
|
||||
private ICSearchScope scope=null;
|
||||
|
||||
public DOMQuery(String displaySearchPattern, IASTName name, LimitTo limitTo, ICSearchScope scope) {
|
||||
private String searchPattern=null;
|
||||
|
||||
public DOMQuery(String displaySearchPattern, IASTName name, LimitTo limitTo, ICSearchScope scope, String searchPattern) {
|
||||
super(CUIPlugin.getWorkspace(), displaySearchPattern, false, null, null, null, displaySearchPattern);
|
||||
this.searchName = name;
|
||||
this.limitTo = limitTo;
|
||||
this.scope = scope;
|
||||
this.searchPattern = searchPattern;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
@ -91,6 +93,7 @@ public class DOMQuery extends CSearchQuery implements ISearchQuery {
|
|||
|
||||
// fix for 43128
|
||||
Set matches=null;
|
||||
IASTName[] foundNames=null;
|
||||
if (!isLocal())
|
||||
matches = DOMSearchUtil.getMatchesFromSearchEngine(scope, searchName, limitTo);
|
||||
|
||||
|
@ -107,7 +110,7 @@ public class DOMQuery extends CSearchQuery implements ISearchQuery {
|
|||
}
|
||||
}
|
||||
} else { // only search against the DOM if the index failed to get results... i.e. don't want duplicates
|
||||
IASTName[] foundNames = DOMSearchUtil.getNamesFromDOM(searchName, limitTo);
|
||||
foundNames = DOMSearchUtil.getNamesFromDOM(searchName, limitTo);
|
||||
|
||||
for (int i=0; i<foundNames.length; i++) {
|
||||
try {
|
||||
|
@ -138,6 +141,23 @@ public class DOMQuery extends CSearchQuery implements ISearchQuery {
|
|||
}
|
||||
}
|
||||
|
||||
if (searchPattern != null && matches.size() == 0 && (foundNames == null || foundNames.length == 0)) {
|
||||
// last try: search the index for the selected string, even if no name was found for that selection
|
||||
matches = DOMSearchUtil.getMatchesFromSearchEngine( scope, searchPattern, limitTo );
|
||||
|
||||
Iterator itr = matches.iterator();
|
||||
while(itr.hasNext()) {
|
||||
Object next = itr.next();
|
||||
if (next instanceof IMatch) {
|
||||
try {
|
||||
collector.acceptMatch((IMatch)next);
|
||||
} catch (CoreException e) {
|
||||
// don't do anything if the match wasn't accepted
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mainSearchPM.done();
|
||||
collector.done();
|
||||
|
||||
|
|
|
@ -29,7 +29,6 @@ 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;
|
||||
|
@ -74,14 +73,7 @@ public abstract class FindAction extends SelectionParseAction {
|
|||
* @return
|
||||
*/
|
||||
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;
|
||||
}
|
||||
return new DOMQuery(DOMSearchUtil.getSearchPattern(name), name, limitTo, scope, searchPattern);
|
||||
}
|
||||
|
||||
|
||||
|
@ -216,9 +208,6 @@ public abstract class FindAction extends SelectionParseAction {
|
|||
} 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);
|
||||
|
|
|
@ -72,7 +72,7 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
|
|||
}
|
||||
|
||||
final Storage storage = new Storage();
|
||||
|
||||
|
||||
IRunnableWithProgress runnable = new IRunnableWithProgress()
|
||||
{
|
||||
// steps:
|
||||
|
@ -82,152 +82,136 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
|
|||
public void run(IProgressMonitor monitor) {
|
||||
int selectionStart = selNode.selStart;
|
||||
int selectionLength = selNode.selEnd - selNode.selStart;
|
||||
|
||||
IASTName[] selectedNames = BLANK_NAME_ARRAY;
|
||||
IASTTranslationUnit tu=null;
|
||||
ParserLanguage lang=null;
|
||||
ICElement project=null;
|
||||
|
||||
if (fEditor.getEditorInput() instanceof ExternalEditorInput) {
|
||||
ExternalEditorInput input = (ExternalEditorInput)fEditor.getEditorInput();
|
||||
try {
|
||||
// get the project for the external editor input's translation unit
|
||||
project = input.getTranslationUnit();
|
||||
while (!(project instanceof ICProject) && project != null) {
|
||||
project = project.getParent();
|
||||
}
|
||||
|
||||
if (project instanceof ICProject) {
|
||||
tu = CDOM.getInstance().getASTService().getTranslationUnit(input.getStorage(), ((ICProject)project).getProject());
|
||||
lang = DOMSearchUtil.getLanguage(input.getStorage().getFullPath(), ((ICProject)project).getProject());
|
||||
projectName = ((ICProject)project).getElementName();
|
||||
}
|
||||
} catch (UnsupportedDialectException e) {
|
||||
operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
IFile resourceFile = null;
|
||||
resourceFile = fEditor.getInputFile();
|
||||
project = new CProject(null, resourceFile.getProject());
|
||||
|
||||
try {
|
||||
tu = CDOM.getInstance().getASTService().getTranslationUnit(
|
||||
resourceFile,
|
||||
CDOM.getInstance().getCodeReaderFactory(
|
||||
CDOM.PARSE_WORKING_COPY_WHENEVER_POSSIBLE));
|
||||
} catch (IASTServiceProvider.UnsupportedDialectException e) {
|
||||
operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
|
||||
return;
|
||||
}
|
||||
lang = DOMSearchUtil.getLanguageFromFile(resourceFile);
|
||||
projectName = resourceFile.getProject().getName();
|
||||
}
|
||||
|
||||
// step 1 starts here
|
||||
selectedNames = DOMSearchUtil.getSelectedNamesFrom(tu, selectionStart, selectionLength, lang);
|
||||
|
||||
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.DECLARATIONS_DEFINITIONS);
|
||||
|
||||
// make sure the names are clean (fix for 95202)
|
||||
boolean modified=false;
|
||||
for(int i=0; i<domNames.length; i++) {
|
||||
if (domNames[i].toCharArray().length == 0) {
|
||||
domNames[i] = null;
|
||||
modified=true;
|
||||
}
|
||||
}
|
||||
if (modified)
|
||||
domNames = (IASTName[])ArrayUtil.removeNulls(IASTName.class, domNames);
|
||||
|
||||
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].getFileLocation();
|
||||
if( location != null )
|
||||
{
|
||||
fileName = location.getFileName();
|
||||
start = location.getNodeOffset();
|
||||
end = location.getNodeOffset() + location.getNodeLength();
|
||||
}
|
||||
}
|
||||
|
||||
if (fileName != null) {
|
||||
storage.setFileName(fileName);
|
||||
storage.setLocatable(new OffsetLocatable(start,end));
|
||||
storage.setResource(ParserUtil.getResourceForFilename( fileName ));
|
||||
} else {
|
||||
operationNotAvailable(CSEARCH_OPERATION_NO_DECLARATION_MESSAGE);
|
||||
}
|
||||
} else {
|
||||
// step 3 starts here
|
||||
ICElement[] scope = new ICElement[1];
|
||||
scope[0] = project;
|
||||
Set matches = DOMSearchUtil.getMatchesFromSearchEngine(SearchEngine.createCSearchScope(scope), searchName, 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);
|
||||
}
|
||||
|
||||
IASTName[] selectedNames = BLANK_NAME_ARRAY;
|
||||
IASTTranslationUnit tu=null;
|
||||
ParserLanguage lang=null;
|
||||
ICElement project=null;
|
||||
|
||||
if (fEditor.getEditorInput() instanceof ExternalEditorInput) {
|
||||
ExternalEditorInput input = (ExternalEditorInput)fEditor.getEditorInput();
|
||||
try {
|
||||
// get the project for the external editor input's translation unit
|
||||
project = input.getTranslationUnit();
|
||||
while (!(project instanceof ICProject) && project != null) {
|
||||
project = project.getParent();
|
||||
}
|
||||
|
||||
if (project instanceof ICProject) {
|
||||
tu = CDOM.getInstance().getASTService().getTranslationUnit(input.getStorage(), ((ICProject)project).getProject());
|
||||
lang = DOMSearchUtil.getLanguage(input.getStorage().getFullPath(), ((ICProject)project).getProject());
|
||||
projectName = ((ICProject)project).getElementName();
|
||||
}
|
||||
} catch (UnsupportedDialectException e) {
|
||||
operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
|
||||
return;
|
||||
}
|
||||
} else if (selectedNames.length == 0){
|
||||
// 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;
|
||||
IFile resourceFile = null;
|
||||
resourceFile = fEditor.getInputFile();
|
||||
project = new CProject(null, resourceFile.getProject());
|
||||
|
||||
try {
|
||||
tu = CDOM.getInstance().getASTService().getTranslationUnit(
|
||||
resourceFile,
|
||||
CDOM.getInstance().getCodeReaderFactory(
|
||||
CDOM.PARSE_WORKING_COPY_WHENEVER_POSSIBLE));
|
||||
} catch (IASTServiceProvider.UnsupportedDialectException e) {
|
||||
operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
|
||||
return;
|
||||
}
|
||||
lang = DOMSearchUtil.getLanguageFromFile(resourceFile);
|
||||
projectName = resourceFile.getProject().getName();
|
||||
}
|
||||
|
||||
return;
|
||||
// step 1 starts here
|
||||
selectedNames = DOMSearchUtil.getSelectedNamesFrom(tu, selectionStart, selectionLength, lang);
|
||||
|
||||
try {
|
||||
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.DECLARATIONS_DEFINITIONS);
|
||||
|
||||
// make sure the names are clean (fix for 95202)
|
||||
boolean modified=false;
|
||||
for(int i=0; i<domNames.length; i++) {
|
||||
if (domNames[i].toCharArray().length == 0) {
|
||||
domNames[i] = null;
|
||||
modified=true;
|
||||
}
|
||||
}
|
||||
if (modified)
|
||||
domNames = (IASTName[])ArrayUtil.removeNulls(IASTName.class, domNames);
|
||||
|
||||
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].getFileLocation();
|
||||
if( location != null )
|
||||
{
|
||||
fileName = location.getFileName();
|
||||
start = location.getNodeOffset();
|
||||
end = location.getNodeOffset() + location.getNodeLength();
|
||||
}
|
||||
}
|
||||
|
||||
if (fileName != null) {
|
||||
storage.setFileName(fileName);
|
||||
storage.setLocatable(new OffsetLocatable(start,end));
|
||||
storage.setResource(ParserUtil.getResourceForFilename( fileName ));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// step 3 starts here
|
||||
ICElement[] scope = new ICElement[1];
|
||||
scope[0] = project;
|
||||
Set matches = DOMSearchUtil.getMatchesFromSearchEngine(SearchEngine.createCSearchScope(scope), searchName, 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;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {} // catch all exceptions from DOM so the indexer can still be tried
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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());
|
||||
|
|
|
@ -35,8 +35,6 @@ import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
|
|||
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.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
|
||||
import org.eclipse.jface.operation.IRunnableWithProgress;
|
||||
|
@ -79,166 +77,153 @@ public class OpenDefinitionAction extends SelectionParseAction implements
|
|||
* @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;
|
||||
|
||||
IASTName[] selectedNames = BLANK_NAME_ARRAY;
|
||||
IASTTranslationUnit tu=null;
|
||||
ParserLanguage lang=null;
|
||||
ICElement project=null;
|
||||
|
||||
if (fEditor.getEditorInput() instanceof ExternalEditorInput) {
|
||||
ExternalEditorInput input = (ExternalEditorInput)fEditor.getEditorInput();
|
||||
try {
|
||||
// get the project for the external editor input's translation unit
|
||||
project = input.getTranslationUnit();
|
||||
while (!(project instanceof ICProject) && project != null) {
|
||||
project = project.getParent();
|
||||
}
|
||||
|
||||
if (project instanceof ICProject) {
|
||||
tu = CDOM.getInstance().getASTService().getTranslationUnit(input.getStorage(), ((ICProject)project).getProject());
|
||||
lang = DOMSearchUtil.getLanguage(input.getStorage().getFullPath(), ((ICProject)project).getProject());
|
||||
projectName = ((ICProject)project).getElementName();
|
||||
}
|
||||
} catch (UnsupportedDialectException e) {
|
||||
operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
IFile resourceFile = null;
|
||||
resourceFile = fEditor.getInputFile();
|
||||
project = new CProject(null, resourceFile.getProject());
|
||||
|
||||
try {
|
||||
tu = CDOM.getInstance().getASTService().getTranslationUnit(
|
||||
resourceFile,
|
||||
CDOM.getInstance().getCodeReaderFactory(
|
||||
CDOM.PARSE_WORKING_COPY_WHENEVER_POSSIBLE));
|
||||
} catch (IASTServiceProvider.UnsupportedDialectException e) {
|
||||
operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
|
||||
return;
|
||||
}
|
||||
lang = DOMSearchUtil.getLanguageFromFile(resourceFile);
|
||||
projectName = findProjectName(resourceFile);
|
||||
}
|
||||
|
||||
// step 1 starts here
|
||||
selectedNames = DOMSearchUtil.getSelectedNamesFrom(tu, selectionStart, selectionLength, lang);
|
||||
|
||||
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);
|
||||
|
||||
// make sure the names are clean (fix for 95202)
|
||||
boolean modified=false;
|
||||
for(int i=0; i<domNames.length; i++) {
|
||||
if (domNames[i].toCharArray().length == 0) {
|
||||
domNames[i] = null;
|
||||
modified=true;
|
||||
}
|
||||
}
|
||||
if (modified)
|
||||
domNames = (IASTName[])ArrayUtil.removeNulls(IASTName.class, domNames);
|
||||
|
||||
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].getFileLocation();
|
||||
fileName = location.getFileName();
|
||||
start = location.getNodeOffset();
|
||||
end = location.getNodeOffset() + location.getNodeLength();
|
||||
}
|
||||
|
||||
if (fileName != null) {
|
||||
storage.setFileName(fileName);
|
||||
storage.setLocatable(new OffsetLocatable(start,end));
|
||||
storage.setResource(ParserUtil.getResourceForFilename( fileName ));
|
||||
} else {
|
||||
operationNotAvailable(CSEARCH_OPERATION_NO_DEFINITION_MESSAGE);
|
||||
}
|
||||
} else {
|
||||
// step 3 starts here
|
||||
ICElement[] scope = new ICElement[1];
|
||||
scope[0] = project;
|
||||
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.setLocatable(theMatch.getLocatable());
|
||||
storage.setResource(ParserUtil.getResourceForFilename(theMatch.getLocation().toOSString()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
operationNotAvailable(CSEARCH_OPERATION_NO_DEFINITION_MESSAGE);
|
||||
}
|
||||
}
|
||||
} else if (selectedNames.length == 0){
|
||||
// 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;
|
||||
}
|
||||
|
||||
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$
|
||||
}
|
||||
};
|
||||
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;
|
||||
|
||||
IASTName[] selectedNames = BLANK_NAME_ARRAY;
|
||||
IASTTranslationUnit tu=null;
|
||||
ParserLanguage lang=null;
|
||||
ICElement project=null;
|
||||
|
||||
if (fEditor.getEditorInput() instanceof ExternalEditorInput) {
|
||||
ExternalEditorInput input = (ExternalEditorInput)fEditor.getEditorInput();
|
||||
try {
|
||||
// get the project for the external editor input's translation unit
|
||||
project = input.getTranslationUnit();
|
||||
while (!(project instanceof ICProject) && project != null) {
|
||||
project = project.getParent();
|
||||
}
|
||||
|
||||
if (project instanceof ICProject) {
|
||||
tu = CDOM.getInstance().getASTService().getTranslationUnit(input.getStorage(), ((ICProject)project).getProject());
|
||||
lang = DOMSearchUtil.getLanguage(input.getStorage().getFullPath(), ((ICProject)project).getProject());
|
||||
projectName = ((ICProject)project).getElementName();
|
||||
}
|
||||
} catch (UnsupportedDialectException e) {
|
||||
operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
IFile resourceFile = null;
|
||||
resourceFile = fEditor.getInputFile();
|
||||
project = new CProject(null, resourceFile.getProject());
|
||||
|
||||
try {
|
||||
tu = CDOM.getInstance().getASTService().getTranslationUnit(
|
||||
resourceFile,
|
||||
CDOM.getInstance().getCodeReaderFactory(
|
||||
CDOM.PARSE_WORKING_COPY_WHENEVER_POSSIBLE));
|
||||
} catch (IASTServiceProvider.UnsupportedDialectException e) {
|
||||
operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE);
|
||||
return;
|
||||
}
|
||||
lang = DOMSearchUtil.getLanguageFromFile(resourceFile);
|
||||
project = new CProject(null, resourceFile.getProject());
|
||||
}
|
||||
|
||||
// step 1 starts here
|
||||
selectedNames = DOMSearchUtil.getSelectedNamesFrom(tu, selectionStart, selectionLength, lang);
|
||||
|
||||
try {
|
||||
if (selectedNames.length > 0 && selectedNames[0] != null) {
|
||||
IASTName searchName = selectedNames[0];
|
||||
// step 2 starts here
|
||||
IASTName[] domNames = DOMSearchUtil.getNamesFromDOM(searchName, ICSearchConstants.DEFINITIONS);
|
||||
|
||||
// make sure the names are clean (fix for 95202)
|
||||
boolean modified=false;
|
||||
for(int i=0; i<domNames.length; i++) {
|
||||
if (domNames[i].toCharArray().length == 0) {
|
||||
domNames[i] = null;
|
||||
modified=true;
|
||||
}
|
||||
}
|
||||
if (modified)
|
||||
domNames = (IASTName[])ArrayUtil.removeNulls(IASTName.class, domNames);
|
||||
|
||||
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].getFileLocation();
|
||||
if (location != null)
|
||||
{
|
||||
fileName = location.getFileName();
|
||||
start = location.getNodeOffset();
|
||||
end = location.getNodeOffset() + location.getNodeLength();
|
||||
}
|
||||
}
|
||||
|
||||
if (fileName != null) {
|
||||
storage.setFileName(fileName);
|
||||
storage.setLocatable(new OffsetLocatable(start,end));
|
||||
storage.setResource(ParserUtil.getResourceForFilename( fileName ));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// step 3 starts here
|
||||
ICElement[] scope = new ICElement[1];
|
||||
scope[0] = project;
|
||||
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.setLocatable(theMatch.getLocatable());
|
||||
storage.setResource(ParserUtil.getResourceForFilename(theMatch.getLocation().toOSString()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {} // catch all exceptions from DOM so the indexer can still be tried
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(getShell());
|
||||
|
|
|
@ -68,7 +68,6 @@ import org.eclipse.ui.texteditor.IDocumentProvider;
|
|||
*/
|
||||
public class SelectionParseAction extends Action {
|
||||
private static final String OPERATOR = "operator"; //$NON-NLS-1$
|
||||
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$
|
||||
|
|
Loading…
Add table
Reference in a new issue