// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Linq; using Microsoft.CodeAnalysis.Razor; namespace Microsoft.CodeAnalysis.Host { internal class TestWorkspaceServices : HostWorkspaceServices { private static readonly Workspace DefaultWorkspace = TestWorkspace.Create(); private readonly HostServices _hostServices; private readonly HostLanguageServices _razorLanguageServices; private readonly IEnumerable _workspaceServices; private readonly Workspace _workspace; public TestWorkspaceServices( HostServices hostServices, IEnumerable workspaceServices, IEnumerable languageServices, Workspace workspace) { if (hostServices == null) { throw new ArgumentNullException(nameof(hostServices)); } if (workspaceServices == null) { throw new ArgumentNullException(nameof(workspaceServices)); } if (languageServices == null) { throw new ArgumentNullException(nameof(languageServices)); } if (workspace == null) { throw new ArgumentNullException(nameof(workspace)); } _hostServices = hostServices; _workspaceServices = workspaceServices; _workspace = workspace; _razorLanguageServices = new TestLanguageServices(this, languageServices); } public override HostServices HostServices => _hostServices; public override Workspace Workspace => _workspace; public override TWorkspaceService GetService() { var service = _workspaceServices.OfType().FirstOrDefault(); if (service == null) { // Fallback to default host services to resolve roslyn specific features. service = DefaultWorkspace.Services.GetService(); } return service; } public override HostLanguageServices GetLanguageServices(string languageName) { if (languageName == RazorLanguage.Name) { return _razorLanguageServices; } // Fallback to default host services to resolve roslyn specific features. return DefaultWorkspace.Services.GetLanguageServices(languageName); } public override IEnumerable SupportedLanguages => new[] { RazorLanguage.Name }; public override bool IsSupported(string languageName) => languageName == RazorLanguage.Name; public override IEnumerable FindLanguageServices(MetadataFilter filter) => throw new NotImplementedException(); } }