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

Implement formatting in the Code Style layer #31276

Merged
merged 17 commits into from
Nov 27, 2018
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src/CodeStyle/CSharp/Analyzers/CSharpCodeStyleResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="EmptyResource" xml:space="preserve">
<value>Remove this value when another is added.</value>
<comment>https://github.com/Microsoft/msbuild/issues/1661</comment>
<data name="Indentation_preferences" xml:space="preserve">
<value>Indentation preferences</value>
</data>
<data name="Space_preferences" xml:space="preserve">
<value>Space preferences</value>
</data>
<data name="Wrapping_preferences" xml:space="preserve">
<value>Wrapping preferences</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;
using Microsoft.VisualStudio.CodingConventions;

namespace Microsoft.CodeAnalysis.CodeStyle
{
internal class CSharpFormattingAnalyzerImpl : AbstractFormattingAnalyzerImpl
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class CSharpFormattingAnalyzer : AbstractFormattingAnalyzer
{
private readonly EditorConfigOptionsApplier _editorConfigOptionsApplier = new EditorConfigOptionsApplier();

public CSharpFormattingAnalyzerImpl(DiagnosticDescriptor descriptor)
: base(descriptor)
{
}
protected override ISyntaxFormattingService SyntaxFormattingService
=> new CSharpSyntaxFormattingService();

protected override OptionSet ApplyFormattingOptions(OptionSet optionSet, ICodingConventionContext codingConventionContext)
{
return _editorConfigOptionsApplier.ApplyConventions(optionSet, codingConventionContext.CurrentConventions, LanguageNames.CSharp);
return _editorConfigOptionsApplier.ApplyConventions(optionSet, codingConventionContext.CurrentConventions);
}
}
}
37 changes: 37 additions & 0 deletions src/CodeStyle/CSharp/Analyzers/EditorConfigOptionsApplier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.CodeAnalysis.Options;
using Microsoft.VisualStudio.CodingConventions;

namespace Microsoft.CodeAnalysis.CodeStyle
{
internal class EditorConfigOptionsApplier
{
public OptionSet ApplyConventions(OptionSet optionSet, ICodingConventionsSnapshot codingConventions)
{
return new CodingConventionsAnalyzerConfigOptions(codingConventions, optionSet);
}

private sealed class CodingConventionsAnalyzerConfigOptions : OptionSet
{
private readonly ICodingConventionsSnapshot _codingConventionsSnapshot;
private readonly OptionSet _fallbackOptions;

public CodingConventionsAnalyzerConfigOptions(ICodingConventionsSnapshot codingConventionsSnapshot, OptionSet fallbackOptions)
{
_codingConventionsSnapshot = codingConventionsSnapshot;
_fallbackOptions = fallbackOptions;
}

public override bool TryGetValue(string key, out string value)
{
if (_codingConventionsSnapshot.TryGetConventionValue(key, out value))
{
return true;
}

return _fallbackOptions.TryGetValue(key, out value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<OutputType>Library</OutputType>
<RootNamespace>Microsoft.CodeAnalysis.CSharp</RootNamespace>
<TargetFramework>netstandard1.3</TargetFramework>
<DefineConstants>$(DefineConstants),CODE_STYLE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisCSharpFixedVersion)" />
Expand All @@ -20,6 +21,46 @@
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.CSharp.CodeStyle.Fixes" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.CSharp.CodeStyle.UnitTests" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Extensions\ContextQuery\SyntaxTokenExtensions_SharedWithCodeStyle.cs" Link="Formatting\ContextQuery\SyntaxTokenExtensions_SharedWithCodeStyle.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Extensions\MemberDeclarationSyntaxExtensions_GetAttributes.cs" Link="Formatting\MemberDeclarationSyntaxExtensions_GetAttributes.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Extensions\SyntaxNodeExtensions_SharedWithCodeStyle.cs" Link="Formatting\SyntaxNodeExtensions_SharedWithCodeStyle.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Extensions\SyntaxTokenExtensions_SharedWithCodeStyle.cs" Link="Formatting\SyntaxTokenExtensions_SharedWithCodeStyle.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Extensions\SyntaxTriviaExtensions.cs" Link="Formatting\SyntaxTriviaExtensions.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\CSharpFormattingOptions.cs" Link="Formatting\CSharpFormattingOptions.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\CSharpFormattingOptions.Parsers.cs" Link="Formatting\CSharpFormattingOptions.Parsers.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\CSharpSyntaxFormattingService.cs" Link="Formatting\CSharpSyntaxFormattingService.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\DefaultOperationProvider.cs" Link="Formatting\DefaultOperationProvider.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Engine\AggregatedFormattingResult.cs" Link="Formatting\Engine\AggregatedFormattingResult.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Engine\CSharpFormatEngine.cs" Link="Formatting\Engine\CSharpFormatEngine.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Engine\CSharpStructuredTriviaFormatEngine.cs" Link="Formatting\Engine\CSharpStructuredTriviaFormatEngine.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Engine\FormattingResult.cs" Link="Formatting\Engine\FormattingResult.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Engine\Trivia\CSharpTriviaFormatter.cs" Link="Formatting\Engine\Trivia\CSharpTriviaFormatter.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Engine\Trivia\CSharpTriviaFormatter.DocumentationCommentExteriorCommentRewriter.cs" Link="Formatting\Engine\Trivia\CSharpTriviaFormatter.DocumentationCommentExteriorCommentRewriter.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Engine\Trivia\TriviaDataFactory.Analyzer.cs" Link="Formatting\Engine\Trivia\TriviaDataFactory.Analyzer.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Engine\Trivia\TriviaDataFactory.CodeShapeAnalyzer.cs" Link="Formatting\Engine\Trivia\TriviaDataFactory.CodeShapeAnalyzer.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Engine\Trivia\TriviaDataFactory.ComplexTrivia.cs" Link="Formatting\Engine\Trivia\TriviaDataFactory.ComplexTrivia.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Engine\Trivia\TriviaDataFactory.cs" Link="Formatting\Engine\Trivia\TriviaDataFactory.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Engine\Trivia\TriviaDataFactory.FormattedComplexTrivia.cs" Link="Formatting\Engine\Trivia\TriviaDataFactory.FormattedComplexTrivia.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Engine\Trivia\TriviaDataFactory.ModifiedComplexTrivia.cs" Link="Formatting\Engine\Trivia\TriviaDataFactory.ModifiedComplexTrivia.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Engine\Trivia\TriviaRewriter.cs" Link="Formatting\Engine\Trivia\TriviaRewriter.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\FormattingHelpers.cs" Link="Formatting\FormattingHelpers.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\AnchorIndentationFormattingRule.cs" Link="Formatting\Rules\AnchorIndentationFormattingRule.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\BaseFormattingRule.cs" Link="Formatting\Rules\BaseFormattingRule.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\ElasticTriviaFormattingRule.cs" Link="Formatting\Rules\ElasticTriviaFormattingRule.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\EndOfFileTokenFormattingRule.cs" Link="Formatting\Rules\EndOfFileTokenFormattingRule.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\IndentBlockFormattingRule.cs" Link="Formatting\Rules\IndentBlockFormattingRule.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\IndentUserSettingsFormattingRule.cs" Link="Formatting\Rules\IndentUserSettingsFormattingRule.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\NewLineUserSettingFormattingRule.cs" Link="Formatting\Rules\NewLineUserSettingFormattingRule.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\QueryExpressionFormattingRule.cs" Link="Formatting\Rules\QueryExpressionFormattingRule.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\SpacingFormattingRule.cs" Link="Formatting\Rules\SpacingFormattingRule.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\StructuredTriviaFormattingRule.cs" Link="Formatting\Rules\StructuredTriviaFormattingRule.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\SuppressFormattingRule.cs" Link="Formatting\Rules\SuppressFormattingRule.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\SyntaxKindEx.cs" Link="Formatting\Rules\SyntaxKindEx.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\TokenBasedFormattingRule.cs" Link="Formatting\Rules\TokenBasedFormattingRule.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Formatting\Rules\WrappingFormattingRule.cs" Link="Formatting\Rules\WrappingFormattingRule.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Utilities\FormattingRangeHelper.cs" Link="Formatting\FormattingRangeHelper.cs" />
</ItemGroup>
<ItemGroup>
<Compile Update="CSharpCodeStyleResources.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down
Loading