예제 #1
0
        private ITextView TestAutoFormat(int position, string textToType, string initialContent = "")
        {
            AstRoot   ast;
            ITextView textView = TextViewTest.MakeTextView(initialContent, position, out ast);

            textView.TextBuffer.Changed += (object sender, TextContentChangedEventArgs e) => {
                List <TextChangeEventArgs> textChanges = TextUtility.ConvertToRelative(e);
                ast.ReflectTextChanges(textChanges);

                if (e.Changes[0].NewText.Length == 1)
                {
                    char ch = e.Changes[0].NewText[0];
                    if (AutoFormat.IsAutoformatTriggerCharacter(ch))
                    {
                        int offset = 0;
                        if (e.Changes[0].NewText[0].IsLineBreak())
                        {
                            position = e.Changes[0].OldPosition + 1;
                            textView.Caret.MoveTo(new SnapshotPoint(e.After, position));
                            offset = -1;
                        }
                        FormatOperations.FormatLine(textView, textView.TextBuffer, ast, offset);
                    }
                }
                else
                {
                    ITextSnapshotLine line = e.After.GetLineFromPosition(position);
                    textView.Caret.MoveTo(new SnapshotPoint(e.After, Math.Min(e.After.Length, line.Length + 1)));
                }
            };

            Typing.Type(textView.TextBuffer, position, textToType);

            return(textView);
        }
예제 #2
0
        private ITextView MakeTextView(string content)
        {
            var editorView = TextViewTest.MakeTextView(content, 0, out AstRoot ast);
            var document   = new EditorDocumentMock(new EditorTreeMock(editorView.EditorBuffer, ast));

            return(editorView.As <ITextView>());
        }
예제 #3
0
        public void RBraceMatch_Braces()
        {
            ITextView     tv = TextViewTest.MakeTextViewRealTextBuffer("a(\"( )\")b", _exportProvider);
            RBraceMatcher bm = new RBraceMatcher(tv, tv.TextBuffer);

            int  startPosition, endPosition;
            bool result;

            result = bm.GetBracesFromPosition(tv.TextBuffer.CurrentSnapshot, 0, false, out startPosition, out endPosition);

            result.Should().BeFalse();

            result = bm.GetBracesFromPosition(tv.TextBuffer.CurrentSnapshot, 1, false, out startPosition, out endPosition);

            result.Should().BeTrue();
            startPosition.Should().Be(1);
            endPosition.Should().Be(7);

            result = bm.GetBracesFromPosition(tv.TextBuffer.CurrentSnapshot, 2, false, out startPosition, out endPosition);

            result.Should().BeTrue();
            startPosition.Should().Be(1);
            endPosition.Should().Be(7);

            result = bm.GetBracesFromPosition(tv.TextBuffer.CurrentSnapshot, 3, false, out startPosition, out endPosition);

            result.Should().BeFalse();
        }
예제 #4
0
        public void Commenter_CommentTest02()
        {
            string original =
                @"
    x <- 1
x <- 2
";
            var textView   = TextViewTest.MakeTextView(original, new TextRange(8, 8)).As <ITextView>();
            var textBuffer = textView.TextBuffer;

            var command = new CommentCommand(textView, textBuffer, _services);
            var status  = command.Status(VSConstants.VSStd2K, (int)VSConstants.VSStd2KCmdID.COMMENT_BLOCK);

            status.Should().Be(CommandStatus.SupportedAndEnabled);

            object o = null;

            command.Invoke(Guid.Empty, 0, null, ref o);

            string expected =
                @"
    #x <- 1
#x <- 2
";

            string actual = textBuffer.CurrentSnapshot.GetText();

            actual.Should().Be(expected);
        }
예제 #5
0
        private ITextView TestAutoFormat(int position, string initialContent = "")
        {
            AstRoot   ast;
            ITextView textView = TextViewTest.MakeTextView(initialContent, position, out ast);

            textView.TextBuffer.Changed += (object sender, TextContentChangedEventArgs e) => {
                ast.ReflectTextChanges(e.ConvertToRelative(), new TextProvider(textView.TextBuffer.CurrentSnapshot));

                if (e.Changes[0].NewText.Length == 1)
                {
                    char ch = e.Changes[0].NewText[0];
                    if (AutoFormat.IsPostProcessAutoformatTriggerCharacter(ch))
                    {
                        position = e.Changes[0].OldPosition + 1;
                        textView.Caret.MoveTo(new SnapshotPoint(e.After, position));
                        FormatOperations.FormatViewLine(textView, textView.TextBuffer, -1, _exportProvider.GetExportedValue <IEditorShell>());
                    }
                }
                else
                {
                    ITextSnapshotLine line = e.After.GetLineFromPosition(position);
                    textView.Caret.MoveTo(new SnapshotPoint(e.After, Math.Min(e.After.Length, line.Length + 1)));
                }
            };

            Typing.Type(textView.TextBuffer, position, "\n");

            return(textView);
        }
예제 #6
0
        public void RBraceMatch_CurlyBraces01()
        {
            ITextView     tv = TextViewTest.MakeTextViewRealTextBuffer("a{\"{ }\"}b", _services);
            RBraceMatcher bm = new RBraceMatcher(tv, tv.TextBuffer);

            int  startPosition, endPosition;
            bool result;

            result = bm.GetBracesFromPosition(tv.TextBuffer.CurrentSnapshot, 0, false, out startPosition, out endPosition);
            result.Should().BeFalse();

            result = bm.GetBracesFromPosition(tv.TextBuffer.CurrentSnapshot, 1, false, out startPosition, out endPosition);

            result.Should().BeTrue();
            startPosition.Should().Be(1);
            endPosition.Should().Be(7);

            result = bm.GetBracesFromPosition(tv.TextBuffer.CurrentSnapshot, 2, false, out startPosition, out endPosition);

            result.Should().BeTrue();
            startPosition.Should().Be(1);
            endPosition.Should().Be(7);

            result = bm.GetBracesFromPosition(tv.TextBuffer.CurrentSnapshot, 3, false, out startPosition, out endPosition);

            result.Should().BeFalse();
        }
예제 #7
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);
 }
예제 #8
0
        public void Commenter_UncommentTest01()
        {
            string original =
                @"
    #x <- 1
x <- 2
";
            ITextView   textView   = TextViewTest.MakeTextView(original, new TextRange(2, 0));
            ITextBuffer textBuffer = textView.TextBuffer;

            var           command = new UncommentCommand(textView, textBuffer, _editorShell);
            CommandStatus status  = command.Status(VSConstants.VSStd2K, (int)VSConstants.VSStd2KCmdID.UNCOMMENT_BLOCK);

            status.Should().Be(CommandStatus.SupportedAndEnabled);

            object o = null;

            command.Invoke(Guid.Empty, 0, null, ref o);

            string expected =
                @"
    x <- 1
x <- 2
";

            string actual = textBuffer.CurrentSnapshot.GetText();

            actual.Should().Be(expected);
        }
예제 #9
0
        private ITextView TestAutoFormat(int position, string initialContent = "")
        {
            var editorView = TextViewTest.MakeTextView(initialContent, position, out AstRoot ast);
            var textView   = editorView.As <ITextView>();
            var af         = new AutoFormat(textView, _services);

            textView.TextBuffer.Changed += (s, e) => {
                var tc = e.ToTextChange();
                ast.ReflectTextChange(tc.Start, tc.OldLength, tc.NewLength, tc.NewTextProvider);

                if (e.Changes[0].NewText.Length == 1)
                {
                    var ch = e.Changes[0].NewText[0];
                    if (af.IsPostProcessAutoformatTriggerCharacter(ch))
                    {
                        position = e.Changes[0].OldPosition + 1;
                        textView.Caret.MoveTo(new SnapshotPoint(e.After, position));
                        FormatOperations.FormatViewLine(editorView, editorView.EditorBuffer, -1, _services);
                    }
                }
                else
                {
                    var line = e.After.GetLineFromPosition(position);
                    textView.Caret.MoveTo(new SnapshotPoint(e.After, Math.Min(e.After.Length, line.Length + 1)));
                }
            };

            Typing.Type(textView.TextBuffer, position, "\n");
            return(textView);
        }
예제 #10
0
        public void FormatScope(string content, int start, int end, string expected)
        {
            var editorView = TextViewTest.MakeTextView(content, out AstRoot ast);

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

            actual.Should().Be(expected);
        }
예제 #11
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();
        }
예제 #12
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("{ }");
        }
예제 #13
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);
        }
예제 #14
0
        public void EmptyFileTest()
        {
            var editorView = TextViewTest.MakeTextView(string.Empty, out AstRoot ast);

            _formatter.FormatRange(editorView, editorView.EditorBuffer, TextRange.EmptyRange);

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

            actual.Should().BeEmpty();
        }
예제 #15
0
        public void FormatArgumentsTest(string content, string expected)
        {
            var editorView = TextViewTest.MakeTextView(content, out AstRoot ast);

            _formatter.FormatRange(editorView, editorView.EditorBuffer, TextRange.EmptyRange);

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

            actual.Should().Be(expected);
        }
예제 #16
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);
        }
예제 #17
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);
        }
예제 #18
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);
        }
예제 #19
0
        public void FormatConditional(string original, int start, int end, string expected)
        {
            var editorView = TextViewTest.MakeTextView(original, 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);
        }
예제 #20
0
        private int?GetSmartIndent(string content, int lineNumber)
        {
            AstRoot   ast;
            ITextView textView = TextViewTest.MakeTextView(content, 0, out ast);
            var       document = new EditorDocumentMock(new EditorTreeMock(textView.TextBuffer, ast));

            ISmartIndentProvider provider = EditorShell.Current.ExportProvider.GetExport <ISmartIndentProvider>().Value;
            ISmartIndent         indenter = provider.CreateSmartIndent(textView);

            return(indenter.GetDesiredIndentation(textView.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(lineNumber)));
        }
예제 #21
0
        public void FormatConditionalTest04()
        {
            const string original   = "if (x > 1)\r\ny<-2";
            var          editorView = TextViewTest.MakeTextView(original, out AstRoot ast);

            _formatter.FormatRange(editorView, editorView.EditorBuffer, 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);
        }
예제 #22
0
        public void Scope(string content, int lineNum, int expectedIndent)
        {
            AstRoot   ast;
            ITextView textView = TextViewTest.MakeTextView(content, 0, out ast);
            var       document = new EditorDocumentMock(new EditorTreeMock(textView.TextBuffer, ast));

            ISmartIndentProvider provider = _exportProvider.GetExportedValue <ISmartIndentProvider>();
            ISmartIndent         indenter = provider.CreateSmartIndent(textView);
            int?indent = indenter.GetDesiredIndentation(textView.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(lineNum));

            indent.Should().HaveValue().And.Be(expectedIndent);
        }
예제 #23
0
        public void FormatConditionalTest05()
        {
            const string original   = "if(true){\n} else {}\n";
            var          editorView = TextViewTest.MakeTextView(original, out AstRoot ast);

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

            const string expected = "if(true){\n} else { }\n";

            actual.Should().Be(expected);
        }
예제 #24
0
        public void Scope(string content, int lineNum, int expectedIndent)
        {
            var editorView = TextViewTest.MakeTextView(content, 0, out AstRoot ast);
            var document   = new EditorDocumentMock(new EditorTreeMock(editorView.EditorBuffer, ast));

            var locator  = _services.GetService <IContentTypeServiceLocator>();
            var provider = locator.GetService <ISmartIndentProvider>(RContentTypeDefinition.ContentType);
            var tv       = editorView.As <ITextView>();
            var indenter = provider.CreateSmartIndent(tv);
            var indent   = indenter.GetDesiredIndentation(tv.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(lineNum));

            indent.Should().HaveValue().And.Be(expectedIndent);
        }
예제 #25
0
        public void SmartIndentTest05()
        {
            AstRoot   ast;
            ITextView textView = TextViewTest.MakeTextView("  x <- 1\r\n", 0, out ast);

            using (var document = new EditorDocumentMock(new EditorTreeMock(textView.TextBuffer, ast))) {
                ISmartIndentProvider provider = _exportProvider.GetExportedValue <ISmartIndentProvider>("ContentTypes", RContentTypeDefinition.ContentType);
                SmartIndenter        indenter = (SmartIndenter)provider.CreateSmartIndent(textView);

                int?indent = indenter.GetDesiredIndentation(textView.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(1), IndentStyle.Block);
                indent.Should().HaveValue().And.Be(2);
            }
        }
예제 #26
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);
        }
예제 #27
0
        public void Scope(string content, int lineNum, int expectedIndent)
        {
            AstRoot   ast;
            ITextView textView = TextViewTest.MakeTextView(content, 0, out ast);
            var       document = new EditorDocumentMock(new EditorTreeMock(textView.TextBuffer, ast));

            var          cs       = _services.GetService <ICompositionService>();
            var          composer = new ContentTypeImportComposer <ISmartIndentProvider>(cs);
            var          provider = composer.GetImport(RContentTypeDefinition.ContentType);
            ISmartIndent indenter = provider.CreateSmartIndent(textView);
            int?         indent   = indenter.GetDesiredIndentation(textView.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(lineNum));

            indent.Should().HaveValue().And.Be(expectedIndent);
        }
예제 #28
0
        public void SmartIndentTest05()
        {
            AstRoot ast;
            var     textView = TextViewTest.MakeTextView("  x <- 1\r\n", 0, out ast);

            using (var document = new EditorDocumentMock(new EditorTreeMock(textView.TextBuffer, ast))) {
                var cs       = _shell.GetService <ICompositionService>();
                var composer = new ContentTypeImportComposer <ISmartIndentProvider>(cs);
                var provider = composer.GetImport(RContentTypeDefinition.ContentType);
                var indenter = (SmartIndenter)provider.CreateSmartIndent(textView);

                int?indent = indenter.GetDesiredIndentation(textView.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(1), IndentStyle.Block);
                indent.Should().HaveValue().And.Be(2);
            }
        }
예제 #29
0
        public void FormatMultiline02()
        {
            const string original =
                @"((x %>%y)
    %>% z %>%a)";
            var editorView = TextViewTest.MakeTextView(original, out AstRoot ast);

            _formatter.FormatRange(editorView, editorView.EditorBuffer, 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);
        }
예제 #30
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);
        }