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
请先登录再写评论。
You can use the <editorActionHandler> extension point for this:
<editorActionHandler action="EditorTab" implementationClass="FQN of your implementation class"/>
Your implementation class should extend EditorWriteActionHandler.
Thank you so much for the answer, I got it to work! For reference, my handler looks like this:
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
Well, I've just realized that I haven't extended
yet, so I assume this would take care of that.Lars
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?!
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.
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)?
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.
Got it working, thanks!
Lars
Dmitry, can you provide a sample code for original implementation and customised implementation based on context appropriate?