0

In IntelliJ IDEA (for Java code at least), it's poissble to instruct the code-formatter to ignore lines with these (see https://stackoverflow.com/a/19492318/117750):

// @formatter:off
...
// @formatter:on

What I'd like to do is automate adding these around a code block. Workflow I want:

  1. Select a block of code.
  2. Invoke an action (with a shortcut or a menu item etc. or with the Cmd-Shit-A).

This command needs to

  • add // @formatter:off on a new line before the first selected line, at the correct indentation.
  • add // @formatter:on on a new line after the last selected line, at the correct indentation.

From what I am reading, it is not possible to do with a Macro. Is it?

If not, do I need to write a plugin to do this? I am happy to write one, can someone give me brief high-level get-started steps on:

  • how to approach this action in a plugin
  • and pointers to get-started with plugins
or9ob
  • 2,313
  • 4
  • 25
  • 45

1 Answers1

3

You can get (close to) what you want using a Live Template. Go to Editor | Live Templates in the settings and add a new template (e.g. under the surround group) with the following text:

// @formatter:off
$SELECTION$
// @formatter:on

live template settings example

You can use the defined live template by selecting some text in the editor and invoking Code | Surround with Live Template... (Ctrl/Cmd+Alt+J) and selecting the live template you have created.

Det
  • 3,640
  • 5
  • 20
  • 27
Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68