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

replaced InteractiveScriptGlobals with CommandLineScriptGlobals as CSX host object type #846

Merged
merged 6 commits into from
May 4, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/OmniSharp.Script/ScriptContextModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public ScriptContextModel(string csxPath, ProjectInfo project, HashSet<string> i
{
Path = csxPath;
ImplicitAssemblyReferences = implicitAssemblyReferences;
CommonUsings = ScriptProjectSystem.DefaultNamespaces;
CommonUsings = ScriptHelper.DefaultNamespaces;
GlobalsType = project.HostObjectType;
}

Expand Down
79 changes: 79 additions & 0 deletions src/OmniSharp.Script/ScriptHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.Scripting.Hosting;

namespace OmniSharp.Script
{
public static class ScriptHelper
{
// aligned with CSI.exe
// https://github.com/dotnet/roslyn/blob/version-2.0.0-rc3/src/Interactive/csi/csi.rsp
internal static readonly IEnumerable<string> DefaultNamespaces = new[]
{
"System",
"System.IO",
"System.Collections.Generic",
"System.Console",
"System.Diagnostics",
"System.Dynamic",
"System.Linq",
"System.Linq.Expressions",
"System.Text",
"System.Threading.Tasks"
};

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

private static readonly Lazy<CSharpCompilationOptions> CompilationOptions = new Lazy<CSharpCompilationOptions>(() =>
{
var compilationOptions = new CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary,
usings: DefaultNamespaces,
allowUnsafe: true,
metadataReferenceResolver: new CachingScriptMetadataResolver(),
sourceReferenceResolver: ScriptSourceResolver.Default,
assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default).
WithSpecificDiagnosticOptions(new Dictionary<string, ReportDiagnostic>
{
// ensure that specific warnings about assembly references are always suppressed
// https://github.com/dotnet/roslyn/issues/5501
{ "CS1701", ReportDiagnostic.Suppress },
{ "CS1702", ReportDiagnostic.Suppress },
{ "CS1705", ReportDiagnostic.Suppress }
});

var topLevelBinderFlagsProperty = typeof(CSharpCompilationOptions).GetProperty("TopLevelBinderFlags", BindingFlags.Instance | BindingFlags.NonPublic);
var binderFlagsType = typeof(CSharpCompilationOptions).GetTypeInfo().Assembly.GetType("Microsoft.CodeAnalysis.CSharp.BinderFlags");

var ignoreCorLibraryDuplicatedTypesMember = binderFlagsType?.GetField("IgnoreCorLibraryDuplicatedTypes", BindingFlags.Static | BindingFlags.Public);
var ignoreCorLibraryDuplicatedTypesValue = ignoreCorLibraryDuplicatedTypesMember?.GetValue(null);
if (ignoreCorLibraryDuplicatedTypesValue != null)
{
topLevelBinderFlagsProperty?.SetValue(compilationOptions, ignoreCorLibraryDuplicatedTypesValue);
}

return compilationOptions;
});

public static ProjectInfo CreateProject(string csxFileName, IEnumerable<MetadataReference> references, IEnumerable<string> namespaces = null)
{
var project = ProjectInfo.Create(
id: ProjectId.CreateNewId(),
version: VersionStamp.Create(),
name: csxFileName,
assemblyName: $"{csxFileName}.dll",
language: LanguageNames.CSharp,
compilationOptions: namespaces == null ? CompilationOptions.Value : CompilationOptions.Value.WithUsings(namespaces),
metadataReferences: references,
parseOptions: ParseOptions,
isSubmission: true,
hostObjectType: typeof(CommandLineScriptGlobals));

return project;
}
}
}
63 changes: 1 addition & 62 deletions src/OmniSharp.Script/ScriptProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.Scripting.Hosting;
using Microsoft.DotNet.ProjectModel;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyModel;
Expand All @@ -21,56 +19,7 @@ namespace OmniSharp.Script
[Export(typeof(IProjectSystem)), Shared]
public class ScriptProjectSystem : IProjectSystem
{
// aligned with CSI.exe
// https://github.com/dotnet/roslyn/blob/version-2.0.0-rc3/src/Interactive/csi/csi.rsp
internal static readonly IEnumerable<string> DefaultNamespaces = new[]
{
"System",
"System.IO",
"System.Collections.Generic",
"System.Console",
"System.Diagnostics",
"System.Dynamic",
"System.Linq",
"System.Linq.Expressions",
"System.Text",
"System.Threading.Tasks"
};

private const string CsxExtension = ".csx";
private static readonly CSharpParseOptions ParseOptions = new CSharpParseOptions(LanguageVersion.Default, DocumentationMode.Parse, SourceCodeKind.Script);

private static readonly Lazy<CSharpCompilationOptions> CompilationOptions = new Lazy<CSharpCompilationOptions>(() =>
{
var compilationOptions = new CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary,
usings: DefaultNamespaces,
allowUnsafe: true,
metadataReferenceResolver: new CachingScriptMetadataResolver(),
sourceReferenceResolver: ScriptSourceResolver.Default,
assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default).
WithSpecificDiagnosticOptions(new Dictionary<string, ReportDiagnostic>
{
// ensure that specific warnings about assembly references are always suppressed
// https://github.com/dotnet/roslyn/issues/5501
{ "CS1701", ReportDiagnostic.Suppress },
{ "CS1702", ReportDiagnostic.Suppress },
{ "CS1705", ReportDiagnostic.Suppress }
});

var topLevelBinderFlagsProperty = typeof(CSharpCompilationOptions).GetProperty("TopLevelBinderFlags", BindingFlags.Instance | BindingFlags.NonPublic);
var binderFlagsType = typeof(CSharpCompilationOptions).GetTypeInfo().Assembly.GetType("Microsoft.CodeAnalysis.CSharp.BinderFlags");

var ignoreCorLibraryDuplicatedTypesMember = binderFlagsType?.GetField("IgnoreCorLibraryDuplicatedTypes", BindingFlags.Static | BindingFlags.Public);
var ignoreCorLibraryDuplicatedTypesValue = ignoreCorLibraryDuplicatedTypesMember?.GetValue(null);
if (ignoreCorLibraryDuplicatedTypesValue != null)
{
topLevelBinderFlagsProperty?.SetValue(compilationOptions, ignoreCorLibraryDuplicatedTypesValue);
}

return compilationOptions;
});

private readonly MetadataFileReferenceCache _metadataFileReferenceCache;

// used for tracking purposes only
Expand Down Expand Up @@ -175,17 +124,7 @@ public void Initalize(IConfiguration configuration)
try
{
var csxFileName = Path.GetFileName(csxPath);
var project = ProjectInfo.Create(
id: ProjectId.CreateNewId(),
version: VersionStamp.Create(),
name: csxFileName,
assemblyName: $"{csxFileName}.dll",
language: LanguageNames.CSharp,
compilationOptions: CompilationOptions.Value,
metadataReferences: commonReferences,
parseOptions: ParseOptions,
isSubmission: true,
hostObjectType: typeof(InteractiveScriptGlobals));
var project = ScriptHelper.CreateProject(csxFileName, commonReferences);

// add CSX project to workspace
_workspace.AddProject(project);
Expand Down
10 changes: 10 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,16 @@ public class MyClass1 {
ContainsCompletions(completions.Select(c => c.CompletionText).Take(1), "MyClass1");
}

[Fact]
public async Task Returns_host_object_members_in_csx()
{
const string source =
"Prin$$";

var completions = await FindCompletionsAsync("dummy.csx", source);
ContainsCompletions(completions.Select(c => c.CompletionText), new[] { "Print", "PrintOptions" });
Copy link
Member Author

Choose a reason for hiding this comment

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

note that we are ahead of VS 😃
dotnet/roslyn#13886

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, VS Code scripting support is well ahead of VS. You're doing great work! 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks! 😄

}

private void ContainsCompletions(IEnumerable<string> completions, params string[] expected)
{
if (!completions.SequenceEqual(expected))
Expand Down
24 changes: 5 additions & 19 deletions tests/TestUtility/TestHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Scripting.Hosting;
using OmniSharp;
using OmniSharp.Script;
using OmniSharp.Services;

namespace TestUtility
Expand All @@ -20,31 +22,15 @@ public static OmniSharpWorkspace CreateCsxWorkspace(TestFile testFile)
public static void AddCsxProjectToWorkspace(OmniSharpWorkspace workspace, TestFile testFile)
{
var references = GetReferences();
var parseOptions = new CSharpParseOptions(
LanguageVersion.Default,
DocumentationMode.Parse,
SourceCodeKind.Script);

var project = ProjectInfo.Create(
id: ProjectId.CreateNewId(),
version: VersionStamp.Create(),
name: testFile.FileName,
assemblyName: $"{testFile.FileName}.dll",
language: LanguageNames.CSharp,
filePath: testFile.FileName,
compilationOptions: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
metadataReferences: references,
parseOptions: parseOptions,
isSubmission: true);

var project = ScriptHelper.CreateProject(testFile.FileName, references.Union(new[] { MetadataReference.CreateFromFile(typeof(CommandLineScriptGlobals).GetTypeInfo().Assembly.Location) }), Enumerable.Empty<string>());
workspace.AddProject(project);

var documentInfo = DocumentInfo.Create(
id: DocumentId.CreateNewId(project.Id),
name: testFile.FileName,
sourceCodeKind: SourceCodeKind.Script,
loader: TextLoader.From(TextAndVersion.Create(testFile.Content.Text, VersionStamp.Create())),
filePath: testFile.FileName);

workspace.AddDocument(documentInfo);
}

Expand Down
1 change: 1 addition & 0 deletions tests/TestUtility/TestUtility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ProjectReference Include="..\..\src\OmniSharp.Host\OmniSharp.Host.csproj" />
<ProjectReference Include="..\..\src\OmniSharp.MSBuild\OmniSharp.MSBuild.csproj" />
<ProjectReference Include="..\..\src\OmniSharp.Roslyn.CSharp\OmniSharp.Roslyn.CSharp.csproj" />
<ProjectReference Include="..\..\src\OmniSharp.Script\OmniSharp.Script.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down