Skip to content

Commit

Permalink
Merge pull request #2343 from tmat/OptionsExternalAccess
Browse files Browse the repository at this point in the history
Eliminate more instances of IWorkspaceOptionsProvider
  • Loading branch information
333fred authored Mar 8, 2022
2 parents 6aace68 + 8ee9810 commit 696cf21
Show file tree
Hide file tree
Showing 28 changed files with 272 additions and 200 deletions.
2 changes: 1 addition & 1 deletion build/Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<MicrosoftTestPackageVersion>17.0.0</MicrosoftTestPackageVersion>
<MSBuildPackageVersion>17.0.0</MSBuildPackageVersion>
<NuGetPackageVersion>6.0.0</NuGetPackageVersion>
<RoslynPackageVersion>4.2.0-1.22074.8</RoslynPackageVersion>
<RoslynPackageVersion>4.2.0-3.22157.8</RoslynPackageVersion>
<XunitPackageVersion>2.4.1</XunitPackageVersion>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Options;
using OmniSharp.Options;
using OmniSharp.Roslyn.CSharp.Workers.Formatting;
using OmniSharp.Roslyn.Options;
using RoslynFormattingOptions = Microsoft.CodeAnalysis.Formatting.FormattingOptions;

Expand Down Expand Up @@ -32,7 +33,6 @@ private static OptionSet GetOptions(OptionSet optionSet, FormattingOptions forma
.WithChangedOption(CSharpFormattingOptions.SpaceWithinCastParentheses, formattingOptions.SpaceWithinCastParentheses)
.WithChangedOption(CSharpFormattingOptions.SpaceWithinOtherParentheses, formattingOptions.SpaceWithinOtherParentheses)
.WithChangedOption(CSharpFormattingOptions.SpaceAfterCast, formattingOptions.SpaceAfterCast)
.WithChangedOption(CSharpFormattingOptions.SpacesIgnoreAroundVariableDeclaration, formattingOptions.SpacesIgnoreAroundVariableDeclaration)
.WithChangedOption(CSharpFormattingOptions.SpaceBeforeOpenSquareBracket, formattingOptions.SpaceBeforeOpenSquareBracket)
.WithChangedOption(CSharpFormattingOptions.SpaceBetweenEmptySquareBrackets, formattingOptions.SpaceBetweenEmptySquareBrackets)
.WithChangedOption(CSharpFormattingOptions.SpaceWithinSquareBrackets, formattingOptions.SpaceWithinSquareBrackets)
Expand All @@ -44,13 +44,13 @@ private static OptionSet GetOptions(OptionSet optionSet, FormattingOptions forma
.WithChangedOption(CSharpFormattingOptions.SpaceBeforeComma, formattingOptions.SpaceBeforeComma)
.WithChangedOption(CSharpFormattingOptions.SpaceBeforeDot, formattingOptions.SpaceBeforeDot)
.WithChangedOption(CSharpFormattingOptions.SpaceBeforeSemicolonsInForStatement, formattingOptions.SpaceBeforeSemicolonsInForStatement)
.WithChangedOption(CSharpFormattingOptions.SpacingAroundBinaryOperator, BinaryOperatorSpacingOptionForStringValue(formattingOptions.SpacingAroundBinaryOperator))
.WithChangedOption(CSharpFormattingOptions.SpacingAroundBinaryOperator, FormattingWorker.BinaryOperatorSpacingOptionForStringValue(formattingOptions.SpacingAroundBinaryOperator))
.WithChangedOption(CSharpFormattingOptions.IndentBraces, formattingOptions.IndentBraces)
.WithChangedOption(CSharpFormattingOptions.IndentBlock, formattingOptions.IndentBlock)
.WithChangedOption(CSharpFormattingOptions.IndentSwitchSection, formattingOptions.IndentSwitchSection)
.WithChangedOption(CSharpFormattingOptions.IndentSwitchCaseSection, formattingOptions.IndentSwitchCaseSection)
.WithChangedOption(CSharpFormattingOptions.IndentSwitchCaseSectionWhenBlock, formattingOptions.IndentSwitchCaseSectionWhenBlock)
.WithChangedOption(CSharpFormattingOptions.LabelPositioning, LabelPositionOptionForStringValue(formattingOptions.LabelPositioning))
.WithChangedOption(CSharpFormattingOptions.LabelPositioning, FormattingWorker.LabelPositionOptionForStringValue(formattingOptions.LabelPositioning))
.WithChangedOption(CSharpFormattingOptions.WrappingPreserveSingleLine, formattingOptions.WrappingPreserveSingleLine)
.WithChangedOption(CSharpFormattingOptions.WrappingKeepStatementsOnSingleLine, formattingOptions.WrappingKeepStatementsOnSingleLine)
.WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInTypes, formattingOptions.NewLinesForBracesInTypes)
Expand All @@ -70,32 +70,6 @@ private static OptionSet GetOptions(OptionSet optionSet, FormattingOptions forma
.WithChangedOption(CSharpFormattingOptions.NewLineForClausesInQuery, formattingOptions.NewLineForClausesInQuery);
}

private static LabelPositionOptions LabelPositionOptionForStringValue(string value)
{
switch (value.ToUpper())
{
case "LEFTMOST":
return LabelPositionOptions.LeftMost;
case "NOINDENT":
return LabelPositionOptions.NoIndent;
default:
return LabelPositionOptions.OneLess;
}
}

private static BinaryOperatorSpacingOptions BinaryOperatorSpacingOptionForStringValue(string value)
{
switch (value.ToUpper())
{
case "IGNORE":
return BinaryOperatorSpacingOptions.Ignore;
case "REMOVE":
return BinaryOperatorSpacingOptions.Remove;
default:
return BinaryOperatorSpacingOptions.Single;
}
}

public OptionSet Process(OptionSet currentOptionSet, OmniSharpOptions options, IOmniSharpEnvironment omnisharpEnvironment)
{
return GetOptions(currentOptionSet, options.FormattingOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ public CompletionService(OmniSharpWorkspace workspace, FormattingOptions formatt
_omniSharpOptions = omniSharpOptions;
}

public async Task<CompletionResponse> Handle(CompletionRequest request)
public Task<CompletionResponse> Handle(CompletionRequest request)
{
return Handle(request, forceExpandedCompletionIndexCreation: false);
}

public async Task<CompletionResponse> Handle(CompletionRequest request, bool forceExpandedCompletionIndexCreation)
{
_logger.LogTrace("Completions requested");

Expand All @@ -76,7 +81,9 @@ public async Task<CompletionResponse> Handle(CompletionRequest request)
_ => CompletionTrigger.Invoke,
};

var options = new OmniSharpCompletionOptions(ShowItemsFromUnimportedNamespaces: _omniSharpOptions.RoslynExtensionsOptions.EnableImportCompletion);
var options = new OmniSharpCompletionOptions(
ShowItemsFromUnimportedNamespaces: _omniSharpOptions.RoslynExtensionsOptions.EnableImportCompletion,
ForceExpandedCompletionIndexCreation: forceExpandedCompletionIndexCreation);

if (request.CompletionTrigger == CompletionTriggerKind.TriggerCharacter &&
!await OmniSharpCompletionService.ShouldTriggerCompletionAsync(completionService, document, position, trigger, roles: null, options, CancellationToken.None))
Expand All @@ -85,7 +92,7 @@ public async Task<CompletionResponse> Handle(CompletionRequest request)
return new CompletionResponse { Items = ImmutableArray<CompletionItem>.Empty };
}

var (completions, expandedItemsAvailable) = await OmniSharpCompletionService.GetCompletionsAsync(completionService, document, position, trigger, roles: null, options, CancellationToken.None);
var completions = await OmniSharpCompletionService.GetCompletionsAsync(completionService, document, position, trigger, roles: null, options, CancellationToken.None);
_logger.LogTrace("Found {0} completions for {1}:{2},{3}",
completions?.Items.IsDefaultOrEmpty != false ? 0 : completions.Items.Length,
request.FileName,
Expand Down Expand Up @@ -119,7 +126,7 @@ CompletionListBuilder.PartialMethodCompletionProvider or
// that completion provider is still creating the cache. We'll mark this completion list as not completed, and the
// editor will ask again when the user types more. By then, hopefully the cache will have populated and we can mark
// the completion as done.
bool expectingImportedItems = expandedItemsAvailable && _omniSharpOptions.RoslynExtensionsOptions.EnableImportCompletion;
bool expectingImportedItems = options.ShowItemsFromUnimportedNamespaces;
var syntax = await document.GetSyntaxTreeAsync();

var replacingSpanStartPosition = sourceText.Lines.GetLinePosition(typedSpan.Start);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;
using OmniSharp.Extensions;
using OmniSharp.Options;
using System;
using System.Composition;
using System.Linq;
Expand All @@ -17,10 +18,10 @@ public class DecompilationExternalSourceService : BaseExternalSourceService, IEx
private readonly Lazy<OmniSharpCSharpDecompiledSourceService> _service;

[ImportingConstructor]
public DecompilationExternalSourceService(ILoggerFactory loggerFactory) : base()
public DecompilationExternalSourceService(ILoggerFactory loggerFactory, OmniSharpOptions omnisharpOptions) : base()
{
_loggerFactory = loggerFactory;
_service = new Lazy<OmniSharpCSharpDecompiledSourceService>(() => new OmniSharpCSharpDecompiledSourceService(_loggerFactory));
_service = new Lazy<OmniSharpCSharpDecompiledSourceService>(() => new OmniSharpCSharpDecompiledSourceService(omnisharpOptions, _loggerFactory));
}

public async Task<(Document document, string documentPath)> GetAndAddExternalSymbolDocument(Project project, ISymbol symbol, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@
using OmniSharp.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;
using Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Formatting;
using Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.MetadataAsSource;
using Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.CSharp.DocumentationComments;
using OmniSharp.Options;
using OmniSharp.Roslyn.CSharp.Workers.Formatting;

namespace OmniSharp.Roslyn.CSharp.Services.Decompilation
{
public class OmniSharpCSharpDecompiledSourceService
{
private readonly OmniSharpOptions _omnisharpOptions;
private readonly ILoggerFactory _loggerFactory;
private static readonly FileVersionInfo decompilerVersion = FileVersionInfo.GetVersionInfo(typeof(CSharpDecompiler).Assembly.Location);

public OmniSharpCSharpDecompiledSourceService(ILoggerFactory loggerFactory)
public OmniSharpCSharpDecompiledSourceService(OmniSharpOptions omnisharpOptions, ILoggerFactory loggerFactory)
{
_omnisharpOptions = omnisharpOptions;
_loggerFactory = loggerFactory;
}

Expand Down Expand Up @@ -58,9 +63,9 @@ public async Task<Document> AddSourceToAsync(Document document, Compilation symb
var node = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

// Apply formatting rules
document = await Formatter.FormatAsync(
document, new[] { node.FullSpan },
options: null, cancellationToken).ConfigureAwait(false);

var options = await FormattingWorker.GetFormattingOptionsAsync(document, _omnisharpOptions).ConfigureAwait(false);
document = await OmniSharpFormatter.FormatAsync(document, new[] { node.FullSpan }, options, cancellationToken).ConfigureAwait(false);

return document;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ public class CodeFormatService : IRequestHandler<CodeFormatRequest, CodeFormatRe
{
private readonly OmniSharpWorkspace _workspace;
private readonly OmniSharpOptions _omnisharpOptions;
private readonly ILoggerFactory _loggerFactory;

[ImportingConstructor]
public CodeFormatService(OmniSharpWorkspace workspace, OmniSharpOptions omnisharpOptions, ILoggerFactory loggerFactory)
public CodeFormatService(OmniSharpWorkspace workspace, OmniSharpOptions omnisharpOptions)
{
_workspace = workspace;
_omnisharpOptions = omnisharpOptions;
_loggerFactory = loggerFactory;
}

public async Task<CodeFormatResponse> Handle(CodeFormatRequest request)
Expand All @@ -34,14 +32,14 @@ public async Task<CodeFormatResponse> Handle(CodeFormatRequest request)

if (request.WantsTextChanges)
{
var textChanges = await FormattingWorker.GetFormattedTextChanges(document, _omnisharpOptions, _loggerFactory);
var textChanges = await FormattingWorker.GetFormattedTextChanges(document, _omnisharpOptions);
return new CodeFormatResponse()
{
Changes = textChanges
};
}

var newText = await FormattingWorker.GetFormattedText(document, _omnisharpOptions, _loggerFactory);
var newText = await FormattingWorker.GetFormattedText(document, _omnisharpOptions);

return new CodeFormatResponse
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ public class FormatAfterKeystrokeService : IRequestHandler<FormatAfterKeystrokeR
{
private readonly OmniSharpWorkspace _workspace;
private readonly OmniSharpOptions _omnisharpOptions;
private readonly ILoggerFactory _loggerFactory;

[ImportingConstructor]
public FormatAfterKeystrokeService(OmniSharpWorkspace workspace, OmniSharpOptions omnisharpOptions, ILoggerFactory loggerFactory)
public FormatAfterKeystrokeService(OmniSharpWorkspace workspace, OmniSharpOptions omnisharpOptions)
{
_workspace = workspace;
_omnisharpOptions = omnisharpOptions;
_loggerFactory = loggerFactory;
}

public async Task<FormatRangeResponse> Handle(FormatAfterKeystrokeRequest request)
Expand All @@ -37,7 +35,7 @@ public async Task<FormatRangeResponse> Handle(FormatAfterKeystrokeRequest reques

var text = await document.GetTextAsync();
int position = text.GetTextPosition(request);
var changes = await FormattingWorker.GetFormattingChangesAfterKeystroke(document, position, request.Char, _omnisharpOptions, _loggerFactory);
var changes = await FormattingWorker.GetFormattingChangesAfterKeystroke(document, position, request.Char, _omnisharpOptions);

return new FormatRangeResponse()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ public class FormatRangeService : IRequestHandler<FormatRangeRequest, FormatRang
{
private readonly OmniSharpWorkspace _workspace;
private readonly OmniSharpOptions _omnisharpOptions;
private readonly ILoggerFactory _loggerFactory;

[ImportingConstructor]
public FormatRangeService(OmniSharpWorkspace workspace, OmniSharpOptions omnisharpOptions, ILoggerFactory loggerFactory)
public FormatRangeService(OmniSharpWorkspace workspace, OmniSharpOptions omnisharpOptions)
{
_workspace = workspace;
_omnisharpOptions = omnisharpOptions;
_loggerFactory = loggerFactory;
}

public async Task<FormatRangeResponse> Handle(FormatRangeRequest request)
Expand All @@ -39,7 +37,7 @@ public async Task<FormatRangeResponse> Handle(FormatRangeRequest request)
var end = text.Lines.GetPosition(new LinePosition(request.EndLine, request.EndColumn));
var syntaxTree = await document.GetSyntaxRootAsync();
var tokenStart = syntaxTree.FindToken(start).FullSpan.Start;
var changes = await FormattingWorker.GetFormattingChanges(document, tokenStart, end, _omnisharpOptions, _loggerFactory);
var changes = await FormattingWorker.GetFormattingChanges(document, tokenStart, end, _omnisharpOptions);

return new FormatRangeResponse()
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ public async Task<IEnumerable<AutoCompleteResponse>> Handle(AutoCompleteRequest
var sourceText = await document.GetTextAsync();
var position = sourceText.GetTextPosition(request);
var service = CompletionService.GetService(document);
var options = new OmniSharpCompletionOptions(ShowItemsFromUnimportedNamespaces: _omniSharpOptions.RoslynExtensionsOptions.EnableImportCompletion);
var (completionList, expandedItemsAvailable) = await OmniSharpCompletionService.GetCompletionsAsync(service, document, position, trigger: default, roles: null, options, CancellationToken.None);
var options = new OmniSharpCompletionOptions(
ShowItemsFromUnimportedNamespaces: _omniSharpOptions.RoslynExtensionsOptions.EnableImportCompletion,
ForceExpandedCompletionIndexCreation: false);
var completionList = await OmniSharpCompletionService.GetCompletionsAsync(service, document, position, trigger: default, roles: null, options, CancellationToken.None);

if (completionList != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Logging;
using OmniSharp.Mef;
using OmniSharp.Models.FixUsings;
using OmniSharp.Options;
using OmniSharp.Roslyn.Utilities;
using OmniSharp.Services;

Expand All @@ -14,17 +15,17 @@ namespace OmniSharp.Roslyn.CSharp.Services.Refactoring
public class FixUsingService : IRequestHandler<FixUsingsRequest, FixUsingsResponse>
{
private readonly OmniSharpWorkspace _workspace;
private readonly ILoggerFactory _loggerFactory;
private readonly OmniSharpOptions _options;
private readonly IEnumerable<ICodeActionProvider> _providers;

[ImportingConstructor]
public FixUsingService(
OmniSharpWorkspace workspace,
ILoggerFactory loggerFactory,
OmniSharpOptions options,
[ImportMany] IEnumerable<ICodeActionProvider> codeActionProviders)
{
_workspace = workspace;
_loggerFactory = loggerFactory;
_options = options;
_providers = codeActionProviders;
}

Expand All @@ -35,7 +36,7 @@ public async Task<FixUsingsResponse> Handle(FixUsingsRequest request)
var oldDocument = _workspace.GetDocument(request.FileName);
if (oldDocument != null)
{
var fixUsingsResponse = await new FixUsingsWorker(_providers)
var fixUsingsResponse = await new FixUsingsWorker(_providers, _options)
.FixUsingsAsync(oldDocument);

response.AmbiguousResults = fixUsingsResponse.AmbiguousResults;
Expand Down
Loading

0 comments on commit 696cf21

Please sign in to comment.