public async Task SynchronizeDocuments_IgnoresTrackedDocuments()
        {
            // Arrange
            var hostDocument = new OmniSharpHostDocument("file.razor", "file.razor", FileKinds.Component);
            var configuredHostDocuments = new[] { hostDocument };
            var msbuildProjectManager = new MSBuildProjectManager(
                Enumerable.Empty<ProjectConfigurationProvider>(),
                ProjectInstanceEvaluator,
                Mock.Of<ProjectChangePublisher>(),
                Dispatcher,
                LoggerFactory);
            var projectManager = CreateProjectSnapshotManager(allowNotifyListeners: true);
            msbuildProjectManager.Initialize(projectManager);
            var hostProject = new OmniSharpHostProject("/path/to/project.csproj", CustomConfiguration, "TestRootNamespace");
            var projectSnapshot = await RunOnForegroundAsync(() =>
            {
                projectManager.ProjectAdded(hostProject);
                projectManager.DocumentAdded(hostProject, hostDocument);
                return projectManager.GetLoadedProject(hostProject.FilePath);
            });
            projectManager.Changed += (sender, args) => throw new XunitException("Should not have been notified");

            // Act & Assert
            await RunOnForegroundAsync(() =>
                msbuildProjectManager.SynchronizeDocuments(
                    configuredHostDocuments,
                    projectSnapshot,
                    hostProject));
        }
        public async Task SynchronizeDocuments_RemovesTrackedDocuments()
        {
            // Arrange
            var msbuildProjectManager = new MSBuildProjectManager(
                Enumerable.Empty<ProjectConfigurationProvider>(),
                ProjectInstanceEvaluator,
                Mock.Of<ProjectChangePublisher>(),
                Dispatcher,
                LoggerFactory);
            var projectManager = CreateProjectSnapshotManager();
            msbuildProjectManager.Initialize(projectManager);
            var hostProject = new OmniSharpHostProject("/path/to/project.csproj", CustomConfiguration, "TestRootNamespace");
            var projectSnapshot = await RunOnForegroundAsync(() =>
            {
                projectManager.ProjectAdded(hostProject);
                var hostDocument = new OmniSharpHostDocument("file.razor", "file.razor", FileKinds.Component);
                projectManager.DocumentAdded(hostProject, hostDocument);
                return projectManager.GetLoadedProject(hostProject.FilePath);
            });

            // Act
            await RunOnForegroundAsync(() =>
                msbuildProjectManager.SynchronizeDocuments(
                    configuredHostDocuments: Array.Empty<OmniSharpHostDocument>(),
                    projectSnapshot,
                    hostProject));

            // Assert
            await RunOnForegroundAsync(() =>
            {
                var refreshedProject = projectManager.GetLoadedProject(hostProject.FilePath);
                Assert.Empty(refreshedProject.DocumentFilePaths);
            });
        }
        private void UpdateProjectState(ProjectInstance projectInstance)
        {
            _foregroundDispatcher.AssertForegroundThread();

            var projectFilePath = projectInstance.GetPropertyValue(MSBuildProjectFullPathPropertyName);

            if (string.IsNullOrEmpty(projectFilePath))
            {
                // This should never be true but we're being extra careful.
                return;
            }

            var projectConfiguration = GetProjectConfiguration(projectInstance, _projectConfigurationProviders);

            if (projectConfiguration == null)
            {
                // Not a Razor project
                return;
            }

            var projectSnapshot = _projectManager.GetLoadedProject(projectFilePath);
            var hostProject     = new OmniSharpHostProject(projectFilePath, projectConfiguration.Configuration, projectConfiguration.RootNamespace);

            if (projectSnapshot == null)
            {
                // Project doesn't exist yet, create it and set it up with all of its host documents.

                _projectManager.ProjectAdded(hostProject);

                foreach (var hostDocument in projectConfiguration.Documents)
                {
                    _projectManager.DocumentAdded(hostProject, hostDocument);
                }
            }
            else
            {
                // Project already exists (project change). Reconfigure the project and add or remove host documents to synchronize it with the configured host documents.

                _projectManager.ProjectConfigurationChanged(hostProject);

                SynchronizeDocuments(projectConfiguration.Documents, projectSnapshot, hostProject);
            }
        }
Exemple #4
0
        public async Task SynchronizeDocuments_UpdatesDocumentKinds()
        {
            // Arrange
            var msbuildProjectManager = new MSBuildProjectManager(
                Enumerable.Empty <ProjectConfigurationProvider>(),
                CreateProjectInstanceEvaluator(),
                Mock.Of <ProjectChangePublisher>(MockBehavior.Strict),
                Dispatcher,
                LoggerFactory);
            var projectManager = CreateProjectSnapshotManager();

            msbuildProjectManager.Initialize(projectManager);
            var hostProject             = new OmniSharpHostProject("/path/to/project.csproj", CustomConfiguration, "TestRootNamespace");
            var configuredHostDocuments = new[]
            {
                new OmniSharpHostDocument("file.cshtml", "file.cshtml", FileKinds.Component),
            };
            var projectSnapshot = await RunOnDispatcherThreadAsync(() =>
            {
                projectManager.ProjectAdded(hostProject);
                var hostDocument = new OmniSharpHostDocument("file.cshtml", "file.cshtml", FileKinds.Legacy);
                projectManager.DocumentAdded(hostProject, hostDocument);
                return(projectManager.GetLoadedProject(hostProject.FilePath));
            }).ConfigureAwait(false);

            // Act
            await RunOnDispatcherThreadAsync(() =>
                                             msbuildProjectManager.SynchronizeDocuments(
                                                 configuredHostDocuments,
                                                 projectSnapshot,
                                                 hostProject)).ConfigureAwait(false);

            // Assert
            await RunOnDispatcherThreadAsync(() =>
            {
                var refreshedProject = projectManager.GetLoadedProject(hostProject.FilePath);
                var documentFilePath = Assert.Single(refreshedProject.DocumentFilePaths);
                var document         = refreshedProject.GetDocument(documentFilePath);
                Assert.Equal("file.cshtml", document.FilePath);
                Assert.Equal("file.cshtml", document.TargetPath);
                Assert.Equal(FileKinds.Component, document.FileKind);
            }).ConfigureAwait(false);
        }
        // Internal for testing
        internal void SynchronizeDocuments(
            IReadOnlyList <OmniSharpHostDocument> configuredHostDocuments,
            OmniSharpProjectSnapshot projectSnapshot,
            OmniSharpHostProject hostProject)
        {
            // Remove any documents that need to be removed
            foreach (var documentFilePath in projectSnapshot.DocumentFilePaths)
            {
                OmniSharpHostDocument associatedHostDocument = null;
                var currentHostDocument = projectSnapshot.GetDocument(documentFilePath).HostDocument;

                for (var i = 0; i < configuredHostDocuments.Count; i++)
                {
                    var configuredHostDocument = configuredHostDocuments[i];
                    if (OmniSharpHostDocumentComparer.Instance.Equals(configuredHostDocument, currentHostDocument))
                    {
                        associatedHostDocument = configuredHostDocument;
                        break;
                    }
                }

                if (associatedHostDocument == null)
                {
                    // Document was removed
                    _projectManager.DocumentRemoved(hostProject, currentHostDocument);
                }
            }

            // Refresh the project snapshot to reflect any removed documents.
            projectSnapshot = _projectManager.GetLoadedProject(projectSnapshot.FilePath);

            // Add any documents that need to be added
            for (var i = 0; i < configuredHostDocuments.Count; i++)
            {
                var hostDocument = configuredHostDocuments[i];
                if (!projectSnapshot.DocumentFilePaths.Contains(hostDocument.FilePath, FilePathComparer.Instance))
                {
                    // Document was added.
                    _projectManager.DocumentAdded(hostProject, hostDocument);
                }
            }
        }