Skip to content

Commit

Permalink
Merge pull request #1143 from rchande/fixGenerateFile
Browse files Browse the repository at this point in the history
Fix generate file
  • Loading branch information
Ravi Chande authored Apr 6, 2018
2 parents c8e6dbc + 9818e23 commit bba24cb
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 4 deletions.
16 changes: 16 additions & 0 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions build.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@
],
"WindowsOnlyTestAssets": [
"AntlrGeneratedFiles"
],
"TestAssetsWithErrors":[
"ProjectWithMissingType"
]
}
1 change: 1 addition & 0 deletions scripts/common.cake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
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">

<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;
}
}
}
41 changes: 39 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,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<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.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()
{
Expand Down

0 comments on commit bba24cb

Please sign in to comment.