-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[CodeCompletion] SR-943 'self' for dot expressions #16534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CodeCompletion] SR-943 'self' for dot expressions #16534
Conversation
May dot expressions finally have a 'self' completion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AnthonyLatsis Could you please add some tests as well? We have a bunch of examples in test/IDE
and validation-test/IDE
@xedin Was about to ask where the tests are. Yes, sure. |
What is the purpose of the |
As for |
@rintaro Thank you. Is this alright as well (Not written by me)? (Note '7 items')
|
@AnthonyLatsis unless you really care that there are exactly 7 items, I would change that line to Also, the idea with DAG is that it lets you have two lines that can be reordered. In your example you probably want the Keyword line to also be DAG. |
lib/IDE/CodeCompletion.cpp
Outdated
SemanticContextKind::CurrentNominal, {}); | ||
Builder.setKeywordKind(CodeCompletionKeywordKind::kw_self); | ||
Builder.addTextChunk("self"); | ||
Builder.addTypeAnnotation(ExprType->getString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code path is also used for completing afterModuleName.
, where self
is not correct. If you move this into getValueExprCompletions
after the code that fixes ExprType
, you can check whether ExprType
is a module (see tryModuleCompletions
for the check). There should also be a test for this.
Thanks for working on this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the hint. By the way, is it ok to show self
completions after self
or instances?
I'm asking because although this isn't wrong in terms of the language, it is either senseless in practice (self.self...)
or something a user would very rarely do for some mysterious reason.
Also, the two CompletionKind
that currently should show self
and Type
are TypeIdentifierWithDot
and TypeIdentifierWithoutDot
. I was a bit confused when I first discovered this. What cases do they cover?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm asking because although this isn't wrong in terms of the language, it is either senseless in practice (self.self...) or something a user would very rarely do for some mysterious reason.
Showing it is fine.
Also, the two CompletionKind that currently should show self and Type are TypeIdentifierWithDot and TypeIdentifierWithoutDot. I was a bit confused when I first discovered this. What cases do they cover?
This is when we know syntactically that the LHS is a type name. These are not the only cases you would want to handle because they won't include typenames in expressions.
let a: Foo<TypeIdentifierWithoutDot >
let b: Foo.<TypeIdentifierWithDot >
_ = Foo.<Neither of the above>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I noticed we don't have a Type
completion for appropriate dot expressions as well.
If you move this into getValueExprCompletions after the code that fixes ExprType, you can check whether ExprType is a module (see tryModuleCompletions for the check). There should also be a test for this.
I'm not familiar enough with the compiler to understand what precisely happens in the "This is horrible" section, but it looks like it waits for generic types, so I suppose it doesn't affect modules whatsoever. That said and taking into account that getValueExprCompletions
is invoked from several other paths apart from DotExpr
, wouldn't it be better to keep everything where it is? Otherwise I would have to check if we're dealing with DotExpr
and write specific logic in a more general function that shouldn't care about that:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, it doesn't actually matter if you do the check before or after that "horrible" section. But you don't just want to handle DotExpr; for example, a postfix expression without a dot:
Foo <HERE>
Should still show self
, but it should also add the leading ".". This can be done by calling addLeadingDot
which will add the dot only if it's needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handled those as well. I've tidied up the code but still haven't moved anything because otherwise I ended up touching paren expressions. I can change that, but there would have to be a way to check for completionKind
in getValueExprCompletions
.
@benlangmuir can you help to understand how results are ordered? |
Don't worry about reviewing the test files too much – I simply added the |
@swift-ci please test |
Build failed |
Build failed |
Wierd...I just ran the tests on the failed files and they passed. |
How are SourceKit tests related to code completion? Is SourceKit responsible for providing completions from other modules? |
SourceKit's completion is built on top of the code in lib/IDE. |
Apologies, I did not suspect that I also needed to run |
@benlangmuir could you trigger the tests please? |
@swift-ci please test |
Build failed |
Build failed |
In case you're not too busy, any nitpicks, @benlangmuir? |
Nope, looks good. Thanks! |
May dot expressions finally have a 'self' completion.
Resolves SR-943.