예제 #1
0
        public void Statements(string original, bool breakMultipleStatements, string expected)
        {
            var options = new RFormatOptions {
                BreakMultipleStatements = breakMultipleStatements
            };
            var f      = new RFormatter(options);
            var actual = f.Format(original);

            actual.Should().Be(expected);
        }
예제 #2
0
        public void ConditionalTest04(string original, string expected)
        {
            RFormatOptions options = new RFormatOptions();

            options.BracesOnNewLine = true;

            RFormatter f      = new RFormatter(options);
            string     actual = f.Format(original);

            actual.Should().Be(expected);
        }
예제 #3
0
        public void FormatFunctionNoSpaceAfterComma(string original, string expected)
        {
            RFormatOptions options = new RFormatOptions();

            options.SpaceAfterComma = false;

            RFormatter f      = new RFormatter(options);
            string     actual = f.Format(original);

            actual.Should().Be(expected);
        }
예제 #4
0
        public void ConditionalTest06(string original, bool spaceBeforeCurly, string expected)
        {
            var options = new RFormatOptions()
            {
                SpaceBeforeCurly = spaceBeforeCurly
            };
            RFormatter f      = new RFormatter(options);
            string     actual = f.Format(original);

            actual.Should().Be(expected);
        }
예제 #5
0
        public void FormatFunctionNoSpaceAfterComma(string original, string expected)
        {
            var options = new RFormatOptions()
            {
                SpaceAfterComma = false
            };
            var f      = new RFormatter(options);
            var actual = f.Format(original);

            actual.Should().Be(expected);
        }
예제 #6
0
        public void Formatter_FormatEquals(string original, bool spaceAroundEquals, string expected)
        {
            var options = new RFormatOptions()
            {
                SpacesAroundEquals = spaceAroundEquals
            };
            RFormatter f      = new RFormatter(options);
            string     actual = f.Format(original);

            actual.Should().Be(expected);
        }
예제 #7
0
        public void ConditionalTest04(string original, string expected)
        {
            var options = new RFormatOptions {
                BracesOnNewLine = true
            };

            var f      = new RFormatter(options);
            var actual = f.Format(original);

            actual.Should().Be(expected);
        }
예제 #8
0
        public void FormatConditionalTest04()
        {
            RFormatOptions options = new RFormatOptions();

            options.BracesOnNewLine = true;

            RFormatter f        = new RFormatter(options);
            string     actual   = f.Format("if(TRUE) { 1 } else {2} x<-1");
            string     expected = "if (TRUE)\n{\n  1\n} else\n{\n  2\n}\nx <- 1";

            actual.Should().Be(expected);
        }
예제 #9
0
        public void Formatter_FormatFunctionNoSpaceAfterComma()
        {
            RFormatOptions options = new RFormatOptions();

            options.SpaceAfterComma = false;

            RFormatter f        = new RFormatter(options);
            string     actual   = f.Format("function(a, b) {return(a+b)}");
            string     expected = "function(a,b) {\n  return(a + b)\n}";

            actual.Should().Be(expected);
        }
예제 #10
0
        public void FormatNoCurlyConditionalTest03()
        {
            RFormatOptions options = new RFormatOptions();

            options.IndentType = IndentType.Tabs;

            RFormatter f        = new RFormatter(options);
            string     actual   = f.Format("if(true)    x<-2");
            string     expected = "if (true)\n\tx <- 2";

            actual.Should().Be(expected);
        }
예제 #11
0
        public void ConditionalTest05()
        {
            RFormatOptions options = new RFormatOptions();

            options.BracesOnNewLine = true;

            RFormatter f        = new RFormatter(options);
            string     actual   = f.Format("if(TRUE) { 1 } else if(FALSE) {2} else {3} x<-1");
            string     expected = "if (TRUE) { 1 } else if (FALSE) { 2 } else { 3 }\nx <- 1";

            actual.Should().Be(expected);
        }
예제 #12
0
        public void ConditionalTest05()
        {
            var options = new RFormatOptions {
                BracesOnNewLine = true
            };

            var f        = new RFormatter(options);
            var actual   = f.Format("if(TRUE) { 1 } else if(FALSE) {2} else {3} x<-1");
            var expected = "if (TRUE) { 1 } else if (FALSE) { 2 } else { 3 }\nx <- 1";

            actual.Should().Be(expected);
        }
예제 #13
0
        public void ConditionalTest03(string original, string expected)
        {
            RFormatOptions options = new RFormatOptions();

            options.BracesOnNewLine = true;
            options.IndentSize      = 2;
            options.IndentType      = IndentType.Tabs;
            options.TabSize         = 2;

            RFormatter f      = new RFormatter(options);
            string     actual = f.Format(original);

            actual.Should().Be(expected);
        }
예제 #14
0
        public void FormatFunctionAlignArguments()
        {
            var options = new RFormatOptions()
            {
                IndentType = IndentType.Tabs,
                TabSize    = 2
            };
            var f        = new RFormatter(options);
            var original = "x <- function (x,  \n intercept=TRUE, tolerance =1e-07, \n    yname = NULL)\n";
            var actual   = f.Format(original);
            var expected = "x <- function(x,\n intercept = TRUE, tolerance = 1e-07,\n\t\tyname = NULL)\n";

            actual.Should().Be(expected);
        }
예제 #15
0
        public void FormatFunctionAlignArguments()
        {
            RFormatOptions options = new RFormatOptions();

            options.IndentType = IndentType.Tabs;
            options.TabSize    = 2;

            RFormatter f        = new RFormatter(options);
            string     original = "x <- function (x,  \n intercept=TRUE, tolerance =1e-07, \n    yname = NULL)\n";
            string     actual   = f.Format(original);
            string     expected = "x <- function(x,\n intercept = TRUE, tolerance = 1e-07,\n\t\tyname = NULL)\n";

            actual.Should().Be(expected);
        }
예제 #16
0
        public void ConditionalTest03(string original, string expected)
        {
            var options = new RFormatOptions {
                BracesOnNewLine = true,
                IndentSize      = 2,
                IndentType      = IndentType.Tabs,
                TabSize         = 2
            };

            var f      = new RFormatter(options);
            var actual = f.Format(original);

            actual.Should().Be(expected);
        }
예제 #17
0
        public void FormatConditionalTest03()
        {
            RFormatOptions options = new RFormatOptions();

            options.BracesOnNewLine = true;
            options.IndentSize      = 2;
            options.IndentType      = IndentType.Tabs;
            options.TabSize         = 2;

            RFormatter f        = new RFormatter(options);
            string     actual   = f.Format("if(a == a+((b+c)/x)){if(func(a,b, c+2, x=2, ...)){}}");
            string     expected = "if (a == a + ((b + c) / x))\n{\n\tif (func(a, b, c + 2, x = 2, ...)) { }\n}";

            actual.Should().Be(expected);
        }
예제 #18
0
        public static int GetBlockIndent(ITextSnapshotLine line, RFormatOptions options)
        {
            int lineNumber = line.LineNumber;

            //Scan the previous lines for the first line that isn't an empty line.
            while (--lineNumber >= 0)
            {
                ITextSnapshotLine previousLine = line.Snapshot.GetLineFromLineNumber(lineNumber);
                if (previousLine.Length > 0)
                {
                    return(OuterIndentSizeFromLine(previousLine, options));
                }
            }

            return(0);
        }
예제 #19
0
        public static bool FormatRange(ITextView textView, ITextBuffer textBuffer,
                                       ITextRange formatRange, RFormatOptions options)
        {
            ITextSnapshot snapshot = textBuffer.CurrentSnapshot;
            int           start    = formatRange.Start;
            int           end      = formatRange.End;

            // When user clicks editor margin to select a line, selection actually
            // ends in the beginning of the next line. In order to prevent formatting
            // of the next line that user did not select, we need to shrink span to
            // format and exclude the trailing line break.
            ITextSnapshotLine line = snapshot.GetLineFromPosition(formatRange.End);

            if (line.Start.Position == formatRange.End && formatRange.Length > 0)
            {
                if (line.LineNumber > 0)
                {
                    line  = snapshot.GetLineFromLineNumber(line.LineNumber - 1);
                    end   = line.End.Position;
                    start = Math.Min(start, end);
                }
            }

            // Expand span to include the entire line
            ITextSnapshotLine startLine = snapshot.GetLineFromPosition(start);
            ITextSnapshotLine endLine   = snapshot.GetLineFromPosition(end);

            // In case of formatting of multiline expressions formatter needs
            // to know the entire expression since otherwise it may not correctly
            // preserve user indentation. Consider 'x >% y' which is a plain statement
            // and needs to be indented at regular scope level vs
            //
            //      a %>% b %>%
            //          x %>% y
            //
            // where user indentation of 'x %>% y' must be preserved. We don't have
            // complete information here since expression may not be syntactically
            // correct and hence AST may not have correct information and besides,
            // the AST is damaged at this point. As a workaround, we will check
            // if the previous line ends with an operator current line starts with
            // an operator.
            int startPosition = FindStartOfExpression(textBuffer, startLine.Start);

            formatRange = TextRange.FromBounds(startPosition, endLine.End);
            return(FormatRangeExact(textView, textBuffer, formatRange, options));
        }
예제 #20
0
        public static bool FormatRangeExact(ITextView textView, ITextBuffer textBuffer,
                                            ITextRange formatRange, RFormatOptions options)
        {
            ITextSnapshot snapshot        = textBuffer.CurrentSnapshot;
            Span          spanToFormat    = new Span(formatRange.Start, formatRange.Length);
            string        spanText        = snapshot.GetText(spanToFormat.Start, spanToFormat.Length);
            string        trimmedSpanText = spanText.Trim();

            RFormatter formatter     = new RFormatter(options);
            string     formattedText = formatter.Format(trimmedSpanText);

            formattedText = formattedText.Trim(); // There may be inserted line breaks after {
            // Apply formatted text without indentation. We then will update the parse tree
            // so we can calculate proper line indents from the AST via the smart indenter.
            if (!spanText.Equals(formattedText, StringComparison.Ordinal))
            {
                // Extract existing indent before applying changes. Existing indent
                // may be used by the smart indenter for function argument lists.
                var startLine = snapshot.GetLineFromPosition(spanToFormat.Start);
                var originalIndentSizeInSpaces = IndentBuilder.TextIndentInSpaces(startLine.GetText(), options.IndentSize);

                var        selectionTracker = new RSelectionTracker(textView, textBuffer, formatRange);
                RTokenizer tokenizer        = new RTokenizer();
                IReadOnlyTextRangeCollection <RToken> oldTokens = tokenizer.Tokenize(spanText);
                IReadOnlyTextRangeCollection <RToken> newTokens = tokenizer.Tokenize(formattedText);

                IncrementalTextChangeApplication.ApplyChangeByTokens(
                    textBuffer,
                    new TextStream(spanText), new TextStream(formattedText),
                    oldTokens, newTokens,
                    formatRange,
                    Resources.AutoFormat, selectionTracker,
                    () => {
                    var ast = UpdateAst(textBuffer);
                    // Apply indentation
                    IndentLines(textView, textBuffer, new TextRange(formatRange.Start, formattedText.Length), ast, options, originalIndentSizeInSpaces);
                });

                return(true);
            }

            return(false);
        }
예제 #21
0
        public static bool FormatRangeExact(ITextView textView, ITextBuffer textBuffer, ITextRange formatRange,
                                            AstRoot ast, RFormatOptions options,
                                            int scopeStatementPosition, bool respectUserIndent = true)
        {
            ITextSnapshot snapshot        = textBuffer.CurrentSnapshot;
            Span          spanToFormat    = new Span(formatRange.Start, formatRange.Length);
            string        spanText        = snapshot.GetText(spanToFormat.Start, spanToFormat.Length);
            string        trimmedSpanText = spanText.Trim();

            if (trimmedSpanText == "}")
            {
                // Locate opening { and its statement
                var scopeNode = ast.GetNodeOfTypeFromPosition <IAstNodeWithScope>(spanToFormat.Start);
                if (scopeNode != null)
                {
                    scopeStatementPosition = scopeNode.Start;
                }
            }

            RFormatter formatter     = new RFormatter(options);
            string     formattedText = formatter.Format(trimmedSpanText);

            formattedText = formattedText.Trim(); // there may be inserted line breaks after {
            formattedText = IndentLines(textBuffer, spanToFormat.Start, ast, formattedText, options, scopeStatementPosition, respectUserIndent);

            if (!spanText.Equals(formattedText, StringComparison.Ordinal))
            {
                var        selectionTracker = new RSelectionTracker(textView, textBuffer);
                RTokenizer tokenizer        = new RTokenizer();
                IReadOnlyTextRangeCollection <RToken> oldTokens = tokenizer.Tokenize(spanText);
                IReadOnlyTextRangeCollection <RToken> newTokens = tokenizer.Tokenize(formattedText);
                IncrementalTextChangeApplication.ApplyChangeByTokens(
                    textBuffer,
                    new TextStream(spanText), new TextStream(formattedText),
                    oldTokens, newTokens,
                    formatRange,
                    Resources.AutoFormat, selectionTracker);
                return(true);
            }

            return(false);
        }
예제 #22
0
        public void Formatter_FormatFunctionInlineIf06()
        {
            RFormatOptions options = new RFormatOptions();

            options.BracesOnNewLine = true;

            RFormatter f = new RFormatter(options);

            string original =
                @"x <- func(a,
   {
      if(TRUE) 1 else 2
   })";

            string actual   = f.Format(original);
            string expected =
                @"x <- func(a,
   {
     if (TRUE) 1 else 2
   })";

            actual.Should().Be(expected);
        }
예제 #23
0
        private static void IndentCaretInNewScope(ITextView textView, IScope scope, SnapshotPoint caretBufferPoint, RFormatOptions options)
        {
            if (scope == null || scope.OpenCurlyBrace == null)
            {
                return;
            }
            ITextSnapshot rSnapshot      = caretBufferPoint.Snapshot;
            ITextBuffer   rTextBuffer    = rSnapshot.TextBuffer;
            int           rCaretPosition = caretBufferPoint.Position;

            var caretLine       = rSnapshot.GetLineFromPosition(rCaretPosition);
            int innerIndentSize = SmartIndenter.InnerIndentSizeFromNode(rTextBuffer, scope, options);

            int openBraceLineNumber = rSnapshot.GetLineNumberFromPosition(scope.OpenCurlyBrace.Start);
            var braceLine           = rSnapshot.GetLineFromLineNumber(openBraceLineNumber);
            var indentLine          = rSnapshot.GetLineFromLineNumber(openBraceLineNumber + 1);

            string lineBreakText = braceLine.GetLineBreakText();

            rTextBuffer.Insert(indentLine.Start, lineBreakText);

            // Fetch the line again since snapshot has changed when line break was inserted
            indentLine = rTextBuffer.CurrentSnapshot.GetLineFromLineNumber(openBraceLineNumber + 1);

            // Map new caret position back to the view
            var positionInView = textView.MapUpToView(indentLine.Start);

            if (positionInView.HasValue)
            {
                var viewIndentLine = textView.TextBuffer.CurrentSnapshot.GetLineFromPosition(positionInView.Value);
                textView.Caret.MoveTo(new VirtualSnapshotPoint(viewIndentLine.Start, innerIndentSize));
            }
        }
예제 #24
0
 public override void ResetSettings()
 {
     _formatOptions = new RFormatOptions();
     CreateLintOptions();
     base.ResetSettings();
 }
예제 #25
0
        private static void IndentCaretInNewScope(ITextView textView, ITextBuffer textBuffer, IScope scope, RFormatOptions options)
        {
            ITextSnapshot snapshot = textBuffer.CurrentSnapshot;

            SnapshotPoint?positionInBuffer = textView.MapDownToBuffer(textView.Caret.Position.BufferPosition, textBuffer);

            if (!positionInBuffer.HasValue || scope == null || scope.OpenCurlyBrace == null)
            {
                return;
            }

            int position = positionInBuffer.Value.Position;
            ITextSnapshotLine caretLine = snapshot.GetLineFromPosition(position);

            int innerIndentSize = SmartIndenter.InnerIndentSizeFromNode(textBuffer, scope, options);

            int openBraceLineNumber         = snapshot.GetLineNumberFromPosition(scope.OpenCurlyBrace.Start);
            ITextSnapshotLine braceLine     = snapshot.GetLineFromLineNumber(openBraceLineNumber);
            ITextSnapshotLine indentLine    = snapshot.GetLineFromLineNumber(openBraceLineNumber + 1);
            string            lineBreakText = braceLine.GetLineBreakText();

            textBuffer.Insert(indentLine.Start, lineBreakText);

            positionInBuffer = textView.MapUpToBuffer(indentLine.Start.Position, textView.TextBuffer);
            if (!positionInBuffer.HasValue)
            {
                return;
            }

            indentLine = textView.TextBuffer.CurrentSnapshot.GetLineFromPosition(positionInBuffer.Value);
            textView.Caret.MoveTo(new VirtualSnapshotPoint(indentLine, innerIndentSize));
        }
예제 #26
0
        public static int OuterIndentSizeFromNode(ITextBuffer textBuffer, IAstNode node, RFormatOptions options)
        {
            if (node != null)
            {
                ITextSnapshotLine startLine = textBuffer.CurrentSnapshot.GetLineFromPosition(node.Start);
                return(OuterIndentSizeFromLine(startLine, options));
            }

            return(0);
        }
예제 #27
0
        public static int InnerIndentSizeFromNode(ITextBuffer textBuffer, IAstNode node, RFormatOptions options)
        {
            if (node != null)
            {
                // Scope indentation is based on the scope defining node i.e.
                // x <- function(a) {
                //      |
                // }
                // caret indent is based on the function definition and not
                // on the position of the opening {
                var scope = node as IScope;
                if (scope != null)
                {
                    var scopeDefiningNode = node.Parent as IAstNodeWithScope;
                    if (scopeDefiningNode != null && scopeDefiningNode.Scope == scope)
                    {
                        node = scopeDefiningNode;
                    }
                }
                ITextSnapshotLine startLine = textBuffer.CurrentSnapshot.GetLineFromPosition(node.Start);
                return(InnerIndentSizeFromLine(startLine, options));
            }

            return(0);
        }
예제 #28
0
        public static void FormatFile(CoreTestFilesFixture fixture, string name, RFormatOptions options)
        {
            Action a = () => FormatFileImplementation(fixture, name, options);

            a.ShouldNotThrow();
        }
예제 #29
0
        private static void FormatFileImplementation(CoreTestFilesFixture fixture, string name, RFormatOptions options)
        {
            string testFile     = fixture.GetDestinationPath(name);
            string baselineFile = testFile + ".formatted";
            string text         = fixture.LoadDestinationFile(name);

            RFormatter formatter = new RFormatter(options);

            string actual = formatter.Format(text);

            if (_regenerateBaselineFiles)
            {
                // Update this to your actual enlistment if you need to update baseline
                baselineFile = Path.Combine(fixture.SourcePath, @"Formatting\", Path.GetFileName(testFile)) + ".formatted";
                TestFiles.UpdateBaseline(baselineFile, actual);
            }
            else
            {
                TestFiles.CompareToBaseLine(baselineFile, actual);
            }
        }
예제 #30
0
 public static void ResetSettings()
 {
     WritableStorage?.ResetSettings();
     _formatOptions = new RFormatOptions();
 }