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

Fix move type for top level statements #54421

Merged
merged 1 commit into from
Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1408,5 +1408,51 @@ record CacheContext(String Message);

await TestMoveTypeToNewFileAsync(code, codeAfterMove, expectedDocumentName, destinationDocumentText);
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveType)]
public async Task MoveClassInTopLevelStatements()
{
var code = @"
using ConsoleApp1;
using System;

var c = new C();
Console.WriteLine(c.Hello);

class [||]C
{
public string Hello => ""Hello"";
}";

var codeAfterMove = @"
using ConsoleApp1;
using System;

var c = new C();
Console.WriteLine(c.Hello);
";

var expectedDocumentName = "C.cs";
var destinationDocumentText = @"class C
{
public string Hello => ""Hello"";
}";

await TestMoveTypeToNewFileAsync(code, codeAfterMove, expectedDocumentName, destinationDocumentText);
}

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveType)]
public async Task MissingInTopLevelStatementsOnly()
{
var code = @"
using ConsoleApp1;
using System;

var c = new object();
[||]Console.WriteLine(c.ToString());
";

await TestMissingAsync(code);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

Expand Down Expand Up @@ -107,6 +109,11 @@ private ImmutableArray<CodeAction> CreateActions(State state, CancellationToken
var manyTypes = MultipleTopLevelTypeDeclarationInSourceDocument(state.SemanticDocument.Root);
var isNestedType = IsNestedType(state.TypeNode);

var syntaxFacts = state.SemanticDocument.Document.GetRequiredLanguageService<ISyntaxFactsService>();
var isClassNextToGlobalStatements = manyTypes
? false
: ClassNextToGlobalStatements(state.SemanticDocument.Root, syntaxFacts);

var suggestedFileNames = GetSuggestedFileNames(
state.TypeNode,
isNestedType,
Expand All @@ -120,7 +127,9 @@ private ImmutableArray<CodeAction> CreateActions(State state, CancellationToken
// case 2: This is a nested type, offer to move to new file.
// case 3: If there is a single type decl in current file, *do not* offer move to new file,
// rename actions are sufficient in this case.
if (manyTypes || isNestedType)
// case 4: If there are top level statements(Global statements) offer to move even
// in cases where there are only one class in the file.
if (manyTypes || isNestedType || isClassNextToGlobalStatements)
{
foreach (var fileName in suggestedFileNames)
{
Expand Down Expand Up @@ -152,6 +161,9 @@ private ImmutableArray<CodeAction> CreateActions(State state, CancellationToken
return actions.ToImmutable();
}

private static bool ClassNextToGlobalStatements(SyntaxNode root, ISyntaxFactsService syntaxFacts)
=> syntaxFacts.ContainsGlobalStatement(root);

private CodeAction GetCodeAction(State state, string fileName, MoveTypeOperationKind operationKind) =>
new MoveTypeCodeAction((TService)this, state, operationKind, fileName);

Expand Down