Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into useDeconstruction
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Sep 1, 2017
2 parents f1e5c45 + 1a36abc commit c7138ba
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 136 deletions.

This file was deleted.

This file was deleted.

29 changes: 29 additions & 0 deletions src/Features/Core/Portable/Scripting/IScriptEnvironmentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Host;

namespace Microsoft.CodeAnalysis.Scripting
{
/// <summary>
/// Provides information on the current script environment.
/// </summary>
internal interface IScriptEnvironmentService : IWorkspaceService
{
/// <summary>
/// Full path of a directory to be used to resolve relative paths specified in #r and #load directives
/// that are used in script that itself doesn't have a path (e.g. interactive submission).
/// </summary>
string BaseDirectory { get; }

/// <summary>
/// Search paths used to find metadata references (#r directive).
/// </summary>
ImmutableArray<string> MetadataReferenceSearchPaths { get; }

/// <summary>
/// Search paths uses to find source references (#load directive).
/// </summary>
ImmutableArray<string> SourceReferenceSearchPaths { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) Microsoft. 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.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.Editor.Implementation.IntelliSense.Completion.FileSystem;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Scripting;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Editor.Completion.FileSystem
Expand Down Expand Up @@ -118,26 +119,34 @@ private static int AfterLastSlashIndex(string text, int position)

protected abstract Task ProvideCompletionsAsync(CompletionContext context, string pathThroughLastSlash);

internal static string GetBaseDirectory(SourceText text, Document document)
protected FileSystemCompletionHelper GetFileSystemCompletionHelper(
Document document,
Glyph itemGlyph,
ImmutableArray<string> extensions,
CompletionItemRules completionRules)
{
string result;
if (document.Project.IsSubmission)
{
var buffer = text.Container.GetTextBuffer();
var snapshot = text.FindCorrespondingEditorTextSnapshot();
if (snapshot == null)
{
return null;
}

result = CurrentWorkingDirectoryDiscoveryService.GetService(snapshot).WorkingDirectory;
}
else
var serviceOpt = document.Project.Solution.Workspace.Services.GetService<IScriptEnvironmentService>();
var searchPaths = serviceOpt?.MetadataReferenceSearchPaths ?? ImmutableArray<string>.Empty;

return new FileSystemCompletionHelper(
Glyph.OpenFolder,
itemGlyph,
searchPaths,
GetBaseDirectory(document, serviceOpt),
extensions,
completionRules);
}

private static string GetBaseDirectory(Document document, IScriptEnvironmentService environmentOpt)
{
var result = PathUtilities.GetDirectoryName(document.FilePath);
if (!PathUtilities.IsAbsolute(result))
{
result = PathUtilities.GetDirectoryName(document.FilePath);
result = environmentOpt?.BaseDirectory;
Contract.Requires(result == null || PathUtilities.IsAbsolute(result));
}

return PathUtilities.IsAbsolute(result) ? result : null;
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Editor.Completion.FileSystem
Expand Down Expand Up @@ -34,28 +33,9 @@ private static ImmutableArray<char> GetCommitCharacters()
return builder.ToImmutableAndFree();
}

private FileSystemCompletionHelper GetFileSystemCompletionHelper(SourceText text, Document document)
{
// TODO: https://github.com/dotnet/roslyn/issues/5263
// Avoid dependency on a specific resolver.
// The search paths should be provided by specialized workspaces:
// - InteractiveWorkspace for interactive window
// - MiscFilesWorkspace for loose .csx files
var searchPaths = (document.Project.CompilationOptions.SourceReferenceResolver as SourceFileResolver)?.SearchPaths ?? ImmutableArray<string>.Empty;

return new FileSystemCompletionHelper(
Glyph.OpenFolder,
Glyph.CSharpFile,
searchPaths: searchPaths,
baseDirectoryOpt: GetBaseDirectory(text, document),
allowableExtensions: ImmutableArray.Create(".csx"),
itemRules: s_rules);
}

protected override async Task ProvideCompletionsAsync(CompletionContext context, string pathThroughLastSlash)
{
var text = await context.Document.GetTextAsync(context.CancellationToken).ConfigureAwait(false);
var helper = GetFileSystemCompletionHelper(text, context.Document);
var helper = GetFileSystemCompletionHelper(context.Document, Glyph.CSharpFile, ImmutableArray.Create(".csx"), s_rules);
context.AddItems(await helper.GetItemsAsync(pathThroughLastSlash, context.CancellationToken).ConfigureAwait(false));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// Copyright (c) Microsoft. 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.Immutable;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Scripting.Hosting;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Editor.Completion.FileSystem
Expand Down Expand Up @@ -46,39 +43,6 @@ private static ImmutableArray<char> GetCommitCharacters()
return builder.ToImmutableAndFree();
}

private FileSystemCompletionHelper GetFileSystemCompletionHelper(SourceText text, Document document)
{
var referenceResolver = document.Project.CompilationOptions.MetadataReferenceResolver;

// TODO: https://github.com/dotnet/roslyn/issues/5263
// Avoid dependency on a specific resolvers.
// The search paths should be provided by specialized workspaces:
// - InteractiveWorkspace for interactive window
// - MiscFilesWorkspace for loose .csx files
ImmutableArray<string> searchPaths;

if (referenceResolver is RuntimeMetadataReferenceResolver rtResolver)
{
searchPaths = rtResolver.PathResolver.SearchPaths;
}
else if (referenceResolver is WorkspaceMetadataFileReferenceResolver workspaceResolver)
{
searchPaths = workspaceResolver.PathResolver.SearchPaths;
}
else
{
searchPaths = ImmutableArray<string>.Empty;
}

return new FileSystemCompletionHelper(
Glyph.OpenFolder,
Glyph.Assembly,
searchPaths: searchPaths,
baseDirectoryOpt: GetBaseDirectory(text, document),
allowableExtensions: ImmutableArray.Create(".dll", ".exe"),
itemRules: s_rules);
}

protected override async Task ProvideCompletionsAsync(CompletionContext context, string pathThroughLastSlash)
{
if (GacFileResolver.IsAvailable && pathThroughLastSlash.IndexOfAny(s_pathIndicators) < 0)
Expand All @@ -89,9 +53,8 @@ protected override async Task ProvideCompletionsAsync(CompletionContext context,

if (pathThroughLastSlash.IndexOf(',') < 0)
{
var text = await context.Document.GetTextAsync(context.CancellationToken).ConfigureAwait(false);
var fileSystemHelper = GetFileSystemCompletionHelper(text, context.Document);
context.AddItems(await fileSystemHelper.GetItemsAsync(pathThroughLastSlash, context.CancellationToken).ConfigureAwait(false));
var helper = GetFileSystemCompletionHelper(context.Document, Glyph.Assembly, ImmutableArray.Create(".dll", ".exe"), s_rules);
context.AddItems(await helper.GetItemsAsync(pathThroughLastSlash, context.CancellationToken).ConfigureAwait(false));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.CodeAnalysis.Editor.Implementation.IntelliSense.Completion.FileSystem;
using Microsoft.CodeAnalysis.Editor.Implementation.Interactive;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Host;
Expand All @@ -30,7 +29,7 @@ namespace Microsoft.CodeAnalysis.Editor.Interactive
{
using RelativePathResolver = Scripting::Microsoft.CodeAnalysis.RelativePathResolver;

internal abstract class InteractiveEvaluator : IInteractiveEvaluator, ICurrentWorkingDirectoryDiscoveryService
internal abstract class InteractiveEvaluator : IInteractiveEvaluator
{
private const string CommandPrefix = "#";

Expand Down Expand Up @@ -86,7 +85,7 @@ internal InteractiveEvaluator(

_contentType = contentType;
_responseFilePath = responseFilePath;
_workspace = new InteractiveWorkspace(hostServices);
_workspace = new InteractiveWorkspace(hostServices, this);
_contentTypeChangedHandler = new EventHandler<ContentTypeChangedEventArgs>(LanguageBufferContentTypeChanged);
_classifierAggregator = classifierAggregator;
_initialWorkingDirectory = initialWorkingDirectory;
Expand Down Expand Up @@ -381,7 +380,6 @@ private void AddSubmission(ITextBuffer subjectBuffer, string languageName)
}

subjectBuffer.ContentTypeChanged += _contentTypeChangedHandler;
subjectBuffer.Properties[typeof(ICurrentWorkingDirectoryDiscoveryService)] = this;

_currentSubmissionBuffer = subjectBuffer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft. 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.Immutable;
using System.Composition;
using Microsoft.CodeAnalysis.Editor.Implementation.Interactive;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Scripting;

namespace Microsoft.CodeAnalysis.Editor.Interactive
{
[ExportWorkspaceServiceFactory(typeof(IScriptEnvironmentService), WorkspaceKind.Interactive), Shared]
internal sealed class InteractiveScriptEnvironmentServiceFactory : IWorkspaceServiceFactory
{
private sealed class Service : IScriptEnvironmentService
{
private readonly InteractiveWorkspace _workspace;

public ImmutableArray<string> MetadataReferenceSearchPaths => _workspace.Evaluator.ReferenceSearchPaths;
public ImmutableArray<string> SourceReferenceSearchPaths => _workspace.Evaluator.SourceSearchPaths;
public string BaseDirectory => _workspace.Evaluator.WorkingDirectory;

public Service(InteractiveWorkspace workspace)
{
_workspace = workspace;
}
}

public IWorkspaceService CreateService(HostWorkspaceServices workspaceServices)
{
if (workspaceServices.Workspace is InteractiveWorkspace interactiveWorkspace)
{
return new Service(interactiveWorkspace);
}

// this service is not applicable to workspaces other than InteractiveWorkspace:
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@

namespace Microsoft.CodeAnalysis.Editor.Implementation.Interactive
{
internal class InteractiveWorkspace : Workspace
internal sealed class InteractiveWorkspace : Workspace
{
public readonly InteractiveEvaluator Evaluator;
private readonly ISolutionCrawlerRegistrationService _registrationService;

internal IInteractiveWindow Window { get; set; }
private SourceTextContainer _openTextContainer;
private DocumentId _openDocumentId;

internal InteractiveWorkspace(HostServices hostServices)
internal InteractiveWorkspace(HostServices hostServices, InteractiveEvaluator evaluator)
: base(hostServices, WorkspaceKind.Interactive)
{
// register work coordinator for this workspace
_registrationService = this.Services.GetService<ISolutionCrawlerRegistrationService>();
_registrationService = Services.GetService<ISolutionCrawlerRegistrationService>();
_registrationService.Register(this);

Evaluator = evaluator;
}

protected override void Dispose(bool finalize)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Immutable;
using System.Composition;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Scripting;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem
{
/// <summary>
/// Environment corresponding to csi running a script with default command line arguments.
/// </summary>
[ExportWorkspaceService(typeof(IScriptEnvironmentService), WorkspaceKind.MiscellaneousFiles), Shared]
internal sealed class MiscellaneousFilesScriptEnvironmentService : IScriptEnvironmentService
{
private static readonly ImmutableArray<string> s_metadataReferenceSearchPaths = ImmutableArray.Create(RuntimeEnvironment.GetRuntimeDirectory());

public ImmutableArray<string> MetadataReferenceSearchPaths => s_metadataReferenceSearchPaths;
public ImmutableArray<string> SourceReferenceSearchPaths => ImmutableArray<string>.Empty;
public string BaseDirectory => null;
}
}
Loading

0 comments on commit c7138ba

Please sign in to comment.