-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fix 'ctor' snippet in nested class #68177
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,12 +32,25 @@ protected override async Task<TextChange> GenerateSnippetTextChangeAsync(Documen | |
| var syntaxFacts = document.GetRequiredLanguageService<ISyntaxFactsService>(); | ||
| var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
| var nodeAtPosition = root.FindNode(TextSpan.FromBounds(position, position)); | ||
| var containingType = nodeAtPosition.FirstAncestorOrSelf<SyntaxNode>(syntaxFacts.IsTypeDeclaration); | ||
|
|
||
| // Skip inner class in nested class if position is out of it. | ||
| // For example "class Outer{\r\n ctor@ \r\n class Inner {} }" | ||
| // Accept even if "static class" (CS0710). | ||
| var containingType = nodeAtPosition.FirstAncestorOrSelf<SyntaxNode>((node) => | ||
| { | ||
| return syntaxFacts.IsTypeDeclaration(node) | ||
| && IsConstructableTypeDeclaration(syntaxFacts, node) | ||
| && node.Span.Contains(position); | ||
| }); | ||
|
|
||
| Contract.ThrowIfNull(containingType); | ||
| var constructorDeclaration = generator.ConstructorDeclaration( | ||
| containingTypeName: syntaxFacts.GetIdentifierOfTypeDeclaration(containingType).ToString(), | ||
| accessibility: Accessibility.Public); | ||
| return new TextChange(TextSpan.FromBounds(position, position), constructorDeclaration.NormalizeWhitespace().ToFullString()); | ||
| } | ||
|
|
||
| // Move this to ISyntaxFacts if approved. | ||
| protected abstract bool IsConstructableTypeDeclaration(ISyntaxFacts syntaxFacts, SyntaxNode 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.
I think the
Containscheck would work. But we have FindTokenOnLeftOfPosition you could use for this.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.
@genlu Could you please explain a little more how to use FindTokenOnLeftPosition instead of Span.Contains?
In the following pattern, I tried, but I get a CloseBraceToken of InnerClass
Uh oh!
There was an error while loading. Please reload this page.
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 helper is what I had in mind. But it is C# specific.
Then there's this, but it seems the behavior might be the same as the code that causing problem in the first place. maybe try to change the code in CSharpSyntaxFacts.GetContainingTypeDeclaration to do something similar to GetContainingTypeOrEnumDeclarations and see what breaks?
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.
@genlu I changed it to process the parent node if CloseBraceToken is found, instead of using Span.Contains.
I tried using semantic model, but gave up on it because it only fails if the ctor is entered at the end of the file.