Skip to content

Commit

Permalink
Merge pull request #60687 from tmat/TSExternalAccess
Browse files Browse the repository at this point in the history
Add TS external access API for ICommentSelectionService
  • Loading branch information
JoeRobich authored Apr 12, 2022
2 parents ae66fbc + e3cabdc commit b0cd71f
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.ExternalAccess.VSTypeScript.Api
{
internal interface IVSTypeScriptCommentSelectionServiceImplementation : ILanguageService
{
Task<VSTypeScriptCommentSelectionInfo> GetInfoAsync(Document document, TextSpan textSpan, CancellationToken cancellationToken);

Task<Document> FormatAsync(Document document, ImmutableArray<TextSpan> changes, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 Microsoft.CodeAnalysis.CommentSelection;

namespace Microsoft.CodeAnalysis.ExternalAccess.VSTypeScript.Api
{
internal readonly struct VSTypeScriptCommentSelectionInfo
{
internal readonly CommentSelectionInfo UnderlyingObject;

internal VSTypeScriptCommentSelectionInfo(CommentSelectionInfo underlyingObject)
{
UnderlyingObject = underlyingObject;
}

public VSTypeScriptCommentSelectionInfo(
bool supportsSingleLineComment,
bool supportsBlockComment,
string singleLineCommentString,
string blockCommentStartString,
string blockCommentEndString) : this(new(
supportsSingleLineComment,
supportsBlockComment,
singleLineCommentString,
blockCommentStartString,
blockCommentEndString))
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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.Immutable;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CommentSelection;
using Microsoft.CodeAnalysis.ExternalAccess.VSTypeScript.Api;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.ExternalAccess.VSTypeScript
{
[ExportLanguageService(typeof(ICommentSelectionService), InternalLanguageNames.TypeScript), Shared]
internal sealed class VSTypeScriptCommentSelectionService : ICommentSelectionService
{
private readonly IVSTypeScriptCommentSelectionServiceImplementation? _impl;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public VSTypeScriptCommentSelectionService(
// Optional to work around test issue: https://github.com/dotnet/roslyn/issues/60690
[Import(AllowDefault = true)] IVSTypeScriptCommentSelectionServiceImplementation? impl)
{
_impl = impl;
}

public async Task<CommentSelectionInfo> GetInfoAsync(Document document, TextSpan textSpan, CancellationToken cancellationToken)
{
// Will never be null in product.
Contract.ThrowIfNull(_impl);

var info = await _impl.GetInfoAsync(document, textSpan, cancellationToken).ConfigureAwait(false);
return info.UnderlyingObject;
}

public Task<Document> FormatAsync(Document document, ImmutableArray<TextSpan> changes, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken)
{
// Will never be null in product.
Contract.ThrowIfNull(_impl);

return _impl.FormatAsync(document, changes, cancellationToken);
}
}
}

0 comments on commit b0cd71f

Please sign in to comment.