[英]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编辑器扩展中,我需要:
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上获取源代码 。
Assuming you start with an ITextBuffer
, you can get to the DTE Project
by doing: 假设您从
ITextBuffer
开始,则可以执行以下操作进入DTE Project
:
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
的引用添加到项目中,则将该引用添加到项目中。
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
,或者修改服务以返回特定于缓冲区/项目的拼写字典实例(然后这些方法不需要将其显式传递给每个调用)。 AdaptersFactory.GetBufferAdapter
to get the VS buffer adapter ( IVsTextBuffer
). AdaptersFactory.GetBufferAdapter
获取VS缓冲区适配器( IVsTextBuffer
)。 IExtensibleObject
, and call IExtensibleObject.GetAutomationObject("Document")
to get the DTE Document
for the buffer. IExtensibleObject
,然后调用IExtensibleObject.GetAutomationObject("Document")
以获取缓冲区的DTE Document
。 ProjectItem
property, then ContainingProject
ProjectItem
属性,然后使用ContainingProject
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
方法将其保存到磁盘。
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. 抱歉,我不确定以编程方式执行此操作的最佳方法。
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.