@@ -42,18 +42,14 @@ def format_tree(self, tree: LarkElement):
4242class BaseFormatter (LarkElementTreeFormatter ):
4343 def __init__ (self , options : FormatterOptions = None ):
4444 super ().__init__ (options )
45- self ._current_line = 1
46- self ._current_indent_level = 0
45+ self ._last_new_line : NewLineOrCommentRule = None
4746
4847 def format_tree (self , tree : LarkElement ):
4948 if isinstance (tree , StartRule ):
5049 self .format_start_rule (tree )
5150
5251 def format_start_rule (self , rule : StartRule ):
5352 self .format_body_rule (rule .body , 0 )
54- # for child in rule.body.children:
55- # if isinstance(child, BlockRule):
56- # self.format_block_rule(child, 1)
5753
5854 def format_block_rule (self , rule : BlockRule , indent_level : int = 0 ):
5955 if self .options .vertically_align_attributes :
@@ -87,7 +83,8 @@ def format_body_rule(self, rule: BodyRule, indent_level: int = 0):
8783 new_children .insert (- 2 , self ._build_newline (indent_level ))
8884 new_children .append (self ._build_newline (indent_level , 2 ))
8985
90- new_children .pop (- 1 )
86+ if new_children :
87+ new_children .pop (- 1 )
9188 rule ._children = new_children
9289
9390 def format_attribute_rule (self , rule : AttributeRule , indent_level : int = 0 ):
@@ -158,7 +155,7 @@ def format_expression(self, rule: ExprTermRule, indent_level: int = 0):
158155 self .format_forobjectexpr (rule .expression , indent_level )
159156
160157 elif isinstance (rule .expression , ExprTermRule ):
161- self .format_expression (rule .expression )
158+ self .format_expression (rule .expression , indent_level )
162159
163160 def format_fortupleexpr (self , expression : ForTupleExprRule , indent_level : int = 0 ):
164161 for child in expression .children :
@@ -169,7 +166,6 @@ def format_fortupleexpr(self, expression: ForTupleExprRule, indent_level: int =
169166 for index in indexes :
170167 expression .children [index ] = self ._build_newline (indent_level )
171168 self ._deindent_last_line ()
172- # expression.children[8] = self._build_newline(indent_level - 1)
173169
174170 def format_forobjectexpr (
175171 self , expression : ForObjectExprRule , indent_level : int = 0
@@ -192,23 +188,28 @@ def _vertically_align_attributes_in_body(self, body: BodyRule):
192188 attributes_sequence .append (child )
193189
194190 elif attributes_sequence :
195- max_length = max (
196- len (attribute .identifier .token .value )
197- for attribute in attributes_sequence
198- )
199- for attribute in attributes_sequence :
200- name_length = len (attribute .identifier .token .value )
201- spaces_to_add = max_length - name_length
202- attribute .children [1 ].set_value (
203- " " * spaces_to_add + attribute .children [1 ].value
204- )
191+ self ._align_attribute_group (attributes_sequence )
205192 attributes_sequence = []
206193
194+ if attributes_sequence :
195+ self ._align_attributes_sequence (attributes_sequence )
196+
197+ def _align_attributes_sequence (self , attributes_sequence : List [AttributeRule ]):
198+ max_length = max (
199+ len (attribute .identifier .token .value )
200+ for attribute in attributes_sequence
201+ )
202+ for attribute in attributes_sequence :
203+ name_length = len (attribute .identifier .token .value )
204+ spaces_to_add = max_length - name_length
205+ attribute .children [1 ].set_value (
206+ " " * spaces_to_add + attribute .children [1 ].value
207+ )
208+
207209 def _vertically_align_object_elems (self , rule : ObjectRule ):
208210 max_length = max (len (elem .key .serialize ()) for elem in rule .elements )
209211 for elem in rule .elements :
210212 key_length = len (elem .key .serialize ())
211- print (elem .key .serialize (), key_length )
212213
213214 spaces_to_add = max_length - key_length
214215
@@ -218,17 +219,6 @@ def _vertically_align_object_elems(self, rule: ObjectRule):
218219
219220 elem .children [1 ].set_value (" " * spaces_to_add + separator .value )
220221
221- def _move_to_next_line (self , times : int = 1 ):
222- self ._current_line += times
223-
224- def _increase_indent_level (self , times : int = 1 ):
225- self ._current_indent_level += times
226-
227- def _decrease_indent_level (self , times : int = 1 ):
228- self ._current_indent_level -= times
229- if self ._current_indent_level < 0 :
230- self ._current_indent_level = 0
231-
232222 def _build_newline (
233223 self , next_line_indent : int = 0 , count : int = 1
234224 ) -> NewLineOrCommentRule :
@@ -247,16 +237,3 @@ def _deindent_last_line(self, times: int = 1):
247237 for i in range (times ):
248238 if token .value .endswith (" " * self .options .indent_length ):
249239 token .set_value (token .value [: - self .options .indent_length ])
250-
251- # def _build_meta(self, indent_level: int = 0, length: int = 0) -> Meta:
252- # result = Meta()
253- # result.empty = length == 0
254- # result.line = self._current_line
255- # result.column = indent_level * self.options.indent_length
256- # # result.start_pos =
257- # # result.end_line =
258- # # result.end_column =
259- # # result.end_pos =
260- # # result.orig_expansion =
261- # # result.match_tree =
262- # return result
0 commit comments