diff options
author | Douglas Gregor <dgregor@apple.com> | 2009-11-19 01:08:35 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2009-11-19 01:08:35 +0000 |
commit | d3c6854153fd6bc6a412a29e4491dbd0a47bdb14 (patch) | |
tree | 6ebb79f2018d2e29f67e446809b9f094405de389 | |
parent | 58588943a8283baf17815d56275bdb381560970c (diff) |
Improve code completion for Objective-C message sends, so that we
provide completion results before each keyword argument, e.g.,
[foo Method:arg WithArg1:arg1 WithArg2:arg2]
We now complete before "WithArg1" and before "WithArg2", in addition
to completing before "Method".
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89290 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/Parse/Action.h | 16 | ||||
-rw-r--r-- | include/clang/Sema/CodeCompleteConsumer.h | 25 | ||||
-rw-r--r-- | lib/Parse/ParseObjc.cpp | 20 | ||||
-rw-r--r-- | lib/Sema/Sema.h | 8 | ||||
-rw-r--r-- | lib/Sema/SemaCodeComplete.cpp | 98 | ||||
-rw-r--r-- | test/Index/complete-objc-message.m | 29 |
6 files changed, 155 insertions, 41 deletions
diff --git a/include/clang/Parse/Action.h b/include/clang/Parse/Action.h index 2a6171c6aa..6ebdf07958 100644 --- a/include/clang/Parse/Action.h +++ b/include/clang/Parse/Action.h @@ -2335,23 +2335,31 @@ public: /// a class method. /// /// This code completion action is invoked when the code-completion token is - /// found after the class name. + /// found after the class name and after each argument. /// /// \param S the scope in which the message expression occurs. /// \param FName the factory name. /// \param FNameLoc the source location of the factory name. + /// \param SelIdents the identifiers that describe the selector (thus far). + /// \param NumSelIdents the number of identifiers in \p SelIdents. virtual void CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName, - SourceLocation FNameLoc){ } + SourceLocation FNameLoc, + IdentifierInfo **SelIdents, + unsigned NumSelIdents){ } /// \brief Code completion for an ObjC message expression that refers to /// an instance method. /// /// This code completion action is invoked when the code-completion token is - /// found after the receiver expression. + /// found after the receiver expression and after each argument. /// /// \param S the scope in which the operator keyword occurs. /// \param Receiver an expression for the receiver of the message. - virtual void CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver) { } + /// \param SelIdents the identifiers that describe the selector (thus far). + /// \param NumSelIdents the number of identifiers in \p SelIdents. + virtual void CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver, + IdentifierInfo **SelIdents, + unsigned NumSelIdents) { } /// \brief Code completion for a list of protocol references in Objective-C, /// such as P1 and P2 in \c id<P1,P2>. diff --git a/include/clang/Sema/CodeCompleteConsumer.h b/include/clang/Sema/CodeCompleteConsumer.h index f8c4c5281c..3c5428bdb9 100644 --- a/include/clang/Sema/CodeCompleteConsumer.h +++ b/include/clang/Sema/CodeCompleteConsumer.h @@ -257,6 +257,10 @@ public: /// result and progressively higher numbers representing poorer results. unsigned Rank; + /// \brief Specifiers which parameter (of a function, Objective-C method, + /// macro, etc.) we should start with when formatting the result. + unsigned StartParameter; + /// \brief Whether this result is hidden by another name. bool Hidden : 1; @@ -277,26 +281,27 @@ public: NestedNameSpecifier *Qualifier = 0, bool QualifierIsInformative = false) : Kind(RK_Declaration), Declaration(Declaration), Rank(Rank), - Hidden(false), QualifierIsInformative(QualifierIsInformative), + StartParameter(0), Hidden(false), + QualifierIsInformative(QualifierIsInformative), StartsNestedNameSpecifier(false), Qualifier(Qualifier) { } /// \brief Build a result that refers to a keyword or symbol. Result(const char *Keyword, unsigned Rank) - : Kind(RK_Keyword), Keyword(Keyword), Rank(Rank), Hidden(false), - QualifierIsInformative(0), StartsNestedNameSpecifier(false), - Qualifier(0) { } + : Kind(RK_Keyword), Keyword(Keyword), Rank(Rank), StartParameter(0), + Hidden(false), QualifierIsInformative(0), + StartsNestedNameSpecifier(false), Qualifier(0) { } /// \brief Build a result that refers to a macro. Result(IdentifierInfo *Macro, unsigned Rank) - : Kind(RK_Macro), Macro(Macro), Rank(Rank), Hidden(false), - QualifierIsInformative(0), StartsNestedNameSpecifier(false), - Qualifier(0) { } + : Kind(RK_Macro), Macro(Macro), Rank(Rank), StartParameter(0), + Hidden(false), QualifierIsInformative(0), + StartsNestedNameSpecifier(false), Qualifier(0) { } /// \brief Build a result that refers to a pattern. Result(CodeCompletionString *Pattern, unsigned Rank) - : Kind(RK_Pattern), Pattern(Pattern), Rank(Rank), Hidden(false), - QualifierIsInformative(0), StartsNestedNameSpecifier(false), - Qualifier(0) { } + : Kind(RK_Pattern), Pattern(Pattern), Rank(Rank), StartParameter(0), + Hidden(false), QualifierIsInformative(0), + StartsNestedNameSpecifier(false), Qualifier(0) { } /// \brief Retrieve the declaration stored in this result. NamedDecl *getDeclaration() const { diff --git a/lib/Parse/ParseObjc.cpp b/lib/Parse/ParseObjc.cpp index fa14bb9a52..9943428bc5 100644 --- a/lib/Parse/ParseObjc.cpp +++ b/lib/Parse/ParseObjc.cpp @@ -1632,11 +1632,14 @@ Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc, ExprArg ReceiverExpr) { if (Tok.is(tok::code_completion)) { if (ReceiverName) - Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc); + Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc, + 0, 0); else - Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get()); + Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(), + 0, 0); ConsumeToken(); } + // Parse objc-selector SourceLocation Loc; IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc); @@ -1674,6 +1677,19 @@ Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc, // We have a valid expression. KeyExprs.push_back(Res.release()); + // Code completion after each argument. + if (Tok.is(tok::code_completion)) { + if (ReceiverName) + Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc, + KeyIdents.data(), + KeyIdents.size()); + else + Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(), + KeyIdents.data(), + KeyIdents.size()); + ConsumeToken(); + } + // Check for another keyword selector. selIdent = ParseObjCSelectorPiece(Loc); if (!selIdent && Tok.isNot(tok::colon)) diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 96f671535c..0da822ec2e 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -3648,8 +3648,12 @@ public: virtual void CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS); virtual void CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName, - SourceLocation FNameLoc); - virtual void CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver); + SourceLocation FNameLoc, + IdentifierInfo **SelIdents, + unsigned NumSelIdents); + virtual void CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver, + IdentifierInfo **SelIdents, + unsigned NumSelIdents); virtual void CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols, unsigned NumProtocols); virtual void CodeCompleteObjCProtocolDecl(Scope *S); diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp index 64c795eecb..59060336fd 100644 --- a/lib/Sema/SemaCodeComplete.cpp +++ b/lib/Sema/SemaCodeComplete.cpp @@ -944,19 +944,40 @@ CodeCompleteConsumer::Result::CreateCodeCompletionString(Sema &S) { return Result; } - Result->AddTypedTextChunk( - Sel.getIdentifierInfoForSlot(0)->getName().str() + std::string(":")); + std::string SelName = Sel.getIdentifierInfoForSlot(0)->getName().str(); + SelName += ':'; + if (StartParameter == 0) + Result->AddTypedTextChunk(SelName); + else { + Result->AddInformativeChunk(SelName); + + // If there is only one parameter, and we're past it, add an empty + // typed-text chunk since there is nothing to type. + if (Method->param_size() == 1) + Result->AddTypedTextChunk(""); + } unsigned Idx = 0; for (ObjCMethodDecl::param_iterator P = Method->param_begin(), PEnd = Method->param_end(); P != PEnd; (void)++P, ++Idx) { if (Idx > 0) { - std::string Keyword = " "; + std::string Keyword; + if (Idx > StartParameter) + Keyword = " "; if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) Keyword += II->getName().str(); Keyword += ":"; - Result->AddTextChunk(Keyword); + if (Idx < StartParameter) { + Result->AddInformativeChunk(Keyword); + } else if (Idx == StartParameter) + Result->AddTypedTextChunk(Keyword); + else + Result->AddTextChunk(Keyword); } + + // If we're before the starting parameter, skip the placeholder. + if (Idx < StartParameter) + continue; std::string Arg; (*P)->getType().getAsStringInternal(Arg, S.Context.PrintingPolicy); @@ -1664,6 +1685,7 @@ static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { if (!CodeCompleter) return; + unsigned Attributes = ODS.getPropertyAttributes(); typedef CodeCompleteConsumer::Result Result; @@ -1718,14 +1740,33 @@ void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { /// \param Results the structure into which we'll add results. static void AddObjCMethods(ObjCContainerDecl *Container, bool WantInstanceMethods, + IdentifierInfo **SelIdents, + unsigned NumSelIdents, DeclContext *CurContext, ResultBuilder &Results) { typedef CodeCompleteConsumer::Result Result; for (ObjCContainerDecl::method_iterator M = Container->meth_begin(), MEnd = Container->meth_end(); M != MEnd; ++M) { - if ((*M)->isInstanceMethod() == WantInstanceMethods) - Results.MaybeAddResult(Result(*M, 0), CurContext); + if ((*M)->isInstanceMethod() == WantInstanceMethods) { + // Check whether the selector identifiers we've been given are a + // subset of the identifiers for this particular method. + Selector Sel = (*M)->getSelector(); + if (NumSelIdents > Sel.getNumArgs()) + continue; + + bool Failed = false; + for (unsigned I = 0; I != NumSelIdents && !Failed; ++I) + if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) + Failed = true; + + if (Failed) + continue; + + Result R = Result(*M, 0); + R.StartParameter = NumSelIdents; + Results.MaybeAddResult(R, CurContext); + } } ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); @@ -1737,12 +1778,14 @@ static void AddObjCMethods(ObjCContainerDecl *Container, for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), E = Protocols.end(); I != E; ++I) - AddObjCMethods(*I, WantInstanceMethods, CurContext, Results); + AddObjCMethods(*I, WantInstanceMethods, SelIdents, NumSelIdents, + CurContext, Results); // Add methods in categories. for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl; CatDecl = CatDecl->getNextClassCategory()) { - AddObjCMethods(CatDecl, WantInstanceMethods, CurContext, Results); + AddObjCMethods(CatDecl, WantInstanceMethods, SelIdents, NumSelIdents, + CurContext, Results); // Add a categories protocol methods. const ObjCList<ObjCProtocolDecl> &Protocols @@ -1750,25 +1793,30 @@ static void AddObjCMethods(ObjCContainerDecl *Container, for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), E = Protocols.end(); I != E; ++I) - AddObjCMethods(*I, WantInstanceMethods, CurContext, Results); + AddObjCMethods(*I, WantInstanceMethods, SelIdents, NumSelIdents, + CurContext, Results); // Add methods in category implementations. if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) - AddObjCMethods(Impl, WantInstanceMethods, CurContext, Results); + AddObjCMethods(Impl, WantInstanceMethods, SelIdents, NumSelIdents, + CurContext, Results); } // Add methods in superclass. if (IFace->getSuperClass()) - AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, CurContext, - Results); + AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, SelIdents, + NumSelIdents, CurContext,Results); // Add methods in our implementation, if any. if (ObjCImplementationDecl *Impl = IFace->getImplementation()) - AddObjCMethods(Impl, WantInstanceMethods, CurContext, Results); + AddObjCMethods(Impl, WantInstanceMethods, SelIdents, NumSelIdents, + CurContext, Results); } void Sema::CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName, - SourceLocation FNameLoc) { + SourceLocation FNameLoc, + IdentifierInfo **SelIdents, + unsigned NumSelIdents) { typedef CodeCompleteConsumer::Result Result; ObjCInterfaceDecl *CDecl = 0; @@ -1794,7 +1842,8 @@ void Sema::CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName, SuperTy = Context.getObjCObjectPointerType(SuperTy); OwningExprResult Super = Owned(new (Context) ObjCSuperExpr(FNameLoc, SuperTy)); - return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get()); + return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(), + SelIdents, NumSelIdents); } // Okay, we're calling a factory method in our superclass. @@ -1816,21 +1865,24 @@ void Sema::CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName, // probably calling an instance method. OwningExprResult Super = ActOnDeclarationNameExpr(S, FNameLoc, FName, false, 0, false); - return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get()); + return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(), + SelIdents, NumSelIdents); } // Add all of the factory methods in this Objective-C class, its protocols, // superclasses, categories, implementation, etc. ResultBuilder Results(*this); Results.EnterNewScope(); - AddObjCMethods(CDecl, false, CurContext, Results); + AddObjCMethods(CDecl, false, SelIdents, NumSelIdents, CurContext, Results); Results.ExitScope(); // This also suppresses remaining diagnostics. HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size()); } -void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver) { +void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver, + IdentifierInfo **SelIdents, + unsigned NumSelIdents) { typedef CodeCompleteConsumer::Result Result; Expr *RecExpr = static_cast<Expr *>(Receiver); @@ -1858,7 +1910,8 @@ void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver) { ReceiverType->isObjCQualifiedClassType()) { if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) - AddObjCMethods(ClassDecl, false, CurContext, Results); + AddObjCMethods(ClassDecl, false, SelIdents, NumSelIdents, CurContext, + Results); } } // Handle messages to a qualified ID ("id<foo>"). @@ -1868,19 +1921,20 @@ void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver) { for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(), E = QualID->qual_end(); I != E; ++I) - AddObjCMethods(*I, true, CurContext, Results); + AddObjCMethods(*I, true, SelIdents, NumSelIdents, CurContext, Results); } // Handle messages to a pointer to interface type. else if (const ObjCObjectPointerType *IFacePtr = ReceiverType->getAsObjCInterfacePointerType()) { // Search the class, its superclasses, etc., for instance methods. - AddObjCMethods(IFacePtr->getInterfaceDecl(), true, CurContext, Results); + AddObjCMethods(IFacePtr->getInterfaceDecl(), true, SelIdents, NumSelIdents, + CurContext, Results); // Search protocols for instance methods. for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(), E = IFacePtr->qual_end(); I != E; ++I) - AddObjCMethods(*I, true, CurContext, Results); + AddObjCMethods(*I, true, SelIdents, NumSelIdents, CurContext, Results); } Results.ExitScope(); diff --git a/test/Index/complete-objc-message.m b/test/Index/complete-objc-message.m index 8f235d39be..b692986cfd 100644 --- a/test/Index/complete-objc-message.m +++ b/test/Index/complete-objc-message.m @@ -82,6 +82,19 @@ void test_qual_id(id<FooTestProtocol,FooTestProtocol2> ptr) { [ptr protocolInstanceMethod:1]; } +@interface Overload +- (int)Method:(int)i; +- (int)Method; +- (int)Method:(float)f Arg1:(int)i1 Arg2:(int)i2; +- (int)Method:(float)f Arg1:(int)i1 OtherArg:(id)obj; +- (int)Method:(float)f SomeArg:(int)i1 OtherArg:(id)obj; +- (int)OtherMethod:(float)f Arg1:(int)i1 Arg2:(int)i2; +@end + +void test_overload(Overload *ovl) { + [ovl Method:1 Arg1:1 OtherArg:ovl]; +} + // RUN: c-index-test -code-completion-at=%s:23:19 %s | FileCheck -check-prefix=CHECK-CC1 %s // CHECK-CC1: {TypedText categoryClassMethod} // CHECK-CC1: {TypedText classMethod1:}{Placeholder (id)a}{Text withKeyword:}{Placeholder (int)b} @@ -104,4 +117,18 @@ void test_qual_id(id<FooTestProtocol,FooTestProtocol2> ptr) { // RUN: c-index-test -code-completion-at=%s:82:8 %s | FileCheck -check-prefix=CHECK-CC6 %s // CHECK-CC6: ObjCInstanceMethodDecl:{TypedText protocolInstanceMethod:}{Placeholder (int)value} // CHECK-CC6: ObjCInstanceMethodDecl:{TypedText secondProtocolInstanceMethod} - +// RUN: c-index-test -code-completion-at=%s:95:8 %s | FileCheck -check-prefix=CHECK-CC7 %s +// CHECK-CC7: ObjCInstanceMethodDecl:{TypedText Method} +// CHECK-CC7: ObjCInstanceMethodDecl:{TypedText Method:}{Placeholder (int)i} +// CHECK-CC7: ObjCInstanceMethodDecl:{TypedText Method:}{Placeholder (float)f}{Text Arg1:}{Placeholder (int)i1}{Text Arg2:}{Placeholder (int)i2} +// CHECK-CC7: ObjCInstanceMethodDecl:{TypedText Method:}{Placeholder (float)f}{Text Arg1:}{Placeholder (int)i1}{Text OtherArg:}{Placeholder (id)obj} +// CHECK-CC7: ObjCInstanceMethodDecl:{TypedText Method:}{Placeholder (float)f}{Text SomeArg:}{Placeholder (int)i1}{Text OtherArg:}{Placeholder (id)obj} +// CHECK-CC7: ObjCInstanceMethodDecl:{TypedText OtherMethod:}{Placeholder (float)f}{Text Arg1:}{Placeholder (int)i1}{Text Arg2:}{Placeholder (int)i2} +// RUN: c-index-test -code-completion-at=%s:95:17 %s | FileCheck -check-prefix=CHECK-CC8 %s +// CHECK-CC8: ObjCInstanceMethodDecl:{Informative Method:}{TypedText } +// CHECK-CC8: ObjCInstanceMethodDecl:{Informative Method:}{TypedText Arg1:}{Placeholder (int)i1}{Text Arg2:}{Placeholder (int)i2} +// CHECK-CC8: ObjCInstanceMethodDecl:{Informative Method:}{TypedText Arg1:}{Placeholder (int)i1}{Text OtherArg:}{Placeholder (id)obj} +// CHECK-CC8: ObjCInstanceMethodDecl:{Informative Method:}{TypedText SomeArg:}{Placeholder (int)i1}{Text OtherArg:}{Placeholder (id)obj} +// RUN: c-index-test -code-completion-at=%s:95:24 %s | FileCheck -check-prefix=CHECK-CC9 %s +// CHECK-CC9: ObjCInstanceMethodDecl:{Informative Method:}{Informative Arg1:}{TypedText Arg2:}{Placeholder (int)i2} +// CHECK-CC9: ObjCInstanceMethodDecl:{Informative Method:}{Informative Arg1:}{TypedText OtherArg:}{Placeholder (id)obj} |