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

Watch for changes in omnisharp.json #804

Merged
merged 7 commits into from
Mar 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace OmniSharp.Services
{
public interface IWorkspaceOptionsProvider
{
OptionSet Process(OptionSet optionSet);
OptionSet Process(OptionSet optionSet, Options.FormattingOptions options);
}
}
36 changes: 25 additions & 11 deletions src/OmniSharp.Host/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Startup(IOmniSharpEnvironment env)
new PhysicalFileProvider(env.Path),
"omnisharp.json",
optional: true,
reloadOnChange: false);
reloadOnChange: true);
Copy link
Member

Choose a reason for hiding this comment

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

👏


Configuration = configBuilder.Build();
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public static CompositionHost CreateCompositionHost(IServiceProvider serviceProv
return config.CreateContainer();
}

public static void InitializeWorkspace(OmniSharpWorkspace workspace, CompositionHost compositionHost, IConfiguration configuration, ILogger logger)
public static void InitializeWorkspace(OmniSharpWorkspace workspace, CompositionHost compositionHost, IConfiguration configuration, ILogger logger, OmniSharpOptions options)
{
var projectEventForwarder = compositionHost.GetExport<ProjectEventForwarder>();
projectEventForwarder.Initialize();
Expand All @@ -135,22 +135,30 @@ public static void InitializeWorkspace(OmniSharpWorkspace workspace, Composition
}
}

ProvideWorkspaceOptions(workspace, compositionHost, logger, options);

// Mark the workspace as initialized
workspace.Initialized = true;
}

private static void ProvideWorkspaceOptions(OmniSharpWorkspace workspace, CompositionHost compositionHost, ILogger logger, OmniSharpOptions options)
{
// run all workspace options providers
foreach (var workspaceOptionsProvider in compositionHost.GetExports<IWorkspaceOptionsProvider>())
{
var providerName = workspaceOptionsProvider.GetType().FullName;

try
{
workspace.Options = workspaceOptionsProvider.Process(workspace.Options);
logger.LogInformation($"Invoking Workspace Options Provider: {providerName}");
workspace.Options = workspaceOptionsProvider.Process(workspace.Options, options.FormattingOptions);
}
catch (Exception e)
{
var message = $"The workspace options provider '{workspaceOptionsProvider.GetType().FullName}' threw exception during initialization.";
var message = $"The workspace options provider '{providerName}' threw exception during initialization.";
logger.LogError(e, message);
}
}

// Mark the workspace as initialized
workspace.Initialized = true;
}

public void Configure(
Expand All @@ -159,7 +167,7 @@ public void Configure(
ILoggerFactory loggerFactory,
ISharedTextWriter writer,
IAssemblyLoader loader,
IOptions<OmniSharpOptions> options)
IOptionsMonitor<OmniSharpOptions> options)
{
if (_env.TransportType == TransportType.Stdio)
{
Expand All @@ -171,10 +179,9 @@ public void Configure(
}

var logger = loggerFactory.CreateLogger<Startup>();

var assemblies = DiscoverOmniSharpAssemblies(loader, logger);

PluginHost = CreateCompositionHost(serviceProvider, options.Value, assemblies);
PluginHost = CreateCompositionHost(serviceProvider, options.CurrentValue, assemblies);
Workspace = PluginHost.GetExport<OmniSharpWorkspace>();

app.UseRequestLogging();
Expand All @@ -192,7 +199,14 @@ public void Configure(
logger.LogInformation($"Omnisharp server running on port '{_env.Port}' at location '{_env.Path}' on host {_env.HostPID}.");
}

InitializeWorkspace(Workspace, PluginHost, Configuration, logger);
InitializeWorkspace(Workspace, PluginHost, Configuration, logger, options.CurrentValue);

// when configuration options change
// run workspace options providers automatically
options.OnChange(o =>
{
ProvideWorkspaceOptions(Workspace, PluginHost, logger, o);
});

logger.LogInformation("Configuration finished.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ namespace OmniSharp.Roslyn.CSharp.Services
[Export(typeof(IWorkspaceOptionsProvider)), Shared]
public class CSharpWorkspaceOptionsProvider : IWorkspaceOptionsProvider
{
private readonly Options.FormattingOptions _formattingOptions;

[ImportingConstructor]
public CSharpWorkspaceOptionsProvider(Options.FormattingOptions formattingOptions)
{
_formattingOptions = formattingOptions;
}

private static OptionSet GetOptions(OptionSet optionSet, Options.FormattingOptions formattingOptions)
{
return optionSet
Expand Down Expand Up @@ -99,9 +91,9 @@ private static BinaryOperatorSpacingOptions BinaryOperatorSpacingOptionForString
}
}

public OptionSet Process(OptionSet workOptionSet)
public OptionSet Process(OptionSet workOptionSet, Options.FormattingOptions options)
{
return GetOptions(workOptionSet, _formattingOptions);
return GetOptions(workOptionSet, options);
}
}
}
6 changes: 3 additions & 3 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FormattingFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ public async Task FormatRespectsIndentationSize()

using (var host = CreateOmniSharpHost(testFile))
{
var optionsProvider = new CSharpWorkspaceOptionsProvider(
var optionsProvider = new CSharpWorkspaceOptionsProvider();

host.Workspace.Options = optionsProvider.Process(host.Workspace.Options,
new FormattingOptions
{
NewLine = "\n",
IndentationSize = 1
});

host.Workspace.Options = optionsProvider.Process(host.Workspace.Options);

var requestHandler = host.GetRequestHandler<CodeFormatService>(OmnisharpEndpoints.CodeFormat);

var request = new CodeFormatRequest { FileName = testFile.FileName };
Expand Down
5 changes: 3 additions & 2 deletions tests/TestUtility/OmniSharpTestHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ public static OmniSharpTestHost Create(string path = null, ITestOutputHelper tes
var sharedTextWriter = new TestSharedTextWriter(testOutput);
var serviceProvider = new TestServiceProvider(environment, loggerFactory, sharedTextWriter);

var omnisharpOptions = new OmniSharpOptions();
var compositionHost = Startup.CreateCompositionHost(
serviceProvider,
options: new OmniSharpOptions(),
options: omnisharpOptions,
assemblies: s_lazyAssemblies.Value);

var workspace = compositionHost.GetExport<OmniSharpWorkspace>();
Expand All @@ -106,7 +107,7 @@ public static OmniSharpTestHost Create(string path = null, ITestOutputHelper tes
var dotNetCli = compositionHost.GetExport<DotNetCliService>();
dotNetCli.SetDotNetPath(dotNetPath);

Startup.InitializeWorkspace(workspace, compositionHost, configuration, logger);
Startup.InitializeWorkspace(workspace, compositionHost, configuration, logger, omnisharpOptions);

return new OmniSharpTestHost(serviceProvider, loggerFactory, workspace, compositionHost);
}
Expand Down