Beispiel #1
0
        public void Comments(string original, string expected)
        {
            RFormatter f      = new RFormatter();
            string     actual = f.Format(original);

            actual.Should().Be(expected);
        }
Beispiel #2
0
        public void ConditionalTest02(string original, string expected)
        {
            RFormatter f      = new RFormatter();
            string     actual = f.Format(original);

            actual.Should().Be(expected);
        }
        public void Multiline(string original, string expected)
        {
            var f      = new RFormatter();
            var actual = f.Format(original);

            actual.Should().Be(expected);
        }
        public void FunctionInlineScope(string original, string expected)
        {
            var f      = new RFormatter();
            var actual = f.Format(original);

            actual.Should().Be(expected);
        }
Beispiel #5
0
        public void Formatter_FormatFunctionInlineIf07()
        {
            RFormatter f = new RFormatter();

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

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

            actual.Should().Be(expected);
        }
Beispiel #6
0
        public void NoCurlyRepeatTest(string original, string expected)
        {
            RFormatter f      = new RFormatter();
            string     actual = f.Format(original);

            actual.Should().Be(expected);
        }
Beispiel #7
0
        public override CommandResult Invoke(Guid group, int id, object inputArg, ref object outputArg)
        {
            string originalText  = TargetBuffer.CurrentSnapshot.GetText();
            string formattedText = string.Empty;
            var    formatter     = new RFormatter(Shell.GetService <IREditorSettings>().FormatOptions);

            try {
                formattedText = formatter.Format(originalText);
            } catch (Exception ex) {
                Debug.Assert(false, "Formatter exception: ", ex.Message);
            }

            if (!string.IsNullOrEmpty(formattedText) && !string.Equals(formattedText, originalText, StringComparison.Ordinal))
            {
                var selectionTracker = new RSelectionTracker(TextView, TargetBuffer, new TextRange(0, TargetBuffer.CurrentSnapshot.Length));
                selectionTracker.StartTracking(automaticTracking: false);

                try {
                    using (var massiveChange = new MassiveChange(TextView, TargetBuffer, Shell, Resources.FormatDocument)) {
                        IREditorDocument document = REditorDocument.TryFromTextBuffer(TargetBuffer);
                        if (document != null)
                        {
                            document.EditorTree.Invalidate();
                        }

                        var caretPosition = TextView.Caret.Position.BufferPosition;
                        var viewPortLeft  = TextView.ViewportLeft;

                        RTokenizer tokenizer = new RTokenizer();
                        string     oldText   = TargetBuffer.CurrentSnapshot.GetText();
                        IReadOnlyTextRangeCollection <RToken> oldTokens = tokenizer.Tokenize(oldText);
                        IReadOnlyTextRangeCollection <RToken> newTokens = tokenizer.Tokenize(formattedText);

#if DEBUG
                        //if (oldTokens.Count != newTokens.Count) {
                        //    for (int i = 0; i < Math.Min(oldTokens.Count, newTokens.Count); i++) {
                        //        if (oldTokens[i].TokenType != newTokens[i].TokenType) {
                        //            Debug.Assert(false, Invariant($"Token type difference at {i}"));
                        //            break;
                        //        } else if (oldTokens[i].Length != newTokens[i].Length) {
                        //            Debug.Assert(false, Invariant($"token length difference at {i}"));
                        //            break;
                        //        }
                        //    }
                        //}
#endif
                        IncrementalTextChangeApplication.ApplyChangeByTokens(
                            TargetBuffer,
                            new TextStream(oldText), new TextStream(formattedText),
                            oldTokens, newTokens,
                            TextRange.FromBounds(0, oldText.Length),
                            Resources.FormatDocument, selectionTracker, Shell);
                    }
                } finally {
                    selectionTracker.EndTracking();
                }
                return(new CommandResult(CommandStatus.Supported, 0));
            }
            return(CommandResult.NotSupported);
        }
Beispiel #8
0
        public void AlignComments()
        {
            RFormatter f        = new RFormatter();
            string     original =
                @"
        # comment1
    if (intercept) 
	{
x<- 1

# comment2
x<-3
}
";
            string actual   = f.Format(original);
            string expected =
                @"
# comment1
if (intercept) {
  x <- 1

  # comment2
  x <- 3
}
";

            actual.Should().Be(expected);
        }
Beispiel #9
0
        private bool FormatRangeExact(ITextRange formatRange)
        {
            var snapshot        = _editorBuffer.CurrentSnapshot;
            var spanText        = snapshot.GetText(formatRange);
            var trimmedSpanText = spanText.Trim();

            var formatter     = new RFormatter(_settings.FormatOptions);
            var formattedText = formatter.Format(trimmedSpanText);

            formattedText = formattedText.Trim(); // There may be inserted line breaks after {
                                                  // Extract existing indent before applying changes. Existing indent
                                                  // may be used by the smart indenter for function argument lists.
            var startLine = snapshot.GetLineFromPosition(formatRange.Start);
            var originalIndentSizeInSpaces = IndentBuilder.TextIndentInSpaces(startLine.GetText(), _settings.IndentSize);

            var selectionTracker = GetSelectionTracker(formatRange);
            var tokenizer        = new RTokenizer();
            var oldTokens        = tokenizer.Tokenize(spanText);
            var newTokens        = tokenizer.Tokenize(formattedText);

            _changeHandler.ApplyChange(
                _editorBuffer,
                new TextStream(spanText), new TextStream(formattedText),
                oldTokens, newTokens,
                formatRange,
                Microsoft.R.Editor.Resources.AutoFormat, selectionTracker,
                () => {
                var ast = UpdateAst(_editorBuffer);
                // Apply indentation
                IndentLines(_editorBuffer, new TextRange(formatRange.Start, formattedText.Length), ast, originalIndentSizeInSpaces);
            });

            return(true);
        }
Beispiel #10
0
        public void PreserveBreaks(string original, string expected)
        {
            RFormatter f      = new RFormatter();
            string     actual = f.Format(original);

            actual.Should().Be(expected);
        }
Beispiel #11
0
        public void EmptyFileTest()
        {
            RFormatter f = new RFormatter();
            string     s = f.Format(string.Empty);

            s.Should().BeEmpty();
        }
Beispiel #12
0
        public void FormatConditionalAlignBraces02()
        {
            RFormatter f        = new RFormatter();
            string     original =
                @"
    if (intercept) 
	{
        if(x>1)
x<- 1
else
if(x>2)
{
x<-3
}
    }
";
            string actual   = f.Format(original);
            string expected =
                @"
if (intercept) {
  if (x > 1)
    x <- 1
  else
    if (x > 2) {
      x <- 3
    }
}
";

            actual.Should().Be(expected);
        }
Beispiel #13
0
        public void FunctionInlineIf(string original, string expected)
        {
            RFormatter f      = new RFormatter();
            string     actual = f.Format(original);

            actual.Should().Be(expected);
        }
Beispiel #14
0
        public void FormatNoCurlyConditionalTest(string original, string expected)
        {
            var f      = new RFormatter();
            var actual = f.Format(original);

            actual.Should().Be(expected);
        }
Beispiel #15
0
        public void SimpleScopesTest02()
        {
            RFormatter f        = new RFormatter();
            string     actual   = f.Format("{{}}");
            string     expected = "{{ }}";

            actual.Should().Be(expected);
        }
Beispiel #16
0
        public void StatementTest01()
        {
            RFormatter f        = new RFormatter();
            string     actual   = f.Format("x<-2");
            string     expected = "x <- 2";

            actual.Should().Be(expected);
        }
Beispiel #17
0
        public void FormatNoCurlyConditionalTest05()
        {
            RFormatter f        = new RFormatter();
            string     actual   = f.Format("if(true) if(false)   x<-2 else {1}");
            string     expected = "if (true)\n  if (false) x <- 2 else {\n    1\n  }";

            actual.Should().Be(expected);
        }
Beispiel #18
0
        public void FormatNoCurlyConditionalTest02()
        {
            RFormatter f        = new RFormatter();
            string     actual   = f.Format("if(true) x<-2 else x<-1");
            string     expected = "if (true) x <- 2 else x <- 1";

            actual.Should().Be(expected);
        }
Beispiel #19
0
        public void FormatConditionalTest02()
        {
            RFormatter f        = new RFormatter();
            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  if (func(a, b, c + 2, x = 2, ...)) { }\n}";

            actual.Should().Be(expected);
        }
Beispiel #20
0
        public void FormatNoCurlyConditionalTest07()
        {
            RFormatter f        = new RFormatter();
            string     actual   = f.Format("if(true) if(false) {  x<-2 } else 1");
            string     expected = "if (true)\n  if (false) {\n    x <- 2\n  } else\n    1";

            actual.Should().Be(expected);
        }
Beispiel #21
0
        public void FormatNoCurlyConditionalTest06()
        {
            RFormatter f        = new RFormatter();
            string     actual   = f.Format("if(true) repeat { x <-1; next;} else z");
            string     expected = "if (true)\n  repeat {\n    x <- 1;\n    next;\n  } else\n  z";

            actual.Should().Be(expected);
        }
Beispiel #22
0
        public void FormatConditionalTest01()
        {
            RFormatter f        = new RFormatter();
            string     actual   = f.Format("if(true){if(false){}}");
            string     expected = "if (true) {\n  if (false) { }\n}";

            actual.Should().Be(expected);
        }
Beispiel #23
0
        public void FormatNoCurlyRepeatTest01()
        {
            RFormatter f        = new RFormatter();
            string     actual   = f.Format("repeat x<-2");
            string     expected = "repeat\n  x <- 2";

            actual.Should().Be(expected);
        }
Beispiel #24
0
        public void Formatter_FormatInlineFunction()
        {
            RFormatter f        = new RFormatter();
            string     actual   = f.Format("function(a,b) a+b");
            string     expected = @"function(a, b) a + b";

            actual.Should().Be(expected);
        }
Beispiel #25
0
        public void Formatter_FormatFunction()
        {
            RFormatter f        = new RFormatter();
            string     actual   = f.Format("function(a,b) {return(a+b)}");
            string     expected = "function(a, b) {\n  return(a + b)\n}";

            actual.Should().Be(expected);
        }
Beispiel #26
0
        public Task <TextEdit[]> FormatAsync(IEditorBufferSnapshot snapshot)
        {
            var settings = _services.GetService <IREditorSettings>();

            return(DoFormatActionAsync(snapshot, () => {
                var formattedText = new RFormatter(settings.FormatOptions).Format(snapshot.GetText());
                snapshot.EditorBuffer.Replace(TextRange.FromBounds(0, snapshot.Length), formattedText);
            }));
        }
Beispiel #27
0
        public void FormatRandom01()
        {
            RFormatter f        = new RFormatter();
            string     original = "a   b 1.  2 Inf\tNULL";

            string actual = f.Format(original);

            actual.Should().Be(@"a b 1. 2 Inf NULL");
        }
Beispiel #28
0
        public void FormatConditionalAlignBraces01()
        {
            RFormatter f        = new RFormatter();
            string     original = "\n    if (intercept) \n\t{\n        x <- cbind(1, x)\n    }\n";
            string     actual   = f.Format(original);
            string     expected = "\nif (intercept) {\n  x <- cbind(1, x)\n}\n";

            actual.Should().Be(expected);
        }
        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);
        }
Beispiel #30
0
        public void FormatForTest()
        {
            RFormatter f        = new RFormatter();
            string     original = @"for (i in  1:6) x[,i]= rowMeans(fmri[[i]])";

            string actual   = f.Format(original);
            string expected = "for (i in 1:6)\n  x[, i] = rowMeans(fmri[[i]])";

            actual.Should().Be(expected);
        }