diff --git a/build.cake b/build.cake index 0623dfff6b..9513362463 100644 --- a/build.cake +++ b/build.cake @@ -433,6 +433,22 @@ Task("PrepareTestAssets:CommonTestAssets") }); }); +Task("PrepareTestAssets:TestAssetsWithErrors") + .IsDependeeOf("PrepareTestAssets") + .DoesForEach(buildPlan.TestAssetsWithErrors, (project) => + { + Information("Restoring and building: {0}...", project); + + var folder = CombinePaths(env.Folders.TestAssets, "test-projects", project); + + DotNetCoreRestore(new DotNetCoreRestoreSettings() + { + ToolPath = env.DotNetCommand, + WorkingDirectory = folder, + Verbosity = DotNetCoreVerbosity.Minimal, + }); + }); + Task("PrepareTestAssets:WindowsTestAssets") .WithCriteria(Platform.Current.IsWindows) .IsDependeeOf("PrepareTestAssets") diff --git a/build.json b/build.json index d5ec7b24df..602ff4ca75 100644 --- a/build.json +++ b/build.json @@ -50,5 +50,8 @@ ], "WindowsOnlyTestAssets": [ "AntlrGeneratedFiles" + ], + "TestAssetsWithErrors":[ + "ProjectWithMissingType" ] } diff --git a/scripts/common.cake b/scripts/common.cake index 1023860523..7a9e246b64 100644 --- a/scripts/common.cake +++ b/scripts/common.cake @@ -281,6 +281,7 @@ public class BuildPlan public string[] LegacyTestAssets { get; set; } public string[] CakeTestAssets { get; set; } public string[] WindowsOnlyTestAssets { get; set; } + public string[] TestAssetsWithErrors { get; set; } public static BuildPlan Load(BuildEnvironment env) { diff --git a/src/OmniSharp.Roslyn.CSharp/Services/Refactoring/V2/RunCodeActionService.cs b/src/OmniSharp.Roslyn.CSharp/Services/Refactoring/V2/RunCodeActionService.cs index 2c0060cc13..28caaa1283 100644 --- a/src/OmniSharp.Roslyn.CSharp/Services/Refactoring/V2/RunCodeActionService.cs +++ b/src/OmniSharp.Roslyn.CSharp/Services/Refactoring/V2/RunCodeActionService.cs @@ -73,7 +73,7 @@ public override async Task Handle(RunCodeActionRequest re var fileChanges = await GetFileChangesAsync(applyChangesOperation.ChangedSolution, solution, directory, request.WantsTextChanges); changes.AddRange(fileChanges); - solution = applyChangesOperation.ChangedSolution; + solution = this.Workspace.CurrentSolution; } if (request.WantsAllCodeActionOperations) @@ -179,7 +179,7 @@ private async Task> GetFileChangesAsync(Soluti } } - this.Workspace.AddDocument(projectChange.ProjectId, newFilePath, newDocument.SourceCodeKind); + this.Workspace.AddDocument(documentId, projectChange.ProjectId, newFilePath, newDocument.SourceCodeKind); } else { diff --git a/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs b/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs index ef4236a1b7..d0e371ca5e 100644 --- a/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs +++ b/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs @@ -86,6 +86,12 @@ public void AddDocument(DocumentInfo documentInfo) public DocumentId AddDocument(ProjectId projectId, string filePath, SourceCodeKind sourceCodeKind = SourceCodeKind.Regular) { var documentId = DocumentId.CreateNewId(projectId); + this.AddDocument(documentId, projectId, filePath, sourceCodeKind); + return documentId; + } + + public DocumentId AddDocument(DocumentId documentId, ProjectId projectId, string filePath, SourceCodeKind sourceCodeKind = SourceCodeKind.Regular) + { var loader = new OmniSharpTextLoader(filePath); var documentInfo = DocumentInfo.Create(documentId, filePath, filePath: filePath, loader: loader, sourceCodeKind: sourceCodeKind); diff --git a/test-assets/test-projects/ProjectWithMissingType/HelloWorld.csproj b/test-assets/test-projects/ProjectWithMissingType/HelloWorld.csproj new file mode 100644 index 0000000000..c0318399b6 --- /dev/null +++ b/test-assets/test-projects/ProjectWithMissingType/HelloWorld.csproj @@ -0,0 +1,21 @@ + + + + Exe + netcoreapp1.0 + false + 7.1 + + + + + + + + + + 1.0.1 + + + + \ No newline at end of file diff --git a/test-assets/test-projects/ProjectWithMissingType/Program.cs b/test-assets/test-projects/ProjectWithMissingType/Program.cs new file mode 100644 index 0000000000..c006e90928 --- /dev/null +++ b/test-assets/test-projects/ProjectWithMissingType/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace ConsoleApplication +{ + public class Program + { + public static void Main(string[] args) + { + Z x = null; + } + } +} \ No newline at end of file diff --git a/tests/OmniSharp.Roslyn.CSharp.Tests/CodeActionsV2Facts.cs b/tests/OmniSharp.Roslyn.CSharp.Tests/CodeActionsV2Facts.cs index e7073a43a8..9ded542c1b 100644 --- a/tests/OmniSharp.Roslyn.CSharp.Tests/CodeActionsV2Facts.cs +++ b/tests/OmniSharp.Roslyn.CSharp.Tests/CodeActionsV2Facts.cs @@ -139,7 +139,6 @@ public void Whatever() [|Console.Write(""should be using System;"");|] } }"; - const string expected = @"public class Class1 { @@ -153,11 +152,49 @@ private static void NewMethod() Console.Write(""should be using System;""); } }"; - var response = await RunRefactoringAsync(code, "Extract Method"); AssertIgnoringIndent(expected, ((ModifiedFileResponse)response.Changes.First()).Buffer); } + [Fact] + public async Task Can_generate_type_and_return_name_of_new_file() + { + using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithMissingType")) + using (var host = CreateOmniSharpHost(testProject.Directory)) + { + var requestHandler = host.GetRequestHandler(OmniSharpEndpoints.V2.RunCodeAction); + var document = host.Workspace.CurrentSolution.Projects.First().Documents.First(); + var buffer = await document.GetTextAsync(); + var path = document.FilePath; + + var request = new RunCodeActionRequest + { + Line = 8, + Column = 12, + FileName = path, + Buffer = buffer.ToString(), + Identifier = "Generate class 'Z' in new file", + WantsTextChanges = true, + WantsAllCodeActionOperations = true + }; + + var response = await requestHandler.Handle(request); + var changes = response.Changes.ToArray(); + Assert.Equal(2, changes.Length); + Assert.NotNull(changes[0].FileName); + + Assert.True(File.Exists(changes[0].FileName)); + Assert.Equal(@"namespace ConsoleApplication +{ + internal class Z + { + } +}".Replace("\r\n", "\n"), ((ModifiedFileResponse)changes[0]).Changes.First().NewText); + + Assert.NotNull(changes[1].FileName); + } + } + [Fact] public async Task Can_send_rename_and_fileOpen_responses_when_codeAction_renames_file() {