|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.PowerShell.EditorServices.Symbols; |
| 10 | +using Newtonsoft.Json.Linq; |
| 11 | +using OmniSharp.Extensions.LanguageServer.Protocol.Models; |
| 12 | + |
| 13 | +namespace Microsoft.PowerShell.EditorServices.CodeLenses |
| 14 | +{ |
| 15 | + internal class PesterCodeLensProvider : ICodeLensProvider |
| 16 | + { |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// The symbol provider to get symbols from to build code lenses with. |
| 20 | + /// </summary> |
| 21 | + private readonly IDocumentSymbolProvider _symbolProvider; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Specifies a unique identifier for the feature provider, typically a |
| 25 | + /// fully-qualified name like "Microsoft.PowerShell.EditorServices.MyProvider" |
| 26 | + /// </summary> |
| 27 | + public string ProviderId => nameof(PesterCodeLensProvider); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Create a new Pester CodeLens provider for a given editor session. |
| 31 | + /// </summary> |
| 32 | + public PesterCodeLensProvider() |
| 33 | + { |
| 34 | + _symbolProvider = new PesterDocumentSymbolProvider(); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Get the Pester CodeLenses for a given Pester symbol. |
| 39 | + /// </summary> |
| 40 | + /// <param name="pesterSymbol">The Pester symbol to get CodeLenses for.</param> |
| 41 | + /// <param name="scriptFile">The script file the Pester symbol comes from.</param> |
| 42 | + /// <returns>All CodeLenses for the given Pester symbol.</returns> |
| 43 | + private CodeLens[] GetPesterLens(PesterSymbolReference pesterSymbol, ScriptFile scriptFile) |
| 44 | + { |
| 45 | + |
| 46 | + var codeLensResults = new CodeLens[] |
| 47 | + { |
| 48 | + new CodeLens() |
| 49 | + { |
| 50 | + Range = pesterSymbol.ScriptRegion.ToRange(), |
| 51 | + Data = JToken.FromObject(new { |
| 52 | + Uri = scriptFile.DocumentUri, |
| 53 | + ProviderId = nameof(PesterCodeLensProvider) |
| 54 | + }), |
| 55 | + Command = new Command() |
| 56 | + { |
| 57 | + Name = "PowerShell.RunPesterTests", |
| 58 | + Title = "Run tests", |
| 59 | + Arguments = JArray.FromObject(new object[] { |
| 60 | + scriptFile.DocumentUri, |
| 61 | + false /* No debug */, |
| 62 | + pesterSymbol.TestName, |
| 63 | + pesterSymbol.ScriptRegion?.StartLineNumber }) |
| 64 | + } |
| 65 | + }, |
| 66 | + |
| 67 | + new CodeLens() |
| 68 | + { |
| 69 | + Range = pesterSymbol.ScriptRegion.ToRange(), |
| 70 | + Data = JToken.FromObject(new { |
| 71 | + Uri = scriptFile.DocumentUri, |
| 72 | + ProviderId = nameof(PesterCodeLensProvider) |
| 73 | + }), |
| 74 | + Command = new Command() |
| 75 | + { |
| 76 | + Name = "PowerShell.RunPesterTests", |
| 77 | + Title = "Debug tests", |
| 78 | + Arguments = JArray.FromObject(new object[] { |
| 79 | + scriptFile.DocumentUri, |
| 80 | + true /* No debug */, |
| 81 | + pesterSymbol.TestName, |
| 82 | + pesterSymbol.ScriptRegion?.StartLineNumber }) |
| 83 | + } |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + return codeLensResults; |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Get all Pester CodeLenses for a given script file. |
| 92 | + /// </summary> |
| 93 | + /// <param name="scriptFile">The script file to get Pester CodeLenses for.</param> |
| 94 | + /// <returns>All Pester CodeLenses for the given script file.</returns> |
| 95 | + public CodeLens[] ProvideCodeLenses(ScriptFile scriptFile) |
| 96 | + { |
| 97 | + var lenses = new List<CodeLens>(); |
| 98 | + foreach (SymbolReference symbol in _symbolProvider.ProvideDocumentSymbols(scriptFile)) |
| 99 | + { |
| 100 | + if (symbol is PesterSymbolReference pesterSymbol) |
| 101 | + { |
| 102 | + if (pesterSymbol.Command != PesterCommandType.Describe) |
| 103 | + { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + lenses.AddRange(GetPesterLens(pesterSymbol, scriptFile)); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return lenses.ToArray(); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Resolve the CodeLens provision asynchronously -- just wraps the CodeLens argument in a task. |
| 116 | + /// </summary> |
| 117 | + /// <param name="codeLens">The code lens to resolve.</param> |
| 118 | + /// <param name="scriptFile">The script file.</param> |
| 119 | + /// <returns>The given CodeLens, wrapped in a task.</returns> |
| 120 | + public CodeLens ResolveCodeLens(CodeLens codeLens, ScriptFile scriptFile) |
| 121 | + { |
| 122 | + // This provider has no specific behavior for |
| 123 | + // resolving CodeLenses. |
| 124 | + return codeLens; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments