Customize Tab Insertion Behavior

Hello,

I'm developing a custom language plugin for editing tabular ASCII data. For this I would like the TAB key to advance to certain, preferrably configurable, tab stops, pretty much like Microsoft Word. Tab stops are spaced irregularly, e.g., at positions 1, 4, 10, 16.

What's the best approach for implementing this? Is there a direct hook for overriding the default behavior of the TAB key? Do I need to implement an Action for inserting a certain number of spaces depending on the cursor position and bind this Action to the TAB key? Or is there out-of-the-box support for tab stops that I just failed to notice?

I've checked the plugin documentation and the forums for information but couldn't find anything related.

Regards
Lars

8 comments
Comment actions Permalink

You can use the <editorActionHandler> extension point for this:

<editorActionHandler action="EditorTab" implementationClass="FQN of your implementation class"/>

Your implementation class should extend EditorWriteActionHandler.

0
Comment actions Permalink

Thank you so much for the answer, I got it to work! For reference, my handler looks like this:

@Override
public void doExecute(final Editor editor, Caret caret, DataContext dataContext) {
    final int column = editor.getCaretModel().getLogicalPosition().column;
    final String blanks = .......;
    
ApplicationManager.getApplication().runWriteAction(
        new Runnable() {
            public void run() {
                EditorModificationUtil.insertStringAtCaret(editor, blanks);
            
}
        }
    );
}


The only minor issue now is that IntelliJ complains about the file indent not being 4, caused by "Detect and use existing file indents for editing" in Settings/Code Style. Is there a way to disable or ignore that option just for my custom language? (Note that the my language is not showing up in Code Style, but only in Colors & Fonts.)

Lars

0
Comment actions Permalink

Well, I've just realized  that I haven't extended

CustomCodeStyleSettings
yet, so I assume this would  take care of that.

Lars
0
Comment actions Permalink

Just a follow-up question:

For IDEA 14, I'm overriding doExecute(), and this works fine.

In IDEA 13, though, doExecute() is final.  How can I extend EditorWriteActionHandler so that the code works in both IDEA 13 and IDEA14? Method execute() is not called in IDEA 14, and the two methods executeWriteAction() simply call each other?!

0
Comment actions Permalink

I'm not sure if it's possible to have a single class compatible with both IDEA 13 and IDEA 14 API. You may need to provide two versions of the class, and two versions of your plugin, if you need to maintain compatibility with IDEA 13.

0
Comment actions Permalink

This is slightly more complex than I thought.

By registering an editor action handler for "EditorTab" this traps all Tab key presses for all files. So in my handler implementation I check the current file to see if it matches the file type of my plugin.

But what should I do when they don't match, e.g. I'm editing a text file instead of my custom language? I would like to call the default EditorTab handler instead, but how do I get a reference to this handler? Or can I limit the scope of the handler in my plugin.xml somehow (like language="..." for the lang properties)?

0
Comment actions Permalink

In your EditorActionHandler implementation, you can declare a constructor parameter of type EditorActionHandler, and the original implementation will be injected there. Then you can delegate to that implementation if the context is not appropriate for your implementation.

0
Comment actions Permalink

Got it working, thanks!

Lars

0

Please sign in to leave a comment.