问题
I am trying to build a Margin (through MEF) as a VS addin. I need to get the lines that contain the definition of a method. I have found people doing such things but it seems most are parsing test. This seems like a really bad idea as it would obviously only work with languages you knew how to parse.
In an addin I can access the document in a programmatic fashion using a higher level model:
var fcm = _application.ActiveDocument.ProjectItem.FileCodeModel;
var elem = fcm.CodeElementFromPoint(point, vsCMElement.vsCMElementFunction);
As an example.
Is there anyway to do this from within the MEF stuff when trying to draw the margin?
回答1:
OK so it can be done.
The general process for this is: 1) ITextBuffer -> ITextDocument (which has a file name) 2) Use the file name find the project item (eg: recurse project items) 3) Get FCM from the project item and use it
回答2:
I don't believe there's any way to do this now without parsing it yourself.
I think that the compiler as a service stuff that they're working on for a future version of VS will help with this.
回答3:
They've added some CodeDOM stuff that could help. You'd be able to navigate the tree of code and search for the text. Here's a very basic introduction: http://msdn.microsoft.com/en-us/library/y2k85ax6%28v=vs.110%29.aspx
来源:https://stackoverflow.com/questions/4985697/visual-studio-extensibility-margins