Esempio n. 1
0
        // Internal for testing
        internal void TryMigrateMiscellaneousDocumentsToProject()
        {
            _foregroundDispatcher.AssertForegroundThread();

            var miscellaneousProject = _projectResolver.GetMiscellaneousProject();

            foreach (var documentFilePath in miscellaneousProject.DocumentFilePaths)
            {
                if (!_projectResolver.TryResolvePotentialProject(documentFilePath, out var projectSnapshot))
                {
                    continue;
                }

                var documentSnapshot = (DefaultDocumentSnapshot)miscellaneousProject.GetDocument(documentFilePath);

                // Remove from miscellaneous project
                var defaultMiscProject = (DefaultProjectSnapshot)miscellaneousProject;
                _projectSnapshotManagerAccessor.Instance.DocumentRemoved(defaultMiscProject.HostProject, documentSnapshot.State.HostDocument);

                // Add to new project

                var textLoader      = new DocumentSnapshotTextLoader(documentSnapshot);
                var defaultProject  = (DefaultProjectSnapshot)projectSnapshot;
                var newHostDocument = _hostDocumentFactory.Create(documentSnapshot.FilePath, documentSnapshot.TargetPath);
                _logger.LogInformation($"Migrating '{documentFilePath}' from the '{miscellaneousProject.FilePath}' project to '{projectSnapshot.FilePath}' project.");
                _projectSnapshotManagerAccessor.Instance.DocumentAdded(defaultProject.HostProject, newHostDocument, textLoader);
            }
        }
        private void MoveDocument(string documentFilePath, DefaultProjectSnapshot fromProject, DefaultProjectSnapshot toProject)
        {
            Debug.Assert(fromProject.DocumentFilePaths.Contains(documentFilePath, FilePathComparer.Instance));
            Debug.Assert(!toProject.DocumentFilePaths.Contains(documentFilePath, FilePathComparer.Instance));

            var documentSnapshot    = (DefaultDocumentSnapshot)fromProject.GetDocument(documentFilePath);
            var currentHostDocument = documentSnapshot.State.HostDocument;

            var textLoader      = new DocumentSnapshotTextLoader(documentSnapshot);
            var newHostDocument = _hostDocumentFactory.Create(documentSnapshot.FilePath, documentSnapshot.TargetPath, documentSnapshot.FileKind);

            _logger.LogInformation($"Moving '{documentFilePath}' from the '{fromProject.FilePath}' project to '{toProject.FilePath}' project.");
            _projectSnapshotManagerAccessor.Instance.DocumentRemoved(fromProject.HostProject, currentHostDocument);
            _projectSnapshotManagerAccessor.Instance.DocumentAdded(toProject.HostProject, newHostDocument, textLoader);
        }
        // Internal for testing
        internal void TryMigrateDocumentsFromRemovedProject(ProjectSnapshot project)
        {
            _foregroundDispatcher.AssertForegroundThread();

            var miscellaneousProject = _projectResolver.GetMiscellaneousProject();

            foreach (var documentFilePath in project.DocumentFilePaths)
            {
                var documentSnapshot = (DefaultDocumentSnapshot)project.GetDocument(documentFilePath);

                if (!_projectResolver.TryResolvePotentialProject(documentFilePath, out var toProject))
                {
                    // This is the common case. It'd be rare for a project to be nested but we need to protect against it anyhow.
                    toProject = miscellaneousProject;
                }

                var textLoader       = new DocumentSnapshotTextLoader(documentSnapshot);
                var defaultToProject = (DefaultProjectSnapshot)toProject;
                _logger.LogInformation($"Migrating '{documentFilePath}' from the '{project.FilePath}' project to '{toProject.FilePath}' project.");
                _projectSnapshotManagerAccessor.Instance.DocumentAdded(defaultToProject.HostProject, documentSnapshot.State.HostDocument, textLoader);
            }
        }
Esempio n. 4
0
        private void UpdateProjectDocuments(IReadOnlyList <DocumentSnapshotHandle> documents, string projectFilePath)
        {
            var project            = (DefaultProjectSnapshot)_projectSnapshotManagerAccessor.Instance.GetLoadedProject(projectFilePath);
            var currentHostProject = project.HostProject;
            var projectDirectory   = _filePathNormalizer.GetDirectory(project.FilePath);
            var documentMap        = documents.ToDictionary(document => EnsureFullPath(document.FilePath, projectDirectory), FilePathComparer.Instance);

            // "Remove" any unnecessary documents by putting them into the misc project
            foreach (var documentFilePath in project.DocumentFilePaths)
            {
                if (documentMap.ContainsKey(documentFilePath))
                {
                    // This document still exists in the updated project
                    continue;
                }

                var documentSnapshot    = (DefaultDocumentSnapshot)project.GetDocument(documentFilePath);
                var currentHostDocument = documentSnapshot.State.HostDocument;

                var textLoader           = new DocumentSnapshotTextLoader(documentSnapshot);
                var newHostDocument      = _hostDocumentFactory.Create(documentSnapshot.FilePath, documentSnapshot.TargetPath);
                var miscellaneousProject = (DefaultProjectSnapshot)_projectResolver.GetMiscellaneousProject();

                _logger.LogInformation($"Moving old '{documentFilePath}' from the '{project.FilePath}' project to '{miscellaneousProject.FilePath}' project.");
                _projectSnapshotManagerAccessor.Instance.DocumentRemoved(project.HostProject, currentHostDocument);
                _projectSnapshotManagerAccessor.Instance.DocumentAdded(miscellaneousProject.HostProject, newHostDocument, textLoader);
            }

            project = (DefaultProjectSnapshot)_projectSnapshotManagerAccessor.Instance.GetLoadedProject(projectFilePath);

            // Update existing documents
            foreach (var documentFilePath in project.DocumentFilePaths)
            {
                if (!documentMap.TryGetValue(documentFilePath, out var documentHandle))
                {
                    // Document exists in the project but not in the configured documents. Chances are the project configuration is from a fallback
                    // configuration case (< 2.1) or the project isn't fully loaded yet.
                    continue;
                }

                var documentSnapshot    = (DefaultDocumentSnapshot)project.GetDocument(documentFilePath);
                var currentHostDocument = documentSnapshot.State.HostDocument;
                var newFilePath         = EnsureFullPath(documentHandle.FilePath, projectDirectory);
                var newHostDocument     = _hostDocumentFactory.Create(newFilePath, documentHandle.TargetPath, documentHandle.FileKind);

                if (HostDocumentComparer.Instance.Equals(currentHostDocument, newHostDocument))
                {
                    // Current and "new" host documents are equivalent
                    continue;
                }

                _logger.LogTrace($"Updating document '{newHostDocument.FilePath}''s file kind to '{newHostDocument.FileKind}' and target path to '{newHostDocument.TargetPath}'.");

                _projectSnapshotManagerAccessor.Instance.DocumentRemoved(currentHostProject, currentHostDocument);

                var remoteTextLoader = _remoteTextLoaderFactory.Create(newFilePath);
                _projectSnapshotManagerAccessor.Instance.DocumentAdded(currentHostProject, newHostDocument, remoteTextLoader);
            }

            project = (DefaultProjectSnapshot)_projectSnapshotManagerAccessor.Instance.GetLoadedProject(project.FilePath);

            // Add any new documents
            foreach (var documentKvp in documentMap)
            {
                var documentFilePath = documentKvp.Key;
                if (project.DocumentFilePaths.Contains(documentFilePath, FilePathComparer.Instance))
                {
                    // Already know about this document
                    continue;
                }

                var documentHandle   = documentKvp.Value;
                var remoteTextLoader = _remoteTextLoaderFactory.Create(documentFilePath);
                var newHostDocument  = _hostDocumentFactory.Create(documentFilePath, documentHandle.TargetPath, documentHandle.FileKind);

                _logger.LogInformation($"Adding new document '{documentFilePath}' to project '{projectFilePath}'.");

                _projectSnapshotManagerAccessor.Instance.DocumentAdded(currentHostProject, newHostDocument, remoteTextLoader);
            }
        }