This is an archive of the discontinued LLVM Phabricator instance.

[CodeCompletion] Signature help for template argument lists
ClosedPublic

Authored by sammccall on Dec 28 2021, 7:18 PM.

Details

Summary

Provide signature while typing template arguments: Foo< ^here >
Here the parameters are e.g. "typename x", and the result type is e.g.
"struct" (class template) or "int" (variable template) or "bool (std::string)"
(function template).

Multiple overloads are possible when a template name is used for several
overloaded function templates.

Fixes https://github.com/clangd/clangd/issues/299

Diff Detail

Unit TestsFailed

Event Timeline

sammccall created this revision.Dec 28 2021, 7:18 PM
sammccall requested review of this revision.Dec 28 2021, 7:18 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptDec 28 2021, 7:18 PM
sammccall updated this revision to Diff 396455.Dec 28 2021, 7:24 PM

Clean up style, remove fixed fixme, add an extra testcase.

nridge added a subscriber: nridge.Jan 1 2022, 4:59 PM

thanks this looks great! just a question around rendering of the result type chunk. feel free to leave a fixme if there's no easy way of doing that.

clang/lib/Parse/ParseTemplate.cpp
1237

s/NameHint/Template ?

clang/lib/Sema/SemaCodeComplete.cpp
3785

the rendering looks a little bit surprising to me foo<arg1,arg2> -> bool (float) we might consider printing parameters first, e.g. foo<arg1,arg2>(float) -> bool. i think this also looks more natural for the following signature help on function parameters.

kadircet accepted this revision.Jan 3 2022, 2:12 AM
This revision is now accepted and ready to land.Jan 3 2022, 2:12 AM
sammccall added inline comments.Jan 3 2022, 4:29 AM
clang/lib/Sema/SemaCodeComplete.cpp
3785

Yeah. There are a few things going on here I wasn't sure how to square:

  1. the "arrow" rendering is clangd-specific, and it reorders chunks by kind (moving the ResultType chunk to the end). So if we want a specific rendering in clangd we need to be able to infer it from the chunk sequence
  2. the chunk sequence should make sense when rendered as text more directly. I think the current version fails this test: bool(float) foo<arg1, arg2>.
  3. the rendering you suggest requires us to reverse the order of bool and float somehow, ideally without some fragile coupling/assumptions in clangd

I think the "natural" chunk sequence is bool foo< arg1 , arg2 > (float). The bool chunk is definitely ResultType.

If we make (float) also ResultType, we know have two RT chunks we have to stick in the signature in the right place. So maybe it's best to make it CK_Text or CK_Informative. I'll give this a spin...

sammccall updated this revision to Diff 397049.Jan 3 2022, 5:42 AM

Change ResultType handling for template functions. Given:

template <typename T> string getName(int count) const &

We return the signature:

{ResultType=string} getName<{placeholder=typename T}>()

Clangd renders this as:

getName<$(0:typename T)>() -> string

We drop the function parameter list because

  • it's potentially long and distracts from the template args we're showing
  • there's no convenient way to print it!
kadircet accepted this revision.Jan 3 2022, 6:53 AM

thanks! as discussed offline i think it's better to ignore function parameters for now.

clang/lib/Sema/SemaCodeComplete.cpp
3781

the comments around the suffix seems to be out-of-date (maybe mention it as a fixme?)

This revision was landed with ongoing or failed builds.Jan 3 2022, 7:28 AM
This revision was automatically updated to reflect the committed changes.