Skip to content

Commit

Permalink
SyntaxFormattingOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Feb 1, 2023
1 parent 0b348a2 commit a0cedf0
Show file tree
Hide file tree
Showing 28 changed files with 169 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<ItemGroup>
<Compile Include="..\..\..\Workspaces\Core\Portable\Editing\DeclarationKind.cs" Link="Editing\DeclarationKind.cs" />
<Compile Include="..\..\..\Workspaces\Core\Portable\Editing\DeclarationModifiers.cs" Link="Editing\DeclarationModifiers.cs" />
<Compile Include="..\..\..\Workspaces\Core\Portable\Formatting\VisualBasic\VisualBasicSyntaxFormattingOptions.cs" Link="Formatting\VisualBasic\VisualBasicSyntaxFormattingOptions.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="CodeStyleResources.resx" GenerateSource="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Rules\NodeBasedFormattingRule.vb" Link="Formatting\Rules\NodeBasedFormattingRule.vb" />
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\Rules\StructuredTriviaFormattingRule.vb" Link="Formatting\Rules\StructuredTriviaFormattingRule.vb" />
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\VisualBasicSyntaxFormatting.vb" Link="Formatting\VisualBasicSyntaxFormatting.vb" />
<Compile Include="..\..\..\Workspaces\VisualBasic\Portable\Formatting\VisualBasicSyntaxFormattingOptions.vb" Link="Formatting\VisualBasicSyntaxFormattingOptions.vb" />
</ItemGroup>
<ItemGroup>
<Import Include="Microsoft.CodeAnalysis.Shared.Extensions" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ private static async Task TokenFormatWorkerAsync(TestWorkspace workspace, ITextB
var options = new IndentationOptions(
new CSharpSyntaxFormattingOptions
{
Common = new SyntaxFormattingOptions.CommonOptions()
{
LineFormatting = new LineFormattingOptions { UseTabs = useTabs }
}
LineFormatting = new() { UseTabs = useTabs }
});

var formatter = new CSharpSmartTokenFormatter(options, rules, (CompilationUnitSyntax)documentSyntax.Root, documentSyntax.Text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ private static async Task AssertIndentUsingSmartTokenFormatterAsync(
var root = (await document.GetSyntaxRootAsync()) as CompilationUnitSyntax;

var options = new IndentationOptions(
CSharpSyntaxFormattingOptions.Default.With(new LineFormattingOptions { UseTabs = useTabs }));
new CSharpSyntaxFormattingOptions() { LineFormatting = new() { UseTabs = useTabs } });

Assert.True(
CSharpIndentationService.ShouldUseSmartTokenFormatterInsteadOfIndenter(
Expand Down Expand Up @@ -1526,7 +1526,7 @@ private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(

var root = (await document.GetSyntaxRootAsync()) as CompilationUnitSyntax;

var options = new IndentationOptions(CSharpSyntaxFormattingOptions.Default.With(new LineFormattingOptions { UseTabs = useTabs }))
var options = new IndentationOptions(new CSharpSyntaxFormattingOptions() { LineFormatting = new() { UseTabs = useTabs } })
{
IndentStyle = indentStyle
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3569,7 +3569,7 @@ private static async Task AutoFormatOnMarkerAsync(string initialMarkup, string e
Assert.Equal(tokenKind, endToken.Kind());

var options = new IndentationOptions(
CSharpSyntaxFormattingOptions.Default.With(new LineFormattingOptions { UseTabs = useTabs }));
new CSharpSyntaxFormattingOptions() { LineFormatting = new() { UseTabs = useTabs } });

var formatter = new CSharpSmartTokenFormatter(options, rules, (CompilationUnitSyntax)documentSyntax.Root, documentSyntax.Text);

Expand Down
4 changes: 2 additions & 2 deletions src/EditorFeatures/Core/Options/TextBufferOptionProviders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static SyntaxFormattingOptions GetSyntaxFormattingOptionsImpl(ITextBuffe
var options = configOptions.GetSyntaxFormattingOptions(fallbackOptions, languageServices);
var lineFormattingOptions = GetLineFormattingOptionsImpl(textBuffer, editorOptions, indentationManager, explicitFormat);

return options.With(lineFormattingOptions);
return options with { LineFormatting = lineFormattingOptions };
}

public static IndentationOptions GetIndentationOptions(this ITextBuffer textBuffer, EditorOptionsService optionsProvider, LanguageServices languageServices, bool explicitFormat)
Expand Down Expand Up @@ -86,7 +86,7 @@ public static CodeCleanupOptions GetCodeCleanupOptions(this ITextBuffer textBuff
var options = configOptions.GetCodeCleanupOptions(allowImportsInHiddenRegions, fallbackOptions, languageServices);
var lineFormattingOptions = GetLineFormattingOptionsImpl(textBuffer, editorOptions, optionsProvider.IndentationManager, explicitFormat);

return options with { FormattingOptions = options.FormattingOptions.With(lineFormattingOptions) };
return options with { FormattingOptions = options.FormattingOptions with { LineFormatting = lineFormattingOptions } };
}

public static IndentingStyle ToEditorIndentStyle(this FormattingOptions2.IndentStyle value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static CSharpSyntaxWrappingOptions GetCSharpSyntaxWrappingOptions(this IO
var newLineBeforeOpenBraceDefault = ((CSharpSyntaxFormattingOptions)fallbackOptions.CleanupOptions.FormattingOptions).NewLines.ToNewLineBeforeOpenBracePlacement();

return new(
options.GetCSharpSyntaxFormattingOptions((CSharpSyntaxFormattingOptions)fallbackOptions.CleanupOptions.FormattingOptions),
new CSharpSyntaxFormattingOptions(options, (CSharpSyntaxFormattingOptions)fallbackOptions.CleanupOptions.FormattingOptions),
operatorPlacement: options.GetOption(CodeStyleOptions2.OperatorPlacementWhenWrapping, fallbackOptions.CodeStyleOptions.OperatorPlacementWhenWrapping),
wrappingColumn: fallbackOptions.WrappingColumn,
newLinesForBracesInObjectCollectionArrayInitializers: options.GetOption(CSharpFormattingOptions2.NewLineBeforeOpenBrace, newLineBeforeOpenBraceDefault).HasFlag(NewLineBeforeOpenBracePlacement.ObjectCollectionArrayInitializers));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,13 +702,16 @@ public static async Task<SyntaxFormattingOptions> GetFormattingOptionsAsync(
{
// LSP doesn't currently support indent size as an option. However, except in special
// circumstances, indent size is usually equivalent to tab size, so we'll just set it.
formattingOptions = formattingOptions.With(new LineFormattingOptions()
formattingOptions = formattingOptions with
{
UseTabs = !options.InsertSpaces,
TabSize = options.TabSize,
IndentationSize = options.TabSize,
NewLine = formattingOptions.NewLine
});
LineFormatting = new()
{
UseTabs = !options.InsertSpaces,
TabSize = options.TabSize,
IndentationSize = options.TabSize,
NewLine = formattingOptions.NewLine
}
};
}

return formattingOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Wrapping

Public Shared Function Create(options As IOptionsReader, fallbackOptions As CodeActionOptions) As VisualBasicSyntaxWrappingOptions
Return New VisualBasicSyntaxWrappingOptions(
formattingOptions:=VisualBasicSyntaxFormattingOptions.Create(options, DirectCast(fallbackOptions.CleanupOptions.FormattingOptions, VisualBasicSyntaxFormattingOptions)),
formattingOptions:=New VisualBasicSyntaxFormattingOptions(options, DirectCast(fallbackOptions.CleanupOptions.FormattingOptions, VisualBasicSyntaxFormattingOptions)),
operatorPlacement:=options.GetOption(CodeStyleOptions2.OperatorPlacementWhenWrapping, fallbackOptions.CodeStyleOptions.OperatorPlacementWhenWrapping),
wrappingColumn:=fallbackOptions.WrappingColumn)
End Function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,14 @@ public static OmniSharpSyntaxFormattingOptionsWrapper Create(
{
FormattingOptions = new CSharpSyntaxFormattingOptions()
{
Common = new()
LineFormatting = new()
{
LineFormatting = new()
{
UseTabs = useTabs,
TabSize = tabSize,
IndentationSize = indentationSize,
NewLine = newLine
},
SeparateImportDirectiveGroups = separateImportDirectiveGroups,
UseTabs = useTabs,
TabSize = tabSize,
IndentationSize = indentationSize,
NewLine = newLine
},
SeparateImportDirectiveGroups = separateImportDirectiveGroups,
Spacing =
(spacingAfterMethodDeclarationName ? SpacePlacement.AfterMethodDeclarationName : 0) |
(spaceBetweenEmptyMethodDeclarationParentheses ? SpacePlacement.BetweenEmptyMethodDeclarationParentheses : 0) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ internal CodeActionOptions GetCodeActionOptions(LanguageServices languageService
{
CleanupOptions = defaultOptions.CleanupOptions with
{
FormattingOptions = defaultOptions.CleanupOptions.FormattingOptions.With(new LineFormattingOptions
FormattingOptions = defaultOptions.CleanupOptions.FormattingOptions with
{
IndentationSize = LineFormattingOptions.IndentationSize,
TabSize = LineFormattingOptions.TabSize,
UseTabs = LineFormattingOptions.UseTabs,
NewLine = LineFormattingOptions.NewLine,
})
LineFormatting = new()
{
IndentationSize = LineFormattingOptions.IndentationSize,
TabSize = LineFormattingOptions.TabSize,
UseTabs = LineFormattingOptions.UseTabs,
NewLine = LineFormattingOptions.NewLine,
}
}
},
ImplementTypeOptions = new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ public static async ValueTask<OmniSharpSyntaxFormattingOptionsWrapper> FromDocum
var defaultOptions = CodeCleanupOptions.GetDefault(document.Project.Services);
var fallbackOptions = defaultOptions with
{
FormattingOptions = defaultOptions.FormattingOptions.With(new LineFormattingOptions
FormattingOptions = defaultOptions.FormattingOptions with
{
IndentationSize = fallbackLineFormattingOptions.IndentationSize,
TabSize = fallbackLineFormattingOptions.TabSize,
UseTabs = fallbackLineFormattingOptions.UseTabs,
NewLine = fallbackLineFormattingOptions.NewLine,
})
LineFormatting = new()
{
IndentationSize = fallbackLineFormattingOptions.IndentationSize,
TabSize = fallbackLineFormattingOptions.TabSize,
UseTabs = fallbackLineFormattingOptions.UseTabs,
NewLine = fallbackLineFormattingOptions.NewLine,
}
}
};

var cleanupOptions = await document.GetCodeCleanupOptionsAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@ public override ValueTask<CleanCodeGenerationOptions> GetCleanCodeGenerationOpti
{
CleanupOptions = CodeCleanupOptions.GetDefault(languageServices) with
{
FormattingOptions = SyntaxFormattingOptions.GetDefault(languageServices).With(new LineFormattingOptions
FormattingOptions = SyntaxFormattingOptions.GetDefault(languageServices) with
{
IndentationSize = lineFormattingOptions.IndentationSize,
TabSize = lineFormattingOptions.TabSize,
UseTabs = lineFormattingOptions.UseTabs,
NewLine = lineFormattingOptions.NewLine,
})
LineFormatting = new()
{
IndentationSize = lineFormattingOptions.IndentationSize,
TabSize = lineFormattingOptions.TabSize,
UseTabs = lineFormattingOptions.UseTabs,
NewLine = lineFormattingOptions.NewLine,
}
}
}
};
return new ValueTask<CleanCodeGenerationOptions>(codeGenerationOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,15 @@ public static SyntaxNode Format(
}

private static SyntaxFormattingOptions GetFormattingOptions(RazorIndentationOptions indentationOptions)
=> CSharpSyntaxFormattingOptions.Default.With(new LineFormattingOptions()
=> new CSharpSyntaxFormattingOptions()
{
UseTabs = indentationOptions.UseTabs,
TabSize = indentationOptions.TabSize,
IndentationSize = indentationOptions.IndentationSize,
NewLine = CSharpSyntaxFormattingOptions.Default.NewLine
});
LineFormatting = new()
{
UseTabs = indentationOptions.UseTabs,
TabSize = indentationOptions.TabSize,
IndentationSize = indentationOptions.IndentationSize,
NewLine = CSharpSyntaxFormattingOptions.Default.NewLine
}
};
}
}
7 changes: 2 additions & 5 deletions src/Workspaces/CSharpTest/Formatting/FormattingTriviaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,7 @@ public void NewLineOptions_LineFeedOnly()

var options = new CSharpSyntaxFormattingOptions()
{
Common = new SyntaxFormattingOptions.CommonOptions { LineFormatting = new LineFormattingOptions { NewLine = "\n" } }
LineFormatting = new() { NewLine = "\n" }
};

var formatted = Formatter.Format(tree, workspace.Services.SolutionServices, options, CancellationToken.None);
Expand Down Expand Up @@ -1792,10 +1792,7 @@ class F

var options = new CSharpSyntaxFormattingOptions()
{
Common = new SyntaxFormattingOptions.CommonOptions
{
LineFormatting = new LineFormattingOptions { UseTabs = true, NewLine = newLine }
}
LineFormatting = new() { UseTabs = true, NewLine = newLine }
};

var formatted = Formatter.Format(tree, workspace.Services.SolutionServices, options, CancellationToken.None);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 System;
using System.Runtime.Serialization;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.VisualBasic.Formatting;

[DataContract]
internal sealed record class VisualBasicSyntaxFormattingOptions : SyntaxFormattingOptions, IEquatable<VisualBasicSyntaxFormattingOptions>
{
public static readonly VisualBasicSyntaxFormattingOptions Default = new();

internal VisualBasicSyntaxFormattingOptions()
: base()
{
}

internal VisualBasicSyntaxFormattingOptions(IOptionsReader options, VisualBasicSyntaxFormattingOptions fallbackOptions)
: base(options, LanguageNames.VisualBasic, fallbackOptions)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.CodeAnalysis.OrganizeImports;
internal readonly record struct OrganizeImportsOptions
{
[DataMember] public bool PlaceSystemNamespaceFirst { get; init; } = AddImportPlacementOptions.Default.PlaceSystemNamespaceFirst;
[DataMember] public bool SeparateImportDirectiveGroups { get; init; } = SyntaxFormattingOptions.CommonOptions.Default.SeparateImportDirectiveGroups;
[DataMember] public bool SeparateImportDirectiveGroups { get; init; } = SyntaxFormattingOptions.CommonDefaults.SeparateImportDirectiveGroups;
[DataMember] public string NewLine { get; init; } = LineFormattingOptions.Default.NewLine;

public OrganizeImportsOptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override SyntaxFormattingOptions DefaultOptions
=> CSharpSyntaxFormattingOptions.Default;

public override SyntaxFormattingOptions GetFormattingOptions(IOptionsReader options, SyntaxFormattingOptions? fallbackOptions)
=> options.GetCSharpSyntaxFormattingOptions((CSharpSyntaxFormattingOptions?)fallbackOptions);
=> new CSharpSyntaxFormattingOptions(options, (CSharpSyntaxFormattingOptions?)fallbackOptions ?? CSharpSyntaxFormattingOptions.Default);

protected override IFormattingResult CreateAggregatedFormattingResult(SyntaxNode node, IList<AbstractFormattingResult> results, SimpleIntervalTree<TextSpan, TextSpanIntervalIntrospector>? formattingSpans = null)
=> new AggregatedFormattingResult(node, results, formattingSpans);
Expand Down
Loading

0 comments on commit a0cedf0

Please sign in to comment.