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

Move fixer down to code-style layer #66780

Merged
merged 11 commits into from
Feb 11, 2023
Merged

Conversation

CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi commented Feb 9, 2023

Closes #43070

This required a bunch of layering work as this fixer depends on the internal ICodeGenerationService. So supporting this meant moving that service (And ancillary types) into teh shared source project we have between CodeStyle and Workspace layers.

This will also help move further analyzers/fixers down that had a similar dependency.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner February 9, 2023 17:52
@CyrusNajmabadi CyrusNajmabadi marked this pull request as draft February 9, 2023 17:53
@@ -134,13 +133,18 @@ internal static class MakeLocalFunctionStaticCodeFixHelper
{
syntaxEditor.ReplaceNode(
identifierNode,
(node, generator) => generator.IdentifierName(parameter.Name.ToIdentifierToken()).WithTriviaFrom(node));
(node, generator) => SyntaxFactory.IdentifierName(parameter.Name.ToIdentifierToken()).WithTriviaFrom(node));
Copy link
Member Author

Choose a reason for hiding this comment

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

this existing SyntaxGenerator helper is internal. Easiest workaround was to just do what it does (since we're at the C# layer).

@@ -262,7 +262,7 @@ public partial class CodeGenerationTests
exception = e;
}

var expectedMessage = string.Format(WorkspacesResources.Cannot_generate_code_for_unsupported_operator_0, method.Name);
var expectedMessage = string.Format(WorkspaceExtensionsResources.Cannot_generate_code_for_unsupported_operator_0, method.Name);
Copy link
Member Author

Choose a reason for hiding this comment

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

a bunch of resources had to move as well. that's the bulk of the add/removes in this PR.

isOverride: symbol.IsOverride,
isSealed: symbol.IsSealed,
isRequired: symbol.IsRequired());
}
Copy link
Member Author

Choose a reason for hiding this comment

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

moved to shared location. i did'nt want to move all these extensions over as it had viral impact of forcing more stuff to move.

result ??= new List<ITypeParameterSymbol>();
type?.Accept(new CollectTypeParameterSymbolsVisitor(result, onlyMethodTypeParameters: false));
return result;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

moved to shared location.

<Compile Include="$(MSBuildThisFileDirectory)CodeGeneration\ParameterGenerator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)CodeGeneration\PropertyGenerator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)CodeGeneration\StatementGenerator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)CodeGeneration\TypeParameterGenerator.cs" />
Copy link
Member Author

Choose a reason for hiding this comment

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

Moving all thsee ICodeGenerationService pieces for C# into shared source project.

@@ -10,20 +10,20 @@ namespace Microsoft.CodeAnalysis.CSharp.CodeGeneration
{
internal class CSharpFlagsEnumGenerator : AbstractFlagsEnumGenerator
{
internal static readonly CSharpFlagsEnumGenerator Instance = new();
private static readonly SyntaxGenerator s_generatorInstance = CSharpSyntaxGenerator.Instance;
Copy link
Member Author

Choose a reason for hiding this comment

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

shared layer can't refer to this internal type. So all these types now have the SyntaxGenerator passed into them. it was a lot less impactful than i thought it would be.

@@ -214,7 +214,7 @@ private static BlockSyntax GenerateBlock(IMethodSymbol accessor)
}
else
{
AddAccessibilityModifiers(@event.DeclaredAccessibility, tokens, info, Accessibility.Private);
CSharpCodeGenerationHelpers.AddAccessibilityModifiers(@event.DeclaredAccessibility, tokens, info, Accessibility.Private);
Copy link
Member Author

Choose a reason for hiding this comment

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

had to qualify this as we have both a method and namespace with the same name in this layer.

if (context.Context.AddImports)
{
var addImportsOptions = await newDocument.GetAddImportPlacementOptionsAsync(context.FallbackOptions, cancellationToken).ConfigureAwait(false);
newDocument = await ImportAdder.AddImportsFromSymbolAnnotationAsync(newDocument, addImportsOptions, cancellationToken).ConfigureAwait(false);
}
#endif
Copy link
Member Author

Choose a reason for hiding this comment

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

carved out as it's not needed by the feature we actually moved down, and because this led to pulling in a huge amount of additional code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want a tracking issue to make this available in code style layer with a follow-up change? Might lead to subtle bugs if we move down a fixer to the shared layer that relies on this codegen option.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. I'll file follow-up!

@@ -77,13 +77,15 @@ public bool CanAddTo(SyntaxNode destination, Solution solution, CancellationToke
return false;
}

#if !CODE_STYLE
// If we are avoiding generating into files marked as generated (but are still regular files)
// then check accordingly. This is distinct from the prior check in that we as a fallback
// will generate into these files is we have no alternative.
if (checkGeneratedCode && document.IsGeneratedCode(cancellationToken))
Copy link
Member Author

Choose a reason for hiding this comment

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

IsGeneratedCode is not available in CODE_STYLE layer. We only have IsGeneratedCodeAsync instead. But trying to use that virally exploded into too much becoming async. So i just carved this out as not supported.

Copy link
Contributor

Choose a reason for hiding this comment

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

This sounds reasonable as code fixers likely won't be dealing with generated code.

@@ -129,7 +129,7 @@ public static int GetPreferredIndex(int index, IList<bool>? availableIndices, bo
var newLineStarter = string.Concat("\n", commentStarter);

// Start the comment with an empty line for visual clarity.
comment = string.Concat(commentStarter, "\r\n", commentStarter, xml.Replace("\n", newLineStarter));
comment = string.Concat(commentStarter, "\r\n", commentStarter, xml!.Replace("\n", newLineStarter));
Copy link
Member Author

Choose a reason for hiding this comment

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

We have a call to string.IsNullOrEmpty above. But it appars as if nullability info isn't flowing along from that in this layer. not sure why.

Copy link
Contributor

Choose a reason for hiding this comment

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

CodeStyle layer is likely depending on older compiler bits, so probably a bug fixed recently in nullability analysis.

@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review February 9, 2023 19:10
Comment on lines +25 to +27
protected override SyntaxGenerator GeneratorImpl
=> Service.LanguageServices.GetRequiredService<SyntaxGenerator>();

Copy link
Contributor

@mavasani mavasani Feb 11, 2023

Choose a reason for hiding this comment

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

Does this need to be in language specific layer? Can it be moved up to CodeGenerationContextInfo base type?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll try that in follow-up!

@mavasani
Copy link
Contributor

This required a bunch of layering work as this fixer depends on the internal ICodeGenerationService. So supporting this meant moving that service (And ancillary types) into teh shared source project we have between CodeStyle and Workspace layers.

This will also help move further analyzers/fixers down that had a similar dependency.

I presume this means we don't need #43056 anymore?

Copy link
Contributor

@mavasani mavasani left a comment

Choose a reason for hiding this comment

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

LGTM. This is fantastic work and should help move more fixers to the shared layer.

@mavasani
Copy link
Contributor

This PR should also close out #43070

@mavasani
Copy link
Contributor

@CyrusNajmabadi we should also be able to move down the below code fixer to shared layer after this PR goes in:

internal class CSharpUseLocalFunctionCodeFixProvider : SyntaxEditorBasedCodeFixProvider

@CyrusNajmabadi
Copy link
Member Author

I presume this means we don't need #43056 anymore?

Correct

@CyrusNajmabadi
Copy link
Member Author

@CyrusNajmabadi we should also be able to move down the below code fixer to shared layer after this PR goes in:

Will do next!

@CyrusNajmabadi CyrusNajmabadi merged commit 90b68d5 into dotnet:main Feb 11, 2023
@CyrusNajmabadi CyrusNajmabadi deleted the moveFixer branch February 11, 2023 14:49
@ghost ghost added this to the Next milestone Feb 11, 2023
@RikkiGibson RikkiGibson modified the milestones: Next, 17.6 P2 Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Port MakeLocalFunctionStaticCodeFixProvider to CodeStyle layer
3 participants