I'm having difficulty getting my VS2012 package to reliably replace text in a box selection using EditPoint.Replace, when the box selection is 0 chars wide (i.e., I've extended the cursor up or down by pressing Shift+Alt+Up/Down) and the box selection covers automatically-inserted indentation.
My code runs through each TextRange in the selection's TextRanges, and replaces the text between that range's StartPoint and EndPoint with the text I want. (Please ignore the issue of newlines in the replacement.) I'd expect this to do just what would happen
if you typed the text in by hand, i.e., insert the same text on all the lines at once, starting on each line from wherever the selection was.
It doesn't always do this, though!
For example, say I start out with this code, with \t representing indentations and | the cursor/selection (hopefully the ambiguity isn't too much of a problem):
{| \treturn 0; }
Then I press Return twice:
{ \t \t| \treturn 0; }
(I can press Up and Down to verify the indentation is there.) Then Shift+Alt+Up to make a 0x2 box selection, extending the cursor over the two indented lines:
{ \t| \t| \treturn 0; }
Then if I make my addin do its thing, I'd expect this:
{ \t<inserted text>| \t<inserted text>| \treturn 0; }
But what I actually get is this:
{<inserted text>\t|<inserted text>\t| \treturn 0; }
Then if I move the cursor, the stray indentation vanishes, leaving me with this:
{<inserted text><inserted text> \treturn 0; }
Not very useful!
On the other hand, if I insert some text, and cover it with a box selection, and get my package to replace it with some text, that works just as I'd expect.
As far as I can tell, after some fiddling around, the indentation doesn't actually exist until text has been inserted after it, and inserting text using a TextRange's EditPoints doesn't seem to do this. I suppose I need a VirtualPoint, but it's not clear
how to make one - all you have for the box selection is the TextRange, which just has EditPoints.
I've put an example project on github that helps demonstrate the problem I'm having, along with some repro steps along the lines of the above:https://github.com/tom-seddon/VS2012InsertTextInBoxSelection
(The only important bit I've changed from the default wizard-generated package is the handler for the command, which you can see at the bottom of https://github.com/tom-seddon/VS2012InsertTextInBoxSelection/blob/master/InsertTextPackage/InsertTextPackage/InsertTextPackagePackage.cs).
This has been driving me nuts. Have I run into some VS2012 limitation, or is there something up with my code?
Thanks,
--Tom