-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Introduce local variable supporting target-type new syntax #76342
Introduce local variable supporting target-type new syntax #76342
Conversation
@@ -54,7 +55,7 @@ protected override Document IntroduceLocal( | |||
[VariableDeclarator( | |||
newLocalNameToken.WithAdditionalAnnotations(RenameAnnotation.Create()), | |||
argumentList: null, | |||
EqualsValueClause(expression.WithoutTrivia()))])); | |||
EqualsValueClause(expression.WithAdditionalAnnotations(Simplifier.Annotation)).WithoutTrivia())])); |
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.
Is there any chance that this might work? It does not but I'm not exactly sure why it doesn't. I thought it might.
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 would not use the simplifier annotation. I would just generate the right expected results based ont he user's options specified.
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 took the time to debug TestIntroduceLocalInExpressionBodiedIndexer
. It is my understanding that var
in that test (instead of explicit type name) is added in a postprocessing step. However, that postprocessing is not able to convert it to "target-type form".
So then your suggestion is a reasonably easy solution here.
Happy to help with this. And we would accept a PR for this as logn as the the following holds:
If you can confirm this is the logic the PR maintains,a nd you have tests for this, i can review asap. I can also review any questions blocking you if you call them out. Thanks! |
Yes, that's I would like to achieve. I added a few comments to specific lines of code where some help would be great |
b01eca3
to
12a96b8
Compare
FYI, it's vacation time. So things may be slow to respond. Feel free to ping me on jan2 if you haven't heard from anyone else! |
12a96b8
to
42634bd
Compare
Thank you for the heads up. It would be great if at least tests could be run for the PR, I would get some feedback. If not, no problem, I can wait. Merry Christmas! |
Tears will automatically run for you pr as long as it builds successfully :-) |
Hm, I'm not too sure what that CI error
is about. |
src/Features/CSharpTest/IntroduceVariable/IntroduceVariableTests.cs
Outdated
Show resolved
Hide resolved
src/Features/CSharp/Portable/IntroduceVariable/CSharpIntroduceVariableService_IntroduceLocal.cs
Outdated
Show resolved
Hide resolved
var updatedExpression = expression.WithoutTrivia(); | ||
|
||
if (options.SimplifierOptions is CSharpSimplifierOptions csOptions && csOptions.ImplicitObjectCreationWhenTypeIsApparent.Value | ||
&& csOptions.GetUseVarPreference() == UseVarPreference.None) |
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.
needs docs explaining what is going on.
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 documented my approach. It is a really conservative one (either target-type new syntax is used exclusively, or codebase opts into using "var" in any case). My reasoning is as follows:
- It's a very conservative change, so it should not introduce some unwanted behavior.
- It's hard for me to tell what codebases in the wild actually use and want (i.e., how complex the implementation should be), so I basically wanted to cover my use-case because I understand it well (and hopefully, it's what other customers will like).
- With my PR, it should be easy to add missing cases, if people actually want it.
That being said, I'm certainly open to modify it.
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 think it is what #76342 (comment) mentions.
src/Features/CSharp/Portable/IntroduceVariable/CSharpIntroduceVariableService_IntroduceLocal.cs
Outdated
Show resolved
Hide resolved
@dotnet/roslyn-infrastructure can someone help this community member with their PR? something has gone wonky with the test machines. Thanks! |
Perhaps it was my fault. I fixed a few failing tests. |
@CyrusNajmabadi Pinging :) |
@@ -47,14 +50,25 @@ protected override Document IntroduceLocal( | |||
? TokenList(ConstKeyword) | |||
: default; | |||
|
|||
var updatedExpression = expression.WithoutTrivia(); | |||
var csOptions = (CSharpSimplifierOptions)options.SimplifierOptions; |
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.
minor, but we do not tend to use things like cs
in our code for abbreviations. We'd either use csharpOptions
here, or just simplifierOptions
(no need for the 'csharp' name to distringuish it.
@@ -47,14 +50,25 @@ protected override Document IntroduceLocal( | |||
? TokenList(ConstKeyword) | |||
: default; | |||
|
|||
var updatedExpression = expression.WithoutTrivia(); | |||
var csOptions = (CSharpSimplifierOptions)options.SimplifierOptions; |
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.
var csOptions = (CSharpSimplifierOptions)options.SimplifierOptions; | |
var simplifierOptions = (CSharpSimplifierOptions)options.SimplifierOptions; |
if (csOptions.ImplicitObjectCreationWhenTypeIsApparent.Value && csOptions.GetUseVarPreference() == UseVarPreference.None | ||
&& expression is ObjectCreationExpressionSyntax objectCreationExpression) | ||
{ | ||
updatedExpression = ImplicitObjectCreationExpression(objectCreationExpression.ArgumentList, objectCreationExpression.Initializer); |
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.
you removed the trivia from expression when making updatedExpression at the top of the method. but not here. consider either doign the same here, or doign updatedExpression is ObjectCreationExpressionSyntax ...
to ensure that original op wasn't lost.
@@ -935,7 +935,7 @@ class Program | |||
{ | |||
static void Main() | |||
{ | |||
G<int>.@class {|Rename:@class|} = new G<int>.@class(); | |||
G<int>.@class {|Rename:@class|} = new(); |
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.
can you add sibling tests that have the same input, but which have the ImplicitObjectCreationWhenTypeIsApparent set to false, to show we get the old output in that case?
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.
everything about the code change looks good. there are some small tweaks i'd like you to make, and some test additions. Preemptive approving assuming that happens. when that change goes in and everything is green, ping me and i can merge. thanks!
Note: if you're low on time, and you'd prefer it, i'm happy to make the above change requests for you. Up to you. |
If you have some time to do it, it would be great. First days after holidays are busy for me, so I cannot do the work immediately. |
…TargetTypeNew-for-IntroduceLocalVariable
Thanks for the contribution :) |
Merged. Thanks very much! |
Thank you for finishing the work! I'm very excited to try this feature in some new Visual Studio version! :) |
Our project uses target-type new syntax. Whenever, I have a line like this one:
and I want to extract a new variable from the marked expression, I get
but I would like to get
That's why I created this PR. Are you interested in such feature?1 Could you give me some pointers how to finish this PR (as it does not work at the moment)?
Footnotes
I sure am as I hit this daily. :) ↩