I use source intervals to detect which node is selected and than in the right-click menu user can see only options that are relevanto to selected node, as it is also made in SmartSuggestions.
I know that use case ;)
Ok, then this means you are regenerating the code (1) (or are you doing replace a node / insert new source at right place? (2))
Thinking a bit about it, I'd try reparse, get node from selection index, find equal old node in old (modified) ast, or replace old (modified) ast with new one.