我目前正在开发一个Eclipse XML编辑器插件。我目前正在努力实现一个快捷功能。我希望能够使用制表键,以便在下面的代码片段中的引号之间跳转。我的意思是,输入查询名称,按"tab",然后跳转类型引号。
<query name="" type="" />
我很困惑在plugin.xml中我应该使用哪个扩展,以及如何一般实现这一点。提前感谢。
假设您的编辑器派生自TextEditor
,那么已经有一个具有定义动作的Tab处理程序。您应该能够通过重写createActions
方法来重写它:
protected void createActions()
{
super.createActions();
IAction action = ..... your IAction to do the tabbing
// Replace tab handler
setAction(ITextEditorActionConstants.SHIFT_RIGHT_TAB, action);
}
您的IAction
应该扩展TextEditorAction
。这样就可以访问编辑器。run
方法可能是:
public void run()
{
ITextEditor ed = getTextEditor();
if (!(ed instanceof AbstractTextEditor))
return;
if (!validateEditorInputState())
return;
AbstractTextEditor editor = (AbstractTextEditor)ed;
ISourceViewer sv = editor.getSourceViewer();
if (sv == null)
return;
IDocument document = sv.getDocument();
if (document == null)
return;
StyledText st = sv.getTextWidget();
if (st == null || st.isDisposed())
return;
int caret = st.getCaretOffset();
// Offset in document of the caret
int offset = AbstractTextEditor.widgetOffset2ModelOffset(sv, caret);
int newOffset = ... your code to change the position
// Set caret from new document offset
int widgetCaret = AbstractTextEditor.modelOffset2WidgetOffset(sv, newOffset);
st.setSelectionRange(widgetCaret, 0);
(部分改编自InsertLineAction
)