From bf6652a96a956e82a2a1e2736909e25ff7b0ea00 Mon Sep 17 00:00:00 2001 From: John Camelon Date: Tue, 4 May 2004 15:08:04 +0000 Subject: [PATCH] Partial fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=50808 --- .../parser/tests/CompletionParseTest.java | 51 +++++++++++++++++++ .../core/parser/ast/IASTCompletionNode.java | 9 +++- .../core/parser/CompletionParser.java | 2 +- .../core/parser/ContextualParser.java | 17 +++++++ .../core/parser/ExpressionParser.java | 9 +++- .../core/parser/ast/ASTCompletionNode.java | 12 ++++- .../internal/core/parser/scanner/Scanner.java | 8 +-- 7 files changed, 100 insertions(+), 8 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CompletionParseTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CompletionParseTest.java index 54562fd9f60..badd3212b3b 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CompletionParseTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CompletionParseTest.java @@ -931,4 +931,55 @@ public class CompletionParseTest extends CompletionParseBaseTest { node.getCompletionContext() ); assertEquals( result.getResultsSize(), 1 ); } + + public void testParameterListFunctionReference() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "int foo( int firstParam, int secondParam );\n"); //$NON-NLS-1$ + writer.write( "void main() { \n"); //$NON-NLS-1$ + writer.write( " int abc;\n"); //$NON-NLS-1$ + writer.write( " int x;\n" ); //$NON-NLS-1$ + writer.write( " foo( x,a"); //$NON-NLS-1$ + String code = writer.toString(); + for( int i = 0; i < 2; ++i ) + { + int index = code.indexOf( "x,a") + 2; //$NON-NLS-1$ + if( i == 1 ) index++; + IASTCompletionNode node = parse( code, index ); + validateCompletionNode(node, (( i == 0 ) ? "" : "a" ), CompletionKind.FUNCTION_REFERENCE, null, true ); //$NON-NLS-1$ //$NON-NLS-2$ + assertNotNull( node.getFunctionParameters() ); + ILookupResult result = node.getCompletionScope().lookup( node.getCompletionPrefix(), + new IASTNode.LookupKind[]{ IASTNode.LookupKind.LOCAL_VARIABLES }, + node.getCompletionContext() ); + assertNotNull(result); + assertEquals( result.getResultsSize(), ( i == 0 ) ? 2 : 1 ); + } + } + + public void testParameterListConstructorReference() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "class A { \n"); //$NON-NLS-1$ + writer.write( "public:\n"); //$NON-NLS-1$ + writer.write( " A( int first, int second );\n"); //$NON-NLS-1$ + writer.write( "};\n" ); //$NON-NLS-1$ + writer.write( "void main() { \n"); //$NON-NLS-1$ + writer.write( " int four, x;"); //$NON-NLS-1$ + writer.write( " A * a = new A( x,f "); //$NON-NLS-1$ + String code = writer.toString(); + for( int i = 0; i < 2; ++i ) + { + int index = code.indexOf( "x,f") + 2; //$NON-NLS-1$ + if( i == 1 ) index++; + IASTCompletionNode node = parse( code, index ); + validateCompletionNode(node, (( i == 0 ) ? "" : "f" ), CompletionKind.CONSTRUCTOR_REFERENCE, null, true ); //$NON-NLS-1$ //$NON-NLS-2$ + assertNotNull( node.getFunctionParameters() ); + ILookupResult result = node.getCompletionScope().lookup( node.getCompletionPrefix(), + new IASTNode.LookupKind[]{ IASTNode.LookupKind.LOCAL_VARIABLES }, + node.getCompletionContext() ); + assertNotNull(result); + assertEquals( result.getResultsSize(), ( i == 0 ) ? 2 : 1 ); + } + } + } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/ast/IASTCompletionNode.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/ast/IASTCompletionNode.java index 6a75015d123..22e83df55b7 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/ast/IASTCompletionNode.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/ast/IASTCompletionNode.java @@ -114,7 +114,14 @@ public interface IASTCompletionNode { * CONSTRUCTOR_REFERENCE * FUNCTION_REFERENCE */ - public String getFunctionName(); + public String getFunctionName(); + + /** + * + * @return the IASTExpression representing the number of parameters + * input in the CONSTRUCTOR_REFERENCE/FUNCTION_REFERENCE context. + */ + public IASTExpression getFunctionParameters(); /** * @return the prefix diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/CompletionParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/CompletionParser.java index 6715c89578e..b8d97ef2033 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/CompletionParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/CompletionParser.java @@ -56,7 +56,7 @@ public class CompletionParser extends ContextualParser implements IParser { public IASTCompletionNode parse(int offset) { scanner.setOffsetBoundary(offset); translationUnit(); - return new ASTCompletionNode( getCompletionKind(), getCompletionScope(), getCompletionContext(), getCompletionPrefix(), reconcileKeywords( getKeywordSet(), getCompletionPrefix() ), getCompletionFunctionName() ); + return new ASTCompletionNode( getCompletionKind(), getCompletionScope(), getCompletionContext(), getCompletionPrefix(), reconcileKeywords( getKeywordSet(), getCompletionPrefix() ), getCompletionFunctionName(), getParameterListExpression() ); } /** diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ContextualParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ContextualParser.java index b79a6bf6383..39ac7bf304d 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ContextualParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ContextualParser.java @@ -118,6 +118,8 @@ public class ContextualParser extends CompleteParser { protected void setCompletionFunctionName() { functionOrConstructorName = currentFunctionName; } + + protected void setCompletionKeywords(KeywordSets.Key key) { this.keywordSet = KeywordSets.getKeywords( key, language ); @@ -162,6 +164,7 @@ public class ContextualParser extends CompleteParser { } private String currentFunctionName = EMPTY_STRING; + protected IASTExpression parameterListExpression; @@ -254,4 +257,18 @@ public class ContextualParser extends CompleteParser { setCompletionKind(kind); checkEndOfFile(); } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.parser.ExpressionParser#setParameterListExpression(org.eclipse.cdt.core.parser.ast.IASTExpression) + */ + protected void setParameterListExpression( + IASTExpression assignmentExpression) { + parameterListExpression = assignmentExpression; + } + /** + * @return Returns the parameterListExpression. + */ + public final IASTExpression getParameterListExpression() { + return parameterListExpression; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ExpressionParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ExpressionParser.java index 25fa12b7e4d..37f8b20a530 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ExpressionParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ExpressionParser.java @@ -740,8 +740,10 @@ public class ExpressionParser implements IExpressionParser, IParserData { IASTExpression assignmentExpression = assignmentExpression(scope,kind,key); while (LT(1) == IToken.tCOMMA) { - consume(); + consume(IToken.tCOMMA); + setParameterListExpression( assignmentExpression ); IASTExpression secondExpression = assignmentExpression(scope,kind,key); + setParameterListExpression( null ); try { assignmentExpression = @@ -766,6 +768,11 @@ public class ExpressionParser implements IExpressionParser, IParserData { return assignmentExpression; } + /** + * @param assignmentExpression + */ + protected void setParameterListExpression(IASTExpression assignmentExpression) { + } /** * @param expression * @throws BacktrackException diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/ASTCompletionNode.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/ASTCompletionNode.java index 1d710bcf817..91d8083ab64 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/ASTCompletionNode.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/ASTCompletionNode.java @@ -14,6 +14,7 @@ import java.util.Iterator; import java.util.Set; import org.eclipse.cdt.core.parser.ast.IASTCompletionNode; +import org.eclipse.cdt.core.parser.ast.IASTExpression; import org.eclipse.cdt.core.parser.ast.IASTNode; import org.eclipse.cdt.core.parser.ast.IASTScope; @@ -28,8 +29,9 @@ public class ASTCompletionNode implements IASTCompletionNode { private final CompletionKind kind; private final Set keywordSet; private final String functionName; + private final IASTExpression parameterListExpression; - public ASTCompletionNode( CompletionKind kind, IASTScope scope, IASTNode context, String prefix, Set keywords, String functionName ) + public ASTCompletionNode( CompletionKind kind, IASTScope scope, IASTNode context, String prefix, Set keywords, String functionName, IASTExpression expression ) { this.kind = kind; this.context = context; @@ -37,6 +39,7 @@ public class ASTCompletionNode implements IASTCompletionNode { this.prefix = prefix; this.keywordSet = keywords; this.functionName = functionName; + this.parameterListExpression = expression; } /* (non-Javadoc) @@ -89,4 +92,11 @@ public class ASTCompletionNode implements IASTCompletionNode { return functionName; } + /* (non-Javadoc) + * @see org.eclipse.cdt.core.parser.ast.IASTCompletionNode#getFunctionParameters() + */ + public IASTExpression getFunctionParameters() { + return parameterListExpression; + } + } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/Scanner.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/Scanner.java index 690e14578cc..da825654839 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/Scanner.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/Scanner.java @@ -2005,7 +2005,7 @@ public class Scanner implements IScanner { */ protected void handleCompletionOnDefinition(String definition) throws EndOfFileException { IASTCompletionNode node = new ASTCompletionNode( IASTCompletionNode.CompletionKind.MACRO_REFERENCE, - null, null, definition, KeywordSets.getKeywords(KeywordSets.Key.EMPTY, scannerData.getLanguage()), EMPTY_STRING ); + null, null, definition, KeywordSets.getKeywords(KeywordSets.Key.EMPTY, scannerData.getLanguage()), EMPTY_STRING, null ); throwEOF( node ); } @@ -2064,19 +2064,19 @@ public class Scanner implements IScanner { IASTCompletionNode node = new ASTCompletionNode( kind, null, null, prefix, - KeywordSets.getKeywords(((kind == IASTCompletionNode.CompletionKind.NO_SUCH_KIND )? KeywordSets.Key.EMPTY : KeywordSets.Key.MACRO), scannerData.getLanguage()), EMPTY_STRING ); + KeywordSets.getKeywords(((kind == IASTCompletionNode.CompletionKind.NO_SUCH_KIND )? KeywordSets.Key.EMPTY : KeywordSets.Key.MACRO), scannerData.getLanguage()), EMPTY_STRING, null ); throwEOF( node ); } protected void handleInvalidCompletion() throws EndOfFileException { - throwEOF( new ASTCompletionNode( IASTCompletionNode.CompletionKind.UNREACHABLE_CODE, null, null, EMPTY_STRING, KeywordSets.getKeywords(KeywordSets.Key.EMPTY, scannerData.getLanguage()) , EMPTY_STRING)); + throwEOF( new ASTCompletionNode( IASTCompletionNode.CompletionKind.UNREACHABLE_CODE, null, null, EMPTY_STRING, KeywordSets.getKeywords(KeywordSets.Key.EMPTY, scannerData.getLanguage()) , EMPTY_STRING, null)); } protected void handleCompletionOnPreprocessorDirective( String prefix ) throws EndOfFileException { - throwEOF( new ASTCompletionNode( IASTCompletionNode.CompletionKind.NO_SUCH_KIND, null, null, prefix, KeywordSets.getKeywords(KeywordSets.Key.PP_DIRECTIVE, scannerData.getLanguage() ), EMPTY_STRING)); + throwEOF( new ASTCompletionNode( IASTCompletionNode.CompletionKind.NO_SUCH_KIND, null, null, prefix, KeywordSets.getKeywords(KeywordSets.Key.PP_DIRECTIVE, scannerData.getLanguage() ), EMPTY_STRING, null)); } /** * @param key