From bac830bcbfb849b16ec6784672b318f988324960 Mon Sep 17 00:00:00 2001 From: Anthony Martin <38542602+anthony-c-martin@users.noreply.github.com> Date: Wed, 21 Aug 2024 20:21:59 -0400 Subject: [PATCH] Fix to enable description trimming in compiled JSON --- .../Assertions/StringAssertionsExtensions.cs | 10 +++ .../Namespaces/SystemNamespaceType.cs | 5 ++ .../HoverTests.cs | 63 +++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/src/Bicep.Core.UnitTests/Assertions/StringAssertionsExtensions.cs b/src/Bicep.Core.UnitTests/Assertions/StringAssertionsExtensions.cs index 55021b4c4ee..4a4a8c510ab 100644 --- a/src/Bicep.Core.UnitTests/Assertions/StringAssertionsExtensions.cs +++ b/src/Bicep.Core.UnitTests/Assertions/StringAssertionsExtensions.cs @@ -89,6 +89,16 @@ public static AndConstraint BeEquivalentToIgnoringNewlines(thi return new AndConstraint(instance); } + public static AndConstraint 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(instance); + } + public static AndConstraint EqualIgnoringNewlines(this StringAssertions instance, string expected, string because = "", params object[] becauseArgs) { var normalizedActual = StringUtils.ReplaceNewlines(instance.Subject, "\n"); diff --git a/src/Bicep.Core/Semantics/Namespaces/SystemNamespaceType.cs b/src/Bicep.Core/Semantics/Namespaces/SystemNamespaceType.cs index 91cda9c0183..06b7cf80f43 100644 --- a/src/Bicep.Core/Semantics/Namespaces/SystemNamespaceType.cs +++ b/src/Bicep.Core/Semantics/Namespaces/SystemNamespaceType.cs @@ -1582,6 +1582,11 @@ static IEnumerable 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 }; } diff --git a/src/Bicep.LangServer.IntegrationTests/HoverTests.cs b/src/Bicep.LangServer.IntegrationTests/HoverTests.cs index ed701815f07..dd9518aa875 100644 --- a/src/Bicep.LangServer.IntegrationTests/HoverTests.cs +++ b/src/Bicep.LangServer.IntegrationTests/HoverTests.cs @@ -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; @@ -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 + { + [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 = @@ -1312,6 +1368,13 @@ private static IEnumerable GetData() return DataSets.NonStressDataSets.ToDynamicTestData(); } + private static async Task RequestHover(ILanguageClient client, BicepFile bicepFile, int cursor) + { + var hovers = await RequestHovers(client, bicepFile, [cursor]); + + return hovers.Single(); + } + private static async Task> RequestHovers(ILanguageClient client, BicepFile bicepFile, IEnumerable cursors) { return await RequestHovers(client, bicepFile.FileUri, bicepFile.LineStarts, cursors);