Beispiel #1
0
        public int FormatSpan(IVsTextLines pBuffer, TextSpan[] ts)
        {
            int hr          = VSConstants.S_OK;
            int startPos    = -1;
            int endPos      = -1;
            var vsTextLines = TextBuffer.GetBufferAdapter <IVsTextLines>();

            if (ErrorHandler.Succeeded(vsTextLines.GetPositionOfLineIndex(ts[0].iStartLine, ts[0].iStartIndex, out startPos)) &&
                ErrorHandler.Succeeded(vsTextLines.GetPositionOfLineIndex(ts[0].iEndLine, ts[0].iEndIndex, out endPos)))
            {
                SnapshotSpan viewSpan = new SnapshotSpan(TextView.TextBuffer.CurrentSnapshot, startPos, endPos - startPos);

                NormalizedSnapshotSpanCollection mappedSpans = TextView.BufferGraph.MapDownToBuffer(
                    viewSpan, SpanTrackingMode.EdgeInclusive, TextBuffer);
                Debug.Assert(mappedSpans.Count == 1);

                if (mappedSpans.Count > 0)
                {
                    RangeFormatter.FormatRange(TextView, TextBuffer,
                                               new TextRange(mappedSpans[0].Start, mappedSpans[0].Length),
                                               REditorSettings.FormatOptions);
                }
            }
            return(hr);
        }
Beispiel #2
0
 public void FormatConditional(string original, int start, int end, string expected) {
     AstRoot ast;
     ITextView textView = TextViewTest.MakeTextView(original, out ast);
     RangeFormatter.FormatRange(textView, textView.TextBuffer, TextRange.FromBounds(start, end), new RFormatOptions(), _editorShell);
     string actual = textView.TextBuffer.CurrentSnapshot.GetText();
     actual.Should().Be(expected);
 }
        public int FormatSpan(IVsTextLines vsTextLines, TextSpan[] ts)
        {
            if (ErrorHandler.Succeeded(vsTextLines.GetPositionOfLineIndex(ts[0].iStartLine, ts[0].iStartIndex, out var startPos)) &&
                ErrorHandler.Succeeded(vsTextLines.GetPositionOfLineIndex(ts[0].iEndLine, ts[0].iEndIndex, out var endPos)))
            {
                var textBuffer = vsTextLines.ToITextBuffer(_services);
                var range      = TextRange.FromBounds(startPos, endPos);

                var snapshot = textBuffer.CurrentSnapshot;
                var point    = snapshot.CreateTrackingPoint(range.End, PointTrackingMode.Positive);
                var text     = snapshot.GetText(range.ToSpan());


                var line     = snapshot.GetLineFromPosition(range.Start);
                var lineTail = snapshot.GetText(Span.FromBounds(range.End, line.End));

                var formatter = new RangeFormatter(_services);
                formatter.FormatRange(TextView.ToEditorView(), textBuffer.ToEditorBuffer(), range);

                // Do not trim whitespace after standalone operators
                if (IsStandaloneOperator(text) && string.IsNullOrEmpty(lineTail))
                {
                    textBuffer.Insert(point.GetPosition(textBuffer.CurrentSnapshot), " ");
                }
            }
            return(VSConstants.S_OK);
        }
Beispiel #4
0
        public Task <TextEdit[]> FormatRangeAsync(IEditorBufferSnapshot snapshot, Range range)
        {
            var editorBuffer   = snapshot.EditorBuffer;
            var editorView     = new EditorView(editorBuffer, range.ToTextRange(snapshot).Start);
            var rangeFormatter = new RangeFormatter(_services, editorView, editorBuffer, new IncrementalTextChangeHandler());

            return(DoFormatActionAsync(snapshot, () => rangeFormatter.FormatRange(range.ToTextRange(snapshot))));
        }
Beispiel #5
0
        public void EmptyFileTest() {
            AstRoot ast;
            ITextView textView = TextViewTest.MakeTextView(string.Empty, out ast);

            RangeFormatter.FormatRange(textView, textView.TextBuffer, TextRange.EmptyRange, new RFormatOptions(), _editorShell);
            string actual = textView.TextBuffer.CurrentSnapshot.GetText();

            actual.Should().BeEmpty();
        }
Beispiel #6
0
        public void FormatScope(string content, int start, int end, string expected)
        {
            AstRoot   ast;
            ITextView textView = TextViewTest.MakeTextView(content, out ast);

            RangeFormatter.FormatRange(textView, textView.TextBuffer, TextRange.FromBounds(start, end), new RFormatOptions());
            string actual = textView.TextBuffer.CurrentSnapshot.GetText();

            actual.Should().Be(expected);
        }
Beispiel #7
0
        public void FormatScope(string content, int start, int end, string expected)
        {
            var editorView = TextViewTest.MakeTextView(content, out AstRoot ast);
            var formatter  = new RangeFormatter(_services, editorView, editorView.EditorBuffer);

            formatter.FormatRange(TextRange.FromBounds(start, end));
            var actual = editorView.EditorBuffer.CurrentSnapshot.GetText();

            actual.Should().Be(expected);
        }
Beispiel #8
0
        public void ArgumentsTest02() {
            AstRoot ast;
            ITextView textView = TextViewTest.MakeTextView("c[[a,b,]]", out ast);

            RangeFormatter.FormatRange(textView, textView.TextBuffer, TextRange.EmptyRange, new RFormatOptions(), _editorShell);
            string actual = textView.TextBuffer.CurrentSnapshot.GetText();
            string expected = "c[[a, b,]]";

            actual.Should().Be(expected);
        }
Beispiel #9
0
        public void FormatSimpleScope()
        {
            AstRoot   ast;
            ITextView textView = TextViewTest.MakeTextView("{}", out ast);

            RangeFormatter.FormatRange(textView, textView.TextBuffer, TextRange.FromBounds(0, 1), ast, new RFormatOptions());
            string actual = textView.TextBuffer.CurrentSnapshot.GetText();

            actual.Should().Be("{ }");
        }
Beispiel #10
0
        public void FormatConditionalTest05() {
            AstRoot ast;
            string original = "if(true){\n} else {}\n";
            ITextView textView = TextViewTest.MakeTextView(original, out ast);
            RangeFormatter.FormatRange(textView, textView.TextBuffer, new TextRange(original.IndexOf("else"), 0), new RFormatOptions(), _editorShell);
            string actual = textView.TextBuffer.CurrentSnapshot.GetText();

            string expected = "if(true){\n} else { }\n";
            actual.Should().Be(expected);
        }
Beispiel #11
0
        public void EmptyArgumentsTest01()
        {
            AstRoot   ast;
            ITextView textView = TextViewTest.MakeTextView("c(,,)", out ast);

            RangeFormatter.FormatRange(textView, textView.TextBuffer, TextRange.EmptyRange, ast, new RFormatOptions());
            string actual   = textView.TextBuffer.CurrentSnapshot.GetText();
            string expected = "c(,,)";

            actual.Should().Be(expected);
        }
Beispiel #12
0
        public void FormatArgumentsTest(string content, string expected)
        {
            var editorView = TextViewTest.MakeTextView(content, out AstRoot ast);
            var formatter  = new RangeFormatter(_services, editorView, editorView.EditorBuffer);

            formatter.FormatRange(TextRange.EmptyRange);

            var actual = editorView.EditorBuffer.CurrentSnapshot.GetText();

            actual.Should().Be(expected);
        }
Beispiel #13
0
        public void EmptyFileTest()
        {
            var editorView = TextViewTest.MakeTextView(string.Empty, out AstRoot ast);
            var formatter  = new RangeFormatter(_services, editorView, editorView.EditorBuffer);

            formatter.FormatRange(TextRange.EmptyRange);

            var actual = editorView.EditorBuffer.CurrentSnapshot.GetText();

            actual.Should().BeEmpty();
        }
Beispiel #14
0
        private void HandleDrop(DragDropInfo dragDropInfo, string userFolder)
        {
            var dataObject = dragDropInfo.Data;

            var document = _wpfTextView.TextBuffer.GetEditorDocument <IREditorDocument>();

            Debug.Assert(document != null, "Text view does not have associated R document.");

            var text = dataObject.GetPlainText(userFolder, dragDropInfo.KeyStates);
            var line = _wpfTextView.TextViewLines.GetTextViewLineContainingYCoordinate(dragDropInfo.Location.Y);

            line = line ?? _wpfTextView.TextViewLines.LastVisibleLine;
            if (line == null)
            {
                return;
            }

            var bufferPosition = line.GetBufferPositionFromXCoordinate(dragDropInfo.Location.X);

            bufferPosition = bufferPosition ?? line.End;

            var textBuffer   = _wpfTextView.TextBuffer;
            var dropPosition = bufferPosition.Value;

            if (_settings.FormatOnPaste)
            {
                _wpfTextView.Caret.MoveTo(dropPosition);
            }

            if (text.StartsWithOrdinal(Environment.NewLine) && Whitespace.IsNewLineBeforePosition(new TextProvider(textBuffer.CurrentSnapshot), dropPosition))
            {
                text = text.TrimStart();
            }

            var es = _services.GetService <IEditorSupport>();

            using (var undoAction = es.CreateUndoAction(_wpfTextView.ToEditorView())) {
                undoAction.Open(Windows_Resources.DragDropOperation);
                textBuffer.Replace(new Span(dropPosition, 0), text);

                if (_settings.FormatOnPaste)
                {
                    var formatter = new RangeFormatter(_services);
                    formatter.FormatRange(_wpfTextView.ToEditorView(), document.EditorBuffer, new TextRange(dropPosition, text.Length));
                }

                if (_wpfTextView.Selection != null)
                {
                    _wpfTextView.Caret.MoveTo(_wpfTextView.Selection.End);
                }
                undoAction.Commit();
            }
        }
Beispiel #15
0
        public void FormatConditionalTest04()
        {
            const string original   = "if (x > 1)\r\ny<-2";
            var          editorView = TextViewTest.MakeTextView(original, out AstRoot ast);
            var          formatter  = new RangeFormatter(_services, editorView, editorView.EditorBuffer);

            formatter.FormatRange(new TextRange(original.IndexOf('y'), 0));

            const string expected = "if (x > 1)\r\n    y <- 2";
            var          actual   = editorView.EditorBuffer.CurrentSnapshot.GetText();

            actual.Should().Be(expected);
        }
Beispiel #16
0
        public void FormatConditionalTest04()
        {
            AstRoot   ast;
            string    original = "if (x > 1)\r\ny<-2";
            ITextView textView = TextViewTest.MakeTextView(original, out ast);

            RangeFormatter.FormatRange(textView, textView.TextBuffer, new TextRange(original.IndexOf('y'), 0), ast, new RFormatOptions());

            string expected = "if (x > 1)\r\n    y <- 2";
            string actual   = textView.TextBuffer.CurrentSnapshot.GetText();

            actual.Should().Be(expected);
        }
Beispiel #17
0
        public int FormatSpan(IVsTextLines vsTextLines, TextSpan[] ts)
        {
            int hr       = VSConstants.S_OK;
            int startPos = -1;
            int endPos   = -1;

            if (ErrorHandler.Succeeded(vsTextLines.GetPositionOfLineIndex(ts[0].iStartLine, ts[0].iStartIndex, out startPos)) &&
                ErrorHandler.Succeeded(vsTextLines.GetPositionOfLineIndex(ts[0].iEndLine, ts[0].iEndIndex, out endPos)))
            {
                var textBuffer = vsTextLines.ToITextBuffer();
                RangeFormatter.FormatRange(TextView, textBuffer, TextRange.FromBounds(startPos, endPos), REditorSettings.FormatOptions);
            }
            return(hr);
        }
Beispiel #18
0
        public void FormatMultiline02() {
            string original =
@"((x %>%y)
    %>% z %>%a)";
            AstRoot ast;
            ITextView textView = TextViewTest.MakeTextView(original, out ast);

            RangeFormatter.FormatRange(textView, textView.TextBuffer, TextRange.FromBounds(
                                  original.IndexOf("%>% z"), original.IndexOf("a") + 2), new RFormatOptions(), _editorShell);
            string actual = textView.TextBuffer.CurrentSnapshot.GetText();
            string expected =
@"((x %>% y)
    %>% z %>% a)";
            actual.Should().Be(expected);
        }
Beispiel #19
0
        public void FormatOneLine() {
            AstRoot ast;
            string original =
@"foo(cache=TRUE)
foo(cache=TRUE)
";
            ITextView textView = TextViewTest.MakeTextView(original, out ast);
            RangeFormatter.FormatRange(textView, textView.TextBuffer, TextRange.FromBounds(0, original.LastIndexOf("foo", StringComparison.Ordinal)), new RFormatOptions(), _editorShell);
            string actual = textView.TextBuffer.CurrentSnapshot.GetText();

            string expected =
@"foo(cache = TRUE)
foo(cache=TRUE)
";
            actual.Should().Be(expected);
        }
Beispiel #20
0
        public void FormatConditionalTest01()
        {
            AstRoot   ast;
            string    original = "if(true){if(false){}}";
            ITextView textView = TextViewTest.MakeTextView(original, out ast);

            RangeFormatter.FormatRange(textView, textView.TextBuffer, new TextRange(4, 3), ast, new RFormatOptions());
            string actual = textView.TextBuffer.CurrentSnapshot.GetText();

            string expected =
                @"if (true) {
  if (false) { }
}";

            actual.Should().Be(expected);
        }
Beispiel #21
0
        public void FormatMultiline02()
        {
            const string original =
                @"((x %>%y)
    %>% z %>%a)";
            var editorView = TextViewTest.MakeTextView(original, out AstRoot ast);

            var formatter = new RangeFormatter(_services, editorView, editorView.EditorBuffer);

            formatter.FormatRange(TextRange.FromBounds(original.IndexOf("%>% z"), original.IndexOf("a") + 2));
            var          actual   = editorView.EditorBuffer.CurrentSnapshot.GetText();
            const string expected =
                @"((x %>% y)
    %>% z %>% a)";

            actual.Should().Be(expected);
        }
Beispiel #22
0
        public void FormatConditionalTest02()
        {
            AstRoot ast;
            string  original =
                @"if (a==a+((b +c) /x)){ 
if(func(a,b,c +2, x =2,...)){}}";

            ITextView textView = TextViewTest.MakeTextView(original, out ast);

            RangeFormatter.FormatRange(textView, textView.TextBuffer, new TextRange(2, 0), ast, new RFormatOptions());

            string actual   = textView.TextBuffer.CurrentSnapshot.GetText();
            string expected =
                @"if (a == a + ((b + c) / x)) {
if(func(a,b,c +2, x =2,...)){}}";

            actual.Should().Be(expected);
        }
Beispiel #23
0
        public void FormatOneLine()
        {
            const string original =
                @"foo(cache=TRUE)
foo(cache=TRUE)
";
            var editorView = TextViewTest.MakeTextView(original, out AstRoot ast);
            var formatter  = new RangeFormatter(_services, editorView, editorView.EditorBuffer);

            formatter.FormatRange(TextRange.FromBounds(0, original.LastIndexOf("foo", StringComparison.Ordinal)));
            var actual = editorView.EditorBuffer.CurrentSnapshot.GetText();

            const string expected =
                @"foo(cache = TRUE)
foo(cache=TRUE)
";

            actual.Should().Be(expected);
        }
Beispiel #24
0
        public int FormatSpan(IVsTextLines vsTextLines, TextSpan[] ts)
        {
            int hr       = VSConstants.S_OK;
            int startPos = -1;
            int endPos   = -1;

            if (ErrorHandler.Succeeded(vsTextLines.GetPositionOfLineIndex(ts[0].iStartLine, ts[0].iStartIndex, out startPos)) &&
                ErrorHandler.Succeeded(vsTextLines.GetPositionOfLineIndex(ts[0].iEndLine, ts[0].iEndIndex, out endPos)))
            {
                var textBuffer = vsTextLines.ToITextBuffer();
                var range      = TextRange.FromBounds(startPos, endPos);
                // Do not format standalone operators
                var text = textBuffer.CurrentSnapshot.GetText(range.ToSpan());
                if (CanFormat(text))
                {
                    RangeFormatter.FormatRange(TextView, textBuffer, range, VsAppShell.Current.GetService <IREditorSettings>().FormatOptions, VsAppShell.Current);
                }
            }
            return(hr);
        }
Beispiel #25
0
        public int FormatSpan(IVsTextLines pBuffer, TextSpan[] ts)
        {
            int hr          = VSConstants.S_OK;
            int startPos    = -1;
            int endPos      = -1;
            var vsTextLines = TextBuffer.GetBufferAdapter <IVsTextLines>();

            if (ErrorHandler.Succeeded(vsTextLines.GetPositionOfLineIndex(ts[0].iStartLine, ts[0].iStartIndex, out startPos)) &&
                ErrorHandler.Succeeded(vsTextLines.GetPositionOfLineIndex(ts[0].iEndLine, ts[0].iEndIndex, out endPos)))
            {
                var rStart = TextView.MapDownToR(startPos);
                var rEnd   = TextView.MapDownToR(endPos);

                if (rStart.HasValue && rEnd.HasValue && rStart.Value < rEnd.Value)
                {
                    RangeFormatter.FormatRange(TextView, TextBuffer, TextRange.FromBounds(rStart.Value, rEnd.Value), REditorSettings.FormatOptions);
                }
            }
            return(hr);
        }
Beispiel #26
0
        public int FormatSpan(IVsTextLines vsTextLines, TextSpan[] ts)
        {
            var startPos = -1;
            var endPos   = -1;

            if (ErrorHandler.Succeeded(vsTextLines.GetPositionOfLineIndex(ts[0].iStartLine, ts[0].iStartIndex, out startPos)) &&
                ErrorHandler.Succeeded(vsTextLines.GetPositionOfLineIndex(ts[0].iEndLine, ts[0].iEndIndex, out endPos)))
            {
                var textBuffer = vsTextLines.ToITextBuffer(_services);
                var range      = TextRange.FromBounds(startPos, endPos);
                // Do not format standalone operators
                var text = textBuffer.CurrentSnapshot.GetText(range.ToSpan());
                if (CanFormat(text))
                {
                    var formatter = new RangeFormatter(_services);
                    formatter.FormatRange(TextView.ToEditorView(), textBuffer.ToEditorBuffer(), range);
                }
            }
            return(VSConstants.S_OK);
        }
Beispiel #27
0
        public void FormatConditionalTest06()
        {
            const string original =
                @"if(true)
   while(true) {
} else {}
";
            var editorView = TextViewTest.MakeTextView(original, out AstRoot ast);
            var formatter  = new RangeFormatter(_services, editorView, editorView.EditorBuffer);

            formatter.FormatRange(new TextRange(original.IndexOf("else"), 0));
            string actual = editorView.EditorBuffer.CurrentSnapshot.GetText();

            const string expected =
                @"if(true)
   while(true) {
   } else { }
";

            actual.Should().Be(expected);
        }
Beispiel #28
0
        public void FormatConditionalTest03()
        {
            AstRoot ast;
            string  original =
                @"if(true){
  if(false){
x<-1
  }
}";
            ITextView textView = TextViewTest.MakeTextView(original, out ast);

            RangeFormatter.FormatRange(textView, textView.TextBuffer, new TextRange(original.IndexOf('x'), 1), new RFormatOptions(), _shell);
            string actual = textView.TextBuffer.CurrentSnapshot.GetText();

            string expected =
                @"if(true){
  if(false){
    x <- 1
  }
}";

            actual.Should().Be(expected);
        }
Beispiel #29
0
        public void FormatScopeLessIf01() {
            string original =
@"
if (x != nrx) 
    stop()
    if (z < ncx)
    stop()
";
            AstRoot ast;
            ITextView textView = TextViewTest.MakeTextView(original, out ast);

            RangeFormatter.FormatRange(textView, textView.TextBuffer, new TextRange(original.IndexOf("if (z"), 0), new RFormatOptions(), _editorShell);
            string actual = textView.TextBuffer.CurrentSnapshot.GetText();
            string expected =
@"
if (x != nrx) 
    stop()
if (z < ncx)
    stop()
";

            actual.Should().Be(expected);
        }
Beispiel #30
0
        public void FormatScopeLessIf01()
        {
            const string original =
                @"
if (x != nrx) 
    stop()
    if (z < ncx)
    stop()
";
            var editorView = TextViewTest.MakeTextView(original, out AstRoot ast);
            var formatter  = new RangeFormatter(_services, editorView, editorView.EditorBuffer);

            formatter.FormatRange(new TextRange(original.IndexOf("if (z"), 0));
            var          actual   = editorView.EditorBuffer.CurrentSnapshot.GetText();
            const string expected =
                @"
if (x != nrx) 
    stop()
if (z < ncx)
    stop()
";

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