Beispiel #1
0
        public sealed override void RemoveDocument(EditorDocument document)
        {
            if (document is null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            JoinableTaskContext.AssertUIThread();

            lock (Lock)
            {
                var key = new DocumentKey(document.ProjectFilePath, document.DocumentFilePath);
                if (_documentsByFilePath.TryGetValue(document.DocumentFilePath, out var documents))
                {
                    documents.Remove(key);

                    if (documents.Count == 0)
                    {
                        _documentsByFilePath.Remove(document.DocumentFilePath);
                    }
                }

                _documents.Remove(key);

                if (document.IsOpenInEditor)
                {
                    OnDocumentClosed(document);
                }
            }
        }
        public sealed override void RemoveDocument(EditorDocument document)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            _foregroundDispatcher.AssertForegroundThread();

            var key = new DocumentKey(document.ProjectFilePath, document.DocumentFilePath);

            if (_documentsByFilePath.TryGetValue(document.DocumentFilePath, out var documents))
            {
                documents.Remove(key);

                if (documents.Count == 0)
                {
                    _documentsByFilePath.Remove(document.DocumentFilePath);
                }
            }

            _documents.Remove(key);

            if (document.IsOpenInEditor)
            {
                OnDocumentClosed(document);
            }
        }
Beispiel #3
0
        protected override void OnDocumentClosed(EditorDocument document)
        {
            var key = new DocumentKey(document.ProjectFilePath, document.DocumentFilePath);

            if (_cookiesByDocument.TryGetValue(key, out var cookie))
            {
                UntrackOpenDocument(cookie, key);
            }
        }
        public sealed override bool TryGetDocument(DocumentKey key, out EditorDocument document)
        {
            _foregroundDispatcher.AssertForegroundThread();

            lock (_lock)
            {
                return(_documents.TryGetValue(key, out document));
            }
        }
Beispiel #5
0
        public sealed override bool TryGetDocument(DocumentKey key, out EditorDocument document)
        {
            JoinableTaskContext.AssertUIThread();

            lock (Lock)
            {
                return(_documents.TryGetValue(key, out document));
            }
        }
Beispiel #6
0
        protected override void OnDocumentOpened(EditorDocument document)
        {
            var cookie = _runningDocumentTable.GetDocumentCookie(document.DocumentFilePath);

            if (cookie != VSConstants.VSCOOKIE_NIL)
            {
                TrackOpenDocument(cookie, new DocumentKey(document.ProjectFilePath, document.DocumentFilePath));
            }
        }
Beispiel #7
0
        protected override void OnDocumentClosed(EditorDocument document)
        {
            JoinableTaskContext.AssertUIThread();

            var key = new DocumentKey(document.ProjectFilePath, document.DocumentFilePath);

            if (_cookiesByDocument.TryGetValue(key, out var cookie))
            {
                UntrackOpenDocument(cookie, key);
            }
        }
Beispiel #8
0
        public sealed override EditorDocument GetOrCreateDocument(
            DocumentKey key,
            EventHandler changedOnDisk,
            EventHandler changedInEditor,
            EventHandler opened,
            EventHandler closed)
        {
            JoinableTaskContext.AssertUIThread();

            lock (Lock)
            {
                if (TryGetDocument(key, out var document))
                {
                    return(document);
                }

                // Check if the document is already open and initialized, and associate a buffer if possible.
                var textBuffer = GetTextBufferForOpenDocument(key.DocumentFilePath);
                document = new EditorDocument(
                    this,
                    ProjectSnapshotManagerDispatcher,
                    JoinableTaskContext,
                    key.ProjectFilePath,
                    key.DocumentFilePath,
                    new FileTextLoader(key.DocumentFilePath, defaultEncoding: null),
                    _fileChangeTrackerFactory.Create(key.DocumentFilePath),
                    textBuffer,
                    changedOnDisk,
                    changedInEditor,
                    opened,
                    closed);

                _documents.Add(key, document);

                if (!_documentsByFilePath.TryGetValue(key.DocumentFilePath, out var documents))
                {
                    documents = new List <DocumentKey>();
                    _documentsByFilePath.Add(key.DocumentFilePath, documents);
                }

                if (!documents.Contains(key))
                {
                    documents.Add(key);
                }

                if (document.IsOpenInEditor)
                {
                    OnDocumentOpened(document);
                }

                return(document);
            }
        }
        protected override void OnDocumentOpened(EditorDocument document)
        {
            JoinableTaskContext.AssertUIThread();

            EnsureDocumentTableAdvised();

            var cookie = _runningDocumentTable.GetDocumentCookie(document.DocumentFilePath);

            if (cookie != VSConstants.VSCOOKIE_NIL)
            {
                TrackOpenDocument(cookie, new DocumentKey(document.ProjectFilePath, document.DocumentFilePath));
            }
        }
        private EditorDocument GetEditorDocument(bool isOpen = false)
        {
            var document = new EditorDocument(
                Mock.Of <EditorDocumentManager>(MockBehavior.Strict),
                ProjectFilePath,
                DocumentFilePath,
                TextLoader,
                FileChangeTracker,
                isOpen ? TextBuffer : null,
                changedOnDisk: null,
                changedInEditor: null,
                opened: null,
                closed: null);

            return(document);
        }
 private async Task Document_ChangedOnDiskAsync(EditorDocument document, CancellationToken cancellationToken)
 {
     try
     {
         // This event is called by the EditorDocumentManager, which runs on the UI thread.
         // However, due to accessing the project snapshot manager, we need to switch to
         // running on the project snapshot manager's specialized thread.
         await _projectSnapshotManagerDispatcher.RunOnDispatcherThreadAsync(() =>
         {
             ProjectManager.DocumentChanged(document.ProjectFilePath, document.DocumentFilePath, document.TextLoader);
         }, cancellationToken).ConfigureAwait(false);
     }
     catch (Exception ex)
     {
         Debug.Fail("EditorDocumentManagerListener.Document_ChangedOnDisk threw exception:" +
                    Environment.NewLine + ex.Message + Environment.NewLine + "Stack trace:" + Environment.NewLine + ex.StackTrace);
     }
 }
Beispiel #12
0
        public void EditorDocument_CreatedWhileClosed()
        {
            // Arrange & Act
            var document = new EditorDocument(
                DocumentManager,
                ProjectFilePath,
                DocumentFilePath,
                TextLoader,
                FileChangeTracker,
                null,
                changedOnDisk: null,
                changedInEditor: null,
                opened: null,
                closed: null);

            // Assert
            Assert.False(document.IsOpenInEditor);
            Assert.Null(document.EditorTextBuffer);
            Assert.Null(document.EditorTextContainer);
        }
Beispiel #13
0
        public sealed override bool TryGetMatchingDocuments(string filePath, out EditorDocument[] documents)
        {
            JoinableTaskContext.AssertUIThread();

            lock (Lock)
            {
                if (!_documentsByFilePath.TryGetValue(filePath, out var keys))
                {
                    documents = null;
                    return(false);
                }

                documents = new EditorDocument[keys.Count];
                for (var i = 0; i < keys.Count; i++)
                {
                    documents[i] = _documents[keys[i]];
                }

                return(true);
            }
        }
        public sealed override bool TryGetMatchingDocuments(string filePath, out EditorDocument[] documents)
        {
            _foregroundDispatcher.AssertForegroundThread();

            lock (_lock)
            {
                if (!_documentsByFilePath.TryGetValue(filePath, out var keys))
                {
                    documents = null;
                    return(false);
                }

                documents = new EditorDocument[keys.Count];
                for (var i = 0; i < keys.Count; i++)
                {
                    documents[i] = _documents[keys[i]];
                }

                return(true);
            }
        }
Beispiel #15
0
 public void EditorDocument_CreatedWhileOpened()
 {
     // Arrange & Act
     using (var document = new EditorDocument(
                DocumentManager,
                Dispatcher,
                JoinableTaskFactory.Context,
                ProjectFilePath,
                DocumentFilePath,
                TextLoader,
                FileChangeTracker,
                TextBuffer,
                changedOnDisk: null,
                changedInEditor: null,
                opened: null,
                closed: null))
     {
         // Assert
         Assert.True(document.IsOpenInEditor);
         Assert.Same(TextBuffer, document.EditorTextBuffer);
         Assert.NotNull(document.EditorTextContainer);
     }
 }
Beispiel #16
0
 public abstract bool TryGetDocument(DocumentKey key, out EditorDocument document);
Beispiel #17
0
 protected abstract void OnDocumentClosed(EditorDocument document);
Beispiel #18
0
 protected abstract void OnDocumentOpened(EditorDocument document);
Beispiel #19
0
 public abstract void RemoveDocument(EditorDocument document);
 protected override void OnDocumentOpened(EditorDocument document)
 {
     Opened.Add(document);
 }
 protected override void OnDocumentClosed(EditorDocument document)
 {
     Closed.Add(document);
 }