Resharper聲稱吃它自己的dogfood,具體地說,他們聲稱Resharper的許多功能都寫在R#(OpenAPI)的頂端。我正在編寫一個簡單的插件來修復當前選定文檔的註釋。當運行這個插件,它如下拋出一個異常:編寫一個修改TextControl的resharper插件?
文檔可以在命令範圍僅
我研究的錯誤,無法找到任何東西,以幫助這裏面進行修改,所以我希望可能,你已經寫了一個插件來實現這一點。如果沒有,我希望這段代碼足以幫助其他人獲得他們自己的插件。
using System;
using System.IO;
using System.Windows.Forms;
using JetBrains.ActionManagement;
using JetBrains.DocumentModel;
using JetBrains.IDE;
using JetBrains.TextControl;
using JetBrains.Util;
namespace TinkerToys.Actions
{
[ActionHandler("TinkerToys.RewriteComment")]
public class RewriteCommentAction : IActionHandler
{
#region Implementation of IActionHandler
/// <summary>
/// Updates action visual presentation. If presentation.Enabled is set to false, Execute
/// will not be called.
/// </summary>
/// <param name="context">DataContext</param>
/// <param name="presentation">presentation to update</param>
/// <param name="nextUpdate">delegate to call</param>
public bool Update(IDataContext context, ActionPresentation presentation, DelegateUpdate nextUpdate)
{
ITextControl textControl = context.GetData(DataConstants.TEXT_CONTROL);
return textControl != null;
}
/// <summary>
/// Executes action. Called after Update, that set ActionPresentation.Enabled to true.
/// </summary>
/// <param name="context">DataContext</param>
/// <param name="nextExecute">delegate to call</param>
public void Execute(IDataContext context, DelegateExecute nextExecute)
{
ITextControl textControl = context.GetData(DataConstants.TEXT_CONTROL);
if (textControl != null) {
TextRange textSelectRange;
ISelectionModel textSelectionModel = textControl.SelectionModel;
if ((textSelectionModel != null) && textSelectionModel.HasSelection()) {
textSelectRange = textSelectionModel.Range;
} else {
textSelectRange = new TextRange(0, textControl.Document.GetTextLength());
}
IDocument textDocument = textControl.Document;
String textSelection = textDocument.GetText(textSelectRange);
if (textSelection != null) {
StringReader sReader = new StringReader(textSelection);
StringWriter sWriter = new StringWriter();
Converter.Convert(sReader, sWriter);
textSelection = sWriter.ToString();
textDocument.ReplaceText(textSelectRange, textSelection);
}
}
}
#endregion
}
}
那麼這個命令的作用範圍是什麼?在發佈之前,我有一些額外的日誌記錄,所以我絕對確定範圍和文本都是有效的。另外,這個錯誤似乎表明我錯過了一些我一直未能找到的範圍。
是的,我想我可以使用宏來完成相同的任務。回想起來,我寫了一個簡單的vs插件來做同樣的事情。我之所以在看R#是因爲它除了原始文本之外還提供了語言特定的元素解析。但對於這個問題,我認爲宏或標準加載項也可以正常工作。