Silly question from an add-in newbie

I want to write a Resharper plugin that gets the selected text, processes
it, and then replaces it. How do I get and set the selected text?

Thanks.


1 comment
Comment actions Permalink

Hello flipdoubt,

most likely you have to write an action which may look as follows:

[ActionHandler("ProcessText")] // your action id
class ProcessTextAction : IActionHandler
{
public bool Update(IDataContext context, ActionPresentation presentation, DelegateUpdate nextUpdate)
{
return context.GetData(DataConstants.TEXT_CONTROL) != null;
}

public void Execute(IDataContext context, DelegateExecute nextExecute)
{
ITextControl editor = context.GetData(DataConstants.TEXT_CONTROL);

ModificationCookie cookie = editor.Document.EnsureWritable();
using (cookie)
{
if (cookie.EnsureWritableResult != EnsureWritableResult.SUCCESS)
return;

CommandProcessor.Instance.BeginCommand("Process Text"); // the text appearing in VS undo history

try
{
TextRange selectedRange = editor.SelectionMode.Range;
string selectedText = editor.Document.GetText (selectedRange);
// analyze the 'selectedText'
editor.Document.ReplaceText (selectedRange, "NewText"); // replace the selected text
}
finally
{
CommandProcessor.Instance.EndCommand();
}
}
}
}



Dmitry Shaporenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


I want to write a Resharper plugin that gets the selected text,
processes it, and then replaces it. How do I get and set the selected
text?

>

Thanks.

>

0

Please sign in to leave a comment.