コード例 #1
0
        private int GetRuleSpacesOrIndentation(
            LineColumn lineColumnBeforeTrivia1, SyntaxTrivia trivia1, LineColumn lineColumnAfterTrivia1, LineColumnDelta existingWhitespaceBetween, SyntaxTrivia trivia2, LineColumnRule rule)
        {
            var lineColumnAfterExistingWhitespace = lineColumnAfterTrivia1.With(existingWhitespaceBetween);

            // next trivia is moved to next line or already on a new line, use indentation
            if (rule.Lines > 0 || lineColumnAfterExistingWhitespace.WhitespaceOnly)
            {
                switch (rule.IndentationOperation)
                {
                case LineColumnRule.IndentationOperations.Absolute:
                    return(Math.Max(0, rule.Indentation));

                case LineColumnRule.IndentationOperations.Default:
                    return(this.Context.GetBaseIndentation(trivia2.RawKind == 0 ? this.EndPosition : trivia2.SpanStart));

                case LineColumnRule.IndentationOperations.Given:
                    return((trivia2.RawKind == 0) ? this.Spaces : Math.Max(0, this.indentation));

                case LineColumnRule.IndentationOperations.Follow:
                    return(Math.Max(0, lineColumnBeforeTrivia1.Column));

                case LineColumnRule.IndentationOperations.Preserve:
                    return(existingWhitespaceBetween.Spaces);

                default:
                    throw Contract.Unreachable;
                }
            }

            // okay, we are not on a its own line, use space information
            switch (rule.SpaceOperation)
            {
            case LineColumnRule.SpaceOperations.Preserve:
                return(Math.Max(rule.Spaces, existingWhitespaceBetween.Spaces));

            case LineColumnRule.SpaceOperations.Force:
                return(Math.Max(rule.Spaces, 0));

            default:
                throw Contract.Unreachable;
            }
        }
コード例 #2
0
        private LineColumn FormatFirstTriviaAndWhitespaceAfter <T>(
            LineColumn lineColumnBeforeTrivia1,
            SyntaxTrivia trivia1,
            LineColumnDelta existingWhitespaceBetween,
            SyntaxTrivia trivia2,
            Formatter <T> format,
            WhitespaceAppender <T> addWhitespaceTrivia,
            List <T> changes,
            CancellationToken cancellationToken)
        {
            var lineColumnAfterTrivia1 = trivia1.RawKind == 0 ?
                                         lineColumnBeforeTrivia1 : lineColumnBeforeTrivia1.With(format(lineColumnBeforeTrivia1, trivia1, changes, cancellationToken));

            var rule            = GetOverallLineColumnRuleBetween(trivia1, existingWhitespaceBetween, trivia2);
            var whitespaceDelta = Apply(lineColumnBeforeTrivia1, trivia1, lineColumnAfterTrivia1, existingWhitespaceBetween, trivia2, rule);

            var span = GetTextSpan(trivia1, trivia2);

            addWhitespaceTrivia(lineColumnAfterTrivia1, whitespaceDelta, span, changes);

            return(lineColumnAfterTrivia1.With(whitespaceDelta));
        }
コード例 #3
0
        protected LineColumn GetLineColumn(LineColumn lineColumn, SyntaxTrivia trivia)
        {
            var text = trivia.ToFullString();

            return(lineColumn.With(GetLineColumnDelta(lineColumn.Column, text)));
        }
コード例 #4
0
        private LineColumnDelta GetLineColumnOfWhitespace(
            LineColumn lineColumn,
            SyntaxTrivia previousTrivia,
            SyntaxTrivia trivia1,
            LineColumnDelta whitespaceBetween,
            SyntaxTrivia trivia2)
        {
            Contract.Requires(IsWhitespaceOrEndOfLine(trivia2));

            // treat elastic as new line as long as its previous trivia is not elastic or
            // it has line break right before it
            if (trivia2.IsElastic())
            {
                // eat up consecutive elastic trivia or next line
                if (trivia1.IsElastic() || IsEndOfLine(trivia1))
                {
                    return(LineColumnDelta.Default);
                }

                // if there was alrady new lines, ignore elastic
                var lineColumnAfterPreviousTrivia = GetLineColumn(lineColumn, previousTrivia);

                var newLineFromPreviousOperation = (whitespaceBetween.Lines > 0) ||
                                                   (lineColumnAfterPreviousTrivia.Line > 0 && lineColumnAfterPreviousTrivia.Column == 0);
                if (newLineFromPreviousOperation && whitespaceBetween.WhitespaceOnly)
                {
                    return(LineColumnDelta.Default);
                }

                return(new LineColumnDelta(lines: 1, spaces: 0, whitespaceOnly: true, forceUpdate: true));
            }

            if (IsEndOfLine(trivia2))
            {
                return(new LineColumnDelta(lines: 1, spaces: 0, whitespaceOnly: true, forceUpdate: false));
            }

            var text = trivia2.ToFullString();

            return(new LineColumnDelta(
                       lines: 0,
                       spaces: text.ConvertTabToSpace(this.OptionSet.GetOption(FormattingOptions.TabSize, this.Language), lineColumn.With(whitespaceBetween).Column, text.Length),
                       whitespaceOnly: true,
                       forceUpdate: false));
        }