Im writing a visual studio (2010) extension with a right click menu whilst in a code view. I want to be able to examine the current code from my menu item event handler but havent been able to find somewhere in the object model to do this.
How do i access the code in the current window in a visual studio extension?
Heres the code i used to get the current document text
DTE dte = Package.GetGlobalService(typeof(DTE)) as DTE ;
TextDocument activeDoc = dte.ActiveDocument.Object() as TextDocument;
var text = activeDoc.CreateEditPoint(activeDoc.StartPoint).GetText(activeDoc.EndPoint);
You can open code into Visual Studio in the following ways: On the Visual Studio menu bar, choose File > Open > Folder, and then browse to the code location. On the context (right-click) menu of a folder containing code, choose the Open in Visual Studio command.
Bring up the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of VS Code or the View: Extensions command (Ctrl+Shift+X). This will show you a list of the most popular VS Code extensions on the VS Code Marketplace.
To open the Manage Extensions dialog, choose Extensions > Manage Extensions. Or, type Extensions in the search box and choose Manage Extensions. The pane on the left categorizes extensions by those that are installed, those available on Visual Studio Marketplace (Online), and those that have updates available.
The Status Bar sits at the bottom of the VS Code workbench and displays information and actions that relate to your workspace. Items are placed into two groups: Primary (left) and Secondary (right).
You may be looking for
Document doc = DTE.ActiveDocument;
TextDocument txt = doc.Object() as TextDocument;
You should then be able to edit work with the TextDocument as needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With