Permalink
Fetching contributors…
Cannot retrieve contributors at this time
53 lines (43 sloc) 1.72 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.Diagnostics;
using Microsoft.CodeAnalysis.Text;
namespace Roslynator.CSharp.Analysis
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class UseLinefeedAsNewLineAnalyzer : BaseDiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get { return ImmutableArray.Create(DiagnosticDescriptors.UseLinefeedAsNewLine); }
}
public override void Initialize(AnalysisContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
base.Initialize(context);
context.RegisterSyntaxTreeAction(Analyze);
}
public static void Analyze(SyntaxTreeAnalysisContext context)
{
if (!context.Tree.TryGetText(out SourceText sourceText))
return;
if (!context.Tree.TryGetRoot(out SyntaxNode root))
return;
foreach (TextLine textLine in sourceText.Lines)
{
int end = textLine.End;
if (textLine.EndIncludingLineBreak - end == 2
&& textLine.Text[end] == '\r'
&& textLine.Text[end + 1] == '\n')
{
context.ReportDiagnostic(
DiagnosticDescriptors.UseLinefeedAsNewLine,
Location.Create(context.Tree, new TextSpan(end, 2)));
}
}
}
}
}