Skip to content
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

Scaffold required resource snippet based on the resourceType & apiVersion #2172

Merged
merged 13 commits into from
May 5, 2021
18 changes: 18 additions & 0 deletions src/Bicep.Core.Samples/Files/Completions/declarations.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,24 @@
"newText": "resource ${1:automationAccount} 'Microsoft.Automation/automationAccounts@2019-06-01' = {\n name: ${2:'name'}\n location: resourceGroup().location\n properties: {\n sku: {\n name: ${3|'Free','Basic'|}\n }\n }\n}\n"
}
},
{
"label": "res-automation-module",
"kind": "snippet",
"detail": "Automation Module",
"documentation": {
"kind": "markdown",
"value": "```bicep\nresource automationAccount 'Microsoft.Automation/automationAccounts@2015-10-31' = {\n name: 'name'\n}\n\nresource automationAccountVariable 'Microsoft.Automation/automationAccounts/modules@2015-10-31' = {\n parent: automationAccount\n name: 'name'\n properties: {\n contentLink: {\n uri: 'https://content-url.nupkg'\n }\n }\n}\n```"
},
"deprecated": false,
"preselect": false,
"sortText": "2_res-automation-module",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "resource automationAccount 'Microsoft.Automation/automationAccounts@2015-10-31' = {\n name: ${1:'name'}\n}\n\nresource ${2:automationAccountVariable} 'Microsoft.Automation/automationAccounts/modules@2015-10-31' = {\n parent: automationAccount\n name: ${3:'name'}\n properties: {\n contentLink: {\n uri: ${4:'https://content-url.nupkg'}\n }\n }\n}"
}
},
{
"label": "res-availability-set",
"kind": "snippet",
Expand Down
88 changes: 88 additions & 0 deletions src/Bicep.Core.Samples/Files/Completions/resourceObject.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
[
{
"label": "for",
"kind": "snippet",
"detail": "for",
"documentation": {
"kind": "markdown",
"value": "```bicep\n[for item in list: {\n\t\n}]\n```"
},
"deprecated": false,
"preselect": false,
"sortText": "1_for",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "[for ${2:item} in ${1:list}: {\n\t$0\n}]"
}
},
{
"label": "for-filtered",
"kind": "snippet",
"detail": "for-filtered",
"documentation": {
"kind": "markdown",
"value": "```bicep\n[for (item, index) in list: if (condition) {\n\t\n}]\n```"
},
"deprecated": false,
"preselect": false,
"sortText": "1_for-filtered",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "[for (${2:item}, ${3:index}) in ${1:list}: if (${4:condition}) {\n\t$0\n}]"
}
},
{
"label": "for-indexed",
"kind": "snippet",
"detail": "for-indexed",
"documentation": {
"kind": "markdown",
"value": "```bicep\n[for (item, index) in list: {\n\t\n}]\n```"
},
"deprecated": false,
"preselect": false,
"sortText": "1_for-indexed",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "[for (${2:item}, ${3:index}) in ${1:list}: {\n\t$0\n}]"
}
},
{
"label": "if",
"kind": "snippet",
"detail": "if",
"deprecated": false,
"preselect": false,
"sortText": "1_if",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "if (${1:condition}) {\n\t$0\n}"
}
},
{
"label": "{}",
"kind": "snippet",
"detail": "{}",
"documentation": {
"kind": "markdown",
"value": "```bicep\n{\n\t\n}\n```"
},
"deprecated": false,
"preselect": true,
"sortText": "2_{}",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "{\n\t$0\n}"
}
}
]
2 changes: 1 addition & 1 deletion src/Bicep.Core.Samples/Files/InvalidModules_LF/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module moduleWithoutPath = {
// missing identifier #completionTest(7) -> empty
module

// #completionTest(24,25) -> object
// #completionTest(24,25) -> moduleObject
module missingValue '' =

var interp = 'hello'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module
//@[7:7) [BCP096 (Error)] Expected a module identifier at this location. ||
//@[7:7) [BCP090 (Error)] This module declaration is missing a file path reference. ||

// #completionTest(24,25) -> object
// #completionTest(24,25) -> moduleObject
module missingValue '' =
//@[20:22) [BCP050 (Error)] The specified module path is empty. |''|
//@[25:25) [BCP118 (Error)] Expected the "{" character, the "[" character, or the "if" keyword at this location. ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module moduleWithoutPath = {
// missing identifier #completionTest(7) -> empty
module

// #completionTest(24,25) -> object
// #completionTest(24,25) -> moduleObject
module missingValue '' =

var interp = 'hello'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module moduleWithoutPath = {
module
//@[7:7) Module <missing>. Type: error. Declaration start char: 0, length: 7

// #completionTest(24,25) -> object
// #completionTest(24,25) -> moduleObject
module missingValue '' =
//@[7:19) Module missingValue. Type: error. Declaration start char: 0, length: 25

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ module
//@[7:7) SkippedTriviaSyntax
//@[7:9) NewLine |\n\n|

// #completionTest(24,25) -> object
//@[35:36) NewLine |\n|
// #completionTest(24,25) -> moduleObject
//@[41:42) NewLine |\n|
module missingValue '' =
//@[0:25) ModuleDeclarationSyntax
//@[0:6) Identifier |module|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ module
//@[0:6) Identifier |module|
//@[7:9) NewLine |\n\n|

// #completionTest(24,25) -> object
//@[35:36) NewLine |\n|
// #completionTest(24,25) -> moduleObject
//@[41:42) NewLine |\n|
module missingValue '' =
//@[0:6) Identifier |module|
//@[7:19) Identifier |missingValue|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,53 @@
"newText": "if (${1:condition}) {\n\t$0\n}"
}
},
{
"label": "insert-required",
"kind": "snippet",
"detail": "Required properties",
"documentation": {
"kind": "markdown",
"value": "```bicep\n{\n\tname: \n\tlocation: \n\t\n}\n```"
},
"deprecated": false,
"preselect": true,
"sortText": "2_insert-required",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "{\n\tname: $1\n\tlocation: $2\n\t$0\n}"
}
},
{
"label": "insert-snippet",
"kind": "snippet",
"detail": "DNS Zone",
"documentation": {
"kind": "markdown",
"value": "```bicep\n{\n name: 'name'\n location: 'global'\n}\n\n```"
},
"deprecated": false,
"preselect": true,
"sortText": "2_insert-snippet",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "{\n name: ${2:'name'}\n location: 'global'\n}\n"
}
},
{
"label": "{}",
"kind": "value",
"kind": "snippet",
"detail": "{}",
"documentation": {
"kind": "markdown",
"value": "```bicep\n{\n\t\n}\n```"
},
"deprecated": false,
"preselect": true,
"sortText": "1_{}",
"sortText": "2_{}",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ resource foo 'ddd'
// #completionTest(23) -> resourceTypes
resource trailingSpace

// #completionTest(19,20) -> object
// #completionTest(19,20) -> resourceObject
resource foo 'ddd'=

// wrong resource type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ resource trailingSpace
//@[24:24) [BCP068 (Error)] Expected a resource type string. Specify a valid resource type of format "<provider>/<types>@<apiVersion>". ||
//@[24:24) [BCP029 (Error)] The resource type is not valid. Specify a valid resource type of format "<provider>/<types>@<apiVersion>". ||

// #completionTest(19,20) -> object
// #completionTest(19,20) -> resourceObject
resource foo 'ddd'=
//@[9:12) [BCP028 (Error)] Identifier "foo" is declared multiple times. Remove or rename the duplicates. |foo|
//@[13:18) [BCP029 (Error)] The resource type is not valid. Specify a valid resource type of format "<provider>/<types>@<apiVersion>". |'ddd'|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ resource foo 'ddd'
// #completionTest(23) -> resourceTypes
resource trailingSpace

// #completionTest(19,20) -> object
// #completionTest(19,20) -> resourceObject
resource foo 'ddd'=

// wrong resource type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ resource foo 'ddd'
resource trailingSpace
//@[9:22) Resource trailingSpace. Type: error. Declaration start char: 0, length: 24

// #completionTest(19,20) -> object
// #completionTest(19,20) -> resourceObject
resource foo 'ddd'=
//@[9:12) Resource foo. Type: error. Declaration start char: 0, length: 20

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ resource trailingSpace
//@[24:24) SkippedTriviaSyntax
//@[24:28) NewLine |\r\n\r\n|

// #completionTest(19,20) -> object
//@[35:37) NewLine |\r\n|
// #completionTest(19,20) -> resourceObject
//@[43:45) NewLine |\r\n|
resource foo 'ddd'=
//@[0:20) ResourceDeclarationSyntax
//@[0:8) Identifier |resource|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ resource trailingSpace
//@[9:22) Identifier |trailingSpace|
//@[24:28) NewLine |\r\n\r\n|

// #completionTest(19,20) -> object
//@[35:37) NewLine |\r\n|
// #completionTest(19,20) -> resourceObject
//@[43:45) NewLine |\r\n|
resource foo 'ddd'=
//@[0:8) Identifier |resource|
//@[9:12) Identifier |foo|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// $1 = 'name'
// $2 = automationAccountVariable
// $3 = 'name'
// $4 = 'https://test-content-url.nupkg'

// Insert snippet here
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource automationAccount 'Microsoft.Automation/automationAccounts@2015-10-31' = {
name: 'name'
}

resource automationAccountVariable 'Microsoft.Automation/automationAccounts/modules@2015-10-31' = {
parent: automationAccount
name: 'name'
properties: {
contentLink: {
uri: 'https://test-content-url.nupkg'
}
}
}
19 changes: 19 additions & 0 deletions src/Bicep.LangServer.UnitTests/Snippets/SnippetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ public void SinglePropertySnippetShouldParseCorrectly()
snippet.FormatDocumentation().Should().Be("name: ''");
}

[TestMethod]
public void SnippetPlaceholderTextWithUrlShouldParseCorrectly()
{
string text = @"var testIdentifier = '${1:http://test-content-url.nupkg}'";

var snippet = new Snippet(text);
snippet.Text.Should().Be(text);

snippet.Placeholders.Should().SatisfyRespectively(
x =>
{
x.Index.Should().Be(1);
x.Name.Should().Be("http://test-content-url.nupkg");
x.Span.ToString().Should().Be("[22:56]");
});

snippet.FormatDocumentation().Should().Be("var testIdentifier = 'http://test-content-url.nupkg'");
}

[TestMethod]
public void SnippetPlaceholderTextWithMultipleChoicesShouldReturnFirstOneByDefault()
{
Expand Down
Loading