Example #1
0
        public void Sections()
        {
            string content =
                @"# NAME1 -----
x <- 1


# NAME2 -----


";
            TextBufferMock textBuffer           = null;
            int            calls                = 0;
            OutlineRegionsChangedEventArgs args = null;

            textBuffer = new TextBufferMock(content, RContentTypeDefinition.ContentType);
            using (var tree = new EditorTree(textBuffer, _shell)) {
                tree.Build();
                using (var editorDocument = new EditorDocumentMock(tree)) {
                    using (var ob = new ROutlineRegionBuilder(editorDocument, _shell)) {
                        var rc1 = new OutlineRegionCollection(0);
                        ob.BuildRegions(rc1);

                        rc1.Should().HaveCount(2);
                        rc1[0].DisplayText.Should().Be("# NAME1");
                        rc1[1].DisplayText.Should().Be("# NAME2");

                        rc1[0].Length.Should().Be(21);
                        rc1[1].Length.Should().Be(13);

                        ob.RegionsChanged += (s, e) => {
                            calls++;
                            args = e;
                        };

                        textBuffer.Insert(2, "A");
                        editorDocument.EditorTree.EnsureTreeReady();

                        // Wait for background/idle tasks to complete
                        var start = DateTime.Now;
                        while (calls == 0 && (DateTime.Now - start).TotalMilliseconds < 2000)
                        {
                            ((IIdleTimeSource)_shell).DoIdle();
                        }

                        calls.Should().Be(1);
                        args.Should().NotBeNull();
                        args.ChangedRange.Start.Should().Be(0);
                        args.ChangedRange.End.Should().Be(textBuffer.CurrentSnapshot.Length);
                        args.Regions.Should().HaveCount(2);

                        args.Regions[0].DisplayText.Should().Be("# ANAME1");
                        args.Regions[1].DisplayText.Should().Be("# NAME2");

                        args.Regions[0].Length.Should().Be(22);
                        args.Regions[1].Length.Should().Be(13);
                    }
                }
            }
        }
        public async Task ParameterTest_ComputeCurrentParameter02()
        {
            await FunctionIndex.GetPackageNameAsync("legend");

            _settings.PartialArgumentNameMatch.Returns(true);

            var textBuffer = new TextBufferMock("legend(bty=1, lt=3)", RContentTypeDefinition.ContentType);
            var eb         = textBuffer.ToEditorBuffer();
            var source     = new RSignatureHelpSource(textBuffer, Services);
            var session    = new SignatureHelpSessionMock(Services, textBuffer, 0);
            var textView   = session.TextView as TextViewMock;
            var signatures = new List <ISignature>();

            using (var tree = new EditorTree(eb, Services)) {
                tree.Build();
                using (var document = new EditorDocumentMock(tree)) {
                    session.TrackingPoint = new TrackingPointMock(textBuffer, 7, PointTrackingMode.Positive, TrackingFidelityMode.Forward);

                    tree.TakeThreadOwnerShip();
                    await source.AugmentSignatureHelpSessionAsync(session, signatures, tree.AstRoot);

                    signatures.Should().ContainSingle();

                    textView.Caret = new TextCaretMock(textView, 8);
                    var sh    = ((RSignatureHelp)signatures[0]).FunctionSignatureHelp;
                    var index = sh.SignatureInfo.ComputeCurrentParameter(tree.BufferSnapshot, tree.AstRoot, 8, _settings);
                    index.Should().Be(11);

                    textView.Caret = new TextCaretMock(textView, 15);
                    index          = sh.SignatureInfo.ComputeCurrentParameter(tree.BufferSnapshot, tree.AstRoot, 15, _settings);
                    index.Should().Be(6);
                }
            }
        }
Example #3
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>());
        }
        public async Task ParameterTest_ComputeCurrentParameter02()
        {
            await PackageIndexUtility.GetFunctionInfoAsync(FunctionIndex, "legend");

            REditorSettings.PartialArgumentNameMatch = true;

            ITextBuffer              textBuffer = new TextBufferMock("legend(bty=1, lt=3)", RContentTypeDefinition.ContentType);
            SignatureHelpSource      source     = new SignatureHelpSource(textBuffer, EditorShell);
            SignatureHelpSessionMock session    = new SignatureHelpSessionMock(textBuffer, 0);
            TextViewMock             textView   = session.TextView as TextViewMock;
            List <ISignature>        signatures = new List <ISignature>();

            using (var tree = new EditorTree(textBuffer, EditorShell)) {
                tree.Build();
                using (var document = new EditorDocumentMock(tree)) {
                    session.TrackingPoint = new TrackingPointMock(textBuffer, 7, PointTrackingMode.Positive, TrackingFidelityMode.Forward);

                    tree.TakeThreadOwnerShip();
                    await source.AugmentSignatureHelpSessionAsync(session, signatures, tree.AstRoot);

                    signatures.Should().ContainSingle();

                    textView.Caret = new TextCaretMock(textView, 8);
                    SignatureHelp sh    = signatures[0] as SignatureHelp;
                    int           index = sh.ComputeCurrentParameter(tree.TextSnapshot, tree.AstRoot, 8);
                    index.Should().Be(11);

                    textView.Caret = new TextCaretMock(textView, 15);
                    index          = sh.ComputeCurrentParameter(tree.TextSnapshot, tree.AstRoot, 15);
                    index.Should().Be(6);
                }
            }
        }
Example #5
0
        private TextViewMock SetupTextView(string content, int startLineNumber, int startColumn)
        {
            var document = new EditorDocumentMock(content);
            var tv       = new TextViewMock(document.TextBuffer());
            var line     = document.EditorBuffer.CurrentSnapshot.GetLineFromLineNumber(startLineNumber);

            tv.Caret.MoveTo(new SnapshotPoint(tv.TextBuffer.CurrentSnapshot, line.Start + startColumn));
            return(tv);
        }
Example #6
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)));
        }
Example #7
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);
        }
Example #8
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);
        }
Example #9
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);
            }
        }
Example #10
0
        private void GetPeekableItems(string content, int position, IList <IPeekableItem> items)
        {
            var document = new EditorDocumentMock(content, @"C:\file.r");

            TextViewMock textView = new TextViewMock(document.TextBuffer, position);

            textView.Caret.MoveTo(new SnapshotPoint(textView.TextBuffer.CurrentSnapshot, position));

            var peekSession = PeekSessionMock.Create(textView, position);
            var factory     = PeekResultFactoryMock.Create();
            var peekSource  = new PeekableItemSource(textView.TextBuffer, factory);

            peekSource.AugmentPeekSession(peekSession, items);
        }
Example #11
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);
        }
Example #12
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);
            }
        }
Example #13
0
        public static OutlineRegionCollection BuildOutlineRegions(IEditorShell editorShell, string content)
        {
            TextBufferMock textBuffer = new TextBufferMock(content, RContentTypeDefinition.ContentType);

            using (var tree = new EditorTree(textBuffer, editorShell)) {
                tree.Build();
                using (var editorDocument = new EditorDocumentMock(tree)) {
                    using (var ob = new ROutlineRegionBuilder(editorDocument, editorShell)) {
                        OutlineRegionCollection rc = new OutlineRegionCollection(0);
                        ob.BuildRegions(rc);
                        return(rc);
                    }
                }
            }
        }
Example #14
0
        private void RunGotoDefTest(string content, int startCaretPosition, int finalCaretPosition)
        {
            var document = new EditorDocumentMock(content);
            var tv       = new TextViewMock(document.TextBuffer);

            tv.Caret.MoveTo(new SnapshotPoint(tv.TextBuffer.CurrentSnapshot, startCaretPosition));

            var cmd = new GoToDefinitionCommand(tv, document.TextBuffer);

            var o      = new object();
            var result = cmd.Invoke(typeof(VSConstants.VSStd97CmdID).GUID, (int)VSConstants.VSStd97CmdID.GotoDefn, null, ref o);

            result.Should().Be(CommandResult.Executed);
            tv.Caret.Position.BufferPosition.Position.Should().Be(finalCaretPosition);
        }
Example #15
0
        public static OutlineRegionCollection BuildOutlineRegions(string content)
        {
            TextBufferMock textBuffer = new TextBufferMock(content, RContentTypeDefinition.ContentType);

            using (EditorTree tree = new EditorTree(textBuffer)) {
                tree.Build();

                EditorDocumentMock      editorDocument = new EditorDocumentMock(tree);
                ROutlineRegionBuilder   ob             = new ROutlineRegionBuilder(editorDocument);
                OutlineRegionCollection rc             = new OutlineRegionCollection(0);
                ob.BuildRegions(rc);

                return(rc);
            }
        }
Example #16
0
        public static OutlineRegionCollection BuildOutlineRegions(IServiceContainer services, string content)
        {
            var textBuffer = new TextBufferMock(content, RContentTypeDefinition.ContentType);
            var eb         = textBuffer.ToEditorBuffer();

            using (var tree = new EditorTree(eb, services)) {
                tree.Build();
                using (var editorDocument = new EditorDocumentMock(tree)) {
                    using (var ob = new ROutlineRegionBuilder(editorDocument, services)) {
                        var rc = new OutlineRegionCollection(0);
                        ob.BuildRegions(rc);
                        return(rc);
                    }
                }
            }
        }
Example #17
0
        public void SmartIndentTest05()
        {
            var editorView = TextViewTest.MakeTextView("  x <- 1\r\n", 0, out AstRoot ast);

            using (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 settings = _services.GetService <IWritableREditorSettings>();
                settings.IndentStyle = IndentStyle.Block;

                var indenter = provider.CreateSmartIndent(tv);
                var indent   = indenter.GetDesiredIndentation(tv.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(1));
                indent.Should().HaveValue().And.Be(2);
            }
        }
        public async Task ParameterTest_ComputeCurrentParameter01()
        {
            var textBuffer   = new TextBufferMock("aov(", RContentTypeDefinition.ContentType);
            var editorBuffer = textBuffer.ToEditorBuffer();
            var source       = new RSignatureHelpSource(textBuffer, Services);
            var session      = new SignatureHelpSessionMock(Services, textBuffer, 0);
            var textView     = session.TextView as TextViewMock;
            var signatures   = new List <ISignature>();

            using (var tree = new EditorTree(editorBuffer, Services)) {
                tree.Build();
                using (var document = new EditorDocumentMock(tree)) {
                    session.TrackingPoint = new TrackingPointMock(textBuffer, 4, PointTrackingMode.Positive, TrackingFidelityMode.Forward);
                    await FunctionIndex.GetPackageNameAsync("aov");

                    tree.TakeThreadOwnerShip();
                    await source.AugmentSignatureHelpSessionAsync(session, signatures, tree.AstRoot);

                    signatures.Should().ContainSingle();

                    var index = GetCurrentParameterIndex(signatures[0], signatures[0].CurrentParameter);
                    index.Should().Be(0);

                    textView.Caret = new TextCaretMock(textView, 5);
                    TextBufferUtility.ApplyTextChange(textBuffer, 4, 0, 1, "a");
                    index = GetCurrentParameterIndex(signatures[0], signatures[0].CurrentParameter);
                    index.Should().Be(0);

                    textView.Caret = new TextCaretMock(textView, 6);
                    TextBufferUtility.ApplyTextChange(textBuffer, 5, 0, 1, ",");
                    tree.EnsureTreeReady();
                    index = GetCurrentParameterIndex(signatures[0], signatures[0].CurrentParameter);
                    index.Should().Be(1);

                    textView.Caret = new TextCaretMock(textView, 7);
                    TextBufferUtility.ApplyTextChange(textBuffer, 6, 0, 1, ",");
                    tree.EnsureTreeReady();
                    index = GetCurrentParameterIndex(signatures[0], signatures[0].CurrentParameter);
                    index.Should().Be(2);
                }
            }
        }
Example #19
0
        private string FormatFromClipboard(string content) {
            ITextBuffer textBuffer = new TextBufferMock(string.Empty, RContentTypeDefinition.ContentType);
            ITextView textView = new TextViewMock(textBuffer);
            var clipboard = new ClipboardDataProvider();

            using (var command = new FormatOnPasteCommand(textView, textBuffer)) {
                command.ClipboardDataProvider = clipboard;

                clipboard.Format = DataFormats.UnicodeText;
                clipboard.Data = content;

                var ast = RParser.Parse(textBuffer.CurrentSnapshot.GetText());
                var document = new EditorDocumentMock(new EditorTreeMock(textBuffer, ast));

                object o = new object();
                command.Invoke(VSConstants.GUID_VSStandardCommandSet97, (int)VSConstants.VSStd97CmdID.Paste, null, ref o);
            }

            return textBuffer.CurrentSnapshot.GetText();
        }
Example #20
0
        private async Task <Session> TriggerSessionAsync(string content, int position)
        {
            var s = new Session {
                Ast          = RParser.Parse(content),
                EditorBuffer = new TextBufferMock(content, RContentTypeDefinition.ContentType).ToEditorBuffer()
            };

            var tree             = new EditorTreeMock(s.EditorBuffer, s.Ast);
            var document         = new EditorDocumentMock(tree);
            var textBuffer       = s.EditorBuffer.As <ITextBuffer>();
            var quickInfoSource  = new QuickInfoSource(textBuffer, Services);
            var quickInfoSession = new QuickInfoSessionMock(textBuffer, position);

            s.QuickInfoContent = new List <object>();

            quickInfoSession.TriggerPoint = new SnapshotPoint(s.EditorBuffer.TextSnapshot(), position);
            s.ApplicableSpan = await quickInfoSource.AugmentQuickInfoSessionAsync(s.Ast, textBuffer, position, quickInfoSession, s.QuickInfoContent);

            return(s);
        }
Example #21
0
        private void GetPeekableItems(string content, int position, IList <IPeekableItem> items, ITextRange selection = null)
        {
            var document = new EditorDocumentMock(content, @"C:\file.r");
            var textView = new TextViewMock(document.TextBuffer(), position);

            if (selection != null)
            {
                textView.Selection.Select(new SnapshotSpan(document.TextBuffer().CurrentSnapshot, new Span(selection.Start, selection.Length)), isReversed: false);
                textView.Caret.MoveTo(new SnapshotPoint(textView.TextBuffer.CurrentSnapshot, selection.End));
            }
            else
            {
                textView.Caret.MoveTo(new SnapshotPoint(textView.TextBuffer.CurrentSnapshot, position));
            }

            var peekSession = PeekSessionMock.Create(textView, position);
            var factory     = PeekResultFactoryMock.Create();
            var peekSource  = new PeekableItemSource(textView.TextBuffer, factory, _services);

            peekSource.AugmentPeekSession(peekSession, items);
        }
Example #22
0
            public async Task ParameterTest_ComputeCurrentParameter01()
            {
                ITextBuffer              textBuffer = new TextBufferMock("aov(", RContentTypeDefinition.ContentType);
                SignatureHelpSource      source     = new SignatureHelpSource(textBuffer);
                SignatureHelpSessionMock session    = new SignatureHelpSessionMock(textBuffer, 0);
                TextViewMock             textView   = session.TextView as TextViewMock;
                List <ISignature>        signatures = new List <ISignature>();

                EditorTree tree = new EditorTree(textBuffer);

                tree.Build();
                var document = new EditorDocumentMock(tree);

                session.TrackingPoint = new TrackingPointMock(textBuffer, 4, PointTrackingMode.Positive, TrackingFidelityMode.Forward);
                await FunctionIndexUtility.GetFunctionInfoAsync("aov");

                tree.TakeThreadOwnerShip();
                await source.AugmentSignatureHelpSessionAsync(session, signatures, tree.AstRoot);

                signatures.Should().ContainSingle();

                int index = GetCurrentParameterIndex(signatures[0] as SignatureHelp, signatures[0].CurrentParameter);

                index.Should().Be(0);

                textView.Caret = new TextCaretMock(textView, 5);
                TextBufferUtility.ApplyTextChange(textBuffer, 4, 0, 1, "a");
                index = GetCurrentParameterIndex(signatures[0] as SignatureHelp, signatures[0].CurrentParameter);
                index.Should().Be(0);

                textView.Caret = new TextCaretMock(textView, 6);
                TextBufferUtility.ApplyTextChange(textBuffer, 5, 0, 1, ",");
                index = GetCurrentParameterIndex(signatures[0] as SignatureHelp, signatures[0].CurrentParameter);
                index.Should().Be(1);

                textView.Caret = new TextCaretMock(textView, 7);
                TextBufferUtility.ApplyTextChange(textBuffer, 6, 0, 1, ",");
                index = GetCurrentParameterIndex(signatures[0] as SignatureHelp, signatures[0].CurrentParameter);
                index.Should().Be(2);
            }
Example #23
0
        public void RRegionBuilder_ConstructionTest()
        {
            TextBufferMock     textBuffer     = new TextBufferMock(string.Empty, RContentTypeDefinition.ContentType);
            EditorTree         tree           = new EditorTree(textBuffer);
            EditorDocumentMock editorDocument = new EditorDocumentMock(tree);

            ROutlineRegionBuilder ob = new ROutlineRegionBuilder(editorDocument);

            ob.EditorDocument.Should().NotBeNull();
            ob.EditorTree.Should().NotBeNull();

            editorDocument.DocumentClosing.GetInvocationList().Should().ContainSingle();

            FieldInfo treeUpdateField = tree.GetType().GetField("UpdateCompleted", BindingFlags.Instance | BindingFlags.NonPublic);
            var       d = (MulticastDelegate)treeUpdateField.GetValue(tree);

            d.GetInvocationList().Should().ContainSingle();

            ob.Dispose();

            editorDocument.DocumentClosing.Should().BeNull();
            treeUpdateField.GetValue(tree).Should().BeNull();
        }
Example #24
0
        public void ConstructionTest()
        {
            var textBuffer = new TextBufferMock(string.Empty, RContentTypeDefinition.ContentType);
            var eb         = textBuffer.ToEditorBuffer();
            var tree       = new EditorTree(eb, _services);

            using (var editorDocument = new EditorDocumentMock(tree)) {
                using (var ob = new ROutlineRegionBuilder(editorDocument, _services)) {
                    ob.EditorDocument.Should().NotBeNull();
                    ob.EditorTree.Should().NotBeNull();

                    editorDocument.Closing.GetInvocationList().Should().ContainSingle();

                    var treeUpdateField = tree.GetType().GetField("UpdateCompleted", BindingFlags.Instance | BindingFlags.NonPublic);
                    var d = (MulticastDelegate)treeUpdateField.GetValue(tree);
                    d.GetInvocationList().Should().ContainSingle();

                    tree.Dispose();

                    editorDocument.Closing.Should().BeNull();
                    treeUpdateField.GetValue(tree).Should().BeNull();
                }
            }
        }