-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Importing Roslyn workspace code
- Loading branch information
Showing
14 changed files
with
1,340 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace MonoDevelop.MSBuild.Editor.LanguageServer; | ||
|
||
/// Unique persistent identifier for a document in a workspace. | ||
/// Dot not change when the document is renamed. | ||
/// The document may not be saved to disk, so may not have a file path. | ||
/// </summary> | ||
public class DocumentId : IEquatable<DocumentId> | ||
{ | ||
readonly Guid guid; | ||
|
||
private DocumentId(Guid guid) | ||
{ | ||
this.guid = guid; | ||
} | ||
|
||
public static DocumentId CreateNewId() => new (Guid.NewGuid()); | ||
|
||
public bool Equals(DocumentId? other) => other is not null && guid == other.guid; | ||
|
||
public override bool Equals(object? obj) => obj is DocumentId id && guid.Equals(id.guid); | ||
|
||
public override int GetHashCode() => guid.GetHashCode(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace MonoDevelop.MSBuild.Editor.LanguageServer; | ||
|
||
/// <summary> | ||
/// Represents a document that is open in the editor. | ||
/// </summary> | ||
public class EditorDocument | ||
{ | ||
readonly object lockObj = new(); | ||
|
||
VersionedSourceText? sourceText; | ||
|
||
internal EditorDocument(EditorWorkspace workspace, DocumentId documentId, SourceText? sourceText) | ||
{ | ||
Workspace = workspace; | ||
DocumentId = documentId; | ||
if (sourceText is not null) { | ||
this.sourceText = new VersionedSourceText(sourceText, 0); | ||
} | ||
} | ||
|
||
public EditorWorkspace Workspace { get; } | ||
public DocumentId DocumentId { get; } | ||
public SourceText SourceText => sourceText?.Text ?? throw new InvalidOperationException("Document has not yet been initialized"); | ||
public int Version => sourceText?.Version ?? -1; | ||
|
||
internal void UpdateText(SourceText sourceText) | ||
{ | ||
VersionedSourceText? newText = null, oldText = null; | ||
lock(lockObj) { | ||
oldText = this.sourceText; | ||
this.sourceText = newText = new VersionedSourceText(sourceText, oldText?.Version + 1 ?? 0); | ||
} | ||
|
||
if (SourceTextUpdated is {} evt) { | ||
evt?.Invoke(this, new DocumentSourceTextUpdated(DocumentId, oldText, newText)); | ||
} | ||
} | ||
|
||
public event EventHandler<DocumentSourceTextUpdated>? SourceTextUpdated; | ||
} | ||
|
||
public record class VersionedSourceText(SourceText Text, int Version); | ||
|
||
public class DocumentSourceTextUpdated : DocumentEventArgs | ||
{ | ||
readonly VersionedSourceText? oldText; | ||
readonly VersionedSourceText newText; | ||
public DocumentSourceTextUpdated(DocumentId documentId, VersionedSourceText? oldText, VersionedSourceText newText) | ||
: base(documentId) | ||
{ | ||
this.oldText = oldText; | ||
this.newText = newText; | ||
} | ||
public SourceText? OldText => oldText?.Text; | ||
public int OldVersion => oldText?.Version ?? -1; | ||
public SourceText NewText => newText.Text; | ||
public int NewVersion => newText.Version; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace MonoDevelop.MSBuild.Editor.LanguageServer; | ||
|
||
/// <summary> | ||
/// Workspace that tracks documents that are open in the editor | ||
/// </summary> | ||
public class EditorWorkspace : Workspace | ||
{ | ||
readonly object openDocumentsLock = new (); | ||
readonly Dictionary<DocumentId,EditorDocument> openDocuments = new(); | ||
|
||
public EditorWorkspace(HostServices services) : base(services) | ||
{ | ||
} | ||
|
||
public EditorDocument StartTrackingOpenDocument(string normalizedAbsoluteFilePath) | ||
{ | ||
var id = GetOrCreateDocumentId(normalizedAbsoluteFilePath); | ||
var document = new EditorDocument(this, id, null); | ||
lock(openDocumentsLock) { | ||
if (!openDocuments.TryAdd(id, document)) { | ||
throw new ArgumentException("Document is already being tracked"); | ||
} | ||
} | ||
|
||
if (DocumentOpened is {} handlers) { | ||
ScheduleTask(() => handlers.Invoke(this, new EditorDocumentEventArgs(document))); | ||
} | ||
|
||
return document; | ||
} | ||
|
||
public void StopTrackingOpenDocument(DocumentId id) | ||
{ | ||
lock(openDocumentsLock) { | ||
if (!openDocuments.Remove(id)) { | ||
throw new ArgumentException("Document is not being tracked"); | ||
} | ||
} | ||
if (DocumentClosed is {} handlers) { | ||
ScheduleTask(() => handlers.Invoke(this, new DocumentEventArgs(id))); | ||
} | ||
} | ||
|
||
public EditorDocument GetOpenDocument(DocumentId id) | ||
{ | ||
if (!openDocuments.TryGetValue(id, out var document)) { | ||
throw new ArgumentException("Document is not being tracked"); | ||
} | ||
return document; | ||
} | ||
|
||
public void UpdateOpenDocument(DocumentId id, SourceText text) | ||
{ | ||
var document = GetOpenDocument(id); | ||
document.UpdateText(text); | ||
} | ||
|
||
public event EventHandler<EditorDocumentEventArgs>? DocumentOpened; | ||
public event EventHandler<DocumentEventArgs>? DocumentClosed; | ||
} | ||
|
||
public class DocumentEventArgs(DocumentId documentId) : EventArgs | ||
{ | ||
public DocumentId DocumentId { get; } = documentId; | ||
} | ||
|
||
public class EditorDocumentEventArgs(EditorDocument document) : DocumentEventArgs(document.DocumentId) | ||
{ | ||
public EditorDocument Document { get; } = document; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// | ||
// https://raw.githubusercontent.com/dotnet/roslyn/dd3795a49875bef2728f01e0284406f33ea1e2e1/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Workspace/Mef/MefWorkspaceServices.cs | ||
// | ||
|
||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis.Host; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Roslyn.Utilities; | ||
|
||
[assembly: DebuggerTypeProxy(typeof(MefWorkspaceServices.LazyServiceMetadataDebuggerProxy), Target = typeof(ImmutableArray<Lazy<IWorkspaceService, WorkspaceServiceMetadata>>))] | ||
|
||
namespace Microsoft.CodeAnalysis.Host.Mef | ||
{ | ||
internal sealed class MefWorkspaceServices : HostWorkspaceServices | ||
{ | ||
private readonly IMefHostExportProvider _exportProvider; | ||
private readonly Workspace _workspace; | ||
|
||
private readonly ImmutableArray<Lazy<IWorkspaceService, WorkspaceServiceMetadata>> _services; | ||
|
||
// map of type name to workspace service | ||
private ImmutableDictionary<Type, Lazy<IWorkspaceService, WorkspaceServiceMetadata>?> _serviceMap | ||
= ImmutableDictionary<Type, Lazy<IWorkspaceService, WorkspaceServiceMetadata>?>.Empty; | ||
/* | ||
// accumulated cache for language services | ||
private ImmutableDictionary<string, MefLanguageServices> _languageServicesMap | ||
= ImmutableDictionary<string, MefLanguageServices>.Empty; | ||
*/ | ||
|
||
public MefWorkspaceServices(IMefHostExportProvider host, Workspace workspace) | ||
{ | ||
_exportProvider = host; | ||
_workspace = workspace; | ||
|
||
var services = host.GetExports<IWorkspaceService, WorkspaceServiceMetadata>(); | ||
var factories = host.GetExports<IWorkspaceServiceFactory, WorkspaceServiceMetadata>() | ||
.Select(lz => new Lazy<IWorkspaceService, WorkspaceServiceMetadata>(() => lz.Value.CreateService(this), lz.Metadata)); | ||
|
||
_services = services.Concat(factories).ToImmutableArray(); | ||
} | ||
|
||
public override HostServices HostServices | ||
{ | ||
get { return (HostServices)_exportProvider; } | ||
} | ||
|
||
internal IMefHostExportProvider HostExportProvider => _exportProvider; | ||
|
||
/* | ||
internal string? WorkspaceKind => _workspace.Kind; | ||
public override Workspace Workspace | ||
{ | ||
get | ||
{ | ||
//#if !CODE_STYLE | ||
// Contract.ThrowIfTrue(_workspace.Kind == CodeAnalysis.WorkspaceKind.RemoteWorkspace, "Access .Workspace off of a RemoteWorkspace MefWorkspaceServices is not supported."); | ||
//#endif | ||
return _workspace; | ||
} | ||
} | ||
*/ | ||
public override TWorkspaceService GetService<TWorkspaceService>() | ||
{ | ||
if (TryGetService(typeof(TWorkspaceService), out var service)) | ||
{ | ||
return (TWorkspaceService)service.Value; | ||
} | ||
else | ||
{ | ||
return default!; | ||
} | ||
} | ||
|
||
private bool TryGetService(Type serviceType, [NotNullWhen(true)] out Lazy<IWorkspaceService, WorkspaceServiceMetadata>? service) | ||
{ | ||
if (!_serviceMap.TryGetValue(serviceType, out service)) | ||
{ | ||
service = ImmutableInterlocked.GetOrAdd(ref _serviceMap, serviceType, serviceType => LayeredServiceUtilities.PickService(serviceType, _workspace.Kind, _services)); | ||
} | ||
|
||
return service != null; | ||
} | ||
|
||
private IEnumerable<string>? _languages; | ||
|
||
private IEnumerable<string> GetSupportedLanguages() | ||
{ | ||
if (_languages == null) | ||
{ | ||
var list = _exportProvider.GetExports<ILanguageService, LanguageServiceMetadata>().Select(lz => lz.Metadata.Language).Concat( | ||
_exportProvider.GetExports<ILanguageServiceFactory, LanguageServiceMetadata>().Select(lz => lz.Metadata.Language)) | ||
.Distinct(); | ||
|
||
Interlocked.CompareExchange(ref _languages, list, null); | ||
} | ||
|
||
return _languages; | ||
} | ||
|
||
public override IEnumerable<string> SupportedLanguages | ||
{ | ||
get { return this.GetSupportedLanguages(); } | ||
} | ||
|
||
public override bool IsSupported(string languageName) | ||
=> this.GetSupportedLanguages().Contains(languageName); | ||
|
||
public override HostLanguageServices GetLanguageServices(string languageName) | ||
{ | ||
var currentServicesMap = _languageServicesMap; | ||
if (!currentServicesMap.TryGetValue(languageName, out var languageServices)) | ||
{ | ||
languageServices = ImmutableInterlocked.GetOrAdd(ref _languageServicesMap, languageName, static (languageName, self) => new MefLanguageServices(self, languageName), this); | ||
} | ||
|
||
if (languageServices.HasServices) | ||
{ | ||
return languageServices; | ||
} | ||
else | ||
{ | ||
// throws exception | ||
#pragma warning disable RS0030 // Do not used banned API 'GetLanguageServices', use 'GetExtendedLanguageServices' instead - allowed in this context. | ||
return base.GetLanguageServices(languageName); | ||
#pragma warning restore RS0030 // Do not used banned APIs | ||
} | ||
} | ||
|
||
public override IEnumerable<TLanguageService> FindLanguageServices<TLanguageService>(MetadataFilter filter) | ||
{ | ||
foreach (var language in this.SupportedLanguages) | ||
{ | ||
#pragma warning disable RS0030 // Do not used banned API 'GetLanguageServices', use 'GetExtendedLanguageServices' instead - allowed in this context. | ||
var services = (MefLanguageServices)this.GetLanguageServices(language); | ||
#pragma warning restore RS0030 // Do not used banned APIs | ||
if (services.TryGetService(typeof(TLanguageService), out var service)) | ||
{ | ||
if (filter(service.Metadata.Data)) | ||
{ | ||
yield return (TLanguageService)service.Value; | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal bool TryGetLanguageServices(string languageName, [NotNullWhen(true)] out MefLanguageServices? languageServices) | ||
=> _languageServicesMap.TryGetValue(languageName, out languageServices); | ||
|
||
internal sealed class LazyServiceMetadataDebuggerProxy(ImmutableArray<Lazy<IWorkspaceService, WorkspaceServiceMetadata>> services) | ||
{ | ||
public (string type, string layer)[] Metadata | ||
=> services.Select(s => (s.Metadata.ServiceType, s.Metadata.Layer)).ToArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.