Access to the project system using the Visual Studio MEF editor extension

I am writing a Visual Studio edition extension using the VS 2010 SDK RC. I would like to know what links to the current project are. How to access the project corresponding to the current editor?

The documentation on the editor extenders does not seem to contain information on how to access unregistered portions of Visual Studio. I did some searches, and it looks like in VS2008 you could write add-ons that could access the project system, but I'm trying to get this functionality from the MEF editor extension.

+4
source share
1 answer

Danielle -

Getting from the project editor is a multi-step process. First you get the file name in the editor, and from there you can find the contained project.

Assuming you have an IWPFTextView, you can get the file name as follows:

public static string GetFilePath(Microsoft.VisualStudio.Text.Editor.IWpfTextView wpfTextView) { Microsoft.VisualStudio.Text.ITextDocument document; if ((wpfTextView == null) || (!wpfTextView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(typeof(Microsoft.VisualStudio.Text.ITextDocument), out document))) return String.Empty; // If we have no document, just ignore it. if ((document == null) || (document.TextBuffer == null)) return String.Empty; return document.FilePath; } 

Once you have the file name, you can get its parent project as follows:

 using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Interop; public static Project GetContainingProject(string fileName) { if (!String.IsNullOrEmpty(fileName)) { var dte2 = (DTE2)Package.GetGlobalService(typeof(SDTE)); if (dte2 != null) { var prjItem = dte2.Solution.FindProjectItem(fileName); if (prjItem != null) return prjItem.ContainingProject; } } return null; } 

From the project, you can go to codemodel, and I assume the links, but I don't need to do this yet.

Hope this helps ...

~ Cameron

+10
source

Source: https://habr.com/ru/post/1304720/


All Articles