Reputation: 34238
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);
Upvotes: 14
Views: 5515
Reputation: 1115
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.
Upvotes: 12