-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Conversation
hi @lameox thanks for the contribution! |
src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/DiscardParameterHighlighter.cs
Outdated
Show resolved
Hide resolved
src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/DiscardParameterHighlighter.cs
Outdated
Show resolved
Hide resolved
src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/DiscardParameterHighlighter.cs
Outdated
Show resolved
Hide resolved
src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/DiscardParameterHighlighter.cs
Outdated
Show resolved
Hide resolved
src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/DiscardParameterHighlighter.cs
Outdated
Show resolved
Hide resolved
} | ||
|
||
|
||
return SpecializedCollections.EmptyEnumerable<TextSpan>(); |
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.
should be impossible to hit this. we should throw here.
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.
can use a switch expression here as well.
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.
Since this is argument syntax (as opposed to parameter syntax), wouldn't this be hit for most arguments?
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.
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.
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'm not seeing how this woudl be subtantively different. Both cases need to check all nodes to see if they should run against them.
src/EditorFeatures/CSharpTest/KeywordHighlighting/DiscardParameterHighlighterTests.cs
Outdated
Show resolved
Hide resolved
namespace Microsoft.CodeAnalysis.Editor.CSharp.Highlighting.KeywordHighlighters | ||
{ | ||
[ExportHighlighter(LanguageNames.CSharp)] | ||
internal class DiscardParameterHighlighter : AbstractKeywordHighlighter |
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.
ther's a different baseclass you can subclass right? where you can specify through a type argument that you are constrained to ArgumentSyntax right?
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.
yes, it's AbstractKeywordHighlighter<ArgumentSyntax>
.
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.
Can be simplified
{ | ||
var syntax = (IdentifierNameSyntax)node.Expression; | ||
|
||
if (syntax.Identifier.Text == "_") |
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.
Switch to typechecks
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.
done
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
@jinujoseph can we get a buddy for this community pr? Thanks! |
if (node.Expression is IdentifierNameSyntax nameSyntax | ||
&& nameSyntax.Identifier.Text == "_") |
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.
💡 Can use pattern matching here:
if (node.Expression is IdentifierNameSyntax { Identifier: { Text: "_" } } nameSyntax)
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 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?
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.
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>(); |
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.
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:[|_|]|}); |
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.
💡 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.
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.
Added tests covering that case.
Added a test to ensure @_ is not highlighted incorrectly.
…d of DiscardParameterHighlighter
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.
c
src/Workspaces/CSharp/Portable/Classification/SyntaxClassification/DiscardSyntaxClassifier.cs
Show resolved
Hide resolved
src/Workspaces/CSharp/Portable/Classification/SyntaxClassification/DiscardSyntaxClassifier.cs
Outdated
Show resolved
Hide resolved
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
Can you add a test for _ = int.Parse("");
?
@JoeRobich Sure thing. This uncovered the missing case for Another thing i was kind of suprised about was the test for _ = int.Parse(""); In this case we get the following symbol classifications:
This is in contrast to all other tests where the order of the |
@lameox Leave it as is. We are likely to make some changes with regards to the |
src/Workspaces/CSharp/Portable/Classification/SyntaxClassification/DiscardSyntaxClassifier.cs
Outdated
Show resolved
Hide resolved
…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.
Close and reopen to trigger retest |
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.
Thanks @lameox!
…rTests.cs Co-Authored-By: Joey Robichaud <joseph.robichaud@microsoft.com>
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.