1010from lark .reconstruct import Reconstructor
1111from lark .tree_matcher import is_discarded_terminal
1212from lark .visitors import Transformer_InPlace
13+
14+ from hcl2 .const import START_LINE_KEY , END_LINE_KEY
1315from hcl2 .parser import reconstruction_parser
1416
1517
@@ -423,7 +425,7 @@ def _dict_is_a_block(self, sub_obj: Any) -> bool:
423425
424426 # if the sub object has "start_line" and "end_line" metadata,
425427 # the block itself is unlabeled, but it is a block
426- if "__start_line__" in sub_obj .keys () or "__end_line__" in sub_obj .keys ():
428+ if START_LINE_KEY in sub_obj .keys () or END_LINE_KEY in sub_obj .keys ():
427429 return True
428430
429431 # if the objects in the array have no metadata and more than 2 keys and
@@ -454,15 +456,16 @@ def _calculate_block_labels(self, block: dict) -> Tuple[List[str], dict]:
454456
455457 # __start_line__ and __end_line__ metadata are not labels
456458 if (
457- "__start_line__" in potential_body .keys ()
458- or "__end_line__" in potential_body .keys ()
459+ START_LINE_KEY in potential_body .keys ()
460+ or END_LINE_KEY in potential_body .keys ()
459461 ):
460462 return [curr_label ], potential_body
461463
462464 # recurse and append the label
463465 next_label , block_body = self ._calculate_block_labels (potential_body )
464466 return [curr_label ] + next_label , block_body
465467
468+ # pylint:disable=R0914
466469 def _transform_dict_to_body (self , hcl_dict : dict , level : int ) -> Tree :
467470 # we add a newline at the top of a body within a block, not the root body
468471 # >2 here is to ignore the __start_line__ and __end_line__ metadata
@@ -473,7 +476,7 @@ def _transform_dict_to_body(self, hcl_dict: dict, level: int) -> Tree:
473476
474477 # iterate through each attribute or sub-block of this block
475478 for key , value in hcl_dict .items ():
476- if key in ["__start_line__" , "__end_line__" ]:
479+ if key in [START_LINE_KEY , END_LINE_KEY ]:
477480 continue
478481
479482 # construct the identifier, whether that be a block type name or an attribute key
@@ -499,7 +502,12 @@ def _transform_dict_to_body(self, hcl_dict: dict, level: int) -> Tree:
499502 [identifier_name ] + block_label_tokens + [block_body ],
500503 )
501504 children .append (block )
502- children .append (self ._newline (level , count = 2 ))
505+ # add empty line after block
506+ new_line = self ._newline (level - 1 )
507+ # add empty line with indentation for next element in the block
508+ new_line .children .append (self ._newline (level ).children [0 ])
509+
510+ children .append (new_line )
503511
504512 # if the value isn't a block, it's an attribute
505513 else :
@@ -562,7 +570,7 @@ def _transform_value_to_expr_term(self, value, level) -> Union[Token, Tree]:
562570
563571 # iterate through the items and add them to the object
564572 for i , (k , dict_v ) in enumerate (value .items ()):
565- if k in ["__start_line__" , "__end_line__" ]:
573+ if k in [START_LINE_KEY , END_LINE_KEY ]:
566574 continue
567575
568576 value_expr_term = self ._transform_value_to_expr_term (dict_v , level + 1 )
0 commit comments