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

Scripting revamp #659

Merged
merged 11 commits into from
Nov 24, 2016
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 OmniSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "OmniSharp.Roslyn", "src\Omn
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "OmniSharp.MSBuild", "src\OmniSharp.MSBuild\OmniSharp.MSBuild.xproj", "{9AF025CA-3706-401F-8D50-59FAD5AFE725}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "OmniSharp.ScriptCs", "src\OmniSharp.ScriptCs\OmniSharp.ScriptCs.xproj", "{F52F6F60-AB29-414D-A24B-DE8EFFBA34C0}"
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "OmniSharp.Script", "src\OmniSharp.Script\OmniSharp.Script.xproj", "{F52F6F60-AB29-414D-A24B-DE8EFFBA34C0}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "OmniSharp.MSBuild.Tests", "tests\OmniSharp.MSBuild.Tests\OmniSharp.MSBuild.Tests.xproj", "{194A88AE-1429-416B-86C1-BF6F7FF47404}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public async Task<QuickFixResponse> Handle(CodeCheckRequest request)
var semanticModel = await document.GetSemanticModelAsync();
IEnumerable<Diagnostic> diagnostics = semanticModel.GetDiagnostics();

//script files can have custom directives such as #load which will be deemed invalid by Roslyn
//we suppress the CS1024 diagnostic for script files for this reason. Roslyn will fix it later too, so this is temporary.
if (document.SourceCodeKind != SourceCodeKind.Regular)
{
diagnostics = diagnostics.Where(diagnostic => diagnostic.Id != "CS1024");
// CS8099 needs to be surpressed so that we can use #load directives in scripts
// additionally, we need to suppress CS1701: https://github.com/dotnet/roslyn/issues/5501
diagnostics = diagnostics.Where(diagnostic => diagnostic.Id != "CS8099" && diagnostic.Id != "CS1701");
}

foreach (var quickFix in diagnostics.Select(MakeQuickFix))
Expand Down
57 changes: 57 additions & 0 deletions src/OmniSharp.Script/FileParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace OmniSharp.Script
{
public class FileParser
{
public static FileParserResult ProcessFile(string path, CSharpParseOptions parseOptions)
{
var result = new FileParserResult();
ParseFile(path, result, parseOptions);
return result;
}

private static void ParseFile(string path, FileParserResult result, CSharpParseOptions parseOptions)
{
var fullPath = Path.GetFullPath(path);
if (result.LoadedScripts.Contains(fullPath))
{
return;
}

result.LoadedScripts.Add(fullPath);

var scriptCode = File.ReadAllText(fullPath);

var syntaxTree = CSharpSyntaxTree.ParseText(scriptCode, parseOptions);

var namespaces = syntaxTree.GetCompilationUnitRoot().Usings.Select(x => x.Name.ToString());
foreach (var ns in namespaces)
{
result.Namespaces.Add(ns.Trim());
}

var refs = syntaxTree.GetCompilationUnitRoot().GetReferenceDirectives().Select(x => x.File.ToString());
foreach (var reference in refs)
{
result.References.Add(reference.Replace("\"", string.Empty));
}

var loads = syntaxTree.GetCompilationUnitRoot().GetLoadDirectives().Select(x => x.File.ToString());
foreach (var load in loads)
{
var filePath = load.Replace("\"", string.Empty);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this work on OSX/Linux?

Copy link
Member Author

@filipw filipw Nov 24, 2016

Choose a reason for hiding this comment

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

yes, this just strips away the quotes, tested on Mac too. when I have i.e. #load "bar.csx" in my CSX and I use GetXXXDirectives() method on the compilation root, the result string comes back with quotes inside i.e."bar.csx" not bar.csx and I want to strip it away to be able to concat the relative path and traverse these resources

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh sorry. I misread the slash in there. :-) I glanced at it and read the slash, not the double-quote. 😄

var currentWorkingDirectory = Path.GetDirectoryName(fullPath);

var loadFullPath = Path.IsPathRooted(filePath) ? filePath : Path.Combine(currentWorkingDirectory, filePath);
if (!string.IsNullOrWhiteSpace(loadFullPath))
{
ParseFile(loadFullPath, result, parseOptions);
}
}
}
}
}
11 changes: 11 additions & 0 deletions src/OmniSharp.Script/FileParserResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace OmniSharp.Script
{
public class FileParserResult
{
public HashSet<string> Namespaces { get; } = new HashSet<string>();
public HashSet<string> References { get; } = new HashSet<string>();
public HashSet<string> LoadedScripts { get; } = new HashSet<string>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>f52f6f60-ab29-414d-a24b-de8effba34c0</ProjectGuid>
<RootNamespace>OmniSharp.ScriptCs</RootNamespace>
<RootNamespace>OmniSharp.Script</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
using System;
using System.Collections.Generic;
using System.Composition;
using System.Reflection;
using Microsoft.CodeAnalysis;

namespace OmniSharp.ScriptCs
namespace OmniSharp.Script
{
[Export, Shared]
public class ScriptCsContext
public class ScriptContext
{
public HashSet<string> CsxFilesBeingProcessed { get; } = new HashSet<string>();

// All of the followings are keyed with the file path
// Each .csx file is wrapped into a project
public Dictionary<string, ProjectInfo> CsxFileProjects { get; } = new Dictionary<string, ProjectInfo>();
public Dictionary<string, List<MetadataReference>> CsxReferences { get; } = new Dictionary<string, List<MetadataReference>>();
public Dictionary<string, List<PortableExecutableReference>> CsxReferences { get; } = new Dictionary<string, List<PortableExecutableReference>>();
public Dictionary<string, List<ProjectInfo>> CsxLoadReferences { get; } = new Dictionary<string, List<ProjectInfo>>();
public Dictionary<string, List<string>> CsxUsings { get; } = new Dictionary<string, List<string>>();

public HashSet<string> ScriptPacks { get; } = new HashSet<string>();

// Nuget and ScriptPack stuff
public HashSet<MetadataReference> CommonReferences { get; } = new HashSet<MetadataReference>();
public HashSet<string> CommonUsings { get; } = new HashSet<string>();

public HashSet<string> CommonUsings { get; } = new HashSet<string> { "System" };
public string RootPath { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
using System.Linq;
using OmniSharp.Roslyn.Models;

namespace OmniSharp.ScriptCs
namespace OmniSharp.Script
{
public class ScriptCsContextModel
public class ScriptContextModel
{
public ScriptCsContextModel(ScriptCsContext context)
public ScriptContextModel(ScriptContext context)
{
RootPath = context.RootPath;
CsxFilesBeingProcessed = context.CsxFilesBeingProcessed;
CsxFileProjects = context.CsxFileProjects.ToDictionary(x => x.Key, x => new ProjectInfoModel(x.Value));
CsxReferences = context.CsxReferences.ToDictionary(x => x.Key, x => x.Value.Select(z => new ReferenceModel(z)));
CsxLoadReferences = context.CsxLoadReferences.ToDictionary(x => x.Key, x => x.Value.Select(z => new ProjectInfoModel(z)));
CsxUsings = context.CsxUsings.ToDictionary(x => x.Key, x => x.Value.AsEnumerable());
ScriptPacks = context.ScriptPacks;
CommonReferences = context.CommonReferences.Select(z => new ReferenceModel(z));
CommonUsings = context.CommonUsings;
}
Expand All @@ -27,13 +26,8 @@ public ScriptCsContextModel(ScriptCsContext context)
public Dictionary<string, IEnumerable<ReferenceModel>> CsxReferences { get; }
public Dictionary<string, IEnumerable<ProjectInfoModel>> CsxLoadReferences { get; }
public Dictionary<string, IEnumerable<string>> CsxUsings { get; }

public HashSet<string> ScriptPacks { get; }

// Nuget and ScriptPack stuff
public IEnumerable<ReferenceModel> CommonReferences { get; }
public IEnumerable<string> CommonUsings { get; }

public string RootPath { get; set; }
}
}
Loading