void UnregisterOpenDocument(OpenDocumentReference reference)
        {
            Runtime.AssertMainThread();

            if (--reference.ReferenceCount > 0)
            {
                return;
            }

            openDocuments.Remove(reference.FilePath);

            // Only use misc workspace with the new editor; old editor has its own
            if (reference.HandleMiscNamespace)
            {
                // In the common case the primary workspace will own the document, so shut down
                // miscellaneous workspace first to avoid adding and then immediately removing
                // the document to the miscellaneous workspace
                miscellaneousFilesWorkspace.OnDocumentClosed(reference.FilePath, reference.TextBuffer);
            }

            var solution = (reference.Owner as SolutionItem)?.ParentSolution;

            if (solution != null)
            {
                TryCloseDocumentInWorkspace(reference.FilePath, reference.TextBuffer.AsTextContainer(), solution);
            }
        }
        public IDisposable RegisterOpenDocument(WorkspaceObject owner, FilePath filePath, ITextBuffer textBuffer, bool handleMiscNamespace = true)
        {
            Runtime.AssertMainThread();

            var path = filePath.IsAbsolute ? filePath.CanonicalPath : filePath;

            if (openDocuments.TryGetValue(path, out var reference))
            {
                reference.ReferenceCount++;
                if (reference.Owner == null)
                {
                    reference.Owner = owner;
                }
                return(new DocumentRegistration {
                    OpenDocument = reference
                });
            }
            reference = new OpenDocumentReference {
                ReferenceCount      = 1,
                FilePath            = path,
                TextBuffer          = textBuffer,
                Owner               = owner,
                HandleMiscNamespace = handleMiscNamespace
            };
            openDocuments.Add(path, reference);

            TryRegisterOpenDocument(reference);

            return(new DocumentRegistration {
                OpenDocument = reference
            });
        }
        void TryRegisterOpenDocument(OpenDocumentReference reference)
        {
            // First offer the document to the primary workspace and see if it's owned by it.
            // This is the common case, so avoid adding the document to the miscellaneous workspace
            // unnecessarily, as it will be immediately removed anyway.
            TryOpenDocumentInWorkspace(reference.Owner, reference.FilePath, reference.TextBuffer);

            // Only use misc workspace with the new editor; old editor has its own
            if (reference.HandleMiscNamespace)
            {
                // If the primary workspace didn't claim the document notify the miscellaneous workspace
                miscellaneousFilesWorkspace.OnDocumentOpened(reference.FilePath, reference.TextBuffer);
            }
        }