Extract out the common portions of the Json and Regex parsing subsystems.#25541
Conversation
| } | ||
|
|
||
| public override bool Equals(object obj) | ||
| => obj is EmbeddedDiagnostic rd && Equals(rd); |
There was a problem hiding this comment.
Yeah, this used to be RegexDiagnostic before i made it EmbeddedDiagnostic. Will fix :)
|
|
||
| public bool Contains(VirtualChar virtualChar) | ||
| { | ||
| foreach (var child in this) |
There was a problem hiding this comment.
Nit: this is a bit odd. Why not simply do a for loop and forego the enumerator?
Is the enumerator used elsewhere?
There was a problem hiding this comment.
yeah. we use the enumerator later on when we develop features. Note: we tehcnically could forgo the enumerator and just do the for-loop. But this is similar to how we walk normal SyntaxNodes in the IDE layer and it felt cleaner so that i would not be exposing the internal details of child-counts and slot lookups and whatnot.
| Value = value; | ||
| } | ||
|
|
||
| public bool IsMissing => VirtualChars.IsEmpty; |
There was a problem hiding this comment.
Is it safe to call .IsEmpty on a default ImmutableArray? Same question for .Length below.
There was a problem hiding this comment.
In a SyntaxToken none of these will be empty. I will add checks for that in the constructor.
|
|
||
| public bool IsMissing => VirtualChars.IsEmpty; | ||
|
|
||
| public EmbeddedSyntaxToken<TSyntaxKind> AddDiagnosticIfNone(EmbeddedDiagnostic diagnostic) |
There was a problem hiding this comment.
It looks like this method is not used or tested. If this method is removed, then WithDiagnostics can probably be removed too and possibly With as well.
Update: Hum, I just realized the tests are not in this PR... Can the relevant tests be moved into this PR along with the code change?
There was a problem hiding this comment.
The tests are in another PR because they destroy github. You can see the tests for regex and json in these PRs:
I would prefer to keep them separate.
There was a problem hiding this comment.
Also note: this is hte common embedded languages subsystem part of the work. There are no explicit tests for this part as this is just an implementation detail of hte final regex parser/features.
| public readonly ImmutableArray<VirtualChar> VirtualChars; | ||
| public readonly ImmutableArray<EmbeddedSyntaxTrivia<TSyntaxKind>> TrailingTrivia; | ||
| internal readonly ImmutableArray<EmbeddedDiagnostic> Diagnostics; | ||
| public readonly object Value; |
There was a problem hiding this comment.
It's not obvious to me what the Value is for. Consider commenting.
| internal abstract class EmbeddedSyntaxTree<TSyntaxKind, TNode, TRoot> | ||
| where TSyntaxKind : struct | ||
| where TNode : EmbeddedSyntaxNode<TSyntaxKind, TNode> | ||
| where TRoot : TNode |
There was a problem hiding this comment.
Is there a scenario where TRoot is not literally TNode?
There was a problem hiding this comment.
Yes. TRoot is never literally TNode. For regex and json it will be RegexCompilationUnit and JsonCompilationUnit respectively.
There was a problem hiding this comment.
Renamed to be clearer. #resolved.
| internal readonly ImmutableArray<EmbeddedDiagnostic> Diagnostics; | ||
| public readonly object Value; | ||
|
|
||
| public EmbeddedSyntaxToken( |
There was a problem hiding this comment.
From usage in subsequent PR, it seems that you never put default arrays for those parameters (but rather use empty arrays). Perhaps assert that immutable arrays in various types are not default?
There was a problem hiding this comment.
correct. i will do that.
|
@jcouv Have addressed all feedback. |
|
Don't forget to retarget the PR to feature branch. |
|
Also, any plans to implement adapt the Roslyn C# parser on top of VirtualChar so we can properly highlight embedded C#? ;-) |
|
@jcouv Have rebased to the new feature branch. @dotnet/roslyn-ide could i get a quick pair of eyes on this small 300 line PR relating to the regex/json features? |
Yes! Definitely. I also want ot make it work for the special strings we use for C# in our tests :) Note: i don't think this will actually be difficult. I don't need to make the C# parser run on top fo virtual chars. I just need to feed it the .ValueText of the token. Then, any spans it passes back, i will map back myself using VirtualChar to the actual span. |
|
@jcouv If you feel pretty good aobut this one, we can merge in as well. |
|
I think PRs into feature branches follow the same process as other PRs, so they can be merged back into master without review of a massive PR. |
Makes sense to me. |
| return TextSpan.FromBounds(start, end); | ||
| } | ||
|
|
||
| private void GetSpan(ref int start, ref int end) |
There was a problem hiding this comment.
Seems like this could be a local function or this logic just get rolled up into GetSpan(). Is there a reason we want a separate private method here?
There was a problem hiding this comment.
tried it out. Didn't think it helped. Plus, either you have the locals conflicting with the params, or hte local function mutates outer state. I didn't like either :)
Followup to #24111 (Extract out the VirtualChar portion of the Regex and JSON editor work).
Precursor to #23984 (regex parser and corresponding tests) and #24110 (json parser and corresponding tests).
Overall map: #24111 (comment)
[jcouv edited]