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

Share ISyntaxFacts/SyntaxGenerator with CodeStyle layer. #41376

Closed
wants to merge 22 commits into from

Conversation

CyrusNajmabadi
Copy link
Member

No description provided.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner February 3, 2020 22:48
@CyrusNajmabadi
Copy link
Member Author

Tagging @mavasani . This is the approach i'm talking about. Will continue trying to make this fully fleshed out.

@mavasani
Copy link
Contributor

mavasani commented Feb 3, 2020

@CyrusNajmabadi I think you also need to link in the implementation types and the extension methods used by the implementations, which means ensuring that the transitive closure of APIs invoked from syntax facts service or its C# and VB implementations are linked into CodeStyle layer + none of these have any Workspace dependencies, otherwise they will fail at runtime from command line.

Btw, I don't like all the explicit linked files in code style projects, and am proposing switching to shared projects in #41363, but we unfortunately did not get to discussing that approach in today's design meeting. For example, if you checkout my branch from that PR, you can just drag and drop ISyntaxFactsService and all its implementations from WorkspacesExtensions.shproj to CompilersExtension.shproj and go from there to move rest of the accessed extensions into CompilersExtension.shproj.

@CyrusNajmabadi
Copy link
Member Author

@CyrusNajmabadi I think you also need to link in the implementation types and the extension methods used by the implementations, which means ensuring that the transitive closure of APIs invoked from syntax facts service or its C# and VB implementations are linked into CodeStyle layer + none of these have any Workspace dependencies, otherwise they will fail at runtime from command line.

Yup. It's a WIP. Will be updating as a go along.

Btw, I don't like all the explicit linked files in code style projects, and am proposing switching to shared projects

That woudl also work for me. How the files come in is a separate concern. I just want to make sure all the code is 'workspace clean'. :)

@CyrusNajmabadi
Copy link
Member Author

if you want, we can also gitter chat.

/// The annotation the reducer uses to identify sub trees to be reduced.
/// The Expand operations add this annotation to nodes so that the Reduce operations later find them.
/// </summary>
public static SyntaxAnnotation Annotation { get; } = new SyntaxAnnotation();
Copy link
Member

Choose a reason for hiding this comment

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

😕 This is a public API. Why are we duplicating it?

Copy link
Member Author

Choose a reason for hiding this comment

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

WIP. :)

@CyrusNajmabadi
Copy link
Member Author

Ok. C# seems done-ish. Working on VB side now.

/// <summary>
/// The annotation used to mark portions of a syntax tree to be formatted.
/// </summary>
public static SyntaxAnnotation Annotation { get; } = new SyntaxAnnotation();
Copy link
Member Author

Choose a reason for hiding this comment

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

note: i'm taking this approach because we have code that i'm sharing that does thing like add these annotations. I could attempt to eitehr not share that code (which would mean ore shuffling around) or #ifdef it. I chose to do this for expediency as i don't really see any issue with annotations being added.

@@ -333,13 +333,13 @@ public static bool IsStaticallyDeclared(SyntaxToken token)
parentNode = parentNode!.Parent!.Parent;

// Check if this is a field constant declaration
if (parentNode.GetModifiers().Any(SyntaxKind.ConstKeyword))
if (parentNode!.GetModifiers().Any(SyntaxKind.ConstKeyword))
Copy link
Member Author

Choose a reason for hiding this comment

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

GetModifiers moved around nullable contexts. wasn't sure the best thing do to here. However, given the heavy use of ! already, this seemed like an acceptable quickpatch.

Copy link
Member Author

Choose a reason for hiding this comment

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

specifically, i didn't want to change runtime behavior. so i just suppressed to keep things safe.

private static SyntaxToken FindTokenOnLeftOfNode(SyntaxNode node)
{
return node.FindTokenOnLeftOfPosition(node.SpanStart);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

code was moved without touching semantics.

@@ -88,6 +88,7 @@ public override TypeSyntax VisitArrayType(IArrayTypeSymbol symbol)

break;
}
#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.

this is one area i did need to change code. That's beacuse .NullableAnnotation doesn't seem to be avialable in this layer yet. i assume we would have to wait to be able to reference a more recent compiler version.

Copy link
Member Author

Choose a reason for hiding this comment

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

if we move forward with this PR we should have a tracking issue to remove these.

public static bool IntersectsWith(this SyntaxToken token, int position)
{
return token.Span.IntersectsWith(position);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

note: i unified this with VB and moved to common layer. there is nothing language specific with this particular extension.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner February 4, 2020 01:09
@CyrusNajmabadi
Copy link
Member Author

This is ready for review @mavasani @sharwell.

@CyrusNajmabadi CyrusNajmabadi changed the title WIP: Share ISyntaxFacts with CodeStyle layer. Share ISyntaxFacts/SyntaxGenerator with CodeStyle layer. Feb 4, 2020
@jinujoseph jinujoseph added Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. labels Feb 4, 2020
[ImportingConstructor]
public CSharpSyntaxGenerator()
{
}
Copy link
Member Author

Choose a reason for hiding this comment

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

note that this is moved into a separate part. This allows hte data-only portion of this type (i.e. all of it by the mef stuff) to be used as a singleton in the codestyle layer. The workspcae layer then languageservice-izes it so it can be imported from that point upwards.

@@ -0,0 +1,135 @@
// Licensed to the .NET Foundation under one or more agreements.
Copy link
Member Author

Choose a reason for hiding this comment

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

all the files intended to be shared end with the _Shared.cs suffix. I believe these should all be pure splits into partial types, and pure moves of the code.

@CyrusNajmabadi
Copy link
Member Author

@sharwell @mavasani this is ready for re-review. This change moves (CSharp|VB)SyntaxGenerator, SyntaxFacts, SyntaxKinds and associated helper classes/extensions down into the CodeStyle layer to be used as statics/singletons.

99.9% of hte change is purely mechanical moves of code into files that can then be shared between the two layers. There is a tiny amount of changing that is very narrowly scoped to make this possible:

  1. ifdefing code out that requires the latest nullable apis to be available from the compiler. We should make a tracking bug to remove these ifdefs.
  2. breaking out the mef portion of code and the non-mef portion. The mef portoin continues to live at the workspaces layer (and eventually the codestyle.fixer layer), but will not be in the codestyle.analyzer layer.

@CyrusNajmabadi
Copy link
Member Author

CyrusNajmabadi commented Feb 10, 2020

I've merged all the latest changes we've all made into this. So this can go in safely into master.

Ths change alone will drop #41363 down by a third :)

<Compile Include="..\..\..\Workspaces\CSharp\Portable\Extensions\SyntaxNodeExtensions_SharedWithCodeStyle.cs" Link="Formatting\SyntaxNodeExtensions_SharedWithCodeStyle.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Extensions\SyntaxTokenExtensions_SharedWithCodeStyle.cs" Link="Formatting\SyntaxTokenExtensions_SharedWithCodeStyle.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Extensions\SyntaxTriviaExtensions.cs" Link="Formatting\SyntaxTriviaExtensions.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\CodeGeneration\CSharpFlagsEnumGenerator.cs" Link="CodeGeneration\CSharpFlagsEnumGenerator.cs" />
Copy link
Contributor

Choose a reason for hiding this comment

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

We want all of these now to happen via the newly added shared projects, not with linked files.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good. Let me figure out how to do that. thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

Should we do the split first, the move all the files over into the shared projects? Looking at the projects now, it still looks like they use linked files. Seems like we can two phase this.

Copy link
Member Author

Choose a reason for hiding this comment

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

benefit of that approach is that the sharedproject pr should have no actual code changes, just project structure changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

You should be able just create a folder named "Extensions" in the required shared projects under: https://github.com/dotnet/roslyn/tree/master/src/Workspaces/SharedUtilitiesAndExtensions. At this point you don't even need to name the files in shared projects end with _Shared or _SharedWithCodeStyle suffix.

Copy link
Contributor

Choose a reason for hiding this comment

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

My refactoring PR already removes all the linked files from CodeStyle layer projects, so it might be easier if that went in?

Copy link
Contributor

Choose a reason for hiding this comment

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

Let me update my PR with the latest sources from master, and you can see if it makes sense.

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!

<Compile Include="..\..\..\Workspaces\CSharp\Portable\Extensions\StatementSyntaxExtensions.cs" Link="Extensions\StatementSyntaxExtensions.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Extensions\StringExtensions.cs" Link="Extensions\StringExtensions.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Extensions\SyntaxListExtensions.cs" Link="Extensions\SyntaxListExtensions.cs" />
<Compile Include="..\..\..\Workspaces\CSharp\Portable\Extensions\SyntaxNodeExtensions_SharedWithCodeStyle.cs" Link="Extensions\SyntaxNodeExtensions_SharedWithCodeStyle.cs" />
Copy link
Contributor

Choose a reason for hiding this comment

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

These will certainly clash with my refactoring PR as I have split and moved bunch of these into the shared layer as well. One of us will have to deal with merge conflicts in few of files being added to shared layer now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. I think that is def the case. But ideally you shouldn't need to split/move these down yourself. the only splitting/moving would be for some extensions that weren't captured in this that are used by some other features?

but in tha tcase, i would argue again that they shouldn't be in that big PR. we should pull them out (like the unnecessary-imports part) so that none of that is in the big PR).

Copy link
Contributor

Choose a reason for hiding this comment

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

but in tha tcase, i would argue again that they shouldn't be in that big PR. we should pull them out

The refactoring PR is now just doing this exact thing: just splitting and moving down extensions into shared layers. Your PR here is doing so for SyntaxGenerator and SyntaxFacts and my PR is doing so for other features. I think the key is we should avoid these efforts in parallel as there will be overlap that leads to big merge conflicts. I would recommend waiting on this PR till the primary refactoring PR goes in.

Copy link
Member Author

Choose a reason for hiding this comment

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

Note: it may be hte case that that's what is going on in that PR... but it's also 8k more adds and 3k more deletes. So it's mixed in with a bunch of other stuff. We might want to consider how this could just go in, and then have the remaining stuff be teh final bit we need to review in isolation.

@mavasani
Copy link
Contributor

@CyrusNajmabadi Are you planning to continue with this work? Otherwise, I can take this up following the same model that you started with here.

@CyrusNajmabadi
Copy link
Member Author

@mavasani i think trying to merge this would be a bit too painful at this point. I'm ok with you taking this :)

@CyrusNajmabadi CyrusNajmabadi deleted the sharedSyntaxFacts branch April 11, 2021 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants