I want to apply a newline and indentation to certain elements in a custom language formatter.
For example, consider the following SQL:
select ast.id, ast.name from (SELECT id, name FROM a WHERE name ='bbb') as ast
I want to insert a newline and indent before commas and keywords, resulting in:
select ast.id
, ast.name
from (SELECT id
, name
FROM a
WHERE name ='bbb') as ast
However, when I run “Code > Reformat File” without any existing newlines, it only inserts newlines, without indentation.
If I manually press Enter before commas or keywords, the indentation is applied as expected:
select ast.id
, ast.name
from (SELECT id
, name
FROM a
WHERE name ='bbb') as ast
The results look different in the images because the post processor replaces keyword elements with uppercase letters. Please ignore the difference.
Below is the formatter implementation under development. The top-level element is SqlBlock, and keywords or commas are represented by SqlKeywordBlock and SqlCommaBlock:
I suspect some conflict between the getSpacing method in SqlBlock and the getIndent methods in the child blocks.
What changes should I make so that indentation is also applied when running “Code > Reformat File”?
For the time being, we have implemented the following temporary measures.
We achieve pseudo-indentation by using the line break and indentation values and setting the number of spaces for each blank block with getSpacing().
If you reformat twice, you knowed that the indentation will be added the second time.
The PreProcessor now inserts a line break before elements that require it, so that when generating blocks in the formatter process, white space blocks before elements that require a line break + indent are not ignored.