【问题标题】:XML Eclipse Plugin. How to use the 'tab' key as a shortcutXML Eclipse 插件。如何使用“tab”键作为快捷方式
【发布时间】:2015-09-24 10:51:57
【问题描述】:

我目前正在开发一个 Eclipse XML 编辑器插件。我目前正在努力实现快捷功能。我希望能够使用 tab 键在下面的代码 sn-p 中的引号之间跳转。我的意思是,输入查询名称,按“制表符”,然后跳过类型引号。

<query name="" type="" />

我很困惑我应该使用 plugin.xml 中的哪个扩展,以及如何通常实现它。提前谢谢了。

【问题讨论】:

    标签: xml eclipse plugins


    【解决方案1】:

    假设您的编辑器是从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)。

    【讨论】:

    • 感谢您的回复。我正在使用文本编辑器。我将如何专门创建一个在引号之间跳转的动作?
    • 非常感谢格雷格
    猜你喜欢
    • 2011-10-02
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    • 2016-12-03
    相关资源
    最近更新 更多