Skip to content

Conversation

@gekka
Copy link
Contributor

@gekka gekka commented May 12, 2023

Fixes #68176
Fixes AB#1818584

Ignore the input if it is entered in the trivia position of the inner class.
Add Test code.

@gekka gekka requested a review from a team as a code owner May 12, 2023 09:59
@ghost ghost added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead Community The pull request was submitted by a contributor who is not a Microsoft employee. labels May 12, 2023
%keyword% Inner
{
}
}".Replace("%keyword%", keyword);
Copy link
Contributor

Choose a reason for hiding this comment

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

Use string interpolation in all tests. This %keyword% notation is really confusing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DoctorKrolic , Should I change to Interpolation?

$@"class Outer
{{
    $$
    { keyword } Inner
    {{
    }}
}}";

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I think it looks if not better then at least more familiar. If you want to improve it even more use raw string literals (I would preffer this).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DoctorKrolic I pushed modified code.

$$"""
class Outer
{
    $$
    {{keyword}} Inner
    {
    }
}
""";

}

// Move this to ISyntaxFacts if approved.
protected abstract bool IsConstructableTypeDeclaration(ISyntaxFacts syntaxFacts, SyntaxNode node);
Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary? There's already a check in CSharpConstructorSnippetProvider.IsValidSnippetLocationAsync

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@genlu Isn't IsValidSnippetLocationAsync check that the constructor can be placed where 'ctor' is entered?
I think that function is not suitable to get the target class name.

Is it acceptable that the decisions required within AbstractConstructorSnippetProvider depend on the processing implemented in the classes that inherited AbstractConstructorSnippetProvider?
I can't think of any other way to determine the constructable type from within an abstract class.

Copy link
Member

Choose a reason for hiding this comment

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

You are right, IsValidSnippetLocationAsync](https://sourceroslyn.io/#Microsoft.CodeAnalysis.Features/Snippets/SnippetProviders/AbstractSnippetProvider.cs,73) is used to decide whether a snippet should placed at the location. So if the check in IsValidSnippetLocationAsync fails, we won't even show the snippet thus it will never get to the point where code change is being calculated.

So to answer your question: yes, it's not only acceptable, it's actually supposed to work this way by design.

{
return syntaxFacts.IsTypeDeclaration(node)
&& IsConstructableTypeDeclaration(syntaxFacts, node)
&& node.Span.Contains(position);
Copy link
Member

Choose a reason for hiding this comment

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

I think the Contains check would work. But we have FindTokenOnLeftOfPosition you could use for this.

Copy link
Contributor Author

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

$$"""
class Outer
{
    class Inner
    {
    }

    $$
}
""";

Copy link
Member

@genlu genlu May 15, 2023

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?

Copy link
Contributor Author

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.

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. untriaged Issues and PRs which have not yet been triaged by a lead

Projects

None yet

Development

Successfully merging this pull request may close these issues.

'ctor' snippet generate wrong name in nested class

3 participants