Bite my bytes

What I learn by day I blog at night - A blog from Microsoft Consultant working from Ljubljana, Slovenia

  Home :: Contact :: Syndication  
  999 Posts :: 7691 Comments :: 235 Trackbacks

Search

Most popular posts
in last 90 days

Categories

My Projects

Archives

Stuff


Copyright © by David Vidmar
 
Contact me!
 
LinkedIn Profile
 
 
 

Let's say you are writing a Visual Studio add-in that does some stuff with selected text, for example string literals in the code.

It would be very user friendly if the user could just click inside that string literal and add-in would figure it out and select a complete string.

before & after

Well, that's just what I had in mind the other day, but I had a hard time finding a code that would do that. It's rare these days that you don't find a code snippet that can speed up your development.

I wrote it myself and this is what I did. I hope I save someone some time with it...

 

Document document = _applicationObject.ActiveDocument;
TextSelection selection = (TextSelection)document.Selection;
TextPoint cursorTextPoint = selection.ActivePoint;                   
 
EditPoint startPoint;
EditPoint endPoint;
 
if (selection.Text.Length > 0)
{
    // some text is selected
    startPoint = selection.TopPoint.CreateEditPoint();
    endPoint = selection.BottomPoint.CreateEditPoint();
    // lets move one char left so we can inspect last character
    endPoint.CharLeft(1);
}
else
{
    // nothing is selected
    startPoint = cursorTextPoint.CreateEditPoint();
    endPoint = cursorTextPoint.CreateEditPoint();
}
 
string stopChar = "\"";
// move left selection margin to stopChar or start of line
while (startPoint.GetText(1) != stopChar && !startPoint.AtStartOfLine)
{
    startPoint.CharLeft(1);                       
}
 
// move right selection margin to stopChar or end of line
while (endPoint.GetText(1) != stopChar && !endPoint.AtEndOfLine)
{
    endPoint.CharRight(1);
}
// compensate for last character
if (!endPoint.AtEndOfLine) endPoint.CharRight(1);
 
// do we have a valid selection?
if (!startPoint.AtStartOfLine && !endPoint.AtEndOfLine)
{
    string selectedText = startPoint.GetText(endPoint);
 
    // do stuff with selected text
    // you can replace selected text like this
    // startPoint.ReplaceText(endPoint, "fresh text");
    }
}
else
{
    MessageBox.Show("Invalid selection!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

Technorati tags: , , ,
Posted on Wednesday, March 05, 2008 12:03 AM | Filed under: Developement |

Feedback

# re: Extending Visual Studio - How to select complete string around the caret 4/3/2008 3:38 PM Igor
Excellent snippet. But ctrl+w still rocks.

# re: Extending Visual Studio - How to select complete string around the caret 8/6/2008 4:50 PM miro
Hi regarding your example, can you please also provide an example how you graphicaly select the selectedtext [defacto change the background of the text?]

thanks a lot
Miro


# re: Extending Visual Studio - How to select complete string around the caret 11/5/2010 4:23 PM Jordan
The examples you gave on writing Visual Studio add –in that works stuff with selected text was an eye opener because it was user friendlier especially if the user clicks inside the string literal. Even though it is hard to create but it is worth the time.

# re: Extending Visual Studio - How to select complete string around the caret 1/23/2011 7:49 AM ealshabaan
What is the name space of _applicatonObject in this line:
Document document = _applicationObject.ActiveDocument;

# re: Extending Visual Studio - How to select complete string around the caret 4/21/2011 11:24 PM led
I thought it would be possible re-use the functionality behind ctrl+w.. but this is just what I needed :D

# re: Extending Visual Studio - How to select complete string around the caret 4/22/2011 1:37 PM led
It is possible to reuse the functionality behind ctrl+w.
You can use
Document doc = _applicationObject.ActiveDocument;
_applicationObject.DTE2.ExecuteCommand("Edit.SelectCurrentWord");
var txtSelection = (TextSelection) doc.Selection;
var selectedText = txtSelection.Text;


Comments have been closed on this topic.