Skip to content

Commit

Permalink
Merge pull request #848 from filipw/features/code-actions-folder
Browse files Browse the repository at this point in the history
Added an option to configure a code actions folder
  • Loading branch information
DustinCampbell authored May 17, 2017
2 parents 4cb692a + 44dbde1 commit 4e59671
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 9 deletions.
9 changes: 2 additions & 7 deletions src/OmniSharp.Abstractions/Options/OmniSharpOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ namespace OmniSharp.Options
{
public class OmniSharpOptions
{
public FormattingOptions FormattingOptions { get; }
public RoslynExtensionsOptions RoslynExtensionsOptions { get; } = new RoslynExtensionsOptions();

public OmniSharpOptions() : this(new FormattingOptions()) { }

public OmniSharpOptions(FormattingOptions options)
{
FormattingOptions = options ?? throw new ArgumentNullException(nameof(options));
}
public FormattingOptions FormattingOptions { get; } = new FormattingOptions();
}
}
32 changes: 32 additions & 0 deletions src/OmniSharp.Abstractions/Options/RoslynExtensionsOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace OmniSharp.Options
{
public class RoslynExtensionsOptions
{
public string[] LocationPaths { get; set; }

public IEnumerable<string> GetNormalizedLocationPaths(IOmniSharpEnvironment env)
{
if (LocationPaths == null || LocationPaths.Length == 0) return Enumerable.Empty<string>();

var normalizePaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var locationPath in LocationPaths)
{
if (Path.IsPathRooted(locationPath))
{
normalizePaths.Add(locationPath);
}
else
{
normalizePaths.Add(Path.Combine(env.TargetDirectory, locationPath));
}
}

return normalizePaths;
}
}
}
2 changes: 2 additions & 0 deletions src/OmniSharp.Abstractions/Services/IAssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace OmniSharp.Services
public interface IAssemblyLoader
{
Assembly Load(AssemblyName name);

IReadOnlyList<Assembly> LoadAllFrom(string folderPath);
}

public static class IAssemblyLoaderExtensions
Expand Down
43 changes: 42 additions & 1 deletion src/OmniSharp.Host/Services/AssemblyLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.Logging;
using OmniSharp.Services;
Expand All @@ -11,7 +14,7 @@ internal class AssemblyLoader : IAssemblyLoader

public AssemblyLoader(ILoggerFactory loggerFactory)
{
this._logger = loggerFactory.CreateLogger<AssemblyLoader>();
_logger = loggerFactory.CreateLogger<AssemblyLoader>();
}

public Assembly Load(AssemblyName name)
Expand All @@ -30,5 +33,43 @@ public Assembly Load(AssemblyName name)
_logger.LogTrace($"Assembly loaded: {name}");
return result;
}

public IReadOnlyList<Assembly> LoadAllFrom(string folderPath)
{
if (string.IsNullOrWhiteSpace(folderPath)) return Array.Empty<Assembly>();

var assemblies = new List<Assembly>();
foreach (var filePath in Directory.EnumerateFiles(folderPath, "*.dll"))
{
var assembly = LoadFromPath(filePath);
if (assembly != null)
{
assemblies.Add(assembly);
}
}

return assemblies;
}

private Assembly LoadFromPath(string assemblyPath)
{
Assembly assembly = null;

try
{
#if NET46
assembly = Assembly.LoadFrom(assemblyPath);
#else
assembly = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
#endif
}
catch (Exception ex)
{
_logger.LogError(ex, $"Failed to load assembly from path: {assemblyPath}");
}

_logger.LogTrace($"Assembly loaded from path: {assemblyPath}");
return assembly;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Composition;
using OmniSharp.Services;

namespace OmniSharp.Roslyn.CSharp.Services.CodeActions
{
[Export(typeof(ICodeActionProvider))]
public class ExternalCodeActionProvider : AbstractCodeActionProvider
{
[ImportingConstructor]
public ExternalCodeActionProvider(ExternalFeaturesHostServicesProvider featuresHostServicesProvider)
: base("ExternalCodeActions", featuresHostServicesProvider.Assemblies)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Reflection;
using OmniSharp.Options;
using OmniSharp.Services;

namespace OmniSharp.Roslyn.CSharp.Services
{
[Export(typeof(IHostServicesProvider))]
[Export(typeof(ExternalFeaturesHostServicesProvider))]
[Shared]
public class ExternalFeaturesHostServicesProvider : IHostServicesProvider
{
public ImmutableArray<Assembly> Assemblies { get; }

[ImportingConstructor]
public ExternalFeaturesHostServicesProvider(IAssemblyLoader loader, OmniSharpOptions options, IOmniSharpEnvironment env)
{
var builder = ImmutableArray.CreateBuilder<Assembly>();

var roslynExtensionsLocations = options.RoslynExtensionsOptions.GetNormalizedLocationPaths(env);
if (roslynExtensionsLocations?.Any() == true)
{
foreach (var roslynExtensionsLocation in roslynExtensionsLocations)
{
builder.AddRange(loader.LoadAllFrom(roslynExtensionsLocation));
}
}

Assemblies = builder.ToImmutable();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System.Collections.Immutable;
using System.Composition;
using System.Reflection;
using OmniSharp.Options;
using OmniSharp.Services;

namespace OmniSharp.Roslyn.CSharp.Services
{
[Export(typeof(IHostServicesProvider))]
[Export(typeof(RoslynFeaturesHostServicesProvider))]
[Shared]
public class RoslynFeaturesHostServicesProvider : IHostServicesProvider
{
public ImmutableArray<Assembly> Assemblies { get; }
Expand All @@ -21,7 +23,8 @@ public RoslynFeaturesHostServicesProvider(IAssemblyLoader loader)

builder.AddRange(loader.Load(Features, CSharpFeatures));

this.Assemblies = builder.ToImmutable();

Assemblies = builder.ToImmutable();
}
}
}
16 changes: 16 additions & 0 deletions src/OmniSharp/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,20 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.CSharp" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.Workspaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

0 comments on commit 4e59671

Please sign in to comment.