0

Could anyone point me in the right direction here? I am adding a custom toolbar and I want buttons for lots of different code snippets. We use so many that it is impracticable to remember all the different shortcut commands for each one, and selecting from a menu takes longer than just clicking a button.

It doesn't really even need to be specifically for a code snippet as defined by Visual studio, just simply pasting some text to the editor window, however I was hoping to be able to have some that would work as surround snippets, which the normal code snippets do.

2
  • So, your question is basically: How to insert text in the active Code-Window with a VS extension? Commented Jul 22, 2014 at 18:00
  • Yeah I suppose that would be a better wording. I am too much of a noob to even know the correct language sometimes :D Commented Jul 23, 2014 at 18:49

1 Answer 1

0

You should call this:

(DTE2)base.GetService(typeof(DTE))

in the Initialize() method of the Package and save the resulting dte-instance in a field. When your button is clicked, you can use ((TextDocument)_DTE.ActiveDocument).Selection.Text) to retrieve the currently selected text, modify it and write it back as new selected text. Maybe you will have to cast the return value of the Selection-Property to TextSelection.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh excellent I was wondering how I was going to implement the surround functionality for the code snippets. And to simply paste in some text stored maybe in some XML somewhere? It seems like I would be able to call part of the code snippet functionality from a button rather than the built in method. Then I could use the normal way of creating a snippet xml file for the snippets themselves.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.