public override VisualStudioDocumentTracker Create(ITextBuffer textBuffer)
        {
            if (textBuffer == null)
            {
                throw new ArgumentNullException(nameof(textBuffer));
            }

            if (!_textDocumentFactory.TryGetTextDocument(textBuffer, out var textDocument))
            {
                Debug.Fail("Text document should be available from the text buffer.");
                return(null);
            }

            var filePath = textDocument.FilePath;
            var project  = _projectService.GetHostProject(textBuffer);

            if (project == null)
            {
                Debug.Fail("Text buffer should belong to a project.");
                return(null);
            }

            var projectPath = _projectService.GetProjectPath(project);

            var tracker = new DefaultVisualStudioDocumentTracker(filePath, projectPath, _projectManager, _editorSettingsManager, _workspace, textBuffer, _importDocumentManager);

            return(tracker);
        }
Ejemplo n.º 2
0
        public void GetTracker_ForRazorTextBufferWithTracker_ReturnsTheFirstTracker()
        {
            // Arrange
            var factory = new DefaultVisualStudioDocumentTrackerFactory(Dispatcher, ProjectManager, ProjectService, Workspace);

            var buffers = new Collection <ITextBuffer>()
            {
                Mock.Of <ITextBuffer>(b => b.ContentType == RazorContentType && b.Properties == new PropertyCollection()),
            };

            var bufferGraph = Mock.Of <IBufferGraph>(g => g.GetTextBuffers(It.IsAny <Predicate <ITextBuffer> >()) == buffers);

            var textView = Mock.Of <IWpfTextView>(v => v.BufferGraph == bufferGraph);

            // Preload the buffer's properties with a tracker, so it's like we've already tracked this one.
            var tracker = new DefaultVisualStudioDocumentTracker(ProjectManager, ProjectService, Workspace, buffers[0]);

            tracker.TextViewsInternal.Add(textView);
            buffers[0].Properties.AddProperty(typeof(VisualStudioDocumentTracker), tracker);

            // Act
            var result = factory.GetTracker(textView);

            // Assert
            Assert.Same(tracker, result);
        }
Ejemplo n.º 3
0
        public void SubjectBuffersDisconnected_ForAnyTextBufferWithTracker_RemovesTextView()
        {
            // Arrange
            var textView1 = Mock.Of <IWpfTextView>();
            var textView2 = Mock.Of <IWpfTextView>();

            var buffers = new Collection <ITextBuffer>()
            {
                Mock.Of <ITextBuffer>(b => b.ContentType == RazorContentType && b.Properties == new PropertyCollection()),
                Mock.Of <ITextBuffer>(b => b.ContentType == NonRazorContentType && b.Properties == new PropertyCollection()),
            };

            // Preload the buffer's properties with a tracker, so it's like we've already tracked this one.
            var tracker = new DefaultVisualStudioDocumentTracker("C:/File/Path/To/Tracker1.cshtml", ProjectManager, ProjectService, EditorSettingsManager, Workspace, buffers[0]);

            tracker.AddTextView(textView1);
            tracker.AddTextView(textView2);
            buffers[0].Properties.AddProperty(typeof(VisualStudioDocumentTracker), tracker);

            tracker = new DefaultVisualStudioDocumentTracker("C:/File/Path/To/Tracker1.cshtml", ProjectManager, ProjectService, EditorSettingsManager, Workspace, buffers[1]);
            tracker.AddTextView(textView1);
            tracker.AddTextView(textView2);
            buffers[1].Properties.AddProperty(typeof(VisualStudioDocumentTracker), tracker);
            var textViewListener = new RazorTextViewConnectionListener(Dispatcher, Mock.Of <RazorEditorFactoryService>(), Workspace);

            // Act
            textViewListener.SubjectBuffersDisconnected(textView2, ConnectionReason.BufferGraphChange, buffers);

            // Assert
            tracker = buffers[0].Properties.GetProperty <DefaultVisualStudioDocumentTracker>(typeof(VisualStudioDocumentTracker));
            Assert.Collection(tracker.TextViews, v => Assert.Same(v, textView1));

            tracker = buffers[1].Properties.GetProperty <DefaultVisualStudioDocumentTracker>(typeof(VisualStudioDocumentTracker));
            Assert.Collection(tracker.TextViews, v => Assert.Same(v, textView1));
        }
Ejemplo n.º 4
0
        public void SubjectBuffersConnected_ForRazorTextBufferWithTracker_AddsEntryForADifferentTextView()
        {
            // Arrange
            var factory = new DefaultVisualStudioDocumentTrackerFactory(Dispatcher, ProjectManager, ProjectService, Workspace);

            var textView1 = Mock.Of <IWpfTextView>();
            var textView2 = Mock.Of <IWpfTextView>();

            var buffers = new Collection <ITextBuffer>()
            {
                Mock.Of <ITextBuffer>(b => b.ContentType == RazorContentType && b.Properties == new PropertyCollection()),
            };

            // Preload the buffer's properties with a tracker, so it's like we've already tracked this one.
            var tracker = new DefaultVisualStudioDocumentTracker(ProjectManager, ProjectService, Workspace, buffers[0]);

            tracker.TextViewsInternal.Add(textView1);
            buffers[0].Properties.AddProperty(typeof(VisualStudioDocumentTracker), tracker);

            // Act
            factory.SubjectBuffersConnected(textView2, ConnectionReason.BufferGraphChange, buffers);

            // Assert
            Assert.Same(tracker, buffers[0].Properties.GetProperty <DefaultVisualStudioDocumentTracker>(typeof(VisualStudioDocumentTracker)));
            Assert.Collection(tracker.TextViews, v => Assert.Same(v, textView1), v => Assert.Same(v, textView2));
        }
        public void AddTextView_AddsToTextViewCollection()
        {
            // Arrange
            var documentTracker = new DefaultVisualStudioDocumentTracker(FilePath, ProjectManager, ProjectService, EditorSettingsManager, Workspace, TextBuffer);
            var textView        = Mock.Of <ITextView>();

            // Act
            documentTracker.AddTextView(textView);

            // Assert
            Assert.Collection(documentTracker.TextViews, v => Assert.Same(v, textView));
        }
        public void RemoveTextView_RemovesTextViewFromCollection_SingleItem()
        {
            // Arrange
            var documentTracker = new DefaultVisualStudioDocumentTracker(FilePath, ProjectManager, ProjectService, EditorSettingsManager, Workspace, TextBuffer);
            var textView        = Mock.Of <ITextView>();

            documentTracker.AddTextView(textView);

            // Act
            documentTracker.RemoveTextView(textView);

            // Assert
            Assert.Empty(documentTracker.TextViews);
        }
        public void AddTextView_SubscribesAfterFirstTextViewAdded()
        {
            // Arrange
            var documentTracker = new DefaultVisualStudioDocumentTracker(FilePath, ProjectManager, ProjectService, EditorSettingsManager, Workspace, TextBuffer);
            var textView        = Mock.Of <ITextView>();

            // Assert - 1
            Assert.False(documentTracker.IsSupportedProject);

            // Act
            documentTracker.AddTextView(textView);

            // Assert - 2
            Assert.True(documentTracker.IsSupportedProject);
        }
        public void RemoveTextView_NoopsWhenRemovingTextViewNotInCollection()
        {
            // Arrange
            var documentTracker = new DefaultVisualStudioDocumentTracker(FilePath, ProjectManager, ProjectService, EditorSettingsManager, Workspace, TextBuffer);
            var textView1       = Mock.Of <ITextView>();

            documentTracker.AddTextView(textView1);
            var textView2 = Mock.Of <ITextView>();

            // Act
            documentTracker.RemoveTextView(textView2);

            // Assert
            Assert.Collection(documentTracker.TextViews, v => Assert.Same(v, textView1));
        }
        public void EditorSettingsManager_Changed_TriggersContextChanged()
        {
            // Arrange
            var documentTracker = new DefaultVisualStudioDocumentTracker(FilePath, ProjectManager, ProjectService, EditorSettingsManager, Workspace, TextBuffer);
            var called          = false;

            documentTracker.ContextChanged += (sender, args) =>
            {
                called = true;
            };

            // Act
            documentTracker.EditorSettingsManager_Changed(null, null);

            // Assert
            Assert.True(called);
        }
Ejemplo n.º 10
0
        public void SubjectBuffersConnected_ForRazorTextBuffer_AddsTextViewToTracker()
        {
            // Arrange
            var textView = Mock.Of <IWpfTextView>();
            var buffers  = new Collection <ITextBuffer>()
            {
                Mock.Of <ITextBuffer>(b => b.ContentType == RazorContentType && b.Properties == new PropertyCollection()),
            };
            VisualStudioDocumentTracker documentTracker = new DefaultVisualStudioDocumentTracker("AFile", ProjectManager, ProjectService, EditorSettingsManager, Workspace, buffers[0]);
            var editorFactoryService = Mock.Of <RazorEditorFactoryService>(factoryService => factoryService.TryGetDocumentTracker(It.IsAny <ITextBuffer>(), out documentTracker) == true);
            var textViewListener     = new RazorTextViewConnectionListener(Dispatcher, editorFactoryService, Workspace);

            // Act
            textViewListener.SubjectBuffersConnected(textView, ConnectionReason.BufferGraphChange, buffers);

            // Assert
            Assert.Collection(documentTracker.TextViews, v => Assert.Same(v, textView));
        }
Ejemplo n.º 11
0
        public void SubjectBuffersConnected(IWpfTextView textView, ConnectionReason reason, Collection <ITextBuffer> subjectBuffers)
        {
            if (textView == null)
            {
                throw new ArgumentException(nameof(textView));
            }

            if (subjectBuffers == null)
            {
                throw new ArgumentNullException(nameof(subjectBuffers));
            }

            _foregroundDispatcher.AssertForegroundThread();

            for (var i = 0; i < subjectBuffers.Count; i++)
            {
                var textBuffer = subjectBuffers[i];
                if (!textBuffer.IsRazorBuffer())
                {
                    continue;
                }

                DefaultVisualStudioDocumentTracker tracker;
                if (!textBuffer.Properties.TryGetProperty(typeof(VisualStudioDocumentTracker), out tracker))
                {
                    tracker = new DefaultVisualStudioDocumentTracker(_projectManager, _projectService, _workspace, textBuffer);
                    textBuffer.Properties.AddProperty(typeof(VisualStudioDocumentTracker), tracker);
                }

                if (!tracker.TextViewsInternal.Contains(textView))
                {
                    tracker.TextViewsInternal.Add(textView);
                    if (tracker.TextViewsInternal.Count == 1)
                    {
                        tracker.Subscribe();
                    }
                }
            }
        }
        public void RemoveTextView_UnsubscribesAfterLastTextViewRemoved()
        {
            // Arrange
            var documentTracker = new DefaultVisualStudioDocumentTracker(FilePath, ProjectManager, ProjectService, EditorSettingsManager, Workspace, TextBuffer);
            var textView1       = Mock.Of <ITextView>();
            var textView2       = Mock.Of <ITextView>();

            documentTracker.AddTextView(textView1);
            documentTracker.AddTextView(textView2);

            // Act - 1
            documentTracker.RemoveTextView(textView1);

            // Assert - 1
            Assert.True(documentTracker.IsSupportedProject);

            // Act - 2
            documentTracker.RemoveTextView(textView2);

            // Assert - 2
            Assert.False(documentTracker.IsSupportedProject);
        }