Resharper API,用于选择文本和远程代码生成 [英] Resharper API for selected text and remote code generation

查看:121
本文介绍了Resharper API,用于选择文本和远程代码生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个Resharper插件,该插件可让我根据突出显示的文本在另一个类中生成代码,而API并不是最透明的.我应该看什么方法才能访问选定的文本以及在当前类之外进行代码生成?

I'd like to write a Resharper plugin that lets me generate code in another class based on highlighted text, and the API isn't the most transparent. What methods should I look at to get access to selected text, and to code generation outside of the current class?

推荐答案

在编写上下文动作或类似内容时(请在此处阅读

When you are writing context action or something like this (read here http://confluence.jetbrains.net/display/ReSharper/ReSharper+7+Plugin+Development about actions and context actions), you receive an instance of IDataContext. Take a text control, document and selection from it in this way:

var textControl = context.GetData(TextControl.DataContext.DataConstants.TEXT_CONTROL);
var document = textControl.Document;
var solution = projectFile.GetSolution();
TextRange selection = textControl.Selection.OneDocRangeWithCaret();

使用document.GetText获取选择范围内的文本.

Use document.GetText to get text for selection range.

为了在当前类之外生成代码,您需要找到其他已声明类的元素.为此,您需要使用IDeclarationsCache,请参阅 http://confluence.jetbrains. net/display/ReSharper/4.01 + Caches +%28R7%29 .当您获得声明的元素时,请使用GetDeclarations()方法来获取对所有类声明(由于部分类而可能有多个声明)的访问,并将其转换为IClassLikeDeclaration.使用AddClassMemberDeclaration方法添加成员,并使用RemoveClassMemberDeclaration删除.添加类成员时,请使用元素工厂创建添加的元素(请参见 http://confluence.jetbrains.net/display/ReSharper/3.2+Creating+Code+Elements+%28R7%29 ).

In order to generate code outside of your current class, you need to find your other class declared element. For this you need to use IDeclarationsCache, see http://confluence.jetbrains.net/display/ReSharper/4.01+Caches+%28R7%29 about it. When you'd get declared element, use GetDeclarations() method to receive access to all of your class declarations (there can be several declarations because of partial classes) and cast it to IClassLikeDeclaration. Use AddClassMemberDeclaration method to add members and RemoveClassMemberDeclaration to remove. When adding class member, use element factory to create added element (see http://confluence.jetbrains.net/display/ReSharper/3.2+Creating+Code+Elements+%28R7%29).

如果您有其他问题,请随时与我联系.

Don't hesitate to contact me if you have further questions.

这篇关于Resharper API,用于选择文本和远程代码生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆