Пример #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);
        }
Пример #2
0
        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);
        }
Пример #3
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);
 }
Пример #4
0
        public void Format_WhenUpperInclusive_ExpectCorrectSql()
        {
            // Arrange
            var rangeFormatter = new RangeFormatter();

            var node = new RangeOperator
            {
                Name           = "ColumnName",
                Lower          = 25,
                Upper          = 100,
                LowerInclusive = false,
                UpperInclusive = true,
            };

            // Act
            var stopwatch       = Stopwatch.StartNew();
            var sqlDataResponse = rangeFormatter.Format(node, 0);

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(sqlDataResponse.Sql, Is.EqualTo("ColumnName > @ColumnName0 AND ColumnName <= @ColumnName1"));
            Assert.That(sqlDataResponse.Params.First(r => r.VarName == "ColumnName0").Value, Is.EqualTo(25));
            Assert.That(sqlDataResponse.Params.First(r => r.VarName == "ColumnName1").Value, Is.EqualTo(100));
        }
Пример #5
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))));
        }
Пример #6
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();
        }
Пример #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);
        }
Пример #8
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);
        }
Пример #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("{ }");
        }
Пример #10
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);
        }
Пример #11
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);
        }
        public void Format_WhenBinaryNode_ExpectWhereStatement()
        {
            // Arrange
            var equalsFormatter      = new EqualsFormatter();
            var lessThanFormatter    = new LessThanFormatter();
            var greaterThanFormatter = new GreaterThanFormatter();
            var rangeFormatter       = new RangeFormatter();

            var whereFormatter = new WhereFormatter(
                equalsFormatter,
                lessThanFormatter,
                greaterThanFormatter,
                rangeFormatter);

            var whereNode2 = new WhereNode
            {
                Statement = new WhereStatement
                {
                    Value = new EqualsOperator
                    {
                        Name      = "FirstName",
                        IsNot     = false,
                        Statement = "FirstName:asd",
                        Value     = "asd",
                    },
                },
            };

            var whereNode = new WhereNode
            {
                Statement = new WhereStatement
                {
                    Value = new EqualsOperator
                    {
                        Name      = "ColumnName",
                        IsNot     = false,
                        Statement = "ColumnName:bob",
                        Value     = "bob",
                    },
                },
                Conjunctive = Conjunctives.And,
                Next        = whereNode2,
            };

            // Act
            var stopwatch       = Stopwatch.StartNew();
            var sqlDataResponse = whereFormatter.Format(whereNode);

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Console.WriteLine(sqlDataResponse.Sql);
        }
Пример #13
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);
        }
Пример #14
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();
        }
Пример #15
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);
        }
Пример #16
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();
            }
        }
Пример #17
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);
        }
Пример #18
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);
        }
Пример #19
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);
        }
Пример #20
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);
        }
Пример #21
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);
        }
Пример #22
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);
        }
Пример #23
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);
        }
Пример #24
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);
        }
Пример #25
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);
        }
Пример #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);
        }
Пример #27
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);
        }
Пример #28
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);
        }
Пример #29
0
        /// <summary>
        /// Creates this instance.
        /// </summary>
        /// <returns>The <see cref="IFormatter"/>.</returns>
        public static IFormatterFactory Create()
        {
            var orderByFormatter = new OrderByFormatter();
            var pageFormatter    = new PostgresPageFormatter();

            var equalsFormatter      = new EqualsFormatter();
            var lessThanFormatter    = new LessThanFormatter();
            var greaterThanFormatter = new GreaterThanFormatter();
            var rangeFormatter       = new RangeFormatter();

            var whereFormatter = new WhereFormatter(
                equalsFormatter,
                lessThanFormatter,
                greaterThanFormatter,
                rangeFormatter);

            var standardFormatter = new Formatter(orderByFormatter, pageFormatter, whereFormatter);
            var countCteFormatter = new CountCteFormatter(orderByFormatter, pageFormatter, whereFormatter);

            return(new FormatterFactory(standardFormatter, countCteFormatter));
        }
Пример #30
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);
        }