-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Conversation
src/Workspaces/Core/Portable/Microsoft.CodeAnalysis.Workspaces.csproj
Outdated
Show resolved
Hide resolved
@@ -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)); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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()); | ||
} |
There was a problem hiding this comment.
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; | ||
} |
There was a problem hiding this comment.
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" /> |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
protected override SyntaxGenerator GeneratorImpl | ||
=> Service.LanguageServices.GetRequiredService<SyntaxGenerator>(); | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
I presume this means we don't need #43056 anymore? |
There was a problem hiding this 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.
This PR should also close out #43070 |
@CyrusNajmabadi we should also be able to move down the below code fixer to shared layer after this PR goes in: roslyn/src/Features/CSharp/Portable/UseLocalFunction/CSharpUseLocalFunctionCodeFixProvider.cs Line 32 in 321601a
|
Correct |
Will do next! |
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.