1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-30 04:15:35 +02:00

Start of new DOM based Completion Parser:

- New UI preference to enable new stuff
- New token type for completion
- Scanner changes to generate new token
- Parser handling for completion at start of statement
- AST service changes to call the scanner/parser in new "mode"
This commit is contained in:
Doug Schaefer 2005-02-18 20:14:10 +00:00
parent ee8352619d
commit 3eaf13dfa3
13 changed files with 363 additions and 7 deletions

View file

@ -0,0 +1,59 @@
/**********************************************************************
* 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 v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
**********************************************************************/
package org.eclipse.cdt.core.dom.ast;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.core.parser.IToken;
/**
* This class represents the node that would occur at the point of
* a context completion.
*
* This node may contain the prefix text of an identifer up to the point.
* If there is no prefix, the completion occurred at the point where a
* new token would have begun.
*
* The node points to the parent node where this node, if replaced by
* a proper node, would reside in the tree.
*
* @author Doug Schaefer
*/
public class ASTCompletionNode {
private IToken completionToken;
private List names = new ArrayList();
public ASTCompletionNode(IToken completionToken) {
this.completionToken = completionToken;
}
public void addName(IASTName name) {
names.add(name);
}
/**
* If the point of completion was at the end of a potential
* identifier, this string contains the text of that identifier.
*
* @return the prefix text up to the point of completion
*/
public String getPrefix() {
return completionToken.getImage();
}
public int getLength() {
return completionToken.getLength();
}
public IASTName[] getNames() {
return (IASTName[])names.toArray(new IASTName[names.size()]);
}
}

View file

@ -26,6 +26,7 @@ public interface IScanner {
public static final int tPOUND = -7;
public void setOffsetBoundary( int offset );
public void setContentAssistMode( int offset );
public void setASTFactory( IASTFactory f );
public void addDefinition(char[] key, char[] value);

View file

@ -315,6 +315,8 @@ public interface IToken {
static public final int tMACROEXP = 138;
static public final int tPOUNDPOUND = 139;
static public final int tCOMPLETION = 140;
static public final int tLAST = 139;
static public final int tLAST = 140;
}

View file

@ -11,6 +11,7 @@ package org.eclipse.cdt.internal.core.dom.parser;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IASTASMDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTBreakStatement;
@ -32,6 +33,7 @@ import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTGotoStatement;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTLabelStatement;
import org.eclipse.cdt.core.dom.ast.IASTName;
@ -62,6 +64,7 @@ import org.eclipse.cdt.core.parser.IToken;
import org.eclipse.cdt.core.parser.OffsetLimitReachedException;
import org.eclipse.cdt.core.parser.ParseError;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression;
/**
* @author jcamelon
@ -107,6 +110,15 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
protected IToken currToken;
protected ASTCompletionNode completionNode;
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.dom.parser.ISourceCodeParser#getCompletionNode()
*/
public ASTCompletionNode getCompletionNode() {
return completionNode;
}
/**
* Look Ahead in the token list to see what is coming.
*
@ -462,7 +474,7 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
((ASTNode) result).setOffset(startingOffset);
result.setParent(mostRelevantScopeNode);
result.setPropertyInParent(IASTFunctionDefinition.FUNCTION_BODY);
while (LT(1) != IToken.tRBRACE) {
while (LT(1) != IToken.tRBRACE && LT(1) != IToken.tCOMPLETION) {
int checkToken = LA(1).hashCode();
try {
IASTStatement s = statement();
@ -484,8 +496,36 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
failParseWithErrorHandling();
}
}
int lastOffset = consume(IToken.tRBRACE).getEndOffset();
IToken token = consume();
int lastOffset = token.getEndOffset();
((ASTNode) result).setLength(lastOffset - startingOffset);
if (token.getType() == IToken.tCOMPLETION) {
if (token.getLength() > 0) {
// At the beginning of a statement, this could be either an expression
// statement or a declaration statement. We'll create both and add them
// to the completion node.
// First the expression statement
IASTExpressionStatement exprStmt = createExpressionStatement();
exprStmt.setParent(result);
exprStmt.setPropertyInParent(IASTCompoundStatement.NESTED_STATEMENT);
IASTIdExpression expr = new CPPASTIdExpression(); // Obviously need a factory
exprStmt.setExpression(expr);
expr.setParent(exprStmt);
expr.setPropertyInParent(IASTExpressionStatement.EXPFRESSION);
IASTName exprName = createName(token);
expr.setName(exprName);
exprName.setParent(expr);
exprName.setPropertyInParent(IASTIdExpression.ID_NAME);
if (completionNode == null)
completionNode = new ASTCompletionNode(token);
completionNode.addName(exprName);
}
}
return result;
}

View file

@ -9,6 +9,7 @@
* IBM Rational Software - Initial API and implementation */
package org.eclipse.cdt.internal.core.dom.parser;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
/**
@ -21,4 +22,7 @@ public interface ISourceCodeParser {
public IASTTranslationUnit parse();
public boolean encounteredError();
public ASTCompletionNode getCompletionNode();
}

View file

@ -1513,6 +1513,15 @@ abstract class BaseScanner implements IScanner {
beforeSecondFetchToken();
if (finished) {
if (contentAssistMode) {
lastToken = nextToken;
nextToken = null;
if (lastToken == null)
throwEOF();
else
return lastToken;
}
if (isCancelled == true)
throw new ParseError(
ParseError.ParseErrorKind.TIMEOUT_OR_CANCELLED);
@ -1541,6 +1550,8 @@ abstract class BaseScanner implements IScanner {
if (nextToken == null) {
if (!exception)
finished = true;
} else if (nextToken.getType() == IToken.tCOMPLETION) {
finished = true;
} else if (nextToken.getType() == IToken.tPOUNDPOUND) {
// time for a pasting
IToken token2 = fetchToken();
@ -2020,6 +2031,11 @@ abstract class BaseScanner implements IScanner {
--bufferPos[bufferStackPos];
if (contentAssistMode && bufferStackPos == 0 && bufferPos[bufferStackPos] + 1 == limit) {
// return the text as a content assist token
return newToken(IToken.tCOMPLETION, CharArrayUtils.extract(buffer, start, bufferPos[bufferStackPos] - start + 1));
}
// Check for macro expansion
Object expObject = definitions.get(buffer, start, len);
@ -4478,6 +4494,8 @@ abstract class BaseScanner implements IScanner {
};
protected int offsetBoundary = -1;
protected boolean contentAssistMode = false;
protected void setupBuiltInMacros(IScannerExtensionConfiguration config) {
@ -4509,6 +4527,14 @@ abstract class BaseScanner implements IScanner {
bufferLimit[0] = offset;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.IScanner#setContentAssistMode(int)
*/
public void setContentAssistMode(int offset) {
bufferLimit[0] = offset;
contentAssistMode = true;
}
protected ParserLanguage getLanguage() {
return language;
}

View file

@ -10,6 +10,7 @@
**********************************************************************/
package org.eclipse.cdt.core.dom;
import org.eclipse.cdt.core.browser.IWorkingCopyProvider;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.internal.core.dom.InternalASTServiceProvider;
import org.eclipse.cdt.internal.core.dom.PartialWorkingCopyCodeReaderFactory;
@ -90,6 +91,14 @@ public class CDOM implements IASTServiceProvider {
public IASTTranslationUnit getTranslationUnit(IFile fileToParse, ICodeReaderFactory fileCreator, IParserConfiguration configuration) throws UnsupportedDialectException {
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 {
return defaultService.getCompletionNode(fileToParse, offset, fileCreator);
}
/**
* @param workingCopyProvider

View file

@ -10,6 +10,7 @@
**********************************************************************/
package org.eclipse.cdt.core.dom;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.core.resources.IFile;
@ -28,4 +29,6 @@ public interface IASTServiceProvider {
public IASTTranslationUnit getTranslationUnit( IFile fileToParse, ICodeReaderFactory fileCreator, IParserConfiguration configuration )throws UnsupportedDialectException;
public ASTCompletionNode getCompletionNode( IFile fileToParse, int offset, ICodeReaderFactory fileCreator) throws UnsupportedDialectException;
}

View file

@ -15,6 +15,7 @@ import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IASTServiceProvider;
import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.dom.IParserConfiguration;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.filetype.ICFileType;
import org.eclipse.cdt.core.filetype.ICFileTypeConstants;
@ -165,8 +166,62 @@ public class InternalASTServiceProvider implements IASTServiceProvider {
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.IASTServiceProvider#getSupportedDialects()
*/
* @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 {
// Get the scanner info
IProject currentProject = fileToParse.getProject();
IScannerInfo scanInfo = null;
IScannerInfoProvider provider = CCorePlugin.getDefault()
.getScannerInfoProvider(currentProject);
if (provider != null) {
IScannerInfo buildScanInfo = provider
.getScannerInformation(fileToParse);
if (buildScanInfo != null)
scanInfo = buildScanInfo;
else
scanInfo = new ScannerInfo();
}
CodeReader reader = fileCreator
.createCodeReaderForTranslationUnit(fileToParse.getLocation()
.toOSString());
ParserLanguage l = getLanguage(fileToParse);
IScannerExtensionConfiguration scannerExtensionConfiguration = null;
if (l == ParserLanguage.CPP)
scannerExtensionConfiguration = CPP_GNU_SCANNER_EXTENSION;
else
scannerExtensionConfiguration = C_GNU_SCANNER_EXTENSION;
IScanner scanner = new DOMScanner(reader, scanInfo, ParserMode.COMPLETE_PARSE,
l, ParserFactory.createDefaultLogService(),
scannerExtensionConfiguration, fileCreator);
scanner.setContentAssistMode(offset);
// assume GCC
ISourceCodeParser parser = null;
if (l == ParserLanguage.C)
parser = new GNUCSourceParser(scanner, ParserMode.COMPLETE_PARSE,
ParserUtil.getParserLogService(),
new GCCParserExtensionConfiguration());
else
parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE,
ParserUtil.getParserLogService(),
new GPPParserExtensionConfiguration());
// Run the parse and return the completion node
parser.parse();
return parser.getCompletionNode();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.dom.IASTServiceProvider#getSupportedDialects()
*/
public String[] getSupportedDialects() {
return dialects;
}

View file

@ -42,6 +42,9 @@ public class CodeAssistPreferencePage extends AbstractPreferencePage {
protected OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
ArrayList overlayKeys = new ArrayList();
// temporary
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, ContentAssistPreference.USE_DOM));
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, ContentAssistPreference.AUTOACTIVATION_DELAY));
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, ContentAssistPreference.AUTOINSERT));
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, ContentAssistPreference.CODEASSIST_PREFIX_COMPLETION));
@ -74,6 +77,9 @@ public class CodeAssistPreferencePage extends AbstractPreferencePage {
layout.numColumns = 2;
contentAssistComposite.setLayout(layout);
// temporary use DOM
addCheckBox(contentAssistComposite, "Use DOM (Work in progress)", ContentAssistPreference.USE_DOM, 0);
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// The following three radio buttons are grouped together
String label = PreferencesMessages.getString("CEditorPreferencePage.ContentAssistPage.searchGroupTitle"); //$NON-NLS-1$

View file

@ -6,11 +6,12 @@ package org.eclipse.cdt.internal.ui.text;
import java.util.Vector;
import org.eclipse.cdt.internal.ui.editor.CSourceViewer;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.editor.CSourceViewer;
import org.eclipse.cdt.internal.ui.text.c.hover.CEditorTextHoverDescriptor;
import org.eclipse.cdt.internal.ui.text.c.hover.CEditorTextHoverProxy;
import org.eclipse.cdt.internal.ui.text.contentassist.CCompletionProcessor;
import org.eclipse.cdt.internal.ui.text.contentassist.CCompletionProcessor2;
import org.eclipse.cdt.internal.ui.text.contentassist.ContentAssistPreference;
import org.eclipse.cdt.ui.CElementContentProvider;
import org.eclipse.cdt.ui.CUIPlugin;
@ -187,7 +188,10 @@ public class CSourceViewerConfiguration extends SourceViewerConfiguration {
ContentAssistant assistant = new ContentAssistant();
IContentAssistProcessor processor = new CCompletionProcessor(getEditor());
IContentAssistProcessor processor
= getPreferenceStore().getBoolean(ContentAssistPreference.USE_DOM)
? (IContentAssistProcessor)new CCompletionProcessor2(getEditor())
: (IContentAssistProcessor)new CCompletionProcessor(getEditor());
assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
//Will this work as a replacement for the configuration lines below?

View file

@ -0,0 +1,144 @@
/**********************************************************************
* 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 v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
**********************************************************************/
package org.eclipse.cdt.internal.ui.text.contentassist;
import java.util.Arrays;
import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.dom.IASTServiceProvider.UnsupportedDialectException;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
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.ITypedef;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IEditorPart;
/**
* @author Doug Schaefer
*/
public class CCompletionProcessor2 implements IContentAssistProcessor {
private IEditorPart editor;
private String errorMessage;
public CCompletionProcessor2(IEditorPart editor) {
this.editor = editor;
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int)
*/
public ICompletionProposal[] computeCompletionProposals(final ITextViewer viewer,
int offset) {
errorMessage = null;
try {
IWorkingCopy workingCopy = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editor.getEditorInput());
ASTCompletionNode completionNode = CDOM.getInstance().getCompletionNode(
(IFile)workingCopy.getResource(),
offset,
new ICodeReaderFactory() {
public CodeReader createCodeReaderForTranslationUnit(String path) {
return new CodeReader(viewer.getDocument().get().toCharArray());
}
public CodeReader createCodeReaderForInclusion(String path) {
return ParserUtil.createReader(path,
Arrays.asList(CUIPlugin.getSharedWorkingCopies()).iterator());
}
public int getUniqueIdentifier() {
return 99;
}
}
);
IASTName[] names = completionNode.getNames();
IASTTranslationUnit tu = names[0].getTranslationUnit();
IBinding binding = names[0].resolveBinding();
int repLength = completionNode.getLength();
int repOffset = offset - repLength;
ICompletionProposal prop = createBindingCompletionProposal(binding, repOffset, repLength);
ICompletionProposal prop2 = createBindingCompletionProposal(binding, repOffset, repLength);
return new ICompletionProposal[] { prop, prop2 };
} catch (UnsupportedDialectException e) {
errorMessage = "Unsupported Dialect Exception";
} catch (Throwable e) {
errorMessage = e.toString();
}
return null;
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, int)
*/
public IContextInformation[] computeContextInformation(ITextViewer viewer,
int offset) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
*/
public char[] getCompletionProposalAutoActivationCharacters() {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters()
*/
public char[] getContextInformationAutoActivationCharacters() {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage()
*/
public String getErrorMessage() {
return errorMessage;
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator()
*/
public IContextInformationValidator getContextInformationValidator() {
// TODO Auto-generated method stub
return null;
}
private ICompletionProposal createBindingCompletionProposal(IBinding binding, int offset, int length) {
ImageDescriptor imageDescriptor = null;
if (binding instanceof ITypedef)
imageDescriptor = CElementImageProvider.getTypedefImageDescriptor();
Image image = imageDescriptor != null
? CUIPlugin.getImageDescriptorRegistry().get( imageDescriptor )
: null;
return new CCompletionProposal(binding.getName(), offset, length, image, binding.getName(), 1);
}
}

View file

@ -22,6 +22,9 @@ import org.eclipse.jface.util.PropertyChangeEvent;
public class ContentAssistPreference {
// Temporary to use DOM for assist
public final static String USE_DOM = "content_assist_use_dom"; //$NON-NLS-1$
/** Preference key for content assist auto activation */
//public final static String AUTOACTIVATION= "content_assist_autoactivation";
/** Preference key for content assist auto activation delay */