Вопрос

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();

Use document.GetText to get text for selection range.

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top