Skip to content

Commit

Permalink
Fix to enable description trimming in compiled JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-c-martin committed Aug 22, 2024
1 parent e42fbe6 commit bac830b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 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

0 comments on commit bac830b

Please sign in to comment.