Skip to content

codelens support #1001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ public async Task StartAsync()
.WithHandler<DocumentRangeFormattingHandler>()
.WithHandler<ReferencesHandler>()
.WithHandler<DocumentSymbolHandler>()
.WithHandler<DocumentHighlightHandler>();
.WithHandler<DocumentHighlightHandler>()
.WithHandler<CodeLensHandlers>();

logger.LogInformation("Handlers added");
logger.LogInformation("Handlers added");
});

_serverStart.SetResult(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

namespace Microsoft.PowerShell.EditorServices.CodeLenses
{
/// <summary>
/// Represents data expected back in an LSP CodeLens response.
/// </summary>
internal class CodeLensData
{
public string Uri { get; set; }

public string ProviderId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.Threading;
using System.Threading.Tasks;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;

namespace Microsoft.PowerShell.EditorServices.CodeLenses
{
/// <summary>
/// Specifies the contract for a Code Lens provider.
/// </summary>
public interface ICodeLensProvider
{
/// <summary>
/// Specifies a unique identifier for the feature provider, typically a
/// fully-qualified name like "Microsoft.PowerShell.EditorServices.MyProvider"
/// </summary>
string ProviderId { get; }

/// <summary>
/// Provides a collection of CodeLenses for the given
/// document.
/// </summary>
/// <param name="scriptFile">
/// The document for which CodeLenses should be provided.
/// </param>
/// <returns>An array of CodeLenses.</returns>
CodeLens[] ProvideCodeLenses(ScriptFile scriptFile);

/// <summary>
/// Resolves a CodeLens that was created without a Command.
/// </summary>
/// <param name="codeLens">
/// The CodeLens to resolve.
/// </param>
/// <param name="scriptFile">
/// A CancellationToken which can be used to cancel the
/// request.
/// </param>
/// <returns>
/// A Task which returns the resolved CodeLens when completed.
/// </returns>
CodeLens ResolveCodeLens(
CodeLens codeLens,
ScriptFile scriptFile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.Collections.Generic;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;

namespace Microsoft.PowerShell.EditorServices.CodeLenses
{
/// <summary>
/// Specifies the contract for an implementation of
/// the ICodeLenses component.
/// </summary>
public interface ICodeLenses
{
/// <summary>
/// Gets the collection of ICodeLensProvider implementations
/// that are registered with this component.
/// </summary>
List<ICodeLensProvider> Providers { get; }

/// <summary>
/// Provides a collection of CodeLenses for the given
/// document.
/// </summary>
/// <param name="scriptFile">
/// The document for which CodeLenses should be provided.
/// </param>
/// <returns>An array of CodeLenses.</returns>
CodeLens[] ProvideCodeLenses(ScriptFile scriptFile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.Management.Automation.Language;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;

namespace Microsoft.PowerShell.EditorServices
{
internal static class IScriptExtentExtensions
{
public static Range ToRange(this IScriptExtent scriptExtent)
{
return new Range
{
Start = new Position
{
Line = scriptExtent.StartLineNumber - 1,
Character = scriptExtent.StartColumnNumber - 1
},
End = new Position
{
Line = scriptExtent.EndLineNumber - 1,
Character = scriptExtent.EndColumnNumber - 1
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerShell.EditorServices.Symbols;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;

namespace Microsoft.PowerShell.EditorServices.CodeLenses
{
internal class PesterCodeLensProvider : ICodeLensProvider
{

/// <summary>
/// The symbol provider to get symbols from to build code lenses with.
/// </summary>
private readonly IDocumentSymbolProvider _symbolProvider;

/// <summary>
/// Specifies a unique identifier for the feature provider, typically a
/// fully-qualified name like "Microsoft.PowerShell.EditorServices.MyProvider"
/// </summary>
public string ProviderId => nameof(PesterCodeLensProvider);

/// <summary>
/// Create a new Pester CodeLens provider for a given editor session.
/// </summary>
public PesterCodeLensProvider()
{
_symbolProvider = new PesterDocumentSymbolProvider();
}

/// <summary>
/// Get the Pester CodeLenses for a given Pester symbol.
/// </summary>
/// <param name="pesterSymbol">The Pester symbol to get CodeLenses for.</param>
/// <param name="scriptFile">The script file the Pester symbol comes from.</param>
/// <returns>All CodeLenses for the given Pester symbol.</returns>
private CodeLens[] GetPesterLens(PesterSymbolReference pesterSymbol, ScriptFile scriptFile)
{

var codeLensResults = new CodeLens[]
{
new CodeLens()
{
Range = pesterSymbol.ScriptRegion.ToRange(),
Data = JToken.FromObject(new {
Uri = scriptFile.DocumentUri,
ProviderId = nameof(PesterCodeLensProvider)
}),
Command = new Command()
{
Name = "PowerShell.RunPesterTests",
Title = "Run tests",
Arguments = JArray.FromObject(new object[] {
scriptFile.DocumentUri,
false /* No debug */,
pesterSymbol.TestName,
pesterSymbol.ScriptRegion?.StartLineNumber })
}
},

new CodeLens()
{
Range = pesterSymbol.ScriptRegion.ToRange(),
Data = JToken.FromObject(new {
Uri = scriptFile.DocumentUri,
ProviderId = nameof(PesterCodeLensProvider)
}),
Command = new Command()
{
Name = "PowerShell.RunPesterTests",
Title = "Debug tests",
Arguments = JArray.FromObject(new object[] {
scriptFile.DocumentUri,
true /* No debug */,
pesterSymbol.TestName,
pesterSymbol.ScriptRegion?.StartLineNumber })
}
}
};

return codeLensResults;
}

/// <summary>
/// Get all Pester CodeLenses for a given script file.
/// </summary>
/// <param name="scriptFile">The script file to get Pester CodeLenses for.</param>
/// <returns>All Pester CodeLenses for the given script file.</returns>
public CodeLens[] ProvideCodeLenses(ScriptFile scriptFile)
{
var lenses = new List<CodeLens>();
foreach (SymbolReference symbol in _symbolProvider.ProvideDocumentSymbols(scriptFile))
{
if (symbol is PesterSymbolReference pesterSymbol)
{
if (pesterSymbol.Command != PesterCommandType.Describe)
{
continue;
}

lenses.AddRange(GetPesterLens(pesterSymbol, scriptFile));
}
}

return lenses.ToArray();
}

/// <summary>
/// Resolve the CodeLens provision asynchronously -- just wraps the CodeLens argument in a task.
/// </summary>
/// <param name="codeLens">The code lens to resolve.</param>
/// <param name="scriptFile">The script file.</param>
/// <returns>The given CodeLens, wrapped in a task.</returns>
public CodeLens ResolveCodeLens(CodeLens codeLens, ScriptFile scriptFile)
{
// This provider has no specific behavior for
// resolving CodeLenses.
return codeLens;
}
}
}
Loading