From ecc7a399f866164d0689d7912511040210543cb1 Mon Sep 17 00:00:00 2001 From: filipw Date: Tue, 2 May 2017 22:51:22 +0200 Subject: [PATCH 1/6] use CommandLineScriptGlobals as script host --- src/OmniSharp.Script/ScriptProjectSystem.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OmniSharp.Script/ScriptProjectSystem.cs b/src/OmniSharp.Script/ScriptProjectSystem.cs index 05223cadab..2847a0bc83 100644 --- a/src/OmniSharp.Script/ScriptProjectSystem.cs +++ b/src/OmniSharp.Script/ScriptProjectSystem.cs @@ -185,7 +185,7 @@ public void Initalize(IConfiguration configuration) metadataReferences: commonReferences, parseOptions: ParseOptions, isSubmission: true, - hostObjectType: typeof(InteractiveScriptGlobals)); + hostObjectType: typeof(CommandLineScriptGlobals)); // add CSX project to workspace _workspace.AddProject(project); From bd6a142ea6176d0e04873406a498f4023dc5b649 Mon Sep 17 00:00:00 2001 From: filipw Date: Tue, 2 May 2017 23:02:02 +0200 Subject: [PATCH 2/6] extracted ScriptHelper --- src/OmniSharp.Script/ScriptContextModel.cs | 2 +- src/OmniSharp.Script/ScriptHelper.cs | 79 +++++++++++++++++++++ src/OmniSharp.Script/ScriptProjectSystem.cs | 63 +--------------- 3 files changed, 81 insertions(+), 63 deletions(-) create mode 100644 src/OmniSharp.Script/ScriptHelper.cs diff --git a/src/OmniSharp.Script/ScriptContextModel.cs b/src/OmniSharp.Script/ScriptContextModel.cs index 188c59381a..7926bc4792 100644 --- a/src/OmniSharp.Script/ScriptContextModel.cs +++ b/src/OmniSharp.Script/ScriptContextModel.cs @@ -10,7 +10,7 @@ public ScriptContextModel(string csxPath, ProjectInfo project, HashSet i { Path = csxPath; ImplicitAssemblyReferences = implicitAssemblyReferences; - CommonUsings = ScriptProjectSystem.DefaultNamespaces; + CommonUsings = ScriptHelper.DefaultNamespaces; GlobalsType = project.HostObjectType; } diff --git a/src/OmniSharp.Script/ScriptHelper.cs b/src/OmniSharp.Script/ScriptHelper.cs new file mode 100644 index 0000000000..32de9afbb6 --- /dev/null +++ b/src/OmniSharp.Script/ScriptHelper.cs @@ -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 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 CompilationOptions = new Lazy(() => + { + var compilationOptions = new CSharpCompilationOptions( + OutputKind.DynamicallyLinkedLibrary, + usings: DefaultNamespaces, + allowUnsafe: true, + metadataReferenceResolver: new CachingScriptMetadataResolver(), + sourceReferenceResolver: ScriptSourceResolver.Default, + assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default). + WithSpecificDiagnosticOptions(new Dictionary + { + // 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 references) + { + var project = ProjectInfo.Create( + id: ProjectId.CreateNewId(), + version: VersionStamp.Create(), + name: csxFileName, + assemblyName: $"{csxFileName}.dll", + language: LanguageNames.CSharp, + compilationOptions: CompilationOptions.Value, + metadataReferences: references, + parseOptions: ParseOptions, + isSubmission: true, + hostObjectType: typeof(CommandLineScriptGlobals)); + + return project; + } + } +} diff --git a/src/OmniSharp.Script/ScriptProjectSystem.cs b/src/OmniSharp.Script/ScriptProjectSystem.cs index 2847a0bc83..4fc20ecbe8 100644 --- a/src/OmniSharp.Script/ScriptProjectSystem.cs +++ b/src/OmniSharp.Script/ScriptProjectSystem.cs @@ -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; @@ -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 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 CompilationOptions = new Lazy(() => - { - var compilationOptions = new CSharpCompilationOptions( - OutputKind.DynamicallyLinkedLibrary, - usings: DefaultNamespaces, - allowUnsafe: true, - metadataReferenceResolver: new CachingScriptMetadataResolver(), - sourceReferenceResolver: ScriptSourceResolver.Default, - assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default). - WithSpecificDiagnosticOptions(new Dictionary - { - // 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 @@ -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(CommandLineScriptGlobals)); + var project = ScriptHelper.CreateProject(csxFileName, commonReferences); // add CSX project to workspace _workspace.AddProject(project); From 5215799b38ec2657557347c6a0f253efd66c4e9c Mon Sep 17 00:00:00 2001 From: filipw Date: Tue, 2 May 2017 23:05:09 +0200 Subject: [PATCH 3/6] use ScriptHelper in tests --- tests/TestUtility/TestHelpers.cs | 18 ++---------------- tests/TestUtility/TestUtility.csproj | 1 + 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/tests/TestUtility/TestHelpers.cs b/tests/TestUtility/TestHelpers.cs index 28f4d7600a..227a4a19e9 100644 --- a/tests/TestUtility/TestHelpers.cs +++ b/tests/TestUtility/TestHelpers.cs @@ -4,6 +4,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using OmniSharp; +using OmniSharp.Script; using OmniSharp.Services; namespace TestUtility @@ -20,22 +21,7 @@ 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); workspace.AddProject(project); var documentInfo = DocumentInfo.Create( diff --git a/tests/TestUtility/TestUtility.csproj b/tests/TestUtility/TestUtility.csproj index b1c9dcf144..8af86a5ab8 100644 --- a/tests/TestUtility/TestUtility.csproj +++ b/tests/TestUtility/TestUtility.csproj @@ -15,6 +15,7 @@ + From 1141b3c37538e2cd0c05d7b3b550e9b932fa11e7 Mon Sep 17 00:00:00 2001 From: filipw Date: Tue, 2 May 2017 23:16:50 +0200 Subject: [PATCH 4/6] added host object test --- .../OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs | 10 ++++++++++ tests/TestUtility/TestHelpers.cs | 6 ++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs b/tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs index f224085fbe..24cf38bb64 100644 --- a/tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs +++ b/tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs @@ -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 = + "Pri$$"; + + var completions = await FindCompletionsAsync("dummy.csx", source); + ContainsCompletions(completions.Select(c => c.CompletionText), new[] { "Print", "PrintOptions" }); + } + private void ContainsCompletions(IEnumerable completions, params string[] expected) { if (!completions.SequenceEqual(expected)) diff --git a/tests/TestUtility/TestHelpers.cs b/tests/TestUtility/TestHelpers.cs index 227a4a19e9..bc10535557 100644 --- a/tests/TestUtility/TestHelpers.cs +++ b/tests/TestUtility/TestHelpers.cs @@ -20,17 +20,15 @@ public static OmniSharpWorkspace CreateCsxWorkspace(TestFile testFile) public static void AddCsxProjectToWorkspace(OmniSharpWorkspace workspace, TestFile testFile) { - var references = GetReferences(); - var project = ScriptHelper.CreateProject(testFile.FileName, references); - + var project = ScriptHelper.CreateProject(testFile.FileName, GetReferences()); 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); } From 172274b9b0f1dec8e9de58d818acd3468a652e2b Mon Sep 17 00:00:00 2001 From: filipw Date: Tue, 2 May 2017 23:35:44 +0200 Subject: [PATCH 5/6] fixed tests --- src/OmniSharp.Script/ScriptHelper.cs | 4 ++-- tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs | 2 +- tests/TestUtility/TestHelpers.cs | 9 ++++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/OmniSharp.Script/ScriptHelper.cs b/src/OmniSharp.Script/ScriptHelper.cs index 32de9afbb6..d6002b8e51 100644 --- a/src/OmniSharp.Script/ScriptHelper.cs +++ b/src/OmniSharp.Script/ScriptHelper.cs @@ -59,7 +59,7 @@ public static class ScriptHelper return compilationOptions; }); - public static ProjectInfo CreateProject(string csxFileName, IEnumerable references) + public static ProjectInfo CreateProject(string csxFileName, IEnumerable references, IEnumerable namespaces = null) { var project = ProjectInfo.Create( id: ProjectId.CreateNewId(), @@ -67,7 +67,7 @@ public static ProjectInfo CreateProject(string csxFileName, IEnumerable c.CompletionText), new[] { "Print", "PrintOptions" }); diff --git a/tests/TestUtility/TestHelpers.cs b/tests/TestUtility/TestHelpers.cs index bc10535557..1782faac13 100644 --- a/tests/TestUtility/TestHelpers.cs +++ b/tests/TestUtility/TestHelpers.cs @@ -1,12 +1,14 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using OmniSharp; +using Microsoft.CodeAnalysis.Scripting.Hosting;using OmniSharp; using OmniSharp.Script; using OmniSharp.Services; + + namespace TestUtility { public static class TestHelpers @@ -20,7 +22,8 @@ public static OmniSharpWorkspace CreateCsxWorkspace(TestFile testFile) public static void AddCsxProjectToWorkspace(OmniSharpWorkspace workspace, TestFile testFile) { - var project = ScriptHelper.CreateProject(testFile.FileName, GetReferences()); + var references = GetReferences(); + var project = ScriptHelper.CreateProject(testFile.FileName, references.Union(new[] { MetadataReference.CreateFromFile(typeof(CommandLineScriptGlobals).GetTypeInfo().Assembly.Location) }), Enumerable.Empty()); workspace.AddProject(project); var documentInfo = DocumentInfo.Create( From 7e606d45545ad0606fdaa50493d21a0daf808198 Mon Sep 17 00:00:00 2001 From: Filip W Date: Wed, 3 May 2017 00:05:55 +0200 Subject: [PATCH 6/6] usings formatting --- tests/TestUtility/TestHelpers.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/TestUtility/TestHelpers.cs b/tests/TestUtility/TestHelpers.cs index 1782faac13..aa4dad6d8b 100644 --- a/tests/TestUtility/TestHelpers.cs +++ b/tests/TestUtility/TestHelpers.cs @@ -3,12 +3,11 @@ using System.Linq; using System.Reflection; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Scripting.Hosting;using OmniSharp; +using Microsoft.CodeAnalysis.Scripting.Hosting; +using OmniSharp; using OmniSharp.Script; using OmniSharp.Services; - - namespace TestUtility { public static class TestHelpers