1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-03 21:53:39 +02:00

bugs 60318 64739

This commit is contained in:
Andrew Niefer 2004-06-02 21:22:37 +00:00
parent 9311234a35
commit 29789baba4
3 changed files with 287 additions and 297 deletions

View file

@ -11,29 +11,14 @@
package org.eclipse.cdt.internal.ui.search.actions; package org.eclipse.cdt.internal.ui.search.actions;
import java.io.CharArrayReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICLogConstants; import org.eclipse.cdt.core.ICLogConstants;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.core.parser.IParser; import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.IScannerInfoProvider;
import org.eclipse.cdt.core.parser.NullSourceElementRequestor;
import org.eclipse.cdt.core.parser.ParseError; import org.eclipse.cdt.core.parser.ParseError;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserFactoryError;
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.parser.ast.ASTUtil; import org.eclipse.cdt.core.parser.ast.ASTUtil;
import org.eclipse.cdt.core.parser.ast.IASTFunction; import org.eclipse.cdt.core.parser.ast.IASTFunction;
import org.eclipse.cdt.core.parser.ast.IASTNode; import org.eclipse.cdt.core.parser.ast.IASTNode;
@ -43,15 +28,10 @@ import org.eclipse.cdt.core.search.ICSearchScope;
import org.eclipse.cdt.core.search.ICSearchConstants.LimitTo; import org.eclipse.cdt.core.search.ICSearchConstants.LimitTo;
import org.eclipse.cdt.core.search.ICSearchConstants.SearchFor; import org.eclipse.cdt.core.search.ICSearchConstants.SearchFor;
import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.search.CSearchMessages;
import org.eclipse.cdt.internal.ui.search.CSearchQuery; import org.eclipse.cdt.internal.ui.search.CSearchQuery;
import org.eclipse.cdt.internal.ui.search.CSearchResultCollector; import org.eclipse.cdt.internal.ui.search.CSearchResultCollector;
import org.eclipse.cdt.internal.ui.search.CSearchUtil; import org.eclipse.cdt.internal.ui.search.CSearchUtil;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.IStructuredSelection;
@ -59,69 +39,15 @@ import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.ui.IWorkbenchSite; import org.eclipse.ui.IWorkbenchSite;
public abstract class FindAction extends Action { public abstract class FindAction extends SelectionParseAction {
protected IWorkbenchSite fSite;
protected CEditor fEditor;
public FindAction(CEditor editor){ public FindAction(CEditor editor){
fEditor=editor; super( editor );
fSite=editor.getSite();
} }
public FindAction(IWorkbenchSite site){ public FindAction(IWorkbenchSite site){
fSite=site; super( site );
} }
protected IParser setupParser(IFile resourceFile){
//Get the scanner info
IProject currentProject = resourceFile.getProject();
IScannerInfo scanInfo = new ScannerInfo();
IScannerInfoProvider provider = CCorePlugin.getDefault().getScannerInfoProvider(currentProject);
if (provider != null){
IScannerInfo buildScanInfo = provider.getScannerInformation(currentProject);
if (buildScanInfo != null){
scanInfo = new ScannerInfo(buildScanInfo.getDefinedSymbols(), buildScanInfo.getIncludePaths());
}
}
//C or CPP?
ParserLanguage language = CoreModel.hasCCNature(currentProject) ? ParserLanguage.CPP : ParserLanguage.C;
IWorkingCopy workingCopy = null;
if( fEditor.isDirty() ){
IWorkingCopy [] workingCopies = CUIPlugin.getSharedWorkingCopies();
if( workingCopies != null ){
for( int i = 0; i < workingCopies.length; i++ ){
if( workingCopies[i].getUnderlyingResource().equals( resourceFile ) ){
workingCopy = workingCopies[i];
break;
}
}
}
}
IParser parser = null;
Reader reader = null;
try {
if( workingCopy == null )
reader = new FileReader(resourceFile.getLocation().toFile());
else
reader = new CharArrayReader( workingCopy.getContents() );
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try
{
parser = ParserFactory.createParser(
ParserFactory.createScanner( reader, resourceFile.getLocation().toOSString(), scanInfo, ParserMode.SELECTION_PARSE, language, new NullSourceElementRequestor(), ParserUtil.getScannerLogService(), null ),
new NullSourceElementRequestor(), ParserMode.SELECTION_PARSE, language, ParserUtil.getParserLogService() );
} catch( ParserFactoryError pfe ){}
return parser;
}
/** /**
* @param node * @param node
*/ */
@ -171,15 +97,6 @@ public abstract class FindAction extends Action {
return createSearchQuery( pattern, CSearchUtil.getSearchForFromNode(node) ); return createSearchQuery( pattern, CSearchUtil.getSearchForFromNode(node) );
} }
protected ISelection getSelection(){
ISelection sel = null;
if (fSite != null){
sel = fSite.getSelectionProvider().getSelection();
}
return sel;
}
public void run() { public void run() {
ISelection sel = getSelection(); ISelection sel = getSelection();
@ -193,7 +110,7 @@ public abstract class FindAction extends Action {
public void run(IStructuredSelection sel){ public void run(IStructuredSelection sel){
Object obj = sel.getFirstElement(); Object obj = sel.getFirstElement();
if( obj == null || !(obj instanceof ICElement ) ){ if( obj == null || !(obj instanceof ICElement ) ){
operationNotAvailableDialog(); operationNotAvailable();
return; return;
} }
ICElement element = (ICElement) obj; ICElement element = (ICElement) obj;
@ -209,9 +126,10 @@ public abstract class FindAction extends Action {
return; return;
} }
final ITextSelection selectedText = sel; SelSearchNode selNode = getSelection( sel );
int selectionStart = selectedText.getOffset();
int selectionEnd = selectionStart + selectedText.getLength(); int selectionStart = selNode.selStart;
int selectionEnd = selNode.selEnd;
IFile resourceFile = fEditor.getInputFile(); IFile resourceFile = fEditor.getInputFile();
IParser parser = setupParser(resourceFile); IParser parser = setupParser(resourceFile);
@ -232,7 +150,7 @@ public abstract class FindAction extends Action {
} }
if (node == null || !( node instanceof IASTNode )){ if (node == null || !( node instanceof IASTNode )){
operationNotAvailableDialog(); operationNotAvailable();
return; return;
} }
@ -242,10 +160,6 @@ public abstract class FindAction extends Action {
NewSearchUI.runQuery(job); NewSearchUI.runQuery(job);
} }
private void operationNotAvailableDialog(){
MessageDialog.openInformation(fEditor.getSite().getShell(),CSearchMessages.getString("CSearchOperation.operationUnavailable.title"), CSearchMessages.getString("CSearchOperation.operationUnavailable.message")); //$NON-NLS-1$ //$NON-NLS-2$
}
abstract protected String getScopeDescription(); abstract protected String getScopeDescription();
abstract protected ICSearchScope getScope(); abstract protected ICSearchScope getScope();

View file

@ -11,25 +11,11 @@
package org.eclipse.cdt.internal.ui.search.actions; package org.eclipse.cdt.internal.ui.search.actions;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICLogConstants; import org.eclipse.cdt.core.ICLogConstants;
import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.parser.IParser; import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.IScannerInfoProvider;
import org.eclipse.cdt.core.parser.NullSourceElementRequestor;
import org.eclipse.cdt.core.parser.ParseError; import org.eclipse.cdt.core.parser.ParseError;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserFactoryError;
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.ParserUtil;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.ast.ASTClassKind; import org.eclipse.cdt.core.parser.ast.ASTClassKind;
import org.eclipse.cdt.core.parser.ast.IASTClassSpecifier; import org.eclipse.cdt.core.parser.ast.IASTClassSpecifier;
import org.eclipse.cdt.core.parser.ast.IASTEnumerationSpecifier; import org.eclipse.cdt.core.parser.ast.IASTEnumerationSpecifier;
@ -46,107 +32,64 @@ import org.eclipse.cdt.core.search.ICSearchConstants;
import org.eclipse.cdt.core.search.IMatch; import org.eclipse.cdt.core.search.IMatch;
import org.eclipse.cdt.core.search.SearchEngine; import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.cdt.core.search.ICSearchConstants.SearchFor; import org.eclipse.cdt.core.search.ICSearchConstants.SearchFor;
import org.eclipse.cdt.internal.ui.dialogs.ElementListSelectionDialog;
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.search.CSearchMessages;
import org.eclipse.cdt.internal.ui.util.EditorUtility; import org.eclipse.cdt.internal.ui.util.EditorUtility;
import org.eclipse.cdt.ui.CSearchResultLabelProvider;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction; import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog; import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PartInitException; import org.eclipse.ui.PartInitException;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.IUpdate; import org.eclipse.ui.texteditor.IUpdate;
public class OpenDeclarationsAction extends Action implements IUpdate { public class OpenDeclarationsAction extends SelectionParseAction implements IUpdate {
private String fDialogTitle; //private String fDialogTitle;
private String fDialogMessage; //private String fDialogMessage;
protected CEditor fEditor;
SearchEngine searchEngine = null; SearchEngine searchEngine = null;
/**
* Creates a new action with the given label and image.
*/
protected OpenDeclarationsAction() {
setText(CEditorMessages.getString("OpenDeclarations.label")); //$NON-NLS-1$
setToolTipText(CEditorMessages.getString("OpenDeclarations.tooltip")); //$NON-NLS-1$
setDescription(CEditorMessages.getString("OpenDeclarations.description")); //$NON-NLS-1$
setDialogTitle(CEditorMessages.getString("OpenDeclarations.dialog.title")); //$NON-NLS-1$
setDialogMessage(CEditorMessages.getString("OpenDeclarations.dialog.message")); //$NON-NLS-1$
searchEngine = new SearchEngine();
}
/** /**
* Creates a new action with the given editor * Creates a new action with the given editor
*/ */
public OpenDeclarationsAction(CEditor editor) { public OpenDeclarationsAction(CEditor editor) {
this(); super( editor );
fEditor = editor; setText(CEditorMessages.getString("OpenDeclarations.label")); //$NON-NLS-1$
setToolTipText(CEditorMessages.getString("OpenDeclarations.tooltip")); //$NON-NLS-1$
setDescription(CEditorMessages.getString("OpenDeclarations.description")); //$NON-NLS-1$
// setDialogTitle(CEditorMessages.getString("OpenDeclarations.dialog.title")); //$NON-NLS-1$
// setDialogMessage(CEditorMessages.getString("OpenDeclarations.dialog.message")); //$NON-NLS-1$
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 );
} }
protected void setDialogTitle(String title) { private static class Storage
fDialogTitle= title;
}
protected void setDialogMessage(String message) {
fDialogMessage= message;
}
/**
* Return the selected string from the editor
* @return The string currently selected, or null if there is no valid selection
*/
protected SelSearchNode getSelectedStringFromEditor() {
if (fEditor.getSelectionProvider() == null) {
return null;
}
try {
ISelectionProvider selectionProvider = fEditor.getSelectionProvider();
ITextSelection textSelection= (ITextSelection) selectionProvider.getSelection();
String seltext = textSelection.getText();
SelSearchNode sel = null;
if (seltext.equals("")) //$NON-NLS-1$
{
int selStart = textSelection.getOffset();
IDocumentProvider prov = fEditor.getDocumentProvider();
IDocument doc = prov.getDocument(fEditor.getEditorInput());
//TODO: Change this to work with qualified identifiers
sel = getSelection(doc, selStart);
}
else {
sel = new SelSearchNode();
sel.selText= seltext;
sel.selStart = textSelection.getOffset();
sel.selEnd = textSelection.getOffset() + textSelection.getLength();
}
return sel;
} catch(Exception x) {
return null;
}
}
private static class Storage
{ {
private IASTOffsetableNamedElement element; private IASTOffsetableNamedElement element;
private IResource resource; private IResource resource;
@ -197,16 +140,12 @@ public class OpenDeclarationsAction extends Action implements IUpdate {
return; return;
} }
// final ArrayList elementsFound = new ArrayList();
final Storage storage = new Storage(); final Storage storage = new Storage();
IRunnableWithProgress runnable = new IRunnableWithProgress() IRunnableWithProgress runnable = new IRunnableWithProgress()
{ {
public void run(IProgressMonitor monitor) { public void run(IProgressMonitor monitor) {
// IWorkingCopyManager fManager = CUIPlugin.getDefault().getWorkingCopyManager();
// ITranslationUnit unit = fManager.getWorkingCopy(fEditor.getEditorInput());
IFile resourceFile = fEditor.getInputFile(); IFile resourceFile = fEditor.getInputFile();
IParser parser = setupParser(resourceFile); IParser parser = setupParser(resourceFile);
int selectionStart = selNode.selStart; int selectionStart = selNode.selStart;
@ -246,7 +185,7 @@ public class OpenDeclarationsAction extends Action implements IUpdate {
IASTOffsetableNamedElement namedElement = storage.getNamedElement(); IASTOffsetableNamedElement namedElement = storage.getNamedElement();
if( namedElement == null ){ if( namedElement == null ){
MessageDialog.openInformation(getShell(),CSearchMessages.getString("CSearchOperation.operationUnavailable.title"), CSearchMessages.getString("CSearchOperation.operationUnavailable.message")); //$NON-NLS-1$ //$NON-NLS-2$ operationNotAvailable();
return; return;
} }
@ -330,7 +269,7 @@ public class OpenDeclarationsAction extends Action implements IUpdate {
CEditor ed = (CEditor)part; CEditor ed = (CEditor)part;
try { try {
IDocument document= ed.getDocumentProvider().getDocument(ed.getEditorInput()); //IDocument document= ed.getDocumentProvider().getDocument(ed.getEditorInput());
//if(line > 3) { //if(line > 3) {
// ed.selectAndReveal(document.getLineOffset(line - 3), 0); // ed.selectAndReveal(document.getLineOffset(line - 3), 0);
//} //}
@ -338,118 +277,47 @@ public class OpenDeclarationsAction extends Action implements IUpdate {
} catch (Exception e) {} } catch (Exception e) {}
} }
} }
/** // /**
* Shows a dialog for resolving an ambigous C element. // * Shows a dialog for resolving an ambigous C element.
* Utility method that can be called by subclassers. // * Utility method that can be called by subclassers.
*/ // */
protected IMatch selectCElement(List elements, Shell shell, String title, String message) { // protected IMatch selectCElement(List elements, Shell shell, String title, String message) {
//
int nResults= elements.size(); // int nResults= elements.size();
//
if (nResults == 0) // if (nResults == 0)
return null; // return null;
//
if (nResults == 1) // if (nResults == 1)
return (IMatch) elements.get(0); // return (IMatch) elements.get(0);
//
//
ElementListSelectionDialog dialog= new ElementListSelectionDialog(shell, new CSearchResultLabelProvider(), false, false); // ElementListSelectionDialog dialog= new ElementListSelectionDialog(shell, new CSearchResultLabelProvider(), false, false);
dialog.setTitle(title); // dialog.setTitle(title);
dialog.setMessage(message); // dialog.setMessage(message);
dialog.setElements(elements); // dialog.setElements(elements);
//
if (dialog.open() == Window.OK) { // if (dialog.open() == Window.OK) {
Object[] selection= dialog.getResult(); // Object[] selection= dialog.getResult();
if (selection != null && selection.length > 0) { // if (selection != null && selection.length > 0) {
nResults= selection.length; // nResults= selection.length;
for (int i= 0; i < nResults; i++) { // for (int i= 0; i < nResults; i++) {
Object current= selection[i]; // Object current= selection[i];
if (current instanceof IMatch) // if (current instanceof IMatch)
return (IMatch) current; // return (IMatch) current;
} // }
} // }
} // }
return null; // return null;
} // }
public SelSearchNode getSelection(IDocument doc, int fPos){ /* (non-Javadoc)
int pos= fPos;
char c;
int fStartPos =0, fEndPos=0;
String selectedWord=null;
try{
while (pos >= 0) {
c= doc.getChar(pos);
if (!Character.isJavaIdentifierPart(c))
break;
--pos;
}
fStartPos= pos + 1;
pos= fPos;
int length= doc.getLength();
while (pos < length) {
c= doc.getChar(pos);
if (!Character.isJavaIdentifierPart(c))
break;
++pos;
}
fEndPos= pos;
selectedWord = doc.get(fStartPos, (fEndPos - fStartPos));
}
catch(BadLocationException e){
}
SelSearchNode sel = new SelSearchNode();
sel.selText = selectedWord;
sel.selStart = fStartPos;
sel.selEnd = fEndPos;
return sel;
}
/* (non-Javadoc)
* @see org.eclipse.ui.texteditor.IUpdate#update() * @see org.eclipse.ui.texteditor.IUpdate#update()
*/ */
public void update() { public void update() {
setEnabled(getSelectedStringFromEditor() != null); setEnabled(getSelectedStringFromEditor() != null);
} }
protected IParser setupParser(IFile resourceFile){
//Get the scanner info
IProject currentProject = resourceFile.getProject();
IScannerInfo scanInfo = new ScannerInfo();
IScannerInfoProvider provider = CCorePlugin.getDefault().getScannerInfoProvider(currentProject);
if (provider != null){
IScannerInfo buildScanInfo = provider.getScannerInformation(currentProject);
if (buildScanInfo != null){
scanInfo = new ScannerInfo(buildScanInfo.getDefinedSymbols(), buildScanInfo.getIncludePaths());
}
}
//C or CPP?
ParserLanguage language = CoreModel.getDefault().hasCCNature(currentProject) ? ParserLanguage.CPP : ParserLanguage.C;
IParser parser = null;
FileReader reader = null;
try {
reader = new FileReader(resourceFile.getLocation().toFile());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try
{
parser = ParserFactory.createParser(
ParserFactory.createScanner( reader, resourceFile.getLocation().toOSString(), scanInfo, ParserMode.SELECTION_PARSE, language, new NullSourceElementRequestor(), ParserUtil.getScannerLogService(), null ),
new NullSourceElementRequestor(), ParserMode.SELECTION_PARSE, language, ParserUtil.getParserLogService() );
} catch( ParserFactoryError pfe ){}
return parser;
}
private SearchFor getSearchForFromNode(IASTNode node){ private SearchFor getSearchForFromNode(IASTNode node){
SearchFor searchFor = null; SearchFor searchFor = null;
@ -492,10 +360,6 @@ public class OpenDeclarationsAction extends Action implements IUpdate {
return searchFor; return searchFor;
} }
class SelSearchNode{
protected String selText;
protected int selStart;
protected int selEnd;
}
} }

View file

@ -0,0 +1,212 @@
/*******************************************************************************
* Copyright (c) 2004 IBM Corporation 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
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Corp. - Rational Software - initial implementation
******************************************************************************/
package org.eclipse.cdt.internal.ui.search.actions;
import java.io.CharArrayReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.IScannerInfoProvider;
import org.eclipse.cdt.core.parser.NullSourceElementRequestor;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserFactoryError;
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.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.search.CSearchMessages;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
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.ui.IEditorSite;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.texteditor.IDocumentProvider;
/**
* @author aniefer
* Created on Jun 2, 2004
*/
public class SelectionParseAction extends Action {
protected IWorkbenchSite fSite;
protected CEditor fEditor;
public SelectionParseAction() {
super();
}
public SelectionParseAction( CEditor editor ) {
super();
fEditor=editor;
fSite=editor.getSite();
}
public SelectionParseAction(IWorkbenchSite site){
super();
fSite=site;
}
protected IParser setupParser(IFile resourceFile) {
//Get the scanner info
IProject currentProject = resourceFile.getProject();
IScannerInfo scanInfo = new ScannerInfo();
IScannerInfoProvider provider = CCorePlugin.getDefault().getScannerInfoProvider(currentProject);
if (provider != null){
IScannerInfo buildScanInfo = provider.getScannerInformation(currentProject);
if (buildScanInfo != null){
scanInfo = new ScannerInfo(buildScanInfo.getDefinedSymbols(), buildScanInfo.getIncludePaths());
}
}
//C or CPP?
ParserLanguage language = CoreModel.hasCCNature(currentProject) ? ParserLanguage.CPP : ParserLanguage.C;
IWorkingCopy workingCopy = null;
if( fEditor.isDirty() ){
IWorkingCopy [] workingCopies = CUIPlugin.getSharedWorkingCopies();
if( workingCopies != null ){
for( int i = 0; i < workingCopies.length; i++ ){
if( workingCopies[i].getUnderlyingResource().equals( resourceFile ) ){
workingCopy = workingCopies[i];
break;
}
}
}
}
IParser parser = null;
Reader reader = null;
try {
if( workingCopy == null )
reader = new FileReader(resourceFile.getLocation().toFile());
else
reader = new CharArrayReader( workingCopy.getContents() );
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try
{
parser = ParserFactory.createParser(
ParserFactory.createScanner( reader, resourceFile.getLocation().toOSString(), scanInfo, ParserMode.SELECTION_PARSE, language, new NullSourceElementRequestor(), ParserUtil.getScannerLogService(), null ),
new NullSourceElementRequestor(), ParserMode.SELECTION_PARSE, language, ParserUtil.getParserLogService() );
} catch( ParserFactoryError pfe ){}
return parser;
}
protected void operationNotAvailable() {
IStatusLineManager statusManager = null;
if (fSite instanceof IViewSite){
statusManager = ((IViewSite) fSite).getActionBars().getStatusLineManager();
}
else if (fSite instanceof IEditorSite){
statusManager = ((IEditorSite) fSite).getActionBars().getStatusLineManager();
}
if( statusManager != null )
statusManager.setErrorMessage(CSearchMessages.getString("CSearchOperation.operationUnavailable.message"));//$NON-NLS-1$
}
//TODO: Change this to work with qualified identifiers
public SelSearchNode getSelection( int fPos ) {
IDocumentProvider prov = fEditor.getDocumentProvider();
IDocument doc = prov.getDocument(fEditor.getEditorInput());
int pos= fPos;
char c;
int fStartPos =0, fEndPos=0;
String selectedWord=null;
try{
while (pos >= 0) {
c= doc.getChar(pos);
if (!Character.isJavaIdentifierPart(c))
break;
--pos;
}
fStartPos= pos + 1;
pos= fPos;
int length= doc.getLength();
while (pos < length) {
c= doc.getChar(pos);
if (!Character.isJavaIdentifierPart(c))
break;
++pos;
}
fEndPos= pos;
selectedWord = doc.get(fStartPos, (fEndPos - fStartPos));
}
catch(BadLocationException e){
}
SelSearchNode sel = new SelSearchNode();
sel.selText = selectedWord;
sel.selStart = fStartPos;
sel.selEnd = fEndPos;
return sel;
}
/**
* Return the selected string from the editor
* @return The string currently selected, or null if there is no valid selection
*/
protected SelSearchNode getSelection( ITextSelection textSelection ) {
if( textSelection == null )
return null;
String seltext = textSelection.getText();
SelSearchNode sel = null;
if (seltext.length() == 0 ) {
int selStart = textSelection.getOffset();
sel = getSelection(selStart);
} else {
sel = new SelSearchNode();
sel.selText= seltext;
sel.selStart = textSelection.getOffset();
sel.selEnd = textSelection.getOffset() + textSelection.getLength();
}
return sel;
}
protected ISelection getSelection() {
ISelection sel = null;
if (fSite != null){
sel = fSite.getSelectionProvider().getSelection();
}
return sel;
}
class SelSearchNode{
protected String selText;
protected int selStart;
protected int selEnd;
}
}