提交 ef07f4ba 编写于 作者: T Tomas Matousek

Add resolvers to misc workspace script projects

上级 7648c822
......@@ -186,7 +186,7 @@ private bool ShouldConsiderSymbol(ISymbol symbol)
// 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;
}
......
......@@ -163,8 +163,7 @@ protected override void RegisterMiscellaneousFilesWorkspaceInformation(Miscellan
miscellaneousFilesWorkspace.RegisterLanguage(
Guids.CSharpLanguageServiceId,
LanguageNames.CSharp,
".csx",
CSharpParseOptions.Default);
".csx");
}
protected override string RoslynLanguageName
......
......@@ -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<MetadataReference> _metadataReferences;
private readonly VersionStamp _version;
private readonly Workspace _workspace;
public HostProject(Workspace workspace, SolutionId solutionId, string languageName, ParseOptions parseOptions, IEnumerable<MetadataReference> metadataReferences)
public HostProject(Workspace workspace, SolutionId solutionId, string languageName, ParseOptions parseOptionsOpt, CompilationOptions compilationOptionsOpt, IEnumerable<MetadataReference> 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<DocumentInfo>(),
projectReferences: SpecializedCollections.EmptyEnumerable<ProjectReference>(),
metadataReferences: _metadataReferences,
......
......@@ -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 @@ internal sealed partial class MiscellaneousFilesWorkspace : Workspace, IVsRunnin
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<ICompilationFactoryService>()?.GetDefaultCompilationOptions();
var parseOptionsOpt = languageServices.GetService<ISyntaxTreeFactoryService>()?.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<IMetadataService>();
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<string>.Empty;
var sourceSearchPaths = ImmutableArray<string>.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<string>(),
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; }
}
}
}
......@@ -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
......
......@@ -26,7 +26,7 @@ internal class LinkedFileReferenceFinder : IReferenceFinder
// 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;
}
......
......@@ -473,8 +473,8 @@ private CompilationSet(VersionStamp version, ValueSource<Compilation> compilatio
// 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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册