Adding missing completion for switch when clause#25133
Adding missing completion for switch when clause#25133jinujoseph merged 30 commits intodotnet:masterfrom
Conversation
…a declaration pattern
| @"switch (1) { case int i $$ }")); | ||
| await VerifyKeywordAsync(AddInsideMethod(@"switch (1) { case int i $$ }")); | ||
| await VerifyKeywordAsync(AddInsideMethod(@"switch (1) { case int i $$: }")); | ||
| await VerifyKeywordAsync(AddInsideMethod(@"switch (1) { case int i $$ break; }")); |
There was a problem hiding this comment.
note: in practice we prefer one test per test method. That way if something fails, you don't have to fix it, only to find the next line then fails.
There was a problem hiding this comment.
hmm.. i actually thought about making a helper for this to test the same code all these cases... I think it would clutter up the tests to have 3 versions for each one. And when working on this, it never happened to me that 1 version would actually fail while another one didn't. I just added this as a precaution.
There was a problem hiding this comment.
VerifyKeywordAsync itself actually does like 5 different tests for variations such as a partially written keyword, and it's all in 1 method
There was a problem hiding this comment.
my point is... I don't think it's very likely that these variations would fail independently (as opposed to the tests hidden inside VerifyKeywordAsync, which often do)
| class C | ||
| { | ||
| const Color Color = null; | ||
| void M() { switch (new object()) { case int $$ } } |
There was a problem hiding this comment.
i don't understnd why there is "const Color Colon" in these tests.
There was a problem hiding this comment.
Because I wanted to have the same environment in this set of tests and just do different things inside the switch statement... but you're right maybe it's not a good idea after all. It really doesn't help readability.
| } | ||
|
|
||
| private static bool IsIdentifierInCasePattern(SyntaxNode node) | ||
| private static T GetAncestorUntilStatement<T>(SyntaxToken token) where T : SyntaxNode |
There was a problem hiding this comment.
Don't we already have a GetAncestor or FindAncestor call that could already be used?
There was a problem hiding this comment.
Yes but it always goes all the way up to the root, I thought that was unnecessary.. but I guess it wouldn't be a performance right? so maybe this is kinda useless
There was a problem hiding this comment.
Usually teh root is no more than 8 items high. So this is a microoptimization that i can't imagine ever being worth it :)
There was a problem hiding this comment.
i didn't think of this that much as an optimization as just... I just want this to be as local search as possible, I don't really care what's outside.
| } | ||
|
|
||
| [Fact, Trait(Traits.Feature, Traits.Features.Completion)] | ||
| public async Task ExceptionFilter1_NotBeforeOpenParen() |
There was a problem hiding this comment.
just a precaution to make sure this never gets mixed up with when inside switch where there's no parentheses required
|
|
||
| return false; | ||
|
|
||
| bool NotATypeName(SyntaxNode node) |
There was a problem hiding this comment.
Naming is hard, especially with negatives... After 10 minutes of pondering and constantly changing the name, I settled on this. Even adding 'Is' makes this slightly less readable, not more. My conclusion was that a longer name wouldn't be an improvement here.
| return false; | ||
| } | ||
|
|
||
| // If the last token missing, the expression is incomplete - possibly because of missing parentheses, |
There was a problem hiding this comment.
note to myself: last token is missing
|
|
||
| [WorkItem(25084, "https://github.com/dotnet/roslyn/issues/25084")] | ||
| [Fact, Trait(Traits.Feature, Traits.Features.Completion)] | ||
| public async Task SwitchPatternCaseWhenClause() |
There was a problem hiding this comment.
note to myself: they're all pattern cases if they have 'when'... wording doesn't make sense
|
|
||
| if (lastToken == context.TargetToken) | ||
| { | ||
| return NotATypeName(expressionOrPattern); |
There was a problem hiding this comment.
note: consider TypeSyntax inside a constant pattern & whether that's relevant here
|
Question: case 1 | whenoffer when before an existing one? Usually completion ignores this sort of thing and provides completion based on what's to the left, so I would think yes. This would be an easy check though. But I think I could see myself clicking after the |
Yes. completion should care only what is to the left 99% of the time (unless there is a VERY good reason to do otherwise). This is because sometimes people jsut want to restart a thought, and we shouldn't penalize them for what's coming after where they currently are. |
|
Then I need to fix the code. It is indeed offered now, but it bypasses the semantic check for whether the expression is actually a type. case SyntaxNode | whenit will currently offer 'when' even if SyntaxNode is known to be a type. (Without the 'when', it won't be offered. This is because I check for TypeSyntax but not inside a constant pattern, as would happen if the 'when' clause is added) |
|
@dotnet/roslyn-ide for review and merging of a community PR. Thanks |
|
@dotnet/roslyn-ide for review and merging of a community PR for 15.7. Thanks |
|
|
||
| [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] | ||
| public async Task TestForSwitchCase_NotAfterIncompleteMemberAccess() => | ||
| await VerifyAbsenceAsync(AddInsideMethod(@"switch (1) { case int.$$ }")); |
There was a problem hiding this comment.
well... now I have an urge to change this to use the same pattern as I did in #25703 (use a theory called 'NotAfterIncompleteExpression' with all these cases as inline data)
There was a problem hiding this comment.
even though here there wouldn't be as big of a benefit since all these tests are just one line
There was a problem hiding this comment.
I quickly made the change, I hope it's fine
|
I see the build failure is the same as #25689 due to some moving of test helpers to a different namespace that happened on master recently (#25643) - I haven't done a merge since then. |
|
@dotnet-bot retest windows_debug_vs-integration_prtest please |
|
@Neme12 I don't generally worry about the integration tests much, as they are fairly noisy and don't block merging. IDE team can correct me if that's wrong. |
|
retest windows_debug_vs-integration_prtest please |
|
retest windows_release_vs-integration_prtest please |
|
retest windows_debug_es_unit32_prtest please |
|
@dpoeschl @dotnet/roslyn-ide This community PR is waiting for a last review. It would be great to merge in time for 15.7. Thanks |
|
Sorry for the delay @Neme12 , we will get this reviewed and make a call on which release today |
|
FYI @jinujoseph |
|
And don't worry about |
|
retest this please |
|
I re-triggered a CI run as the PR last passed a week ago, but the branch changed since. |
|
Awesome, thanks! |


fixes #25084
fixes #25293