Skip to content

Commit

Permalink
Use wrapper pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Feb 11, 2022
1 parent f052935 commit 98aa5ea
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 174 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Formatting;

namespace Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.CSharp.Formatting
{
internal enum OmniSharpLabelPositionOptions
{
LeftMost = LabelPositionOptions.LeftMost,
OneLess = LabelPositionOptions.OneLess,
NoIndent = LabelPositionOptions.NoIndent
}

internal enum OmniSharpBinaryOperatorSpacingOptions
{
Single = BinaryOperatorSpacingOptions.Single,
Ignore = BinaryOperatorSpacingOptions.Ignore,
Remove = BinaryOperatorSpacingOptions.Remove
}

internal static class OmniSharpSyntaxFormattingOptionsFactory
{
public static OmniSharpSyntaxFormattingOptionsWrapper Create(
bool useTabs,
int tabSize,
int indentationSize,
string newLine,
bool separateImportDirectiveGroups,
bool spacingAfterMethodDeclarationName,
bool spaceWithinMethodDeclarationParenthesis,
bool spaceBetweenEmptyMethodDeclarationParentheses,
bool spaceAfterMethodCallName,
bool spaceWithinMethodCallParentheses,
bool spaceBetweenEmptyMethodCallParentheses,
bool spaceAfterControlFlowStatementKeyword,
bool spaceWithinExpressionParentheses,
bool spaceWithinCastParentheses,
bool spaceWithinOtherParentheses,
bool spaceAfterCast,
bool spaceBeforeOpenSquareBracket,
bool spaceBetweenEmptySquareBrackets,
bool spaceWithinSquareBrackets,
bool spaceAfterColonInBaseTypeDeclaration,
bool spaceAfterComma,
bool spaceAfterDot,
bool spaceAfterSemicolonsInForStatement,
bool spaceBeforeColonInBaseTypeDeclaration,
bool spaceBeforeComma,
bool spaceBeforeDot,
bool spaceBeforeSemicolonsInForStatement,
OmniSharpBinaryOperatorSpacingOptions spacingAroundBinaryOperator,
bool indentBraces,
bool indentBlock,
bool indentSwitchSection,
bool indentSwitchCaseSection,
bool indentSwitchCaseSectionWhenBlock,
OmniSharpLabelPositionOptions labelPositioning,
bool wrappingPreserveSingleLine,
bool wrappingKeepStatementsOnSingleLine,
bool newLinesForBracesInTypes,
bool newLinesForBracesInMethods,
bool newLinesForBracesInProperties,
bool newLinesForBracesInAccessors,
bool newLinesForBracesInAnonymousMethods,
bool newLinesForBracesInControlBlocks,
bool newLinesForBracesInAnonymousTypes,
bool newLinesForBracesInObjectCollectionArrayInitializers,
bool newLinesForBracesInLambdaExpressionBody,
bool newLineForElse,
bool newLineForCatch,
bool newLineForFinally,
bool newLineForMembersInObjectInit,
bool newLineForMembersInAnonymousTypes,
bool newLineForClausesInQuery)
=> new(new CSharpSyntaxFormattingOptions(
useTabs: useTabs,
tabSize: tabSize,
indentationSize: indentationSize,
newLine: newLine,
separateImportDirectiveGroups: separateImportDirectiveGroups,
spacing:
(spacingAfterMethodDeclarationName ? SpacePlacement.AfterMethodDeclarationName : 0) |
(spaceBetweenEmptyMethodDeclarationParentheses ? SpacePlacement.BetweenEmptyMethodDeclarationParentheses : 0) |
(spaceWithinMethodDeclarationParenthesis ? SpacePlacement.WithinMethodDeclarationParenthesis : 0) |
(spaceAfterMethodCallName ? SpacePlacement.AfterMethodCallName : 0) |
(spaceBetweenEmptyMethodCallParentheses ? SpacePlacement.BetweenEmptyMethodCallParentheses : 0) |
(spaceWithinMethodCallParentheses ? SpacePlacement.WithinMethodCallParentheses : 0) |
(spaceAfterControlFlowStatementKeyword ? SpacePlacement.AfterControlFlowStatementKeyword : 0) |
(spaceWithinExpressionParentheses ? SpacePlacement.WithinExpressionParentheses : 0) |
(spaceWithinCastParentheses ? SpacePlacement.WithinCastParentheses : 0) |
(spaceBeforeSemicolonsInForStatement ? SpacePlacement.BeforeSemicolonsInForStatement : 0) |
(spaceAfterSemicolonsInForStatement ? SpacePlacement.AfterSemicolonsInForStatement : 0) |
(spaceWithinOtherParentheses ? SpacePlacement.WithinOtherParentheses : 0) |
(spaceAfterCast ? SpacePlacement.AfterCast : 0) |
(spaceBeforeOpenSquareBracket ? SpacePlacement.BeforeOpenSquareBracket : 0) |
(spaceBetweenEmptySquareBrackets ? SpacePlacement.BetweenEmptySquareBrackets : 0) |
(spaceWithinSquareBrackets ? SpacePlacement.WithinSquareBrackets : 0) |
(spaceAfterColonInBaseTypeDeclaration ? SpacePlacement.AfterColonInBaseTypeDeclaration : 0) |
(spaceBeforeColonInBaseTypeDeclaration ? SpacePlacement.BeforeColonInBaseTypeDeclaration : 0) |
(spaceAfterComma ? SpacePlacement.AfterComma : 0) |
(spaceBeforeComma ? SpacePlacement.BeforeComma : 0) |
(spaceAfterDot ? SpacePlacement.AfterDot : 0) |
(spaceBeforeDot ? SpacePlacement.BeforeDot : 0),
spacingAroundBinaryOperator: (BinaryOperatorSpacingOptions)spacingAroundBinaryOperator,
newLines:
(newLineForMembersInObjectInit ? NewLinePlacement.BeforeMembersInObjectInitializers : 0) |
(newLineForMembersInAnonymousTypes ? NewLinePlacement.BeforeMembersInAnonymousTypes : 0) |
(newLineForElse ? NewLinePlacement.BeforeElse : 0) |
(newLineForCatch ? NewLinePlacement.BeforeCatch : 0) |
(newLineForFinally ? NewLinePlacement.BeforeFinally : 0) |
(newLinesForBracesInTypes ? NewLinePlacement.BeforeOpenBraceInTypes : 0) |
(newLinesForBracesInAnonymousTypes ? NewLinePlacement.BeforeOpenBraceInAnonymousTypes : 0) |
(newLinesForBracesInObjectCollectionArrayInitializers ? NewLinePlacement.BeforeOpenBraceInObjectCollectionArrayInitializers : 0) |
(newLinesForBracesInProperties ? NewLinePlacement.BeforeOpenBraceInProperties : 0) |
(newLinesForBracesInMethods ? NewLinePlacement.BeforeOpenBraceInMethods : 0) |
(newLinesForBracesInAccessors ? NewLinePlacement.BeforeOpenBraceInAccessors : 0) |
(newLinesForBracesInAnonymousMethods ? NewLinePlacement.BeforeOpenBraceInAnonymousMethods : 0) |
(newLinesForBracesInLambdaExpressionBody ? NewLinePlacement.BeforeOpenBraceInLambdaExpressionBody : 0) |
(newLinesForBracesInControlBlocks ? NewLinePlacement.BeforeOpenBraceInControlBlocks : 0) |
(newLineForClausesInQuery ? NewLinePlacement.BetweenQueryExpressionClauses : 0),
labelPositioning: (LabelPositionOptions)labelPositioning,
indentation:
(indentBraces ? IndentationPlacement.Braces : 0) |
(indentBlock ? IndentationPlacement.BlockContents : 0) |
(indentSwitchCaseSection ? IndentationPlacement.SwitchCaseContents : 0) |
(indentSwitchCaseSectionWhenBlock ? IndentationPlacement.SwitchCaseContentsWhenBlock : 0) |
(indentSwitchSection ? IndentationPlacement.SwitchSection : 0),
wrappingKeepStatementsOnSingleLine: wrappingKeepStatementsOnSingleLine,
wrappingPreserveSingleLine: wrappingPreserveSingleLine));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ namespace Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Formatting
{
internal static class OmniSharpFormatter
{
public static Task<Document> FormatAsync(Document document, IEnumerable<TextSpan>? spans, OmniSharpSyntaxFormattingOptions options, CancellationToken cancellationToken)
=> Formatter.FormatAsync(document, spans, options.ToSyntaxFormattingOptions(), rules: null, cancellationToken);
public static Task<Document> FormatAsync(Document document, IEnumerable<TextSpan>? spans, OmniSharpSyntaxFormattingOptionsWrapper options, CancellationToken cancellationToken)
=> Formatter.FormatAsync(document, spans, options.UnderlyingObject, rules: null, cancellationToken);

public static async Task<Document> OrganizeImportsAsync(Document document, OmniSharpOrganizeImportsOptions options, CancellationToken cancellationToken)
public static async Task<Document> OrganizeImportsAsync(Document document, OmniSharpOrganizeImportsOptionsWrapper options, CancellationToken cancellationToken)
{
var organizeImportsService = document.GetLanguageService<IOrganizeImportsService>();
if (organizeImportsService is null)
{
return document;
}

return await organizeImportsService.OrganizeImportsAsync(document, options.ToOrganizeImportsOptions(), cancellationToken).ConfigureAwait(false);
return await organizeImportsService.OrganizeImportsAsync(document, options.UnderlyingObject, cancellationToken).ConfigureAwait(false);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.OrganizeImports;

namespace Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Formatting
{
internal readonly struct OmniSharpOrganizeImportsOptionsWrapper
{
internal readonly OrganizeImportsOptions UnderlyingObject;

private OmniSharpOrganizeImportsOptionsWrapper(OrganizeImportsOptions underlyingObject)
{
UnderlyingObject = underlyingObject;
}

public OmniSharpOrganizeImportsOptionsWrapper(
bool placeSystemNamespaceFirst,
bool separateImportDirectiveGroups,
string newLine) : this(new OrganizeImportsOptions(
placeSystemNamespaceFirst,
separateImportDirectiveGroups,
newLine))
{
}
}
}

This file was deleted.

Loading

0 comments on commit 98aa5ea

Please sign in to comment.