private void WriteToken(SyntaxToken token, int indentation, SpacingKind spacingKind = SpacingKind.AsIs)
        {
            if (token.Text.Length > 0 || token.Trivia.Length > 0 || (token.IsMissing && _options.InsertMissingTokens))
            {
                WriteTrivia(token, indentation, spacingKind, token.Kind != SyntaxKind.EndOfTextToken);
            }

            if (_newCursorPosition == -1)
            {
                if (_cursorPosition >= token.TextStart && _cursorPosition <= token.End)
                {
                    _newCursorPosition = _builder.Length + (_cursorPosition - token.TextStart);
                }
                else if (_cursorPosition < token.TextStart)
                {
                    _newCursorPosition = _builder.Length;
                }
            }

            if (token.IsMissing && _options.InsertMissingTokens)
            {
                var text = SyntaxFacts.GetText(token.Kind);

                if (!string.IsNullOrEmpty(text))
                {
                    _builder.Append(text);
                }
            }
            else
            {
                _builder.Append(token.Text);
            }
        }
        private void WriteTrivia(SyntaxToken token, int indentation, SpacingKind spacingKind, bool hasFollowingToken)
        {
            var trivia         = token.Trivia;
            var cursorInTrivia = _cursorPosition >= token.TriviaStart && _cursorPosition < token.TextStart;

            switch (spacingKind)
            {
            case SpacingKind.NoSpace:
                // all spacing is removed
                return;

            case SpacingKind.SingleSpace:
                // all spacing is replace by a single space
                _builder.Append(" ");
                return;
            }

            if (spacingKind != SpacingKind.AsIs && TextFacts.HasLineBreaks(trivia))
            {
                // adjust and write all trivia lines
                for (int lineStart = 0, lineEnd = 0; lineStart < trivia.Length; lineStart = lineEnd)
                {
                    int whitespaceEnd = SkipWhitespace(trivia, lineStart);

                    if (lineStart == 0)
                    {
                        // write existing whitespace for first line, since this is whitespace that follows the last
                        // token on the last line (may contain trailing comments)
                        _builder.Append(trivia, lineStart, whitespaceEnd - lineStart);
                    }
                    else
                    {
                        // write standardized indentation instead of existing whitespace
                        WriteIndentation(indentation);
                    }

                    // write remainder of line (possible comments and/or EOL)
                    var nextLineBreakStart = TextFacts.GetNextLineBreakStart(trivia, whitespaceEnd);
                    var nextLineStart      = TextFacts.GetNextLineStart(trivia, whitespaceEnd);
                    lineEnd = nextLineStart >= 0 ? nextLineStart : trivia.Length;
                    _builder.Append(trivia, whitespaceEnd, lineEnd - whitespaceEnd);

                    // if the last thing in the trivia was a line break, add indentation for following token.
                    if (lineEnd >= trivia.Length && nextLineBreakStart >= 0 && hasFollowingToken)
                    {
                        WriteIndentation(indentation);
                    }
                }
            }
            else
            {
                switch (spacingKind)
                {
                case SpacingKind.NoSpaceIfOnSameLine:
                    // there was no line break, so make it have no spacing by writing nothing
                    break;

                case SpacingKind.SingleSpaceIfOnSameLine:
                    // there was no line break, so make it a single space by writing a single space
                    _builder.Append(" ");
                    break;

                case SpacingKind.NewLine:
                    // there was no line break so add one
                    _builder.AppendLine();
                    WriteIndentation(indentation);
                    break;

                case SpacingKind.AsIs:
                case SpacingKind.AlignOnly:
                default:
                    _builder.Append(trivia);
                    break;
                }
            }
        }