Usage of SyntaxAnnotations
-
2012年1月28日 11:47
Hi,
Let me explain my problem with an hypothetical example:
An application should annotate all IdentifierTokens that contain the letter X.
To achieve this I'd subclass SyntaxAnnotation with IdentifierContainsXAnnotation and add a
property XPosition that indicates the position of the first character X inside the identifier name.
I'd go through all IdentifierTokens of a syntaxTree and add IdentifierContainsXAnnotations accordingly.
Later, I want to get the all tokens with an IdentifierContainsXAnnotation from a SyntaxTree.
How do I do that?
All annotation-related search methods I can find take an *instance* of an annotation,
meaning that I have to know exactly, what annotation instance I am looking for.
To do this, I'd have to save all assigned annotations somewhere, defying the purpose of
putting them into the tree in the first place.
Is there a way to get all annotations from a Token/Node
or at least all annotations of a given type?
全部回复
-
2012年1月30日 23:09所有者:
As far as I can tell, there is no public API to get all annotations / all annotations of a particular type from a Token / Trivia / Node. I am not sure, but I think the reason such an API doesn't exist at the moment is because trees and nodes end up being processed by multiple consumers (3rd party consumers, consumers in the IDE Services layer inside VS etc.) and it shouldn't be possible for one consumer to modify / remove annotations added by another (lest it breaks any assumptions that the latter has about them). However, I agree that having the abitlity to 'get all annotations of a particular type created by me' certainly seems useful.
Could you please log this as a suggestion through Connect along with a description of your scenario (as above)? Alternately, if you do not wish to track this issue, just let me know and I can log this internally.
Note: For the specific hypothetical example that you posted above, you don't need to use multiple annotation instances necessarily. You could use a single annotation instance to annotate 'all identifier tokens that have a character X'. Then you could get all identifier tokens in your tree that have this annotation [var identifierTokensWithX = root.GetAnnotatedNodesAndTokens(myAnnotationInstance)] and compute the 'position of the first character X in the identifier name' on the fly for each of these identifier tokens [var psoitionOfX = identifierTokenWithX.GetText().IndexOf('X')].
Thanks for the feedback!
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team- 已标记为答案 3dGrabber 2012年1月31日 9:28
-
2012年1月31日 9:26
Hi Shyam,
> it shouldn't be possible for one consumer to modify / remove annotations added by another ...
That's what I suspected.
For some reason I m unable to submit anything on Connect/Uservoice.
If you are able to forward the issue, please go ahead:Problem: A consumer of the Syntax API cannot retrieve SyntaxAnnotations from a SyntaxTree without knowing all the SyntaxAnnotation *instances* it is looking for. In most situations, this defies the purpose of putting annotations in the tree in the first place. See an example of the problem here: http://social.msdn.microsoft.com/Forums/en-US/roslyn/thread/fbe48b62-d738-4a7b-a04f-41b7926c46b9 Suggested Solutions: * Let the consumer enumerate ALL SyntaxAnnotations of a tree/node. * Let the consumer enumerate all SyntaxAnnotations of a given (sub)TYPE of a tree/node. * Have the API store the annotations as Key/Value Pairs - a consumer can ask the API for a new key, the API makes sure there are no key collisions - a consumer can store annotations with a certain key - a consumer retrieve annotations stored with a certain key
IMHO, from a consumer perspective - the way it is now - the API is broken.
Thank you for your effortsIvo
- 已编辑 3dGrabber 2012年1月31日 9:27 layout
-
2012年1月31日 21:00所有者:Thank you Ivo! I have logged this internally.
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team -
2012年2月4日 3:10所有者:I just wanted to let you know that this issue is now fixed in our internal builds :) In a future release you should be able to enumerate all SyntaxAnnotations of a given (sub)TYPE that may be present on a paricular node / token / trivia.
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team- 已编辑 Shyam NamboodiripadMicrosoft Employee, Owner 2012年2月4日 3:13
-
2012年2月5日 12:00
Great to hear! :)
Thank you

