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

Add a codefix for BCP035 #4570

Merged
merged 8 commits into from
Sep 30, 2021

Conversation

pakrym
Copy link
Contributor

@pakrym pakrym commented Sep 23, 2021

Fixes: #4563

https://i.imgur.com/ZCDJMxn.gif

@pakrym pakrym changed the title pakrym/BCP035-code-fix Add a codefix for BCP035 Sep 23, 2021
…ode-fix

# Conflicts:
#	src/Bicep.Core/Diagnostics/DiagnosticBuilder.cs
@pakrym pakrym marked this pull request as ready for review September 23, 2021 22:53
.gitignore Show resolved Hide resolved
/// <summary>
/// Generate a string that represents this Syntax element optionally preserving formatting.
/// </summary>
public static string ToTextPreserveFormatting(this SyntaxBase syntax)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToTextPreserveFormatting

In \src\Bicep.Core.IntegrationTests\ParserTests.cs, we have FilesShouldRoundTripSuccessfully() and Oneliners_ShouldRoundTripSuccessfully(). Can you update them to also assert that the result from ToTextPreserveFormatting() matches what the tests get from their own print visitor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to remove the last usage of PrintVisitor and stumbled on this test code in PrintProgram_AnyProgram_ShouldRoundTrip:

			printVisitor.Visit(program);
            string programText = buffer.ToString();

            buffer.Clear();
            printVisitor.Visit(program);
            string formattedProgramText = buffer.ToString();

            formattedProgramText.Should().Be(programText);

Am I missing something or does it compare two equivalent outputs of printVisitor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should've been:


            printVisitor.Visit(formattedProgram);
            string formattedProgramText = buffer.ToString();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I won't try to fix it in this PR, but FYI it starts failing if you try to format the formattedProgram

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a good question for @shenglol who did the formatter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh good catch. That's definitely a bug. I've created #4605 to track it and will try to fix it later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks for fixing it. I've assigned #4605 to you and linked it.

@majastrz
Copy link
Member

majastrz commented Sep 24, 2021

If you start with this Bicep file (don't include the | char):

resource foo 'Microsoft.Storage/storageAccounts@2021-04-01' = {
  |
}

And then leave the cursor at the | and then click the codefix, you get the following result:

resource foo 'Microsoft.Storage/storageAccounts@2021-04-01' = {
  
  kind:
  location:
  name:
  sku:
}

Would it be possible to get rid of the extra line above the kind property?

@majastrz
Copy link
Member

I added some comments, but this is such a great feature! Thanks for contributing this! ❤

@majastrz
Copy link
Member

Btw, there's another case with discriminated object types. The codefix doesn't appear for a resource like this:

resource bar 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
  
}

This is a rarer case, so we don't need to block this PR on this part. The experience here would have to be different because there are n options for required properties. I think we'd need a separate code fix for each possible value of the discriminator property (similar to how we generate the required property snippets for the same).

@majastrz
Copy link
Member

The other thing I noticed is that the code fix only adds one level of required properties. For example with modules when I start with an empty body I get this:

module mod 'passthrough.bicep' = {
  name:
  params:
}

Then I have to specify the curly braces and click the code fix again to get the required params as well.

Should we make it recursive to match the snippets?

@majastrz
Copy link
Member

Found an indentation bug as well. If I start with:

module mod 'passthrough.bicep' = {
  name: 2
  params: {}
}

Then click the code fix on params, I get this:

module mod 'passthrough.bicep' = {
  name: 2
  params: {
  myInput:
}
}

@pakrym
Copy link
Contributor Author

pakrym commented Sep 25, 2021

I've added more tests and improved indentation.

Should we make it recursive to match the snippets?

Happy to look into it but would prefer to do it in a separate PR.

@pakrym
Copy link
Contributor Author

pakrym commented Sep 27, 2021

CI failure seems to be the build flakiness.

…ode-fix

# Conflicts:
#	src/Bicep.LangServer.IntegrationTests/CodeActionTests.cs
@@ -42,7 +42,6 @@ public static async Task<ILanguageClient> StartServerWithClientConnectionAsync(T
creationOptions = creationOptions with
{
SnippetsProvider = creationOptions.SnippetsProvider ?? SnippetsProvider,
NamespaceProvider = creationOptions.NamespaceProvider ?? TestTypeHelper.CreateEmptyProvider(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes it so the test server uses the same set of type definitions (AzTypes) as the normal bicep invocation.

@pakrym
Copy link
Contributor Author

pakrym commented Sep 27, 2021

Not sure if my change caused the ModifyBicepConfigFileAndVerifyCallsToFileResolver failure.

@shenglol
Copy link
Contributor

Not sure if my change caused the ModifyBicepConfigFileAndVerifyCallsToFileResolver failure.

Nope that's a flaky test we are working on fixing.

@pakrym
Copy link
Contributor Author

pakrym commented Sep 29, 2021

I think I've addressed all comments in this PR and the OSx failure is build flakiness. Please tell me if there is something else I've missed.

@shenglol shenglol linked an issue Sep 29, 2021 that may be closed by this pull request
Copy link
Contributor

@shenglol shenglol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! I just requeued the build jobs and hopefully they will pass this time.

Copy link
Member

@majastrz majastrz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM too. Thanks for adding this!

@anthony-c-martin anthony-c-martin merged commit e17f6ee into Azure:main Sep 30, 2021
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants