diff --git a/src/EditorFeatures/Core/Implementation/ReferenceHighlighting/AbstractDocumentHighlightsService.cs b/src/EditorFeatures/Core/Implementation/ReferenceHighlighting/AbstractDocumentHighlightsService.cs index 5a312428c7a54..8a90b0062620f 100644 --- a/src/EditorFeatures/Core/Implementation/ReferenceHighlighting/AbstractDocumentHighlightsService.cs +++ b/src/EditorFeatures/Core/Implementation/ReferenceHighlighting/AbstractDocumentHighlightsService.cs @@ -186,7 +186,7 @@ private async Task> CreateSpansAsync( // Document once https://github.com/dotnet/roslyn/issues/5260 is fixed. if (document == null) { - Debug.Assert(solution.Workspace.Kind == "Interactive"); + Debug.Assert(solution.Workspace.Kind == WorkspaceKind.Interactive || solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles); continue; } diff --git a/src/VisualStudio/CSharp/Impl/CSharpPackage.cs b/src/VisualStudio/CSharp/Impl/CSharpPackage.cs index 3c9f68460ffc2..cf5305f4c317c 100644 --- a/src/VisualStudio/CSharp/Impl/CSharpPackage.cs +++ b/src/VisualStudio/CSharp/Impl/CSharpPackage.cs @@ -163,8 +163,7 @@ protected override void RegisterMiscellaneousFilesWorkspaceInformation(Miscellan miscellaneousFilesWorkspace.RegisterLanguage( Guids.CSharpLanguageServiceId, LanguageNames.CSharp, - ".csx", - CSharpParseOptions.Default); + ".csx"); } protected override string RoslynLanguageName diff --git a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/MiscellaneousFilesWorkspace.HostProject.cs b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/MiscellaneousFilesWorkspace.HostProject.cs index 44b37709a8f51..f5194821d8d62 100644 --- a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/MiscellaneousFilesWorkspace.HostProject.cs +++ b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/MiscellaneousFilesWorkspace.HostProject.cs @@ -20,22 +20,24 @@ private sealed class HostProject : IVisualStudioHostProject internal IVisualStudioHostDocument Document { get; set; } private readonly string _assemblyName; - private readonly ParseOptions _parseOptions; + private readonly ParseOptions _parseOptionsOpt; + private readonly CompilationOptions _compilationOptionsOpt; private readonly IEnumerable _metadataReferences; private readonly VersionStamp _version; private readonly Workspace _workspace; - public HostProject(Workspace workspace, SolutionId solutionId, string languageName, ParseOptions parseOptions, IEnumerable metadataReferences) + public HostProject(Workspace workspace, SolutionId solutionId, string languageName, ParseOptions parseOptionsOpt, CompilationOptions compilationOptionsOpt, IEnumerable metadataReferences) { Debug.Assert(workspace != null); Debug.Assert(languageName != null); - Debug.Assert(parseOptions != null); Debug.Assert(metadataReferences != null); _workspace = workspace; - this.Id = ProjectId.CreateNewId(debugName: "Miscellaneous Files"); - this.Language = languageName; - _parseOptions = parseOptions; + _parseOptionsOpt = parseOptionsOpt; + _compilationOptionsOpt = compilationOptionsOpt; + + Id = ProjectId.CreateNewId(debugName: "Miscellaneous Files"); + Language = languageName; // the assembly name must be unique for each collection of loose files. since the name doesn't matter // a random GUID can be used. @@ -48,14 +50,14 @@ public HostProject(Workspace workspace, SolutionId solutionId, string languageNa public ProjectInfo CreateProjectInfoForCurrentState() { var info = ProjectInfo.Create( - this.Id, + Id, _version, name: ServicesVSResources.Miscellaneous_Files, assemblyName: _assemblyName, - language: this.Language, + language: Language, filePath: null, - compilationOptions: null, // we don't have compilation options? - parseOptions: _parseOptions, + compilationOptions: _compilationOptionsOpt, + parseOptions: _parseOptionsOpt, documents: SpecializedCollections.EmptyEnumerable(), projectReferences: SpecializedCollections.EmptyEnumerable(), metadataReferences: _metadataReferences, diff --git a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/MiscellaneousFilesWorkspace.cs b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/MiscellaneousFilesWorkspace.cs index 36df5df06736c..594ed470b0b0b 100644 --- a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/MiscellaneousFilesWorkspace.cs +++ b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/MiscellaneousFilesWorkspace.cs @@ -16,6 +16,7 @@ using Roslyn.Utilities; using Microsoft.CodeAnalysis.Editor.Shared.Utilities; using System.Threading.Tasks; +using Microsoft.CodeAnalysis.Host; namespace Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem { @@ -68,9 +69,9 @@ public MiscellaneousFilesWorkspace( saveEventsService.StartSendingSaveEvents(); } - public void RegisterLanguage(Guid languageGuid, string languageName, string scriptExtension, ParseOptions parseOptions) + public void RegisterLanguage(Guid languageGuid, string languageName, string scriptExtension) { - _languageInformationByLanguageGuid.Add(languageGuid, new LanguageInformation(languageName, scriptExtension, parseOptions)); + _languageInformationByLanguageGuid.Add(languageGuid, new LanguageInformation(languageName, scriptExtension)); } internal void StartSolutionCrawler() @@ -328,21 +329,46 @@ private void AttachToDocument(uint docCookie, string moniker) // This should always succeed since we only got here if we already confirmed the moniker is acceptable var languageInformation = TryGetLanguageInformation(moniker); Contract.ThrowIfNull(languageInformation); - var parseOptions = languageInformation.ParseOptions; - if (Path.GetExtension(moniker) == languageInformation.ScriptExtension) + var languageServices = Services.GetLanguageServices(languageInformation.LanguageName); + var compilationOptionsOpt = languageServices.GetService()?.GetDefaultCompilationOptions(); + var parseOptionsOpt = languageServices.GetService()?.GetDefaultParseOptions(); + + if (parseOptionsOpt != null && + compilationOptionsOpt != null && + PathUtilities.GetExtension(moniker) == languageInformation.ScriptExtension) { - parseOptions = parseOptions.WithKind(SourceCodeKind.Script); + parseOptionsOpt = parseOptionsOpt.WithKind(SourceCodeKind.Script); + + var metadataService = Services.GetService(); + var directory = PathUtilities.GetDirectoryName(moniker); + + // TODO (https://github.com/dotnet/roslyn/issues/5325, https://github.com/dotnet/roslyn/issues/13886): + // - Need to have a way to specify these somewhere in VS options. + // - Use RuntimeMetadataReferenceResolver like in InteractiveEvaluator.CreateMetadataReferenceResolver + // - Add default namespace imports + // - Add default script globals available in 'csi foo.csx' environment: CommandLineScriptGlobals + + var referenceSearchPaths = ImmutableArray.Empty; + var sourceSearchPaths = ImmutableArray.Empty; + + var referenceResolver = new WorkspaceMetadataFileReferenceResolver( + metadataService, + new RelativePathResolver(referenceSearchPaths, directory)); + + compilationOptionsOpt = compilationOptionsOpt. + WithMetadataReferenceResolver(referenceResolver). + WithSourceReferenceResolver(new SourceFileResolver(sourceSearchPaths, directory)); } // First, create the project - var hostProject = new HostProject(this, CurrentSolution.Id, languageInformation.LanguageName, parseOptions, _metadataReferences); + var hostProject = new HostProject(this, CurrentSolution.Id, languageInformation.LanguageName, parseOptionsOpt, compilationOptionsOpt, _metadataReferences); // Now try to find the document. We accept any text buffer, since we've already verified it's an appropriate file in ShouldIncludeFile. var document = _documentProvider.TryGetDocumentForFile( hostProject, moniker, - parseOptions.Kind, + parseOptionsOpt?.Kind ?? SourceCodeKind.Regular, getFolderNames: _ => SpecializedCollections.EmptyReadOnlyList(), canUseTextBuffer: _ => true); @@ -453,16 +479,14 @@ void IVisualStudioHostProjectContainer.NotifyNonDocumentOpenedForProject(IVisual private class LanguageInformation { - public LanguageInformation(string languageName, string scriptExtension, ParseOptions parseOptions) + public LanguageInformation(string languageName, string scriptExtension) { this.LanguageName = languageName; this.ScriptExtension = scriptExtension; - this.ParseOptions = parseOptions; } public string LanguageName { get; } public string ScriptExtension { get; } - public ParseOptions ParseOptions { get; } } } } diff --git a/src/VisualStudio/VisualBasic/Impl/LanguageService/VisualBasicPackage.vb b/src/VisualStudio/VisualBasic/Impl/LanguageService/VisualBasicPackage.vb index d255570f64d76..eb8f95b83d7f6 100644 --- a/src/VisualStudio/VisualBasic/Impl/LanguageService/VisualBasicPackage.vb +++ b/src/VisualStudio/VisualBasic/Impl/LanguageService/VisualBasicPackage.vb @@ -156,8 +156,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.VisualBasic miscellaneousFilesWorkspace.RegisterLanguage( Guids.VisualBasicLanguageServiceId, LanguageNames.VisualBasic, - ".vbx", - VisualBasicParseOptions.Default) + ".vbx") End Sub Protected Overrides ReadOnly Property RoslynLanguageName As String diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LinkedFileReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LinkedFileReferenceFinder.cs index 96517b4d24d72..73e524d25a7d5 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LinkedFileReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LinkedFileReferenceFinder.cs @@ -26,7 +26,7 @@ public async Task> DetermineCascadedSymbolsAs // Document once https://github.com/dotnet/roslyn/issues/5260 is fixed. if (originalDocument == null) { - Debug.Assert(solution.Workspace.Kind == "Interactive"); + Debug.Assert(solution.Workspace.Kind == WorkspaceKind.Interactive || solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles); continue; } diff --git a/src/Workspaces/Core/Portable/SemanticModelWorkspaceService/SemanticModelWorkspaceServiceFactory.cs b/src/Workspaces/Core/Portable/SemanticModelWorkspaceService/SemanticModelWorkspaceServiceFactory.cs index d07cfbf78d7f1..a2b2005d2050d 100644 --- a/src/Workspaces/Core/Portable/SemanticModelWorkspaceService/SemanticModelWorkspaceServiceFactory.cs +++ b/src/Workspaces/Core/Portable/SemanticModelWorkspaceService/SemanticModelWorkspaceServiceFactory.cs @@ -473,8 +473,8 @@ private static ImmutableDictionary AddOrUpdateNewTreeToO // Document once https://github.com/dotnet/roslyn/issues/5260 is fixed. if (documentId == null) { - Debug.Assert(newProject.Solution.Workspace.Kind == "Interactive"); - continue; + Debug.Assert(newProject.Solution.Workspace.Kind == WorkspaceKind.Interactive || newProject.Solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles); + continue; } map = map.SetItem(documentId, newTree);