You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've implemented my first Roslyn code fixer and everything seems to be working reliably, except for one case that has been frustrating me. I'm hoping someone can point out a better way of doing things to avoid the problem.
My fixer does 2 core things:
Create a new static method in a well-known class. If the class already exists, the method is added to that class. If the class doesn't exist, then the fixer adds a new source file, creates the class in the source file, and then adds the method to that class.
Rewrire the user's source code from calling a legacy method to calling my generated method instead.
This generally works great. But things fall apart if the well-known class is located in the same source file as the user's code which I need to rewrite. What happens is that I add the method to the class, which works fine. But then when I try to edit the user's code, the TextSpan I had pointing to where I should perform the edit is no longer valid. That makes sense since I just inserted some new code and changed the underlying document.
I thought about changing the order I do things, and rewrite the user's code first and THEN generate the new method. But that doesn't work since rewriting the user's code requires that the target method be present in order to get its symbol info.
So how can I readily recover a valid TextSpan that was created on an old document and make it point to the same place on a new document?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi there,
I've implemented my first Roslyn code fixer and everything seems to be working reliably, except for one case that has been frustrating me. I'm hoping someone can point out a better way of doing things to avoid the problem.
My fixer does 2 core things:
Create a new static method in a well-known class. If the class already exists, the method is added to that class. If the class doesn't exist, then the fixer adds a new source file, creates the class in the source file, and then adds the method to that class.
Rewrire the user's source code from calling a legacy method to calling my generated method instead.
This generally works great. But things fall apart if the well-known class is located in the same source file as the user's code which I need to rewrite. What happens is that I add the method to the class, which works fine. But then when I try to edit the user's code, the TextSpan I had pointing to where I should perform the edit is no longer valid. That makes sense since I just inserted some new code and changed the underlying document.
I thought about changing the order I do things, and rewrite the user's code first and THEN generate the new method. But that doesn't work since rewriting the user's code requires that the target method be present in order to get its symbol info.
So how can I readily recover a valid TextSpan that was created on an old document and make it point to the same place on a new document?
My existing code can be found here: https://github.com/geeknoid/LoggingGenerator/tree/master/Microsoft.Extensions.Logging.Analyzers
Thanks for any help.
Beta Was this translation helpful? Give feedback.
All reactions