How to indent a code block?

Hi,
I have this piece of code

Module: Synopsis: Author: cek Copyright: See LICENSE file in this distribution.

if (a + b) do-something(); end;


I want to indent it like this

Module: Synopsis: Author: cek Copyright: See LICENSE file in this distribution.

if (a + b)   do-something(); end;


I wrote the necessary code (FormattingModelBuilder, FormattingBlock etc.) to set the indent for every block, this is the output of my printAST function (taken from pycharm's formatter).

AST tree for short.dylan: Element(FILE) (0,112)   Element(HEADERS) (0,80)     Element(HEADER) (0,8)       PsiElement(DylanTokenType.KEY) (0,6)       PsiElement(DylanTokenType.HEADER_SEPARATOR) (6,7)       Element(HEADER_VALUES) (7,8)         PsiElement(DylanTokenType.CRLF) (7,8)     Element(HEADER) (8,18)       PsiElement(DylanTokenType.KEY) (8,16)       PsiElement(DylanTokenType.HEADER_SEPARATOR) (16,17)       Element(HEADER_VALUES) (17,18)         PsiElement(DylanTokenType.CRLF) (17,18)     Element(HEADER) (18,30)       PsiElement(DylanTokenType.KEY) (18,24)       PsiElement(DylanTokenType.HEADER_SEPARATOR) (24,25)       PsiWhiteSpace (25,26)       Element(HEADER_VALUES) (26,30)         PsiElement(DylanTokenType.VALUE) (26,29)         PsiElement(DylanTokenType.CRLF) (29,30)     Element(HEADER) (30,80)       PsiElement(DylanTokenType.KEY) (30,39)       PsiElement(DylanTokenType.HEADER_SEPARATOR) (39,40)       PsiWhiteSpace (40,41)       Element(HEADER_VALUES) (41,80)         PsiElement(DylanTokenType.VALUE) (41,79)         PsiElement(DylanTokenType.CRLF) (79,80)   PsiWhiteSpace (80,81)   Element(SOURCE_RECORDS) (81,112)     Element(BODY) (81,112)       Element(OPERAND_EXPR) (81,111)         Element(OPERAND) (81,111)           Element(IF_STATEMENT) (81,111)             PsiElement(DylanTokenType.IF) (81,83)             PsiWhiteSpace (83,84)             PsiElement(DylanTokenType.LPAREN) (84,85)             Element(PLUS_EXPR) (85,90)               Element(OPERAND_EXPR) (85,86)                 Element(OPERAND) (85,86)                   Element(VARIABLE_NAME) (85,86)                     PsiElement(DylanTokenType.NONDEFINING_NONEXPRESSION_WORD) (85,86)               PsiWhiteSpace (86,87)               PsiElement(DylanTokenType.PLUS) (87,88)               PsiWhiteSpace (88,89)               Element(OPERAND_EXPR) (89,90)                 Element(OPERAND) (89,90)                   Element(VARIABLE_NAME) (89,90)                     PsiElement(DylanTokenType.NONDEFINING_NONEXPRESSION_WORD) (89,90)             PsiElement(DylanTokenType.RPAREN) (90,92)             Element(BODY) (92,108)               Element(OPERAND_EXPR) (92,106)                 Element(OPERAND) (92,106)                   Element(VARIABLE_NAME) (92,104)                     PsiElement(DylanTokenType.NONDEFINING_NONEXPRESSION_WORD) (92,104)                   PsiElement(DylanTokenType.LPAREN) (104,105)                   PsiElement(DylanTokenType.RPAREN) (105,106)               PsiElement(DylanTokenType.semicolon) (106,108)             Element(IF_TAIL) (108,111)               PsiElement(DylanTokenType.END) (108,111)       PsiElement(DylanTokenType.semicolon) (111,112)   <block FILE 0:112 <Indent: NONE>>     <block HEADERS 0:80 <Indent: NONE>>       <block HEADER 0:8 <Indent: NONE>>         "Module" 0:6 <Indent: NONE>         ":" 6:7 <Indent: NONE>         <block HEADER_VALUES 7:8 <Indent: NONE>>           " " 7:8 <Indent: NONE>       <block HEADER 8:18 <Indent: NONE>>         "Synopsis" 8:16 <Indent: NONE>         ":" 16:17 <Indent: NONE>         <block HEADER_VALUES 17:18 <Indent: NONE>>           " " 17:18 <Indent: NONE>       <block HEADER 18:30 <Indent: NONE>>         "Author" 18:24 <Indent: NONE>         ":" 24:25 <Indent: NONE>         <block HEADER_VALUES 26:30 <Indent: NONE>>           "cek" 26:29 <Indent: NONE>           " " 29:30 <Indent: NONE>       <block HEADER 30:80 <Indent: NONE>>         "Copyright" 30:39 <Indent: NONE>         ":" 39:40 <Indent: NONE>         <block HEADER_VALUES 41:80 <Indent: NONE>>           "See LICENSE file in this distribution." 41:79 <Indent: NONE>           " " 79:80 <Indent: NONE>     <block SOURCE_RECORDS 81:112 <Indent: NONE>>       <block BODY 81:112 <Indent: NONE>>         <block OPERAND_EXPR 81:111 <Indent: NONE>>           <block OPERAND 81:111 <Indent: NONE>>             <block IF_STATEMENT 81:111 <Indent: NONE>>               "if" 81:83 <Indent: NONE>               "(" 84:85 <Indent: NONE>               <block PLUS_EXPR 85:90 <Indent: NONE>>                 <block OPERAND_EXPR 85:86 <Indent: NONE>>                   <block OPERAND 85:86 <Indent: NONE>>                     <block VARIABLE_NAME 85:86 <Indent: NONE>>                       "a" 85:86 <Indent: NONE>                 "+" 87:88 <Indent: NONE>                 <block OPERAND_EXPR 89:90 <Indent: NONE>>                   <block OPERAND 89:90 <Indent: NONE>>                     <block VARIABLE_NAME 89:90 <Indent: NONE>>                       "b" 89:90 <Indent: NONE>               ") " 90:92 <Indent: NONE>               <block BODY 92:108 <Indent: NORMAL>>                 <block OPERAND_EXPR 92:106 <Indent: NONE>>                   <block OPERAND 92:106 <Indent: NONE>>                     <block VARIABLE_NAME 92:104 <Indent: NONE>>                       "do-something" 92:104 <Indent: NONE>                     "(" 104:105 <Indent: NONE>                     ")" 105:106 <Indent: NONE>                 spacing: <Spacing: minSpaces=0 maxSpaces=0 minLineFeeds=0>                 "; " 106:108 <Indent: NONE>               <block IF_TAIL 108:111 <Indent: NONE>>                 "end" 108:111 <Indent: NONE>         spacing: <Spacing: minSpaces=0 maxSpaces=0 minLineFeeds=0>         ";" 111:112 <Indent: NONE>


(You can ignore the headers stuff).

After running the formatter the code doesn't change its indentation, despite BODY block having a normalIndent.

So, what am I doing wrong?

Thanks for your help.

2 comments

As far as I understand from your dump, whitespace characters (in particular the line break preceding the block you want to be indented) are included in the text ranges of your blocks. For the formatter to work, all whitespace needs to be outside of blocks.

0

Thank you, changing the parser to return a WHITE_SPACE for \n fixed this problem.

0

Please sign in to leave a comment.