Dynamic Live Template

Answered

I'm trying to create a "dynamic" Live Template and I'm beginning to think what I'm trying to accomplish may not be possible. I'm not tied to Groovy Script, but it looks to be the most promising alternative to what I'm trying to accomplish. Here's what I'm trying to accomplish:

  • Whatever is typed after hitting <kbd>TAB</kbd> and before hitting <kbd>Enter</kbd> is passed to the template, formatted and written back to the Editor. My goal is to have the resulting string be a specified length, but that's less important for the time being. It's really more about how to get keyboard input to the template and back out again.

For example (blk is the abbreviation):

"blk" <kbd>TAB</kbd> "Hello world" <kbd>Enter</kbd> becomes
# ==================== Hello world ====================

with the resulting string always being 80 characters long. In other words, reformat the typed input *before* writing it out to the editor.

Thanks in advance.

EDIT: This question is specific to PyCharm and Python.

[Cross posted per suggestion in other thread.]

6 comments
Comment actions Permalink

Try using com.intellij.codeInsight.template.postfix.templates.PostfixTemplate that allows programmatic generation of content depending on current context.

0
Comment actions Permalink

Thank you for taking the time to leave a potential solution to my issue. After researching PostfixTemplates more thoroughly, it seems the solution to this will require a custom plugin. I didn't mention Python or PyCharm in my question (I should have) but according to the documentation,

You can edit the predefined postfix templates, for example, to replace a long key with a shorter one, or to expand the list of applicable expression types. Due to PyCharm limitations, you cannot create any custom postfix templates for Python.

This is confirmed in `PyCharm 2020.3`.  Hopefully, this situation will change in the future.

0
Comment actions Permalink

This applies to provided postfix templates that user should be able to edit directly in settings. Your implementation maybe doesn't necessarily to be editable via default settings UI.

0
Comment actions Permalink

Unfortunately, when I select Postfix Completion from Preferences, there is no ability to create custom Python templates (the option does exist for SQL, TypeScript and JavaScript, however).

 

0
Comment actions Permalink

You must provide a plugin with custom implementation of your Live Template behavior. What you asked for is not available by any existing means in the IDE.

1
Comment actions Permalink

Thank you. That is what I suspected, but am grateful to have it confirmed.

Cheers.

0

Please sign in to leave a comment.