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

Introduce a DiscardSyntaxClassifier #40396

Merged
merged 25 commits into from
Jan 27, 2020
Merged

Conversation

lameox
Copy link
Contributor

@lameox lameox commented Dec 14, 2019

fixes #39768 .

This is my first contribution so please let me know if there is anything else needed apart from the exported highlighter or if there is a better place in the codebase to introduce the changes.

@lameox lameox requested a review from a team as a code owner December 14, 2019 19:56
@dnfclas
Copy link

dnfclas commented Dec 14, 2019

CLA assistant check
All CLA requirements met.

@jinujoseph jinujoseph added Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. labels Dec 15, 2019
@CyrusNajmabadi
Copy link
Member

hi @lameox thanks for the contribution!

}


return SpecializedCollections.EmptyEnumerable<TextSpan>();
Copy link
Member

Choose a reason for hiding this comment

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

should be impossible to hit this. we should throw here.

Copy link
Member

Choose a reason for hiding this comment

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

can use a switch expression here as well.

Copy link
Member

Choose a reason for hiding this comment

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

Since this is argument syntax (as opposed to parameter syntax), wouldn't this be hit for most arguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah i think so. This is why I originally used AbstractKeywordHighlighter instead of AbstractKeywordHighlighter<ArgumentSyntax> and overwrote IsHighlightableNode so GetHighlightsForNode only got called for the required types of nodes. However it was suggested I should use AbstractKeywordHighlighter<ArgumentSyntax> which unfortunately seals IsHighlightableNode. Therefore this code always executes and I need to return the empty enumerable.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not seeing how this woudl be subtantively different. Both cases need to check all nodes to see if they should run against them.

namespace Microsoft.CodeAnalysis.Editor.CSharp.Highlighting.KeywordHighlighters
{
[ExportHighlighter(LanguageNames.CSharp)]
internal class DiscardParameterHighlighter : AbstractKeywordHighlighter
Copy link
Member

Choose a reason for hiding this comment

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

ther's a different baseclass you can subclass right? where you can specify through a type argument that you are constrained to ArgumentSyntax right?

Copy link
Member

Choose a reason for hiding this comment

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

yes, it's AbstractKeywordHighlighter<ArgumentSyntax>.

Copy link
Member

@CyrusNajmabadi CyrusNajmabadi left a comment

Choose a reason for hiding this comment

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

Can be simplified

{
var syntax = (IdentifierNameSyntax)node.Expression;

if (syntax.Identifier.Text == "_")
Copy link
Member

Choose a reason for hiding this comment

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

Switch to typechecks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

@CyrusNajmabadi CyrusNajmabadi left a comment

Choose a reason for hiding this comment

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

Lgtm

@CyrusNajmabadi
Copy link
Member

@jinujoseph can we get a buddy for this community pr? Thanks!

Comment on lines 25 to 26
if (node.Expression is IdentifierNameSyntax nameSyntax
&& nameSyntax.Identifier.Text == "_")
Copy link
Member

Choose a reason for hiding this comment

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

💡 Can use pattern matching here:

if (node.Expression is IdentifierNameSyntax { Identifier: { Text: "_" } } nameSyntax)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I still need to get used to pattern matching tbh. Should all new code aim to make use of pattern matching or is it up to preference?

Copy link
Member

Choose a reason for hiding this comment

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

It's up to preference. in some places it works nicely (like here), in others, it can feel more forced/unpleasant.

}


return SpecializedCollections.EmptyEnumerable<TextSpan>();
Copy link
Member

Choose a reason for hiding this comment

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

Since this is argument syntax (as opposed to parameter syntax), wouldn't this be hit for most arguments?

{
void Method()
{
int i = int.TryParse("""", out var {|Cursor:[|_|]|});
Copy link
Member

Choose a reason for hiding this comment

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

💡 Should add a test showing that this does not highlight the argument if you write it as @_. This token has the same ValueText as _, but different Text, and in this case the difference is meaningful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added tests covering that case.

@sharwell sharwell changed the title Introduce a DiscardParameterHighlighter which fixes #39768 Introduce a DiscardParameterHighlighter Dec 16, 2019
Added a test to ensure @_ is not highlighted incorrectly.
@lameox lameox changed the title Introduce a DiscardParameterHighlighter Introduce a DiscardArgumentHighlighter Dec 16, 2019
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi left a comment

Choose a reason for hiding this comment

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

c

Copy link
Member

@JoeRobich JoeRobich left a comment

Choose a reason for hiding this comment

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

LGTM

Can you add a test for _ = int.Parse("");?

@lameox
Copy link
Contributor Author

lameox commented Jan 15, 2020

@JoeRobich Sure thing. This uncovered the missing case for VariableDeclaratorSyntax. From what i can tell i don't actually need symbols for those and can just check the Identifier for _.

Another thing i was kind of suprised about was the test for

 _ = int.Parse("");

In this case we get the following symbol classifications:

Keyword "_"
Static "Parse"
Method "Parse"

This is in contrast to all other tests where the order of the Static "Parse" and Method "Parse" is reversed. For now i just reversed the expected order in the test but this seems kind of fishy to me. Should i keep it like that or investigate further?

@JoeRobich
Copy link
Member

JoeRobich commented Jan 15, 2020

Should i keep it like that or investigate further?

@lameox Leave it as is. We are likely to make some changes with regards to the Static classification in 16.6.

…tion/DiscardSyntaxClassifier.cs

Co-Authored-By: Joey Robichaud <joseph.robichaud@microsoft.com>
…n't discards from the compilers view. Kept the tests around and tweaked them so they now check that nothing is classified in these cases.
@lameox
Copy link
Contributor Author

lameox commented Jan 16, 2020

Close and reopen to trigger retest

@lameox lameox closed this Jan 16, 2020
@lameox lameox reopened this Jan 16, 2020
Copy link
Member

@JoeRobich JoeRobich left a comment

Choose a reason for hiding this comment

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

Thanks @lameox!

…rTests.cs

Co-Authored-By: Joey Robichaud <joseph.robichaud@microsoft.com>
@JoeRobich JoeRobich merged commit 9161fa5 into dotnet:master Jan 27, 2020
@lameox lameox deleted the highlight-out-_ branch January 28, 2020 19:26
@JoeRobich JoeRobich added this to the 16.5.P3 milestone Feb 28, 2020
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.

Discard in 'out _' is not classified as a keyword
8 participants