Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -30,7 +30,7 @@ internal sealed class CSharpRemoveUnusedValuesCodeFixProvider()
ExpressionStatementSyntax, LocalDeclarationStatementSyntax, VariableDeclaratorSyntax,
ForEachStatementSyntax, SwitchSectionSyntax, SwitchLabelSyntax, CatchClauseSyntax, CatchClauseSyntax>
{
protected override ISyntaxFormatting GetSyntaxFormatting()
protected override ISyntaxFormatting SyntaxFormatting
=> CSharpSyntaxFormatting.Instance;

protected override BlockSyntax WrapWithBlockIfNecessary(IEnumerable<StatementSyntax> statements)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ public static async Task<CollectionExpressionSyntax> CreateCollectionExpressionA

var document = await ParsedDocument.CreateAsync(workspaceDocument, cancellationToken).ConfigureAwait(false);

#if CODE_STYLE
var formattingOptions = CSharpSyntaxFormattingOptions.Default;
#else
var formattingOptions = (CSharpSyntaxFormattingOptions)await workspaceDocument.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
#endif

var options = await workspaceDocument.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false);
var formattingOptions = (CSharpSyntaxFormattingOptions)options.GetFormattingOptions(CSharpSyntaxFormatting.Instance);
var indentationOptions = new IndentationOptions(formattingOptions);

var wrappingLength = formattingOptions.CollectionExpressionWrappingLength;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ protected override StatementSyntax WrapWithBlockIfAppropriate(
protected override ExpressionSyntax ConvertToExpression(IThrowOperation throwOperation)
=> CSharpUseConditionalExpressionHelpers.ConvertToExpression(throwOperation);

protected override ISyntaxFormatting GetSyntaxFormatting()
protected override ISyntaxFormatting SyntaxFormatting
=> CSharpSyntaxFormatting.Instance;
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ protected override ExpressionSyntax WrapReturnExpressionIfNecessary(ExpressionSy
protected override ExpressionSyntax ConvertToExpression(IThrowOperation throwOperation)
=> CSharpUseConditionalExpressionHelpers.ConvertToExpression(throwOperation);

protected override ISyntaxFormatting GetSyntaxFormatting()
protected override ISyntaxFormatting SyntaxFormatting
=> CSharpSyntaxFormatting.Instance;
}
2 changes: 0 additions & 2 deletions src/Analyzers/Core/Analyzers/Analyzers.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
<Compile Include="$(MSBuildThisFileDirectory)FileHeaders\FileHeader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ForEachCast\AbstractForEachCastDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Formatting\AbstractFormattingAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Formatting\FormatterHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Formatting\FormattingAnalyzerHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\DeserializationConstructorCheck.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\DiagnosticHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)EnforceOnBuildValues.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CodeStyle;

Expand Down Expand Up @@ -41,7 +43,63 @@ private void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context, CompilationOpt
if (ShouldSkipAnalysis(context, compilationOptions, notification: null))
return;

var options = context.GetAnalyzerOptions().GetSyntaxFormattingOptions(SyntaxFormatting);
FormattingAnalyzerHelper.AnalyzeSyntaxTree(context, SyntaxFormatting, Descriptor, options);
var tree = context.Tree;
var cancellationToken = context.CancellationToken;

var oldText = tree.GetText(cancellationToken);
var root = tree.GetRoot(cancellationToken);
var span = context.FilterSpan.HasValue ? context.FilterSpan.GetValueOrDefault() : root.FullSpan;
var spans = SpecializedCollections.SingletonEnumerable(span);
var formattingOptions = context.GetAnalyzerOptions().GetSyntaxFormattingOptions(SyntaxFormatting);
var formattingChanges = SyntaxFormatting.GetFormattingResult(root, spans, formattingOptions, rules: default, cancellationToken).GetTextChanges(cancellationToken);

// formattingChanges could include changes that impact a larger section of the original document than
// necessary. Before reporting diagnostics, process the changes to minimize the span of individual
// diagnostics.
foreach (var formattingChange in formattingChanges)
{
var change = formattingChange;
Contract.ThrowIfNull(change.NewText);

if (change.NewText.Length > 0 && !change.Span.IsEmpty)
{
// Handle cases where the change is a substring removal from the beginning. In these cases, we want
// the diagnostic span to cover the unwanted leading characters (which should be removed), and
// nothing more.
var offset = change.Span.Length - change.NewText.Length;
if (offset >= 0)
{
if (oldText.GetSubText(new TextSpan(change.Span.Start + offset, change.NewText.Length)).ContentEquals(SourceText.From(change.NewText)))
{
change = new TextChange(new TextSpan(change.Span.Start, offset), "");
}
else
{
// Handle cases where the change is a substring removal from the end. In these cases, we want
// the diagnostic span to cover the unwanted trailing characters (which should be removed), and
// nothing more.
if (oldText.GetSubText(new TextSpan(change.Span.Start, change.NewText.Length)).ContentEquals(SourceText.From(change.NewText)))
{
change = new TextChange(new TextSpan(change.Span.Start + change.NewText.Length, offset), "");
}
}
}
}

Contract.ThrowIfNull(change.NewText);
if (change.NewText.Length == 0 && change.Span.IsEmpty)
{
// No actual change (allows for the formatter to report a NOP change without triggering a
// diagnostic that can't be fixed).
continue;
}

var location = Location.Create(tree, change.Span);
context.ReportDiagnostic(Diagnostic.Create(
Descriptor,
location,
additionalLocations: null,
properties: null));
}
}
}
68 changes: 0 additions & 68 deletions src/Analyzers/Core/Analyzers/Formatting/FormatterHelper.cs

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion src/Analyzers/Core/CodeFixes/CodeFixes.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<Compile Include="$(MSBuildThisFileDirectory)DocumentationComments\AbstractRemoveDocCommentNodeCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FileHeaders\AbstractFileHeaderCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ForEachCast\AbstractForEachCastCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Formatting\FormattingCodeFixHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Formatting\FormattingCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Iterator\AbstractIteratorCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MakeFieldReadonly\AbstractMakeFieldReadonlyCodeFixProvider.cs" />
Expand Down

This file was deleted.

Loading