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 generate file #1143

Merged
merged 10 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -73,7 +73,7 @@ public override async Task<RunCodeActionResponse> 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)
Expand Down Expand Up @@ -179,7 +179,7 @@ private async Task<IEnumerable<ModifiedFileResponse>> GetFileChangesAsync(Soluti
}
}

this.Workspace.AddDocument(projectChange.ProjectId, newFilePath, newDocument.SourceCodeKind);
this.Workspace.AddDocument(documentId, projectChange.ProjectId, newFilePath, newDocument.SourceCodeKind);
}
else
{
Expand Down
6 changes: 6 additions & 0 deletions src/OmniSharp.Roslyn/OmniSharpWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
21 changes: 21 additions & 0 deletions test-assets/test-projects/ProjectWithMissingType/HelloWorld.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll probably want to add this to build.json so it gets restored and built before the test runs: https://github.com/OmniSharp/omnisharp-roslyn/blob/master/build.json#L40.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do. Thanks!


<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<LangVersion>7.1</LangVersion>
</PropertyGroup>

<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App">
<Version>1.0.1</Version>
</PackageReference>
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions test-assets/test-projects/ProjectWithMissingType/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Z x = null;
}
}
}
32 changes: 30 additions & 2 deletions tests/OmniSharp.Roslyn.CSharp.Tests/CodeActionsV2Facts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public void Whatever()
[|Console.Write(""should be using System;"");|]
}
}";

const string expected =
@"public class Class1
{
Expand All @@ -153,11 +152,40 @@ 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<RunCodeActionService>(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.NotNull(changes[1].FileName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you also assert that the file was generated on disk?

}
}

[Fact]
public async Task Can_send_rename_and_fileOpen_responses_when_codeAction_renames_file()
{
Expand Down