<commit_before>﻿namespace StyleCop.Analyzers.SpacingRules
{
    using System.Collections.Immutable;
    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.CSharp;
    using Microsoft.CodeAnalysis.Diagnostics;

    /// <summary>
    /// A nullable type symbol within a C# element is not spaced correctly.
    /// </summary>
    /// <remarks>
    /// <para>A violation of this rule occurs when the spacing around a nullable type symbol is not correct.</para>
    ///
    /// <para>A nullable type symbol should never be preceded by whitespace, unless the symbol is the first character on
    /// the line.</para>
    /// </remarks>
    [DiagnosticAnalyzer(LanguageNames.CSharp)]
    public class SA1018NullableTypeSymbolsMustNotBePrecededBySpace : DiagnosticAnalyzer
    {
        public const string DiagnosticId = "SA1018";
        private const string Title = "Nullable type symbols must be spaced correctly";
        private const string MessageFormat = "Nullable type symbol must not be preceded by a space.";
        private const string Category = "StyleCop.CSharp.SpacingRules";
        private const string Description = "A nullable type symbol within a C# element is not spaced correctly.";
        private const string HelpLink = "http://www.stylecop.com/docs/SA1018.html";

        private static readonly DiagnosticDescriptor Descriptor =
            new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, AnalyzerConstants.DisabledNoTests, Description, HelpLink);

        private static readonly ImmutableArray<DiagnosticDescriptor> _supportedDiagnostics =
            ImmutableArray.Create(Descriptor);

        /// <inheritdoc/>
        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
        {
            get
            {
                return _supportedDiagnostics;
            }
        }

        /// <inheritdoc/>
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxTreeAction(HandleSyntaxTree);
        }

        private void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
        {
            SyntaxNode root = context.Tree.GetCompilationUnitRoot(context.CancellationToken);
            foreach (var token in root.DescendantTokens())
            {
                switch (token.CSharpKind())
                {
                case SyntaxKind.QuestionToken:
                    HandleQuestionToken(context, token);
                    break;

                default:
                    break;
                }
            }
        }

        private void HandleQuestionToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
        {
            if (token.IsMissing)
                return;

            if (token.Parent.CSharpKind() != SyntaxKind.NullableType)
                return;

            bool hasPrecedingSpace = false;
            if (!token.HasLeadingTrivia)
            {
                // only the first token on the line has leading trivia, and those are ignored
                SyntaxToken precedingToken = token.GetPreviousToken();
                if (precedingToken.HasTrailingTrivia)
                    hasPrecedingSpace = true;
            }

            if (hasPrecedingSpace)
            {
                // nullable type symbol must not be preceded by a space
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation()));
            }
        }
    }
}
<commit_msg>Fix handling of nullable symbol at the beginning of a line<commit_after>﻿namespace StyleCop.Analyzers.SpacingRules
{
    using System.Collections.Immutable;
    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.CSharp;
    using Microsoft.CodeAnalysis.Diagnostics;

    /// <summary>
    /// A nullable type symbol within a C# element is not spaced correctly.
    /// </summary>
    /// <remarks>
    /// <para>A violation of this rule occurs when the spacing around a nullable type symbol is not correct.</para>
    ///
    /// <para>A nullable type symbol should never be preceded by whitespace, unless the symbol is the first character on
    /// the line.</para>
    /// </remarks>
    [DiagnosticAnalyzer(LanguageNames.CSharp)]
    public class SA1018NullableTypeSymbolsMustNotBePrecededBySpace : DiagnosticAnalyzer
    {
        public const string DiagnosticId = "SA1018";
        private const string Title = "Nullable type symbols must be spaced correctly";
        private const string MessageFormat = "Nullable type symbol must not be preceded by a space.";
        private const string Category = "StyleCop.CSharp.SpacingRules";
        private const string Description = "A nullable type symbol within a C# element is not spaced correctly.";
        private const string HelpLink = "http://www.stylecop.com/docs/SA1018.html";

        private static readonly DiagnosticDescriptor Descriptor =
            new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, AnalyzerConstants.DisabledNoTests, Description, HelpLink);

        private static readonly ImmutableArray<DiagnosticDescriptor> _supportedDiagnostics =
            ImmutableArray.Create(Descriptor);

        /// <inheritdoc/>
        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
        {
            get
            {
                return _supportedDiagnostics;
            }
        }

        /// <inheritdoc/>
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxTreeAction(HandleSyntaxTree);
        }

        private void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
        {
            SyntaxNode root = context.Tree.GetCompilationUnitRoot(context.CancellationToken);
            foreach (var token in root.DescendantTokens())
            {
                switch (token.CSharpKind())
                {
                case SyntaxKind.QuestionToken:
                    HandleQuestionToken(context, token);
                    break;

                default:
                    break;
                }
            }
        }

        private void HandleQuestionToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
        {
            if (token.IsMissing)
                return;

            if (token.Parent.CSharpKind() != SyntaxKind.NullableType)
                return;

            if (token.IsFirstTokenOnLine(context.CancellationToken))
                return;

            SyntaxToken precedingToken = token.GetPreviousToken();
            if (precedingToken.HasTrailingTrivia)
            {
                // nullable type symbol must not be preceded by a space
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation()));
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.072992700729927, 'fraction_numerical': 0.0035036496350364962, 'mean_word_length': 2.1297396071265418, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1896694', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1567, 'n_words': 456}