Keymap bindings that work only for my custom language

Good day :)

I am implementing a custom programming language plugin; the language uses unicode characters. I have successfully registered some "EditorAction"s in order to provide an autocomplete function for inserting unicode characters (so, for instance, this action is bound to the '>' key, and -> gets converted into →, => to ⇒, and so on).

However, I would like the actions to be called only while editing a file in my language. As it stands, the replacements will happen even when I am editing java files, say.

Is it possible to enforce this?

Thank you!

Comment actions Permalink

The correct way to implement an action which is triggered by typing a character in the editor is the TypedHandlerDelegate class and the typedHandler extension point. In your implementation, you can check the file type and return Result.STOP if you've made the replacement and Result.CONTINUE if the file is not of the type that you want to handle.

Comment actions Permalink

Thank you Dmitry,

I would also like to have certain key-combinations introduce special characters (for instance, ctrl-'>' could insert '⇒', and so on). Unfortunately the TypedHandlerDelegate receives no information about modifier keys.

I also speculate that in the future I might have certain actions execute on certain keystrokes while editing a file in my language, but not while editing a file in another language. Is that possible to do?

Comment actions Permalink

Ah, I have found the answer:

How to find the Language of the Document of a given Editor:

Document document = editor.getDocument();
Project project = editor.getProject();
VirtualFile vfile = FileDocumentManager.getInstance().getFile(document);
PsiFile psiFile = PsiManager.getInstance(project).findFile(vfile);

Language documentLanguage = psiFile.getLanguage();

if (language != MyLanguage.INSTANCE) doNothing();

else doSomething();


Please sign in to leave a comment.