IntelliJ Community . TabifierPlugin

 
TabifierPlugin 

HOME INDEX SEARCH CHANGES GO  

Tabifier

The tabifier plugin retabs Java code so that syntactic elements are aligned vertically. Extensive control over alignment is provided.

For example, I like to align variable declarations and assignment statements like this (see CodeAlignment):

                   int          v1;                  // loop variable
                   String       s2;                  // second string
    public  static PrintWriter  p;
    private static boolean      debugging    = true; // initialize
    private static OutputStream outputstream = null;

    outputstream = new FileOutputStream(f, true);
    p            = new DebuggingPrintWriter(outputstream, true);

IDEA likes to declare them with single spaces, like this:

    int v1; // loop variable
    String s2; // second string
    public static PrintWriter p;
    private static boolean debugging = true; // initialize
    private static OutputStream outputstream = null;

    outputstream = new FileOutputStream(f, true);
    p = new DebuggingPrintWriter(outputstream, true);

The tabifier plugin automatically reformats the text into the first version, or any variation of it you choose.

The newest version has many bug fixes and other improvements. It does a much better job at handling nested expressions and method calls. It also has several new features, such as the ability to right justify integer literals, e.g.

    public static final int ONE    =  1;
    public static final int TWO    =  2;
    public static final int TEN    = 10;
    public static final int ELEVEN = 11;
It can align method declaration and method call parameters, even with multiple parameters per line:
    public ExpressionListParser(TokenColumn       openParend,
                                ColumnChoice      params,
                                TokenColumn       closeParend,
                                CodeStyleSettings codeStyleSettings, TabifierSettings settings,
                                NestedParser      parser,            int              nestingLevel)

And its expression alignment is now recursive, e.g.

            if ((psiExpressionList.getExpressions().length               >  expressionListIndex &&
                 psiExpressionList.getExpressions()[expressionListIndex] == child                 ) ||
                (sawNoParameters                                         == false               &&
                 expressionListIndex                                     >                    0   )   )
It is capable of reformatting "if" statements as follows:
        if (param1 == p3 &&
            j      >  5     ) p3 = 7;
....
        if      (param1 == p3) p3     = 6;
        else if (param1 == 2 ) method1(param1, true, 3);
        else                   param1 = 7;
Method calls can be reformatted to align parentheses, commas, or parameters:
        align_params               = (ColumnSetting ) find(ALIGN_PARAMS              );
        align_semicolon            = (ColumnSetting ) find(ALIGN_SEMICOLON           );
        align_semicolon_with_comma = (BooleanSetting) find(ALIGN_SEMICOLON_WITH_COMMA);

Typecast parentheses and types as well as many other expression components can also be aligned.

Using the Tabifier plugin

The plugin operates on selected lines of text. Put the caret anywhere in the first line to be re-aligned and then select to anywhere in the last line to be re-aligned. If no selection is made, the plugin selects the entire current line (or, optionally, selects the entire file); otherwise the plugin extends the selection to the beginning of the first line and the end of the last line.

The plugin parses the entire Java file and reformats any construct it understands (within the selection). Therefore it is safe to select as much text as you want, even the entire file. The plugin operates much as the IDEA Code Layout tool does.

The plugin adds itself to the editor popup menu, and assigns itself a shortcut of Ctrl-Shift-Alt-T.

If the Reformat plugin is also installed, it will automatically invoke the tabifier after a code layout operation (invoked by Ctrl-Alt-L or Ctrl-Alt-R), thus making the process of code layout and reformatting seamless.

Controlling Alignment

Tabifier plugin settings are accessed via the Options...IDE Settings menu item. You may force alignment of any or all of the following syntactic items:

  • modifiers (only applies to field or variable declarations)
  • variable type (only applies to field or variable declarations)
  • variable name (or left-hand side of assignment statement)
  • operator (usually "=", but may be a multicharacter operator like "+=")
  • right-hand expression (the initialization expression for fields and variables)
  • comma or semicolon
  • trailing comments

Version 3.0/4.0 supplies many more alignment options. See the configuration panel, which now sports a handy Preview pane so you can see what the alignment will look like. (Thanks to MaximS? and the rest of the IntelliJ crew!)

For each of these, if alignment is to take place, you can specify how many spaces or tab characters are to be appended to the longest text to reach the next alignment column (tab stop). In the example above, all columns except comma/semicolon were aligned, and spacing was set for each to one space character.

If tab characters (instead of spaces) are specified, alignment takes place as if tab characters were appended to the longest previous column. The editor setting (IDE Settings...Code Style...Indents and Braces) for tab size is used.

Whether tabs or spaces are actually emitted during reformatting depends on the project settings for "Use Tabs" and "Smart Tabs." If "Use Tabs" is unchecked, all reformatting is done with spaces, even though the appearance is as if tab characters were used. If "Use Tabs" is checked, tabs are used wherever possible, unless "Smart Tabs" is also checked, in which case only the block indentation is achieved using tabs, and the remainder is done using spaces.

Modifier Reordering and Alignment

If modifier reordering and alignment is specified, modifiers will be reordered according to the Java spec recommendation (as implemented in the java.util.Modifier class) and aligned vertically. Mutually exclusive modifiers, such as public/private/protected, occupy the same column. For example,

static public final int CONSTANT = 0;
private static int nextID = 0;

would, with modifier reordering and alignment enabled, appear as:

public  static final int CONSTANT = 0;
private static       int nextID = 0;

If modifier reordering and alignment is disabled, modifiers remain intact. In this case, and with variable types aligned, the example is reformatted as:

static public final int CONSTANT = 0;
private static      int NextID = 0;

No space/tab specification is permitted for modifiers since modifiers begin immediately at column zero (relative to the indentation of the entire block, of course.)

Controlling Grouping

When multiple lines of text are selected, the tabifier plugin groups them automatically for formatting based on indentation level. For example,

    int i = 0;
    while (i < 10)
    {
        int j = i;
        ...
    }

the declarations of i and j are tabified independently.

Additional grouping rules are available. Presence of the following may be specified to force a new formatting group:

  • blank lines
  • non-blank lines other than declaration or assignment statements
  • a change from declaration to assignment statements or vice versa
  • Version 4.0 methods whose names differ from each other within the first characters

Options Not Affecting Appearance

  • Depending on the user's choice, if no selection has been made in the file, either the entire file or only the line containing the cursor will be tabified. Default is the latter.
  • Version 3.0/4.0 User can choose to apply Code Layout to the contents of the Preview pane before it is tabified, thereby seeing the effect of the Reformat plugin (which runs code layout, then itself, then tabifier). If statement then- and else- statements seem to be put on a new line regardless of settings at the moment, making the 'if' alignment options less interesting.
  • Version 3.0/4.0 Enable or disable debug output. In order for this to work, it appears you have to add the following to bin\log.xml:
<category name="com.wrq.tabifier">
   <priority value="INFO"/>
   <appender-ref ref="CONSOLE-DEBUG"/>
</category>

Requirements

This plugin requires IDEA build 957, but may work with build 922 or later; it has been tested with Aurora EAP build 957.

Other Information

The plugin source contains a JUnit test suite. It makes use of the new testing framework provided by IntelliJ.

Possible Enhancements

The following list is relative to version 4.0.

  • Alignment of javadoc comment parameter descriptions. For that matter, reflowing the javadoc text.

Please also notify me of any bugs or other suggestions for improvement. Thanks!

-- DaveKriewall - 15 Feb 2007

Change History

  • 15 Feb 2007 - Version 5.3
    • Fix intermittent spacing problem with "new" expressions, e.g.

   String[] args = new String[n];

would sometimes lose the space between "new" and "String" after being tabified.

    • Handle leading annotations in variable and method declarations, so that modifiers are handled properly, e.g.

    @CARD32 public final int target, internalFormat;
    @INT32 protected int x, y, width;
    @CARD32 public final int format, type;

can be tabified as follows:

    @CARD32 public    final int target, internalFormat;
    @INT32  protected       int x,      y,             width;
    @CARD32 public    final int format, type;

  • 23 Sep 2006 - Version 5.2
    • Fix parsing bug where "new int[20]" and "new String[20]" did not recognize "int" (a primitive type) as an alignable item. Result was that "String[20]" was aligned after "int[20]" instead of below "int[20]".
    • Fix parsing bug which caused annotations to be discarded.
    • Split up configuration panels so that the sample text was more tailored for individual sections and the effect of settings changes can be more easily seen.
  • 30 May 2005 - Version 5.1
    • Adjacent one-line method declarations are aligned as a group.
    • Primarily a re-release to support new versions of IDEA (4.5.4 and Irida EAP).
  • 20 Aug 2004 - Version 5.0
    • Fixed problem parsing "new" expressions (due to PSI tree change in IDEA 4.5) which caused misalignment of '=' assignment operators.
    • Plugin now uses code style settings for spaces within parentheses properly.
    • Compiled for IDEA 4.5. Fixed configuration panel which now properly shows preview text again.
  • 14 May 2004 - Version 4.8
    • Align single-line code blocks (e.g. one line method declarations).
    • Improve determination of trailing vs. non-trailing comments.
    • Fix "new thing[] {...}" array initializer parsing.
  • 13 Apr 2004 - Version 4.7
    • Re-enabled preview pane; works with IDEA production build 4.0.
    • Save and restore "Rearrange and align modifiers" setting properly.
    • Use new icon, courtesy of Alexey Efimov.
  • 08 Dec 2003 - Version 4.5
    • Fixes for submitted bugs involving continuation lines (e.g., method call parameters on several lines).
    • Provide separate controls for parameter alignment in method declarations and method calls.
    • Provide separate controls for first parameter of line vs. remainder of parameters.
    • Align right-justified literals with respect to themselves, not other tokens.
    • Added several "force space before/within {construct}" options:
      • Space within empty parentheses "( )" and braces "{ }"
      • Eliminate space before assignment operators
      • Force space before array initializer left brace
      • Force spaces within non-empty array initializer braces
    • Improved Preview pane contents to cover all options.
  • 05 Dec 2003 - Version 4.4
    • bug fixes: handle continuation line properly (e.g. with parameters on multiple lines; with all combinations of alignment settings).
  • 03 Nov 2003 - Version 4.2 (4.3 is equivalent)
    • bug fix: class comments are no longer aligned with trailing comments. Class comments are defined as any comment with no preceding text on the line, or occurring before the first field or method definition of the class, or occurring after the closing right brace of the class.
    • enhancement: array initializers are now formatted properly; but this formatting can be completely disabled on the configuration panel.
  • 30 Oct 2003 - Version 4.1
    • bug fix: allow negative integers (and all prefixed expressions involving integers) to be right justified as integer constants are.
  • 29 Oct 2003 - Version 4.0
    • Complete rewrite of parsing and alignment code. Pretty much only the UI is the same. Necessary for handling recursive or nested syntactic constructs.
  • 12 Sep 2003 - Version 3.0
    • First cut at next major release. Works with EAP build 922.
  • 17 Jun 2003 - Version 2.9
    • Tabifier settings dialog moved from Project Settings to IDE Settings under the Option menu item. You'll need to set them one last time; all settings henceforth apply to all projects.
    • bug fix: tabifier now handles illegal syntax better; a variable declaration with a missing semicolon, for example, will no longer scare the tabifier into throwing an exception and quitting.
  • 13 Jun 2003 - Version 2.8
    • bug fix: new functionality of version 2.7 did not handle groups of assignments where some were prefixed and others not, e.g.
        int i = 1;
        if (args.length == 0) i = 0;
        else                  i = 1;
was incorrectly tabified to
                              int i = 1;
        if (args.length == 0)     i = 0;
        else                      i = 1;
Now lines that have prefixes are aligned independently of those that don't, and tabifier produces output like the first example.
  • 12 Jun 2003 - Version 2.7
    • bug fix: assignment statements preceded by other text now align properly; for example
        if (args.length == 0) i = 0;
        else i = 1;
becomes
        if (args.length == 0) i = 0;
        else                  i = 1;
  • 12 Jun 2003 - Version 2.7
    • bug fix: when tabifying a declaration with a preceding comment, but beginning the selection after the comment, tabifier skipped the statement -- but does no longer. For example, if only the three lines after the comment are selected in the following:
    /**
     * experiment with modifiers.
     */
    public static final String PARAM_MAIN_PANEL = "mainpanel";
    public static final String VALUE_MAIN_PANEL_TABS = "tabs";
    public static final String DEFAULT_MAIN_PANEL = VALUE_MAIN_PANEL_TABS;
tabifier now properly aligns them all as follows:
    public static final String PARAM_MAIN_PANEL      = "mainpanel";
    public static final String VALUE_MAIN_PANEL_TABS = "tabs";
    public static final String DEFAULT_MAIN_PANEL    = VALUE_MAIN_PANEL_TABS;
  • 12 Jun 2003 - Version 2.7
    • bug fix: indentation of lines in switch statements could be off by a level if "Indent case from switch" code layout option was selected.

  • 27 May 2003 - Version 2.6
    • bug fix: An extra leading space could be emitted when (for example) variable types were not aligned but variable names were.
    • enhancement: user can control what happens when no selection is made: tabify entire file, or only the line containing the cursor (default)
    • debugging output now contains version number.

  • 24 May 2003 - Version 2.5 - bug fix:
    • modified to work with Aurora build 816. Interface PsiType was replaced with PsiTypeElement in the Psi tree, which broke declaration parsing. Dynamically deteced, so this version still works with 3.0.x.

  • 22 May 2003 - Version 2.4 - bug fix:
    • multiline field or variable declarations (int a=1, \n b=2, \n c=3;) could be incorrectly aligned as if "b" and "c" were assignment expressions. If the grouping option was set to format adjacent declarations and assignments independently, then "b" and "c" would be aligned relative to each other but not to "a". Now treat "b" and "c" as declarations. This forces "a", "b" and "c" to be formatted together in all cases.

  • 16 May 2003 - Version 2.3 - enhancements:
    • The Reformat plugin chains to Tabifier after IDEA's Reformat Code... tool runs, so tabification happens automatically on the same file (or same selection of file) as was reformatted by the code layout tool. That is, Ctrl-Alt-L or Ctrl-Alt-R invoke tabifier after IDEA is done reformatting code. Requires Reformat Plugin version 0.2 or later.
    • renamed the package to com.wrq.tabifier to avoid conflict with Jordan's Selector plugin, which contains an older version of the tabifier. That plugin may ultimately go away, since IntelliJ is implementing block selection capability in Aurora.

  • 03 May 2003 - Version 2.2 - bug fixes:
    • fixed indentation problems when tab size did not equal indentation.
    • when aligning by emitting tabs (instead of only spaces -- based on project "Use Tabs" setting), modifiers are now properly aligned and comments and quoted strings are unaffected.

  • 4/28/2003 - Version 2.1 - bug fixes.

  • 4/23/2003 - Version 2.0 - major revision. Tabifier now uses program structure information (Psi) to identify text as declaration (field or variable) or assignment statements. Only this text is aligned; other text is left intact. Therefore the entire file may be tabified in a single operation (select all text, tabify). Many additional options for controlling the alignment of text have been added. Modifiers may be reordered and aligned. Alignment may occur at variable type, variable name, assignment operator, right-hand expression, trailing comma/semicolon, and/or trailing comments.

  • 2/27/2003 - Version 1.9 - 1. New version number scheme. 2. DaveKriewall has taken over development and has added new features: "Align modifiers" and "Align trailing // comments". Dave promises more features to come. Thanks Dave!

  • 10/7/2002 - Version 1.7 - OpenAPI for EditorSettings changed in Build 655. Unfortunately, this version cannot be backwards compatible.

  • 9/26/2002 - Version 1.6 - OpenAPI for Preferences panel changed in Build 650. This new version should work with 650 and previous builds.

  • 7/30/2002 - Version 1.5 - Bug fix - was tabifying next line if entire previous line was selected.

  • 7/28/2002 - Version 1.4 - Required update for OpenAPI change. Also, now has a preferences panel in the Project Options. You can set the number of tabs that get inserted between the type and variable name. You can also set Tabifier to always insert spaces regardless of your Editor settings.

  • 7/20/2002 - Version 1.3 - Tabifier now respects the global setting for tab characters and inserts spaces if that's your preference.

  • 7/19/2002 - Version 1.2 - It turns out that there's an API that does a lot of the basic editor modification work: EditorAction and EditorWriteActionHandler. I've modified the code to use it.

  • 7/19/2002 - Version 1.1 - This version checks for writable files and puts up the "Do You Want To Checkout" dialog. Also, fixed some minor mistakes in the XML file and changed the name of the plugin from "JZTabifier" to "Tabifier".

  • 7/18/2002 - First Release

IntelliJPluginWebForm
IntelliJPluginName: Tabifier
IntelliJPluginVersion: 5.3
IntelliJPluginVendor: Dave Kriewall
IntelliJPluginBinary:
IntelliJPluginSource:
IntelliJPluginClassification: CodeHelper?, ProgramStructureInterface, LookAndFeel
TopicClassification: IntelliJPluginPackage
TopicShortDescription: This plugin respaces Java code so that syntactic elements are aligned vertically.
TestedOnOS: OsWin2K
ShouldRunOnOS: AnyOS

e d i t a t t a c h r e f - b y d i f f s m o r e
Have ideas, requests, problems regarding this site? Send feedback.
Copyright © 2000-2003 by the contributing authors. All materials at intellij.org are the property of the contributing authors.