Skip to content
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

Place caret in parameter list when generating constructors. #21076

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Z
{
int a;

public Z(int a)
public Z(int a{|Navigation:)|}
{
this.a = a;
}
Expand All @@ -56,7 +56,7 @@ class Z
{
int a;

public Z(int a) => this . a = a ;
public Z(int a{|Navigation:)|} => this . a = a ;
}",
options: Option(CSharpCodeStyleOptions.PreferExpressionBodiedConstructors, CSharpCodeStyleOptions.WhenPossibleWithNoneEnforcement));
}
Expand All @@ -77,7 +77,7 @@ class Z
{
int a;

public Z(int a) => this . a = a ;
public Z(int a{|Navigation:)|} => this . a = a ;
}",
options: Option(CSharpCodeStyleOptions.PreferExpressionBodiedConstructors, CSharpCodeStyleOptions.WhenOnSingleLineWithNoneEnforcement));
}
Expand All @@ -100,7 +100,7 @@ class Z
int a;
int b;

public Z(int a, int b)
public Z(int a, int b{|Navigation:)|}
{
this . a = a ;
this . b = b ;
Expand All @@ -127,7 +127,7 @@ class Z
int a;
string b;

public Z(int a, string b)
public Z(int a, string b{|Navigation:)|}
{
this.a = a;
this.b = b;
Expand Down Expand Up @@ -163,7 +163,7 @@ public Z(int a)
this.a = a;
}

public Z(string b)
public Z(string b{|Navigation:)|}
{
this.b = b;
}
Expand Down Expand Up @@ -198,7 +198,7 @@ public Z(int a)
this.a = a;
}

public Z(int a, string b)
public Z(int a, string b{|Navigation:)|}
{
this.a = a;
this.b = b;
Expand Down Expand Up @@ -234,7 +234,7 @@ public Z(int a)
this.a = a;
}

public Z(int a, string b)
public Z(int a, string b{|Navigation:)|}
{
this.a = a;
this.b = b;
Expand Down Expand Up @@ -270,7 +270,7 @@ public Z(int a)
this.a = a;
}

public Z(int a, string b) : this(a)
public Z(int a, string b{|Navigation:)|} : this(a)
{
this.b = b;
}
Expand All @@ -294,7 +294,7 @@ public Z(int a)
this.a = a;
}

public Z(int a, string b)
public Z(int a, string b{|Navigation:)|}
{
this.a = a;
this.b = b;
Expand All @@ -313,7 +313,7 @@ await TestInRegularAndScriptAsync(
}",
@"class Z
{
public Z(int a, string b)
public Z(int a, string b{|Navigation:)|}
{
A = a;
B = b;
Expand All @@ -335,7 +335,7 @@ await TestInRegularAndScriptAsync(
}",
@"class Z
{
public Z(int a, string b)
public Z(int a, string b{|Navigation:)|}
{
this.A = a;
this.B = b;
Expand All @@ -362,7 +362,7 @@ struct S
{
int i;

public S(int i)
public S(int i{|Navigation:)|}
{
this.i = i;
}
Expand All @@ -383,7 +383,7 @@ struct S

struct S
{
public S(int i) : this()
public S(int i{|Navigation:)|} : this()
{
this.i = i;
}
Expand Down Expand Up @@ -412,7 +412,7 @@ struct S

int y;

public S(int y) : this()
public S(int y{|Navigation:)|} : this()
{
this.y = y;
}
Expand All @@ -439,7 +439,7 @@ struct S

int y;

public S(int i) : this()
public S(int i{|Navigation:)|} : this()
{
this.i = i;
}
Expand All @@ -462,7 +462,7 @@ class Program<T>
{
int i;

public Program(int i)
public Program(int i{|Navigation:)|}
{
this.i = i;
}
Expand Down Expand Up @@ -534,7 +534,7 @@ await TestInRegularAndScriptAsync(
{
int yield;

public Program(int yield)
public Program(int yield{|Navigation:)|}
{
this.yield = yield;
}
Expand Down Expand Up @@ -577,7 +577,7 @@ class Z
{
(int, string) a;

public Z((int, string) a)
public Z((int, string) a{|Navigation:)|}
{
this.a = a;
}
Expand All @@ -597,7 +597,7 @@ await TestInRegularAndScriptAsync(
{
int _field;

public Program(int field)
public Program(int field{|Navigation:)|}
{
_field = field;
}
Expand All @@ -617,7 +617,7 @@ await TestInRegularAndScriptAsync(
{
int _field;

public Program(int field)
public Program(int field{|Navigation:)|}
{
this._field = field;
}
Expand All @@ -637,7 +637,7 @@ await TestInRegularAndScriptAsync(
}",
@"abstract class Contribution
{
public Contribution(string title, int number)
public Contribution(string title, int number{|Navigation:)|}
{
Title = title;
Number = number;
Expand Down Expand Up @@ -679,7 +679,7 @@ class Z
{
int a;

public Z(int a)
public Z(int a{|Navigation:)|}
{
this.a = a;
}
Expand All @@ -703,7 +703,7 @@ class Z
{
int a;

public Z(int a)
public Z(int a{|Navigation:)|}
{
this.a = a;
}
Expand Down Expand Up @@ -741,7 +741,7 @@ class Z
{
int a;

public Z()
public Z({|Navigation:)|}
{
}
}",
Expand All @@ -767,7 +767,7 @@ class Z
int a;
string b;

public Z(string b, int a)
public Z(string b, int a{|Navigation:)|}
{
this.b = b;
this.a = a;
Expand Down Expand Up @@ -799,7 +799,7 @@ class Z
int a;
string b;

public Z(int a, string b)
public Z(int a, string b{|Navigation:)|}
{
this.a = a;
this.b = b ?? throw new ArgumentNullException(nameof(b));
Expand Down Expand Up @@ -832,7 +832,7 @@ class Z
int a;
string b;

public Z(int a, string b)
public Z(int a, string b{|Navigation:)|}
{
if (b == null)
{
Expand Down Expand Up @@ -897,6 +897,30 @@ public void M()
}

public void N() { }
}");
}

[WorkItem(21067, "https://github.com/dotnet/roslyn/pull/21067")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructorFromMembers)]
public async Task TestFinalCaretPosition()
{
await TestInRegularAndScriptAsync(
@"using System.Collections.Generic;

class Z
{
[|int a;|]
}",
@"using System.Collections.Generic;

class Z
{
int a;

public Z(int a{|Navigation:)|}
{
this.a = a;
}
}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public async Task ApplyAsync(
}

var updatedSolution = operations.OfType<ApplyChangesOperation>().FirstOrDefault()?.ChangedSolution ?? oldSolution;
TryStartRenameSession(workspace, oldSolution, updatedSolution, cancellationToken);
TryNavigateToLocationOrStartRenameSession(workspace, oldSolution, updatedSolution, cancellationToken);
}

private TextDocument TryGetSingleChangedText(
Expand Down Expand Up @@ -259,7 +259,7 @@ private static void ProcessOperations(
}
}

private void TryStartRenameSession(Workspace workspace, Solution oldSolution, Solution newSolution, CancellationToken cancellationToken)
private void TryNavigateToLocationOrStartRenameSession(Workspace workspace, Solution oldSolution, Solution newSolution, CancellationToken cancellationToken)
{
var changedDocuments = newSolution.GetChangedDocuments(oldSolution);
foreach (var documentId in changedDocuments)
Expand All @@ -272,9 +272,16 @@ private void TryStartRenameSession(Workspace workspace, Solution oldSolution, So

var root = document.GetSyntaxRootSynchronously(cancellationToken);

var renameTokenOpt = root.GetAnnotatedNodesAndTokens(RenameAnnotation.Kind)
.Where(s => s.IsToken)
.Select(s => s.AsToken())
var navigationTokenOpt = root.GetAnnotatedTokens(NavigationAnnotation.Kind)
.FirstOrNullable();
if (navigationTokenOpt.HasValue)
{
var navigationService = workspace.Services.GetService<IDocumentNavigationService>();
navigationService.TryNavigateToPosition(workspace, documentId, navigationTokenOpt.Value.SpanStart);
return;
}

var renameTokenOpt = root.GetAnnotatedTokens(RenameAnnotation.Kind)
.FirstOrNullable();

if (renameTokenOpt.HasValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.ChangeSignature;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Notification;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
using Xunit;

Expand Down Expand Up @@ -39,9 +40,11 @@ public async Task TestChangeSignatureViaCodeActionAsync(

var codeIssueOrRefactoring = await GetCodeRefactoringAsync(workspace, testOptions);
await TestActionsAsync(workspace, expectedCode, index, codeIssueOrRefactoring.Actions,
conflictSpans: ImmutableArray<Text.TextSpan>.Empty,
renameSpans: ImmutableArray<Text.TextSpan>.Empty,
warningSpans: ImmutableArray<Text.TextSpan>.Empty, ignoreTrivia: true);
conflictSpans: ImmutableArray<TextSpan>.Empty,
renameSpans: ImmutableArray<TextSpan>.Empty,
warningSpans: ImmutableArray<TextSpan>.Empty,
navigationSpans: ImmutableArray<TextSpan>.Empty,
ignoreTrivia: true);
}
}
else
Expand Down
Loading