Watching a token for renaming

SyntaxEditor for WPF Forum

Posted 4 years ago by Matt Whitfield
Version: 16.1.0630
Avatar

In visual studio, if you start typing over the name of a symbol where it is defined, then you are offered the opportunity with a smart tag to press Ctrl+. and have options appear to perform refactoring.

I can't see any built in services that provide this sort of tracking behavior - so I thought I would ask just in case I missed it. Otherwise my plan would be to subscribe to selection change events, and track the token ID and start offset - offering the ability to perform renaming if there is a match. Does that seem like a valid approach?

The one thing that I am not sure how to approach is the best way to determine the difference between the creation of that token in the first place and editing it later. Any advice would be appreciated...

Comments (3)

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Matt,

No we don't have anything that tracks that.  I would probably suggest you watch the document text changed event to see if typing occurred, and see if you are on an identifier.  If so, you could possibly look at the undo history.  You should have full access to the text change operations in each text change in the undo history.  So you could theoretically walk those, combined with snapshot examination, and determine if typing was in an existing token or not.


Actipro Software Support

Posted 4 years ago by Matt Whitfield
Avatar

Ok, so the methodology I used was to combine the derivation of the type of selection change event from the other thread (http://www.actiprosoftware.com/community/thread/23346/determining-cause-of-ieditorviewselectionchan) and inspecting the token under the cursor if it wasn't a typing change, and storing the text range and token Id.

Then, if it was a typing change and a watched token existed, I brought the text range forward to the current snapshot with ExpandLastEdge, and checked:

1) whether the typing occurred within the text range of the watched token

2) whether the watched token represented the same type (i.e. I hadn't written keywords etc when changing an identifier)

If both of those conditions are true, I put up a smart tag session activated with Ctrl+Period to enable refactoring.

If it was a selection only change, then I simply checked whether the caret was still within the text range of the watch token's text range (again bought forward to the current snapshot with ExpandLastEdge). If it wasn't in that range, I cleared the watched token.

That works pretty well for me, so I thought I'd let you know in case that sort of functionality is something you'd consider worthwhile when implementing smart tags yourself...

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Thanks Matt, I'm glad it's working well!


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.