

Rather, only blocks for the text range covered by the formatting operation and their parents are built.įor every block, the plugin specifies the following properties: If the formatting operation does not affect the entire file (for example, if the formatter is called to format the pasted block of text), a complete tree of blocks is not built. To change the default "block name" taken from class name, return custom Block.getDebugName(). or Tools | View PSI Structure of Current File.: To invoke PsiViewer with the possibility of inspecting Block Structure, use Tools | View PSI Structure.
Code formatter in eclipse how to#
To better understand how to build the block structure, use PsiViewer and inspect formatting blocks built for an existing language. Otherwise, the formatter may delete the characters between blocks. The formatter modifies only the characters between blocks, and the tree of blocks must be built so that the bottom-level blocks cover all non-whitespace characters in the file. Its children cover individual classes in the file, blocks on the next level cover methods inside classes, etc.

The structure of blocks is usually built so that it mirrors the PSI structure of the file – for example, in Java code, the top-level formatting block covers the entire file. The formatting model is requested to insert the calculated whitespace characters at necessary positions in the file. The formatting engine calculates the sequence of whitespace characters (spaces, tabs, and/or line breaks) that needs to be placed at every block boundary, based on the plugin's formatting model. The formatting model is requested to build the structure of the file as applies to formatting, as a tree of blocks ( Block) with an associated indent, wrap, alignment, and spacing settings. The formatting model builder ( FormattingModelBuilder), implemented by the plugin, provides a formatting model ( FormattingModel) for the document to be formatted. The process of formatting a file or a file fragment consists of the following main steps: The formatting engine, provided by the IDE, calculates the smallest number of whitespace modifications that need to be performed on the file to make it match the constraints.

In this framework, the plugin specifies the constraints on the whitespaces between different syntax elements. The IntelliJ Platform includes a powerful framework for implementing custom language formatters.
