From 229dce01a0c1949a80a1325948761fd9c58c7237 Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Tue, 21 Jun 2005 17:06:55 +0000 Subject: [PATCH] Added support for completion in external files. Also cleaned up the service provider. --- .../src/org/eclipse/cdt/core/dom/CDOM.java | 22 +++--- .../cdt/core/dom/IASTServiceProvider.java | 12 ++++ .../core/dom/InternalASTServiceProvider.java | 69 +++++++++---------- .../contentassist/CCompletionProcessor2.java | 23 +++++-- 4 files changed, 77 insertions(+), 49 deletions(-) diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/dom/CDOM.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/dom/CDOM.java index 648c4d32a71..9aded80b4ac 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/dom/CDOM.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/dom/CDOM.java @@ -139,15 +139,19 @@ public class CDOM implements IASTServiceProvider { return defaultService.getTranslationUnit(fileToParse, fileCreator, configuration ); } - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.IASTServiceProvider#getCompletionNode(org.eclipse.core.resources.IFile, int, org.eclipse.cdt.core.dom.ICodeReaderFactory) - */ - public ASTCompletionNode getCompletionNode(IFile fileToParse, int offset, - ICodeReaderFactory fileCreator) throws UnsupportedDialectException { - //TODO - At this time, we purely delegate blindly - //In the future, we may need to delegate based upon context provided - return defaultService.getCompletionNode(fileToParse, offset, fileCreator); - } + public ASTCompletionNode getCompletionNode(IFile fileToParse, int offset, + ICodeReaderFactory fileCreator) throws UnsupportedDialectException { + //TODO - At this time, we purely delegate blindly + //In the future, we may need to delegate based upon context provided + return defaultService.getCompletionNode(fileToParse, offset, fileCreator); + } + + public ASTCompletionNode getCompletionNode(IStorage fileToParse, IProject project, int offset, + ICodeReaderFactory fileCreator) throws UnsupportedDialectException { + //TODO - At this time, we purely delegate blindly + //In the future, we may need to delegate based upon context provided + return defaultService.getCompletionNode(fileToParse, project, offset, fileCreator); + } /** * This method allows a UI component to register its IWorkingCopyProvider to the CDOM. diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/dom/IASTServiceProvider.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/dom/IASTServiceProvider.java index dafdf3d6308..c23682dd012 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/dom/IASTServiceProvider.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/dom/IASTServiceProvider.java @@ -97,4 +97,16 @@ public interface IASTServiceProvider { */ public ASTCompletionNode getCompletionNode( IFile fileToParse, int offset, ICodeReaderFactory fileCreator) throws UnsupportedDialectException; + /** + * Returns a parse tree that represents the content provided as parameters. + * + * @param fileToParse the file in question + * @param project the project containing the scanner info + * @param offset the offset at which you require completion at + * @param fileCreator @see CDOM#getCodeReaderFactory(int) + * @return syntactical parse tree + * @throws UnsupportedDialectException + */ + public ASTCompletionNode getCompletionNode( IStorage fileToParse, IProject project, int offset, ICodeReaderFactory fileCreator) throws UnsupportedDialectException; + } diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/dom/InternalASTServiceProvider.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/dom/InternalASTServiceProvider.java index 3e61b43756a..95f1753e6a7 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/dom/InternalASTServiceProvider.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/dom/InternalASTServiceProvider.java @@ -42,7 +42,6 @@ import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IStorage; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.content.IContentType; /** @@ -69,14 +68,14 @@ public class InternalASTServiceProvider implements IASTServiceProvider { * @see org.eclipse.cdt.core.dom.IASTServiceProvider#getTranslationUnit() */ public IASTTranslationUnit getTranslationUnit(IFile fileToParse) throws UnsupportedDialectException { - return getTranslationUnit( fileToParse, fileToParse.getLocation().toOSString(), fileToParse.getProject(), SavedCodeReaderFactory.getInstance(), null ); + return getTranslationUnit( fileToParse.getLocation().toOSString(), fileToParse, SavedCodeReaderFactory.getInstance(), null ); } /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.IASTServiceProvider#getTranslationUnit(org.eclipse.cdt.core.dom.ICodeReaderFactory) */ public IASTTranslationUnit getTranslationUnit(IFile fileToParse, ICodeReaderFactory fileCreator) throws UnsupportedDialectException { - return getTranslationUnit( fileToParse, fileToParse.getLocation().toOSString(), fileToParse.getProject(), fileCreator, null ); + return getTranslationUnit( fileToParse.getLocation().toOSString(), fileToParse, fileCreator, null ); } /* (non-Javadoc) @@ -84,36 +83,31 @@ public class InternalASTServiceProvider implements IASTServiceProvider { */ public IASTTranslationUnit getTranslationUnit( IFile fileToParse, ICodeReaderFactory fileCreator, IParserConfiguration configuration) throws UnsupportedDialectException { - return getTranslationUnit( fileToParse, fileToParse.getLocation().toOSString(), fileToParse.getProject(), fileCreator, configuration ); + return getTranslationUnit( fileToParse.getLocation().toOSString(), fileToParse, fileCreator, configuration ); } public IASTTranslationUnit getTranslationUnit( - IStorage fileToParse, String os_path, IProject project, ICodeReaderFactory fileCreator, IParserConfiguration configuration ) throws UnsupportedDialectException + String filename, IResource infoProvider, ICodeReaderFactory fileCreator, IParserConfiguration configuration ) throws UnsupportedDialectException { + IProject project = infoProvider.getProject(); IScannerInfo scanInfo = null; if( configuration == null ) { IScannerInfoProvider provider = CCorePlugin.getDefault().getScannerInfoProvider(project); if (provider != null){ - IScannerInfo buildScanInfo = null; - if( fileToParse instanceof IResource ) - buildScanInfo = provider.getScannerInformation( (IResource) fileToParse ); - else - buildScanInfo = provider.getScannerInformation( project); + IScannerInfo buildScanInfo = provider.getScannerInformation(infoProvider); - if (buildScanInfo != null){ - scanInfo = buildScanInfo; - } - else - scanInfo = new ScannerInfo(); + if (buildScanInfo != null) + scanInfo = buildScanInfo; + else + scanInfo = new ScannerInfo(); } } else scanInfo = configuration.getScannerInfo(); - - CodeReader reader = fileCreator.createCodeReaderForTranslationUnit( os_path ); + CodeReader reader = fileCreator.createCodeReaderForTranslationUnit(filename); if( reader == null ) return null; IScanner scanner = null; @@ -121,7 +115,7 @@ public class InternalASTServiceProvider implements IASTServiceProvider { if( configuration == null ) { - ParserLanguage l = getLanguage(fileToParse.getFullPath(), project); + ParserLanguage l = getLanguage(filename, project); IScannerExtensionConfiguration scannerExtensionConfiguration = null; if( l == ParserLanguage.CPP ) scannerExtensionConfiguration = CPP_GNU_SCANNER_EXTENSION; @@ -175,31 +169,34 @@ public class InternalASTServiceProvider implements IASTServiceProvider { return tu; } - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.IASTServiceProvider#getCompletionNode(org.eclipse.core.resources.IFile, int, org.eclipse.cdt.core.dom.ICodeReaderFactory) - */ - public ASTCompletionNode getCompletionNode(IFile fileToParse, int offset, - ICodeReaderFactory fileCreator) throws UnsupportedDialectException { + public ASTCompletionNode getCompletionNode(IStorage fileToParse, IProject project, int offset, + ICodeReaderFactory fileCreator) throws UnsupportedDialectException { + return getCompletionNode(fileToParse.getFullPath().toOSString(), project, offset, fileCreator); + } + + public ASTCompletionNode getCompletionNode(IFile fileToParse, int offset, + ICodeReaderFactory fileCreator) throws UnsupportedDialectException { + return getCompletionNode(fileToParse.getLocation().toOSString(), fileToParse, offset, fileCreator); + } + + public ASTCompletionNode getCompletionNode(String filename, IResource infoProvider, int offset, + ICodeReaderFactory fileCreator) throws UnsupportedDialectException { // Get the scanner info - IProject currentProject = fileToParse.getProject(); IScannerInfo scanInfo = null; + IProject project = infoProvider.getProject(); - IScannerInfoProvider provider = CCorePlugin.getDefault() - .getScannerInfoProvider(currentProject); + IScannerInfoProvider provider = CCorePlugin.getDefault().getScannerInfoProvider(project); if (provider != null) { - IScannerInfo buildScanInfo = provider - .getScannerInformation(fileToParse); + IScannerInfo buildScanInfo = provider.getScannerInformation(infoProvider); if (buildScanInfo != null) scanInfo = buildScanInfo; else scanInfo = new ScannerInfo(); } - CodeReader reader = fileCreator - .createCodeReaderForTranslationUnit(fileToParse.getLocation() - .toOSString()); + CodeReader reader = fileCreator.createCodeReaderForTranslationUnit(filename); - ParserLanguage l = getLanguage(fileToParse.getLocation(), currentProject); + ParserLanguage l = getLanguage(filename, project); IScannerExtensionConfiguration scannerExtensionConfiguration = null; if (l == ParserLanguage.CPP) scannerExtensionConfiguration = CPP_GNU_SCANNER_EXTENSION; @@ -239,12 +236,12 @@ public class InternalASTServiceProvider implements IASTServiceProvider { return dialects; } - private ParserLanguage getLanguage( IPath path, IProject project ) + private ParserLanguage getLanguage( String filename, IProject project ) { //FIXME: ALAIN, for headers should we assume CPP ?? // The problem is that it really depends on how the header was included. String id = null; - IContentType contentType = CCorePlugin.getContentType(project, path.lastSegment()); + IContentType contentType = CCorePlugin.getContentType(project, filename); if (contentType != null) { id = contentType.getId(); } @@ -266,10 +263,10 @@ public class InternalASTServiceProvider implements IASTServiceProvider { } public IASTTranslationUnit getTranslationUnit(IStorage fileToParse, IProject project, ICodeReaderFactory fileCreator) throws UnsupportedDialectException{ - return getTranslationUnit( fileToParse, fileToParse.getFullPath().toOSString(), project, fileCreator, null ); + return getTranslationUnit( fileToParse.getFullPath().toOSString(), project, fileCreator, null ); } public IASTTranslationUnit getTranslationUnit(IStorage fileToParse, IProject project) throws UnsupportedDialectException { - return getTranslationUnit( fileToParse, fileToParse.getFullPath().toOSString(), project, SavedCodeReaderFactory.getInstance(), null ); + return getTranslationUnit( fileToParse.getFullPath().toOSString(), project, SavedCodeReaderFactory.getInstance(), null ); } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor2.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor2.java index 7df5e04adaf..d569c68e434 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor2.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor2.java @@ -20,9 +20,12 @@ import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.cdt.internal.ui.CUIMessages; import org.eclipse.cdt.internal.ui.text.CParameterListValidator; +import org.eclipse.cdt.internal.ui.util.ExternalEditorInput; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor; import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IStorage; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExtension; import org.eclipse.core.runtime.IExtensionPoint; @@ -59,10 +62,22 @@ public class CCompletionProcessor2 implements IContentAssistProcessor { int offset) { try { IWorkingCopy workingCopy = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editor.getEditorInput()); - ASTCompletionNode completionNode = CDOM.getInstance().getCompletionNode( - (IFile)workingCopy.getResource(), - offset, - CDOM.getInstance().getCodeReaderFactory(CDOM.PARSE_WORKING_COPY_WHENEVER_POSSIBLE)); + ASTCompletionNode completionNode = null; + IFile file = (IFile)workingCopy.getResource(); + if (file != null) + completionNode = CDOM.getInstance().getCompletionNode( + file, + offset, + CDOM.getInstance().getCodeReaderFactory(CDOM.PARSE_WORKING_COPY_WHENEVER_POSSIBLE)); + else if (editor.getEditorInput() instanceof ExternalEditorInput) { + IStorage storage = ((ExternalEditorInput)(editor.getEditorInput())).getStorage(); + IProject project = workingCopy.getCProject().getProject(); + completionNode = CDOM.getInstance().getCompletionNode( + storage, + project, + offset, + CDOM.getInstance().getCodeReaderFactory(CDOM.PARSE_WORKING_COPY_WHENEVER_POSSIBLE)); + } errorMessage = CUIMessages.getString(noCompletions);