File: Syntax\InternalSyntax\SyntaxTrivia.cs
Web Access
Project: src\src\Compilers\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.csproj (Microsoft.CodeAnalysis.CSharp)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
 
using System.Diagnostics;
using Roslyn.Utilities;
 
namespace Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax
{
    internal class SyntaxTrivia : CSharpSyntaxNode
    {
        public readonly string Text;
 
        internal SyntaxTrivia(SyntaxKind kind, string text, DiagnosticInfo[]? diagnostics = null, SyntaxAnnotation[]? annotations = null)
            : base(kind, diagnostics, annotations, text.Length)
        {
            this.Text = text;
            if (kind == SyntaxKind.PreprocessingMessageTrivia)
            {
                SetFlags(NodeFlags.ContainsSkippedText);
            }
        }
 
        public override bool IsTrivia => true;
 
        internal static SyntaxTrivia Create(SyntaxKind kind, string text)
        {
            return new SyntaxTrivia(kind, text);
        }
 
        public override string ToFullString()
        {
            return this.Text;
        }
 
        public override string ToString()
        {
            return this.Text;
        }
 
        internal override GreenNode GetSlot(int index)
        {
            throw ExceptionUtilities.Unreachable();
        }
 
        public override int Width
        {
            get
            {
                Debug.Assert(this.FullWidth == this.Text.Length);
                return this.FullWidth;
            }
        }
 
        public override int GetLeadingTriviaWidth()
        {
            return 0;
        }
 
        public override int GetTrailingTriviaWidth()
        {
            return 0;
        }
 
        internal override GreenNode SetDiagnostics(DiagnosticInfo[]? diagnostics)
        {
            return new SyntaxTrivia(this.Kind, this.Text, diagnostics, GetAnnotations());
        }
 
        internal override GreenNode SetAnnotations(SyntaxAnnotation[]? annotations)
        {
            return new SyntaxTrivia(this.Kind, this.Text, GetDiagnostics(), annotations);
        }
 
        public override TResult Accept<TResult>(CSharpSyntaxVisitor<TResult> visitor)
        {
            return visitor.VisitTrivia(this);
        }
 
        public override void Accept(CSharpSyntaxVisitor visitor)
        {
            visitor.VisitTrivia(this);
        }
 
        protected override void WriteTriviaTo(System.IO.TextWriter writer)
        {
            writer.Write(Text);
        }
 
        public static implicit operator CodeAnalysis.SyntaxTrivia(SyntaxTrivia trivia)
        {
            return new CodeAnalysis.SyntaxTrivia(token: default, trivia, position: 0, index: 0);
        }
 
        public override bool IsEquivalentTo(GreenNode? other)
        {
            if (!base.IsEquivalentTo(other))
            {
                return false;
            }
 
            if (this.Text != ((SyntaxTrivia)other).Text)
            {
                return false;
            }
 
            return true;
        }
 
        internal override SyntaxNode CreateRed(SyntaxNode? parent, int position)
        {
            throw ExceptionUtilities.Unreachable();
        }
    }
}