Skip to content

Commit

Permalink
Trim descriptions in compiled JSON (#14870)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-c-martin authored Aug 23, 2024
1 parent 9bbe7ae commit acb882c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/Bicep.Core.UnitTests/Assertions/StringAssertionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ public static AndConstraint<StringAssertions> BeEquivalentToIgnoringNewlines(thi
return new AndConstraint<StringAssertions>(instance);
}

public static AndConstraint<StringAssertions> BeEquivalentToIgnoringTrailingWhitespace(this StringAssertions instance, string expected, string because = "", params object[] becauseArgs)
{
var normalizedActual = string.Join("\n", StringUtils.SplitOnNewLine(instance.Subject).Select(x => x.TrimEnd()));
var normalizedExpected = string.Join("\n", StringUtils.SplitOnNewLine(expected).Select(x => x.TrimEnd()));

normalizedActual.Should().BeEquivalentTo(normalizedExpected, because, becauseArgs);

return new AndConstraint<StringAssertions>(instance);
}

public static AndConstraint<StringAssertions> EqualIgnoringNewlines(this StringAssertions instance, string expected, string because = "", params object[] becauseArgs)
{
var normalizedActual = StringUtils.ReplaceNewlines(instance.Subject, "\n");
Expand Down
5 changes: 5 additions & 0 deletions src/Bicep.Core/Semantics/Namespaces/SystemNamespaceType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,11 @@ static IEnumerable<Decorator> GetAlwaysPermittedDecorators()
if (decorated is DescribableExpression describable &&
functionCall.Parameters.FirstOrDefault() is { } description)
{
if (description is StringLiteralExpression stringLiteral)
{
description = stringLiteral with { Value = StringUtils.NormalizeIndent(stringLiteral.Value) };
}

return describable with { Description = description };
}

Expand Down
63 changes: 63 additions & 0 deletions src/Bicep.LangServer.IntegrationTests/HoverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.ResourceStack.Common.Json;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Client;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
Expand Down Expand Up @@ -1066,6 +1067,61 @@ param baz t.|*
h => h!.Contents.MarkupContent!.Value.Should().Be("```bicep\n*: int\n``` \n \n"));
}

[TestMethod]
public async Task Compiled_json_contains_trimmed_descriptions_in_hovers()
{
// https://github.com/Azure/bicep/issues/14864
var jsonContents = CompilationHelper.Compile("""
type fooType = {
@description('''Description
```sql
AzureMetrics
| where ResourceProvider == 'MICROSOFT.ANALYSISSERVICES'
```
''')
foo2: string?
}
param foo fooType = {}
""");

var (bicepparamText, cursor) = ParserHelper.GetFileWithSingleCursor("""
using 'main.json'
param foo = {
fo|o2:
}
""");

var jsonUri = new Uri("file:///path/to/main.json");
var paramsUri = new Uri("file:///path/to/params.bicepparam");

var files = new Dictionary<Uri, string>
{
[jsonUri] = jsonContents.Template.ToJson(),
[paramsUri] = bicepparamText,
};

using var helper = await LanguageServerHelper.StartServerWithText(this.TestContext, files, paramsUri, services => services.WithNamespaceProvider(BuiltInTestTypes.Create()));
var client = helper.Client;

var hover = await RequestHover(client, SourceFileFactory.CreateBicepFile(paramsUri, bicepparamText), cursor);
hover!.Contents!.MarkupContent!.Value
.Should().BeEquivalentToIgnoringTrailingWhitespace("""
```bicep
foo2: null | string
```
Description
```sql
AzureMetrics
| where ResourceProvider == 'MICROSOFT.ANALYSISSERVICES'
```
""");
}

private string GetManifestFileContents(string? documentationUri, string? description)
{
string annotations =
Expand Down Expand Up @@ -1312,6 +1368,13 @@ private static IEnumerable<object[]> GetData()
return DataSets.NonStressDataSets.ToDynamicTestData();
}

private static async Task<Hover?> RequestHover(ILanguageClient client, BicepFile bicepFile, int cursor)
{
var hovers = await RequestHovers(client, bicepFile, [cursor]);

return hovers.Single();
}

private static async Task<IEnumerable<Hover?>> RequestHovers(ILanguageClient client, BicepFile bicepFile, IEnumerable<int> cursors)
{
return await RequestHovers(client, bicepFile.FileUri, bicepFile.LineStarts, cursors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "17593581540296161693"
"templateHash": "3208032528583566429"
},
"name": "Sample module",
"description": "Sample description",
Expand Down Expand Up @@ -61,7 +61,7 @@
"minValue": 0,
"maxValue": 1023,
"metadata": {
"description": "The OS disk size (in GB)\r\n- Minimum value is 0\r\n- Maximum value is 1023\r\n"
"description": "The OS disk size (in GB)\n- Minimum value is 0\n- Maximum value is 1023\n"
}
},
"agentCount": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "11093856560557107596"
"templateHash": "16920590476746429823"
},
"name": "Sample module",
"description": "Sample description",
Expand All @@ -22,7 +22,7 @@
"minValue": 0,
"maxValue": 1023,
"metadata": {
"description": "The OS disk size (in GB)\r\n- Minimum value is 0\r\n- Maximum value is 1023\r\n"
"description": "The OS disk size (in GB)\n- Minimum value is 0\n- Maximum value is 1023\n"
}
},
"DiskSizeGB": {
Expand Down

0 comments on commit acb882c

Please sign in to comment.