|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections.Immutable; |
| 6 | +using System.Security; |
| 7 | +using Microsoft.CodeAnalysis.LanguageServer.HostWorkspace.ProjectTelemetry; |
| 8 | +using Microsoft.CodeAnalysis.MetadataAsSource; |
| 9 | +using Microsoft.CodeAnalysis.MSBuild; |
| 10 | +using Microsoft.CodeAnalysis.Options; |
| 11 | +using Microsoft.CodeAnalysis.ProjectSystem; |
| 12 | +using Microsoft.CodeAnalysis.Shared.Extensions; |
| 13 | +using Microsoft.CodeAnalysis.Shared.TestHooks; |
| 14 | +using Microsoft.CodeAnalysis.Text; |
| 15 | +using Microsoft.CodeAnalysis.Workspaces.ProjectSystem; |
| 16 | +using Microsoft.CommonLanguageServerProtocol.Framework; |
| 17 | +using Microsoft.Extensions.Logging; |
| 18 | +using Microsoft.VisualStudio.Composition; |
| 19 | +using Roslyn.Utilities; |
| 20 | +using static Microsoft.CodeAnalysis.MSBuild.BuildHostProcessManager; |
| 21 | + |
| 22 | +namespace Microsoft.CodeAnalysis.LanguageServer.HostWorkspace; |
| 23 | + |
| 24 | +internal sealed class FileBasedProgramsProjectSystem : LanguageServerProjectLoader, ILspMiscellaneousFilesWorkspaceProvider |
| 25 | +{ |
| 26 | + private readonly ILspServices _lspServices; |
| 27 | + private readonly ILogger<FileBasedProgramsProjectSystem> _logger; |
| 28 | + private readonly IMetadataAsSourceFileService _metadataAsSourceFileService; |
| 29 | + |
| 30 | + public FileBasedProgramsProjectSystem( |
| 31 | + ILspServices lspServices, |
| 32 | + IMetadataAsSourceFileService metadataAsSourceFileService, |
| 33 | + LanguageServerWorkspaceFactory workspaceFactory, |
| 34 | + IFileChangeWatcher fileChangeWatcher, |
| 35 | + IGlobalOptionService globalOptionService, |
| 36 | + ILoggerFactory loggerFactory, |
| 37 | + IAsynchronousOperationListenerProvider listenerProvider, |
| 38 | + ProjectLoadTelemetryReporter projectLoadTelemetry, |
| 39 | + ServerConfigurationFactory serverConfigurationFactory, |
| 40 | + BinlogNamer binlogNamer) |
| 41 | + : base( |
| 42 | + workspaceFactory.FileBasedProgramsProjectFactory, |
| 43 | + workspaceFactory.TargetFrameworkManager, |
| 44 | + workspaceFactory.ProjectSystemHostInfo, |
| 45 | + fileChangeWatcher, |
| 46 | + globalOptionService, |
| 47 | + loggerFactory, |
| 48 | + listenerProvider, |
| 49 | + projectLoadTelemetry, |
| 50 | + serverConfigurationFactory, |
| 51 | + binlogNamer) |
| 52 | + { |
| 53 | + _lspServices = lspServices; |
| 54 | + _logger = loggerFactory.CreateLogger<FileBasedProgramsProjectSystem>(); |
| 55 | + _metadataAsSourceFileService = metadataAsSourceFileService; |
| 56 | + } |
| 57 | + |
| 58 | + public Workspace Workspace => ProjectFactory.Workspace; |
| 59 | + |
| 60 | + public async Task<TextDocument?> AddMiscellaneousDocumentAsync(Uri uri, SourceText documentText, string languageId, ILspLogger logger) |
| 61 | + { |
| 62 | + var documentPath = ProtocolConversions.GetDocumentFilePathFromUri(uri); |
| 63 | + var container = documentText.Container; |
| 64 | + if (_metadataAsSourceFileService.TryAddDocumentToWorkspace(documentPath, container, out var documentId)) |
| 65 | + { |
| 66 | + var metadataWorkspace = _metadataAsSourceFileService.TryGetWorkspace(); |
| 67 | + Contract.ThrowIfNull(metadataWorkspace); |
| 68 | + var metadataDoc = metadataWorkspace.CurrentSolution.GetRequiredDocument(documentId); |
| 69 | + return metadataDoc; |
| 70 | + } |
| 71 | + |
| 72 | + var languageInfoProvider = _lspServices.GetRequiredService<ILanguageInfoProvider>(); |
| 73 | + if (!languageInfoProvider.TryGetLanguageInformation(uri, languageId, out var languageInformation)) |
| 74 | + { |
| 75 | + // Only log here since throwing here could take down the LSP server. |
| 76 | + logger.LogError($"Could not find language information for {uri} with absolute path {documentPath}"); |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + // For Razor files we need to override the language name to C# as thats what code is generated |
| 81 | + var isRazor = languageInformation.LanguageName == "Razor"; |
| 82 | + var languageName = isRazor ? LanguageNames.CSharp : languageInformation.LanguageName; |
| 83 | + var loadedProject = await CreateAndTrackInitialProjectAsync(documentPath, language: languageName); |
| 84 | + var documentFileInfo = new DocumentFileInfo(documentPath, logicalPath: documentPath, isLinked: false, isGenerated: false, folders: default); |
| 85 | + var projectFileInfo = new ProjectFileInfo() |
| 86 | + { |
| 87 | + Language = languageName, |
| 88 | + FilePath = uri.IsFile ? VirtualProject.GetVirtualProjectPath(documentPath) : documentPath, |
| 89 | + CommandLineArgs = uri.IsFile ? ["/langversion:preview", "/features:FileBasedProgram=true"] : ["/langversion:preview"], |
| 90 | + Documents = isRazor ? [] : [documentFileInfo], |
| 91 | + AdditionalDocuments = isRazor ? [documentFileInfo] : [], |
| 92 | + AnalyzerConfigDocuments = [], |
| 93 | + ProjectReferences = [], |
| 94 | + PackageReferences = [], |
| 95 | + ProjectCapabilities = [], |
| 96 | + ContentFilePaths = [], |
| 97 | + FileGlobs = [] |
| 98 | + }; |
| 99 | + await loadedProject.UpdateWithNewProjectInfoAsync(projectFileInfo, _logger); |
| 100 | + var workspaceProject = ProjectFactory.Workspace.CurrentSolution.GetRequiredProject(loadedProject.ProjectId); |
| 101 | + var document = isRazor ? workspaceProject.AdditionalDocuments.Single() : workspaceProject.Documents.Single(); |
| 102 | + |
| 103 | + if (uri.IsFile && languageInformation.LanguageName == LanguageNames.CSharp) |
| 104 | + { |
| 105 | + // light up the proper file-based program experience. |
| 106 | + ProjectsToLoadAndReload.AddWork(new ProjectToLoad(documentPath, ProjectGuid: null, ReportTelemetry: true)); |
| 107 | + loadedProject.NeedsReload += (_, _) => ProjectsToLoadAndReload.AddWork(new ProjectToLoad(documentPath, ProjectGuid: null, ReportTelemetry: false)); |
| 108 | + |
| 109 | + _ = Task.Run(async () => |
| 110 | + { |
| 111 | + await ProjectsToLoadAndReload.WaitUntilCurrentBatchCompletesAsync(); |
| 112 | + await ProjectInitializationHandler.SendProjectInitializationCompleteNotificationAsync(); |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + Contract.ThrowIfFalse(document.FilePath == documentPath); |
| 117 | + return document; |
| 118 | + } |
| 119 | + |
| 120 | + public void TryRemoveMiscellaneousDocument(Uri uri, bool removeFromMetadataWorkspace) |
| 121 | + { |
| 122 | + // support unloading |
| 123 | + } |
| 124 | + |
| 125 | + protected override async Task<(RemoteProjectFile? projectFile, BuildHostProcessKind preferred, BuildHostProcessKind actual)> TryLoadProjectAsync( |
| 126 | + BuildHostProcessManager buildHostProcessManager, ProjectToLoad projectToLoad, CancellationToken cancellationToken) |
| 127 | + { |
| 128 | + const BuildHostProcessKind buildHostKind = BuildHostProcessKind.NetCore; |
| 129 | + var buildHost = await buildHostProcessManager.GetBuildHostAsync(buildHostKind, cancellationToken); |
| 130 | + var documentPath = projectToLoad.Path; |
| 131 | + Contract.ThrowIfFalse(Path.GetExtension(documentPath) == ".cs"); |
| 132 | + |
| 133 | + var fakeProjectPath = VirtualProject.GetVirtualProjectPath(documentPath); |
| 134 | + var contentToLoad = VirtualProject.MakeVirtualProjectContent(documentPath); |
| 135 | + |
| 136 | + var loadedFile = await buildHost.LoadProjectAsync(fakeProjectPath, contentToLoad, languageName: LanguageNames.CSharp, cancellationToken); |
| 137 | + return (loadedFile, preferred: buildHostKind, actual: buildHostKind); |
| 138 | + } |
| 139 | +} |
0 commit comments