提交 ba4923f5 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #14111 from CyrusNajmabadi/moveTypeWithFolders

MoveFile moves existing file to root of project when renaming them.
......@@ -54,7 +54,7 @@ class Class2 { }";
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveType)]
[WorkItem(14008, "https://github.com/dotnet/roslyn/issues/14008")]
public async Task TestFolders()
public async Task TestMoveToNewFileWithFolders()
{
var code =
@"
......
......@@ -33,6 +33,28 @@ class Inner { }
await TestRenameFileToMatchTypeAsync(code, expectedDocumentName);
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveType)]
public async Task TestRenameFileWithFolders()
{
var code =
@"
<Workspace>
<Project Language=""C#"" AssemblyName=""Assembly1"" CommonReferences=""true"">
<Document Folders=""A\B"">
[||]class Class1
{
class Inner { }
}
</Document>
</Project>
</Workspace>";
var expectedDocumentName = "Class1.cs";
await TestRenameFileToMatchTypeAsync(code, expectedDocumentName,
destinationDocumentContainers: new[] { "A", "B" });
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveType)]
public async Task TestMissing_TypeNameMatchesFileName_RenameFile()
{
......
......@@ -73,7 +73,8 @@ protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspa
string originalCode,
string expectedDocumentName = null,
bool expectedCodeAction = true,
bool compareTokens = true)
bool compareTokens = true,
IList<string> destinationDocumentContainers = null)
{
using (var workspace = await CreateWorkspaceFromFileAsync(originalCode, parseOptions: null, compilationOptions: null))
{
......@@ -82,10 +83,8 @@ protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspa
Assert.True(expectedDocumentName != null, $"{nameof(expectedDocumentName)} should be present if {nameof(expectedCodeAction)} is true.");
var oldDocumentId = workspace.Documents[0].Id;
string expectedText;
IList<TextSpan> spans;
MarkupTestFile.GetSpans(originalCode, out expectedText, out spans);
var expectedText = workspace.Documents[0].TextBuffer.CurrentSnapshot.GetText();
var spans = workspace.Documents[0].SelectedSpans;
var codeActionTitle = string.Format(RenameFileCodeActionTitle, expectedDocumentName);
......@@ -96,6 +95,12 @@ protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspa
// the original source document does not exist in the new solution.
var newSolution = oldSolutionAndNewSolution.Item2;
Assert.Null(newSolution.GetDocument(oldDocumentId));
if (destinationDocumentContainers != null)
{
var newDocument = newSolution.Projects.First().Documents.First();
Assert.Equal(destinationDocumentContainers, newDocument.Folders);
}
}
else
{
......
......@@ -26,16 +26,17 @@ internal override Task<IEnumerable<CodeActionOperation>> GetOperationsAsync()
/// </summary>
private IEnumerable<CodeActionOperation> RenameFileToMatchTypeName()
{
var solution = SemanticDocument.Document.Project.Solution;
var oldDocument = SemanticDocument.Document;
var solution = oldDocument.Project.Solution;
var text = SemanticDocument.Text;
var oldDocumentId = SemanticDocument.Document.Id;
var newDocumentId = DocumentId.CreateNewId(SemanticDocument.Document.Project.Id, FileName);
var oldDocumentId = oldDocument.Id;
var newDocumentId = DocumentId.CreateNewId(oldDocument.Project.Id, FileName);
// currently, document rename is accomplished by a remove followed by an add.
// the workspace takes care of resolving conflicts if the document name is not unique in the project
// by adding numeric suffixes to the new document being added.
var newSolution = solution.RemoveDocument(oldDocumentId);
newSolution = newSolution.AddDocument(newDocumentId, FileName, text);
newSolution = newSolution.AddDocument(newDocumentId, FileName, text, oldDocument.Folders);
return new CodeActionOperation[]
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册