Skip to content

Commit

Permalink
Add resolvers to misc workspace script projects
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Feb 27, 2017
1 parent 7648c82 commit ef07f4b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private async Task<ImmutableArray<DocumentHighlights>> 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;
}

Expand Down
3 changes: 1 addition & 2 deletions src/VisualStudio/CSharp/Impl/CSharpPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ protected override void RegisterMiscellaneousFilesWorkspaceInformation(Miscellan
miscellaneousFilesWorkspace.RegisterLanguage(
Guids.CSharpLanguageServiceId,
LanguageNames.CSharp,
".csx",
CSharpParseOptions.Default);
".csx");
}

protected override string RoslynLanguageName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task<ImmutableArray<SymbolAndProjectId>> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@ private static ImmutableDictionary<DocumentId, SyntaxTree> 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);
Expand Down

0 comments on commit ef07f4b

Please sign in to comment.