Permalink
Fetching contributors…
Cannot retrieve contributors at this time
68 lines (53 sloc) 2.33 KB
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Roslynator.CSharp.Analysis
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AddNewLineBeforeStatementAnalyzer : BaseDiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get { return ImmutableArray.Create(DiagnosticDescriptors.AddNewLineBeforeStatement); }
}
public override void Initialize(AnalysisContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
base.Initialize(context);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeBlock, SyntaxKind.Block);
context.RegisterSyntaxNodeAction(AnalyzeSwitchSection, SyntaxKind.SwitchSection);
}
public static void AnalyzeBlock(SyntaxNodeAnalysisContext context)
{
var block = (BlockSyntax)context.Node;
Analyze(context, block.Statements);
}
public static void AnalyzeSwitchSection(SyntaxNodeAnalysisContext context)
{
var switchSection = (SwitchSectionSyntax)context.Node;
Analyze(context, switchSection.Statements);
}
private static void Analyze(SyntaxNodeAnalysisContext context, SyntaxList<StatementSyntax> statements)
{
if (statements.Count <= 1)
return;
int previousEndLine = statements[0].GetSpanEndLine();
for (int i = 1; i < statements.Count; i++)
{
StatementSyntax statement = statements[i];
if (!statement.IsKind(SyntaxKind.Block, SyntaxKind.EmptyStatement)
&& statement.GetSpanStartLine() == previousEndLine)
{
context.ReportDiagnostic(DiagnosticDescriptors.AddNewLineBeforeStatement, statement);
}
previousEndLine = statement.GetSpanEndLine();
}
}
}
}