简体   繁体   English

Visual Studio 2010:如何通过编辑器扩展在项目中创建和/或更新特定文件?

[英]Visual Studio 2010: How do I create and/or update a specific file within a Project from an Editor Extension?

I am working on extending the spell checker that has recently been updated on the code gallery: http://code.msdn.microsoft.com/spellCheckerSample 我正在扩展最近在代码库中更新的拼写检查器: http : //code.msdn.microsoft.com/spellCheckerSample

I would like to update it so that the "ignore" list of words comes from CustomDictionary.xml , the same file that the static code analysis tools use. 我想对其进行更新,以使单词“忽略”列表来自CustomDictionary.xml ,这是静态代码分析工具使用的同一文件。

From within my VS 2010 editor extension, I need to: 在VS 2010编辑器扩展中,我需要:

  1. Find the project in which the current file resides. 查找当前文件所在的项目。
  2. Find the "CustomDictionary.XML" file, if it exists. 查找“ CustomDictionary.XML”文件(如果存在)。
  3. If it exists, I need to check it out from source control 如果存在,我需要从源代码管理中签出
  4. If it doesn't exist, I need to add it to the current project (and to the source control) 如果不存在,则需要将其添加到当前项目(以及源代码管理)中
  5. Add text to the CustomDictionary.XML file (this is the easy part). 将文本添加到CustomDictionary.XML文件(这是简单的部分)。

Is anyone aware of any examples that would help me learn how to do this? 有人知道有什么例子可以帮助我学习如何做吗? I am using the VS2010 Release Candidate. 我正在使用VS2010 Release Candidate。

Thanks. 谢谢。

~ Cameron 〜卡梅伦

Here's what I have so far: 这是我到目前为止的内容:

public ISpellingDictionaryService GetSpellingDictionaryService(ITextBuffer textBuffer)
{
    var bufferAdapter = AdaptersFactory.GetBufferAdapter(textBuffer);
    if (bufferAdapter != null)
    {
        var extensibleObject = bufferAdapter as IExtensibleObject;
        if (extensibleObject != null)
        {
            object document;
            extensibleObject.GetAutomationObject("Document", null, out document);
            // Control never gets here... GetAutomationObject appears to recurse.
        }
    }
}

First, it would probably be best to base your extension of the updated version of the spell checker sample that I released; 首先,最好将我发布的拼写检查器示例的更新版本作为扩展的基础。 you can grab the source on github . 您可以在github上获取源代码

Find the project of a given buffer 查找给定缓冲区的项目

Assuming you start with an ITextBuffer , you can get to the DTE Project by doing: 假设您从ITextBuffer开始,则可以执行以下操作进入DTE Project

  1. In whatever MEF component you have (for the spell checker, the SpellingDictionaryService is the best place), add the following MEF import: 在您拥有的任何MEF组件中(对于拼写检查器而言, SpellingDictionaryService都是最佳选择),添加以下MEF导入:

    [Import] IVsEditorAdaptersFactoryService AdaptersFactory = null; [导入] IVsEditorAdaptersFactoryService AdaptersFactory = null;

    Also, add a reference to Microsoft.VisualStudio.Editor.dll to the project, if it isn't there already. 另外,如果尚未将对Microsoft.VisualStudio.Editor.dll的引用添加到项目中,则将该引用添加到项目中。

  2. Either modify the methods on the ISpellingDictionaryService to also take an ITextBuffer , or modify the service to return a spelling dictionary instance that is specific to a buffer/project (and then the methods don't need to have one passed explicitly to each call). 修改ISpellingDictionaryService上的方法以也接受ITextBuffer ,或者修改服务以返回特定于缓冲区/项目的拼写字典实例(然后这些方法不需要将其显式传递给每个调用)。
  3. Use AdaptersFactory.GetBufferAdapter to get the VS buffer adapter ( IVsTextBuffer ). 使用AdaptersFactory.GetBufferAdapter获取VS缓冲区适配器( IVsTextBuffer )。
  4. Cast this to IExtensibleObject , and call IExtensibleObject.GetAutomationObject("Document") to get the DTE Document for the buffer. 将其强制转换为IExtensibleObject ,然后调用IExtensibleObject.GetAutomationObject("Document")以获取缓冲区的DTE Document
  5. From here, you can use the ProjectItem property, then ContainingProject 在这里,您可以使用ProjectItem属性,然后使用ContainingProject

Find CustomDictionary.xml file or add it, if necessary 查找CustomDictionary.xml文件或根据需要添加它

Once you have the Project , you can use project.ProjectItems.Item("CustomDictionary.xml") to find it (though I think this may only work for the top-level directory), which throws an ArgumentException if it doesn't exist. 一旦有了Project ,就可以使用project.ProjectItems.Item("CustomDictionary.xml")来找到它(尽管我认为这可能仅适用于顶级目录),如果不存在它就会抛出ArgumentException If you need to add it, then, you can write the file to disk in the project location and use project.ProjectItems.AddFromFile(string filename) to add it. 如果需要添加,则可以将文件写入项目位置的磁盘,并使用project.ProjectItems.AddFromFile(string filename)进行添加。 You could also check for the existence of the project file by just looking for it on disk first (creating or just writing to it), and then calling AddFromFile at the end, either only calling it if you created the file or calling it anyways and handling the exception. 您还可以通过以下方式检查项目文件是否存在:首先在磁盘上查找(创建或写入),然后最后调用AddFromFile,或者仅在创建文件时调用它,或者无论如何都调用它,然后处理异常。 When you have added it and finished modifying it, you can get the ProjectItem for that file and use the ProjectItem.Save method to save it to disk. 添加并完成修改后,可以获取该文件的ProjectItem ,并使用ProjectItem.Save方法将其保存到磁盘。

Check out or add to source control 检出或添加到源代码管理

From the Project DTE object (or any DTE object, actually), you can use the SourceControl property, which will let you check if the item is under source control and check it out. 您可以从Project DTE对象(或实际上是任何DTE对象)使用SourceControl属性,该属性将使您检查项目是否在源代码管理下并检出。 However, I think this only has the ability to let you "check out" a file (I think this does a tf edit , if you are using TFS), and not the ability to add a file to source control. 但是,我认为这只能让您“签出”文件(如果您使用的是TFS,我认为可以进行tf edit ),而不能将文件添加到源代码管理中。 I'm not sure the best way to do this programmatically, sorry. 抱歉,我不确定以编程方式执行此操作的最佳方法。

Some references, from MSDN: 来自MSDN的一些参考资料:

Top-level DTE object members 顶级DTE对象成员
DTE.Project members DTE.Project成员
DTE.SourceControl members DTE.SourceControl成员

Got it working with a combination of Noah's help and something I found on the MSDN forums. 在Noah的帮助和我在MSDN论坛上找到的东西的帮助下,它可以正常工作。 I'm not sure it would be better to get the DTE object another way, but this is working as far as I can see: 我不确定以其他方式获取DTE对象会更好,但是据我所知,这是可行的:

var bufferAdapter = AdaptersFactory.GetBufferAdapter(textBuffer);
if (bufferAdapter != null)
{
    var persistFileFormat = bufferAdapter as IPersistFileFormat;
    if (persistFileFormat != null)
    {
        string ppzsFilename;
        uint iii;
        persistFileFormat.GetCurFile(out ppzsFilename, out iii);

        var dte2 = (DTE2) Shell.Package.GetGlobalService(typeof (SDTE));

        ProjectItem prjItem = dte2.Solution.FindProjectItem(ppzsFilename);

        var containingProject = prjItem.ContainingProject;

        // Now use the containingProject.Items to find my file, etc, etc
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2010 SDK打开项目特定的编辑器 - Visual Studio 2010 SDK open a project specific editor 如何使用Visual Studio 2010为C#Web服务项目创建.msi安装程序? - How do I create an .msi installer with Visual Studio 2010 for a C# webservices project? 如何将Microsoft Visual C ++ 2010 SP1与我的Visual Studio安装程序项目一起打包 - How do I package Microsoft Visual C++ 2010 SP1 with my Visual Studio installer project 更改Visual Studio 2015扩展中项目中文件ProjectItem的内容吗? - Change the contents of a file ProjectItem within a Project in a Visual Studio 2015 Extension? 如何创建Visual Studio ...文件扩展名shell? - How to create Visual Studio…file extension shells? 如何在 Visual Studio 2019 中创建与 Visual Studio 2015 兼容的扩展? - How do I create a Visual Studio 2015 compatible extension in Visual Studio 2019? 如何在 Visual Studio 2019 中创建 SwCsharpAddin 项目? - How do I create the SwCsharpAddin project in Visual Studio 2019? 如何在Visual Studio C中创建“项目概述”# - How do I create a “Project overview” in Visual Studio C# 如何在Visual Studio 2010最终建模项目中创建属性? - How can I create properties in Visual studio 2010 ultimate modeling project? 如何在Visual Studio 2010中将自己的扩展名添加到html / aspx代码编辑器的上下文菜单中? - How to add your own extension in Visual Studio 2010 to the context menu of the html/aspx code editor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM