Skip to content

Commit

Permalink
Merge pull request #1448 from OmniSharp/feature/3.1
Browse files Browse the repository at this point in the history
Updated to Roslyn 3.1
  • Loading branch information
Ravi Chande authored Apr 10, 2019
2 parents 1834ea6 + 39862c2 commit 4ffabfa
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 74 deletions.
2 changes: 1 addition & 1 deletion build/Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<MSBuildPackageVersion>16.0.461</MSBuildPackageVersion>
<NuGetPackageVersion>5.0.0-rtm.5856</NuGetPackageVersion>
<RoslynPackageVersion>3.0.0-beta4-19126-05</RoslynPackageVersion>
<RoslynPackageVersion>3.1.0-beta2-19205-01</RoslynPackageVersion>
<XunitPackageVersion>2.4.0</XunitPackageVersion>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/OmniSharp.Abstractions/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ internal static class Configuration
{
public static bool ZeroBasedIndices = false;

public const string RoslynVersion = "3.0.0.0";
public const string RoslynVersion = "3.1.0.0";
public const string RoslynPublicKeyToken = "31bf3856ad364e35";

public readonly static string RoslynFeatures = GetRoslynAssemblyFullName("Microsoft.CodeAnalysis.Features");
Expand Down
6 changes: 3 additions & 3 deletions src/OmniSharp.Http.Driver/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.CSharp" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.Workspaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"/>
</dependentAssembly>

<dependentAssembly>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public class RunCodeActionService : BaseCodeActionService<RunCodeActionRequest,
{
private readonly IAssemblyLoader _loader;
private readonly Lazy<Assembly> _workspaceAssembly;
private readonly Lazy<Type> _renameDocumentOperation;
private readonly Lazy<FieldInfo> _oldDocumentId;
private readonly Lazy<FieldInfo> _newDocumentId;
private readonly Lazy<FieldInfo> _newFileName;

private const string RenameDocumentOperation = "Microsoft.CodeAnalysis.CodeActions.RenameDocumentOperation";

Expand All @@ -48,10 +44,6 @@ public RunCodeActionService(
{
_loader = loader;
_workspaceAssembly = _loader.LazyLoad(Configuration.RoslynWorkspaces);
_renameDocumentOperation = _workspaceAssembly.LazyGetType(RenameDocumentOperation);
_oldDocumentId = _renameDocumentOperation.LazyGetField("_oldDocumentId", BindingFlags.NonPublic | BindingFlags.Instance);
_newDocumentId = _renameDocumentOperation.LazyGetField("_newDocumentId", BindingFlags.NonPublic | BindingFlags.Instance);
_newFileName = _renameDocumentOperation.LazyGetField("_newFileName", BindingFlags.NonPublic | BindingFlags.Instance);
}

public override async Task<RunCodeActionResponse> Handle(RunCodeActionRequest request)
Expand All @@ -75,24 +67,15 @@ public override async Task<RunCodeActionResponse> Handle(RunCodeActionRequest re
{
if (o is ApplyChangesOperation applyChangesOperation)
{
var fileChanges = await GetFileChangesAsync(applyChangesOperation.ChangedSolution, solution, directory, request.WantsTextChanges);
var fileChangesResult = await GetFileChangesAsync(applyChangesOperation.ChangedSolution, solution, directory, request.WantsTextChanges, request.WantsAllCodeActionOperations);

changes.AddRange(fileChanges);
solution = this.Workspace.CurrentSolution;
changes.AddRange(fileChangesResult.FileChanges);
solution = fileChangesResult.Solution;
}

if (request.WantsAllCodeActionOperations)
{
if (IsRenameDocumentOperation(o, out var originalDocumentId, out var newDocumentId, out var newFileName))
{
var originalDocument = solution.GetDocument(originalDocumentId);
string newFilePath = GetNewFilePath(newFileName, originalDocument.FilePath);
var text = await originalDocument.GetTextAsync();
var temp = solution.RemoveDocument(originalDocumentId);
solution = temp.AddDocument(newDocumentId, newFileName, text, originalDocument.Folders, newFilePath);
changes.Add(new RenamedFileResponse(originalDocument.FilePath, newFilePath));
}
else if (o is OpenDocumentOperation openDocumentOperation)
if (o is OpenDocumentOperation openDocumentOperation)
{
var document = solution.GetDocument(openDocumentOperation.DocumentId);
changes.Add(new OpenFileResponse(document.FilePath));
Expand All @@ -112,31 +95,10 @@ public override async Task<RunCodeActionResponse> Handle(RunCodeActionRequest re
};
}

private static string GetNewFilePath(string newFileName, string currentFilePath)
{
var directory = Path.GetDirectoryName(currentFilePath);
return Path.Combine(directory, newFileName);
}

bool IsRenameDocumentOperation(CodeActionOperation o, out DocumentId oldDocumentId, out DocumentId newDocumentId, out string name)
private async Task<(Solution Solution, IEnumerable<FileOperationResponse> FileChanges)> GetFileChangesAsync(Solution newSolution, Solution oldSolution, string directory, bool wantTextChanges, bool wantsAllCodeActionOperations)
{
if (o.GetType() == _renameDocumentOperation.Value)
{
oldDocumentId = _oldDocumentId.GetValue<DocumentId>(o);
newDocumentId = _newDocumentId.GetValue<DocumentId>(o);
name = _newFileName.GetValue<string>(o);
return true;
}

oldDocumentId = default(DocumentId);
newDocumentId = default(DocumentId);
name = null;
return false;
}

private async Task<IEnumerable<ModifiedFileResponse>> GetFileChangesAsync(Solution newSolution, Solution oldSolution, string directory, bool wantTextChanges)
{
var filePathToResponseMap = new Dictionary<string, ModifiedFileResponse>();
var solution = oldSolution;
var filePathToResponseMap = new Dictionary<string, FileOperationResponse>();
var solutionChanges = newSolution.GetChanges(oldSolution);

foreach (var projectChange in solutionChanges.GetProjectChanges())
Expand Down Expand Up @@ -185,6 +147,7 @@ private async Task<IEnumerable<ModifiedFileResponse>> GetFileChangesAsync(Soluti
}

this.Workspace.AddDocument(documentId, projectChange.ProjectId, newFilePath, newDocument.SourceCodeKind);
solution = this.Workspace.CurrentSolution;
}
else
{
Expand All @@ -197,32 +160,57 @@ private async Task<IEnumerable<ModifiedFileResponse>> GetFileChangesAsync(Soluti
foreach (var documentId in projectChange.GetChangedDocuments())
{
var newDocument = newSolution.GetDocument(documentId);
var oldDocument = oldSolution.GetDocument(documentId);
var filePath = newDocument.FilePath;

if (!filePathToResponseMap.TryGetValue(filePath, out var modifiedFileResponse))
// file rename
if (oldDocument != null && newDocument.Name != oldDocument.Name)
{
modifiedFileResponse = new ModifiedFileResponse(filePath);
filePathToResponseMap[filePath] = modifiedFileResponse;
if (wantsAllCodeActionOperations)
{
var newFilePath = GetNewFilePath(newDocument.Name, oldDocument.FilePath);
var text = await oldDocument.GetTextAsync();
var temp = solution.RemoveDocument(documentId);
solution = temp.AddDocument(DocumentId.CreateNewId(oldDocument.Project.Id, newDocument.Name), newDocument.Name, text, oldDocument.Folders, newFilePath);

filePathToResponseMap[filePath] = new RenamedFileResponse(oldDocument.FilePath, newFilePath);
filePathToResponseMap[newFilePath] = new OpenFileResponse(newFilePath);
}
continue;
}

if (wantTextChanges)
if (!filePathToResponseMap.TryGetValue(filePath, out var fileOperationResponse))
{
var oldDocument = oldSolution.GetDocument(documentId);
var linePositionSpanTextChanges = await TextChanges.GetAsync(newDocument, oldDocument);

modifiedFileResponse.Changes = modifiedFileResponse.Changes != null
? modifiedFileResponse.Changes.Union(linePositionSpanTextChanges)
: linePositionSpanTextChanges;
fileOperationResponse = new ModifiedFileResponse(filePath);
filePathToResponseMap[filePath] = fileOperationResponse;
}
else

if (fileOperationResponse is ModifiedFileResponse modifiedFileResponse)
{
var text = await newDocument.GetTextAsync();
modifiedFileResponse.Buffer = text.ToString();
if (wantTextChanges)
{
var linePositionSpanTextChanges = await TextChanges.GetAsync(newDocument, oldDocument);

modifiedFileResponse.Changes = modifiedFileResponse.Changes != null
? modifiedFileResponse.Changes.Union(linePositionSpanTextChanges)
: linePositionSpanTextChanges;
}
else
{
var text = await newDocument.GetTextAsync();
modifiedFileResponse.Buffer = text.ToString();
}
}
}
}

return filePathToResponseMap.Values;
return (solution, filePathToResponseMap.Values);
}

private static string GetNewFilePath(string newFileName, string currentFilePath)
{
var directory = Path.GetDirectoryName(currentFilePath);
return Path.Combine(directory, newFileName);
}
}
}
}
2 changes: 1 addition & 1 deletion src/OmniSharp.Roslyn/MetadataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public string GetSymbolName(ISymbol symbol)
var service = _csharpMetadataAsSourceService.CreateInstance(temporaryDocument.Project.LanguageServices);
var method = _csharpMetadataAsSourceService.GetMethod(AddSourceToAsync);

var documentTask = method.Invoke<Task<Document>>(service, new object[] { temporaryDocument, topLevelSymbol, default(CancellationToken) });
var documentTask = method.Invoke<Task<Document>>(service, new object[] { temporaryDocument, await metadataProject.GetCompilationAsync(), topLevelSymbol, default(CancellationToken) });
metadataDocument = await documentTask;

_metadataDocumentCache[fileName] = metadataDocument;
Expand Down
2 changes: 1 addition & 1 deletion src/OmniSharp.Script/ScriptProjectProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ScriptProjectProvider
"System.Threading.Tasks"
};

private static readonly CSharpParseOptions ParseOptions = new CSharpParseOptions(LanguageVersion.Latest, DocumentationMode.Parse, SourceCodeKind.Script);
private static readonly CSharpParseOptions ParseOptions = new CSharpParseOptions(LanguageVersion.CSharp8, DocumentationMode.Parse, SourceCodeKind.Script);

private readonly Lazy<CSharpCompilationOptions> _compilationOptions;
private readonly Lazy<CSharpCommandLineArguments> _commandLineArgs;
Expand Down
6 changes: 3 additions & 3 deletions src/OmniSharp.Stdio.Driver/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.CSharp" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.Workspaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"/>
</dependentAssembly>

<dependentAssembly>
Expand Down
4 changes: 3 additions & 1 deletion tests/OmniSharp.Roslyn.CSharp.Tests/CodeActionsV2Facts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ public async Task Can_send_rename_and_fileOpen_responses_when_codeAction_renames
Assert.Equal(2, changes.Length);
Assert.Equal(FileModificationType.Renamed, changes[0].ModificationType);
Assert.Contains("Class1.cs", ((RenamedFileResponse)changes[0]).NewFileName);
Assert.False(File.Exists(((RenamedFileResponse)changes[0]).FileName), "The old renamed file exists - even though it should not.");
Assert.True(File.Exists(((RenamedFileResponse)changes[0]).NewFileName), "The new renamed file doesn't exist - even though it should.");
Assert.Equal(FileModificationType.Opened, changes[1].ModificationType);
}
}
Expand Down Expand Up @@ -353,4 +355,4 @@ private static Range GetSelection(TextRange range)
};
}
}
}
}
6 changes: 3 additions & 3 deletions tests/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.CSharp" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.Workspaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"/>
</dependentAssembly>

<dependentAssembly>
Expand Down

0 comments on commit 4ffabfa

Please sign in to comment.