Skip to content

Commit

Permalink
Rebase from main
Browse files Browse the repository at this point in the history
  • Loading branch information
bhsubra committed May 27, 2021
1 parent dae29aa commit 6de1c07
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 49 deletions.
11 changes: 11 additions & 0 deletions src/Bicep.Core.Samples/Files/Completions/moduleObject.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@
"textEdit": {
"range": {},
"newText": "{\n\t$0\n}"
},
"command": {
"command": "bicep.Telemetry",
"arguments": [
{
"Properties": {
"name": "{}"
},
"EventName": "ModuleBodySnippetInsertion"
}
]
}
}
]
30 changes: 12 additions & 18 deletions src/Bicep.Core.Samples/Files/Completions/resourceObject.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,6 @@
"newText": "if (${1:condition}) {\n\t$0\n}"
}
},
{
"label": "snippet",
"kind": "snippet",
"detail": "Web Deploy for a Web Application",
"documentation": {
"kind": "markdown",
"value": "```bicep\n\n\n```"
},
"deprecated": false,
"preselect": true,
"sortText": "2_snippet",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "\n"
}
},
{
"label": "{}",
"kind": "snippet",
Expand All @@ -101,6 +83,18 @@
"textEdit": {
"range": {},
"newText": "{\n\t$0\n}"
},
"command": {
"command": "bicep.Telemetry",
"arguments": [
{
"Properties": {
"name": "{}",
"type": "error"
},
"EventName": "ResourceBodySnippetInsertion"
}
]
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@
"textEdit": {
"range": {},
"newText": "{\n\tname: $1\n\tparams: {\n\t\tarrayParam: $2\n\t\tobjParam: {\n\t\t}\n\t\tstringParamB: $3\n\t}\n\t$0\n}"
},
"command": {
"command": "bicep.Telemetry",
"arguments": [
{
"Properties": {
"name": "required-properties"
},
"EventName": "ModuleBodySnippetInsertion"
}
]
}
},
{
Expand All @@ -101,6 +112,17 @@
"textEdit": {
"range": {},
"newText": "{\n\t$0\n}"
},
"command": {
"command": "bicep.Telemetry",
"arguments": [
{
"Properties": {
"name": "{}"
},
"EventName": "ModuleBodySnippetInsertion"
}
]
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@
"textEdit": {
"range": {},
"newText": "{\n\tname: $1\n\tlocation: $2\n\t$0\n}"
},
"command": {
"command": "bicep.Telemetry",
"arguments": [
{
"Properties": {
"name": "required-properties",
"type": "Microsoft.Network/dnsZones@2018-05-01"
},
"EventName": "ResourceBodySnippetInsertion"
}
]
}
},
{
Expand All @@ -101,6 +113,18 @@
"textEdit": {
"range": {},
"newText": "{\n name: ${2:'name'}\n location: 'global'\n}\n"
},
"command": {
"command": "bicep.Telemetry",
"arguments": [
{
"Properties": {
"name": "snippet",
"type": "Microsoft.Network/dnsZones@2018-05-01"
},
"EventName": "ResourceBodySnippetInsertion"
}
]
}
},
{
Expand All @@ -119,6 +143,18 @@
"textEdit": {
"range": {},
"newText": "{\n\t$0\n}"
},
"command": {
"command": "bicep.Telemetry",
"arguments": [
{
"Properties": {
"name": "{}",
"type": "Microsoft.Network/dnsZones@2018-05-01"
},
"EventName": "ResourceBodySnippetInsertion"
}
]
}
}
]
18 changes: 10 additions & 8 deletions src/Bicep.LangServer.IntegrationTests/TelemetryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ public async Task VerifyTopLevelDeclarationSnippetInsertionFiresTelemetryEvent()
var telemetryReceived = new TaskCompletionSource<TelemetryEventParams>();

var client = await IntegrationTestHelper.StartServerWithClientConnectionAsync(
options => options.OnTelemetryEvent(telemetry => {
telemetryReceived.SetResult(telemetry);
}),
TestContext,
options =>
{
options.OnTelemetryEvent(telemetry => telemetryReceived.SetResult(telemetry));
},
fileResolver: new InMemoryFileResolver(fileSystemDict));

var mainUri = DocumentUri.FromFileSystemPath("/main.bicep");
Expand Down Expand Up @@ -67,11 +69,11 @@ public async Task VerifyTopLevelDeclarationSnippetInsertionFiresTelemetryEvent()

TelemetryEventParams telemetryEventParams = await IntegrationTestHelper.WithTimeoutAsync(telemetryReceived.Task);

telemetryEventParams.Data.Keys.Count.Should().Be(2);
telemetryEventParams.Data.Keys.Should().Contain("eventName");
telemetryEventParams.Data.Keys.Should().Contain("properties");
telemetryEventParams.Data["eventName"].ToString().Should().Be(TelemetryConstants.EventNames.TopLevelDeclarationSnippetInsertion);
telemetryEventParams.Data["properties"].ToString().Should().BeEquivalentToIgnoringNewlines(@"{
telemetryEventParams.ExtensionData.Keys.Count.Should().Be(2);
telemetryEventParams.ExtensionData.Keys.Should().Contain("eventName");
telemetryEventParams.ExtensionData.Keys.Should().Contain("properties");
telemetryEventParams.ExtensionData["eventName"].ToString().Should().Be(TelemetryConstants.EventNames.TopLevelDeclarationSnippetInsertion);
telemetryEventParams.ExtensionData["properties"].ToString().Should().BeEquivalentToIgnoringNewlines(@"{
""name"": ""res-aks-cluster""
}");
}
Expand Down
28 changes: 20 additions & 8 deletions src/Bicep.LangServer/Completions/BicepCompletionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ private IEnumerable<CompletionItem> GetDeclarationCompletions(BicepCompletionCon
resourceSnippet.Detail,
resourceSnippet.Text,
context.ReplacementRange,
resourceSnippet.CompletionPriority)
.WithCommand(command);
command,
resourceSnippet.CompletionPriority);
}
}

Expand Down Expand Up @@ -416,9 +416,9 @@ private IEnumerable<CompletionItem> CreateResourceBodyCompletions(SemanticModel
snippet.Detail,
snippet.Text,
context.ReplacementRange,
command,
snippet.CompletionPriority,
preselect: true)
.WithCommand(command);
preselect: true);
}
}
}
Expand All @@ -433,19 +433,18 @@ private IEnumerable<CompletionItem> CreateModuleBodyCompletions(SemanticModel mo
foreach (Snippet snippet in snippets)
{
string prefix = snippet.Prefix;
BicepTelemetryEvent telemetryEvent = BicepTelemetryEvent.Create(TelemetryConstants.EventNames.ResourceBodySnippetInsertion);
BicepTelemetryEvent telemetryEvent = BicepTelemetryEvent.Create(TelemetryConstants.EventNames.ModuleBodySnippetInsertion);
telemetryEvent.Set("name", prefix);
telemetryEvent.Set("type", typeSymbol.Name);

Command command = Command.Create(TelemetryConstants.CommandName, telemetryEvent);

yield return CreateContextualSnippetCompletion(prefix,
snippet.Detail,
snippet.Text,
context.ReplacementRange,
command,
snippet.CompletionPriority,
preselect: true)
.WithCommand(command);
preselect: true);
}
}
}
Expand Down Expand Up @@ -998,6 +997,19 @@ private static CompletionItem CreateContextualSnippetCompletion(string label, st
.Preselect(preselect)
.Build();

/// <summary>
/// Creates a completion with a contextual snippet with command option. This will look like a snippet to the user.
/// </summary>
private static CompletionItem CreateContextualSnippetCompletion(string label, string detail, string snippet, Range replacementRange, Command command, CompletionPriority priority = CompletionPriority.Medium, bool preselect = false) =>
CompletionItemBuilder.Create(CompletionItemKind.Snippet, label)
.WithSnippetEdit(replacementRange, snippet)
.WithCommand(command)
.WithDetail(detail)
.WithDocumentation($"```bicep\n{new Snippet(snippet).FormatDocumentation()}\n```")
.WithSortText(GetSortText(label, priority))
.Preselect(preselect)
.Build();

/// <summary>
/// Creates a completion with a contextual snippet. This will look like a snippet to the user.
/// </summary>
Expand Down
28 changes: 21 additions & 7 deletions src/Bicep.LangServer/Handlers/BicepTelemetryHandler.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Bicep.LanguageServer.Telemetry;
using MediatR;
using OmniSharp.Extensions.JsonRpc;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Workspace;

namespace Bicep.LanguageServer.Handlers
Expand All @@ -20,16 +22,28 @@ public class BicepTelemetryHandler : ExecuteCommandHandlerBase<BicepTelemetryEve
{
private readonly ITelemetryProvider TelemetryProvider;

public BicepTelemetryHandler(ITelemetryProvider telemetryProvider, ISerializer serializer)
: base(TelemetryConstants.CommandName, serializer)
public BicepTelemetryHandler(ITelemetryProvider telemetryProvider)
{
TelemetryProvider = telemetryProvider;
}

public override Task<Unit> Handle(BicepTelemetryEvent bicepTelemetryEvent, CancellationToken cancellationToken)
public override Task<BicepTelemetryEvent> Handle(ExecuteCommandParams<BicepTelemetryEvent> request, CancellationToken cancellationToken)
{
TelemetryProvider.PostEvent(bicepTelemetryEvent);
return Unit.Task;
JArray? arguments = request.Arguments;
if (arguments is not null && arguments.Any() &&
arguments[0] is JToken jToken &&
jToken.ToObject<BicepTelemetryEvent>() is BicepTelemetryEvent telemetryEvent)
{
TelemetryProvider.PostEvent(telemetryEvent);
return Task.FromResult(telemetryEvent);
}

return Task.FromResult(BicepTelemetryEvent.Create(string.Empty));
}

protected override ExecuteCommandRegistrationOptions CreateRegistrationOptions(ExecuteCommandCapability capability, ClientCapabilities clientCapabilities) => new()
{
Commands = new Container<string>(TelemetryConstants.CommandName)
};
}
}
3 changes: 0 additions & 3 deletions src/Bicep.LangServer/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
using Bicep.LanguageServer.Snippets;
using Bicep.LanguageServer.Telemetry;
using Microsoft.Extensions.DependencyInjection;
using OmniSharp.Extensions.LanguageServer.Protocol.Client;
using OmniSharp.Extensions.LanguageServer.Protocol.Window;
using OmniSharp.Extensions.LanguageServer.Server;
using OmnisharpLanguageServer = OmniSharp.Extensions.LanguageServer.Server.LanguageServer;

Expand Down Expand Up @@ -67,7 +65,6 @@ private Server(CreationOptions creationOptions, Action<LanguageServerOptions> on
.WithHandler<BicepDidChangeWatchedFilesHandler>()
.WithHandler<BicepSignatureHelpHandler>()
.WithHandler<BicepSemanticTokensHandler>()
#pragma warning restore 0612
.WithHandler<BicepTelemetryHandler>()
.WithServices(services => RegisterServices(creationOptions, services));

Expand Down
10 changes: 6 additions & 4 deletions src/Bicep.LangServer/Snippets/SnippetsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ private void CacheResourceDeclarationAndDependencies(string template, string man

if (declaredSymbol.DeclaringSyntax is ResourceDeclarationSyntax resourceDeclarationSyntax)
{
string type = declaredSymbol.Type.Name;

CacheResourceDeclaration(resourceDeclarationSyntax, type, template, description);
CacheResourceDependencies(kvp.Value, template, type);
if (declaredSymbol.Type is TypeSymbol typeSymbol && typeSymbol.TypeKind != TypeKind.Error)
{
string type = typeSymbol.Name;
CacheResourceDeclaration(resourceDeclarationSyntax, type, template, description);
CacheResourceDependencies(kvp.Value, template, type);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.LangServer/Telemetry/BicepTelemetryEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Bicep.LanguageServer.Telemetry
{
public class BicepTelemetryEvent : TelemetryEventParams
public record BicepTelemetryEvent : TelemetryEventParams
{
public Dictionary<string, string> Properties { get; set; } = new Dictionary<string, string>();

Expand Down

0 comments on commit 6de1c07

Please sign in to comment.