@@ -41,7 +41,7 @@ def visit_statement(self, statement: Statement, p: P) -> J:
4141 else :
4242 min_lines = max (self ._style .minimum .around_top_level_classes_functions if isinstance (statement , (ClassDeclaration , MethodDeclaration )) else 0 ,
4343 self ._style .minimum .after_top_level_imports if prev_import else 0 )
44- statement = minimum_lines_for_tree (statement , min_lines )
44+ statement = _adjusted_lines_for_tree (statement , min_lines , self . _style . keep_maximum . in_declarations )
4545 else :
4646 in_block = isinstance (parent_cursor .value , Block )
4747 in_class = in_block and isinstance (parent_cursor .parent_tree_cursor ().value , ClassDeclaration )
@@ -54,9 +54,16 @@ def visit_statement(self, statement: Statement, p: P) -> J:
5454 min_lines = max (min_lines , self ._style .minimum .around_class )
5555 elif is_first and isinstance (statement , MethodDeclaration ):
5656 min_lines = max (min_lines , self ._style .minimum .before_first_method )
57+
58+ # This seems to correspond to how IntelliJ interprets this configuration
59+ max_lines = self ._style .keep_maximum .in_declarations if \
60+ isinstance (statement , (ClassDeclaration , MethodDeclaration )) else \
61+ self ._style .keep_maximum .in_code
62+
5763 if prev_import :
5864 min_lines = max (min_lines , self ._style .minimum .after_local_imports )
59- statement = minimum_lines_for_tree (statement , min_lines )
65+
66+ statement = _adjusted_lines_for_tree (statement , min_lines , max_lines )
6067 return statement
6168
6269 def post_visit (self , tree : T , p : P ) -> Optional [T ]:
@@ -68,39 +75,35 @@ def visit(self, tree: Optional[Tree], p: P, parent: Optional[Cursor] = None) ->
6875 return tree if self ._stop else super ().visit (tree , p , parent )
6976
7077
71- def minimum_lines_for_right_padded (tree : JRightPadded [J2 ], min_lines ) -> JRightPadded [J2 ]:
72- return tree .with_element (minimum_lines_for_tree (tree .element , min_lines ))
78+ def _adjusted_lines_for_right_padded (tree : JRightPadded [J2 ], min_lines : int , max_lines : int ) -> JRightPadded [J2 ]:
79+ return tree .with_element (_adjusted_lines_for_tree (tree .element , min_lines , max_lines ))
7380
7481
75- def minimum_lines_for_tree (tree : J , min_lines ) -> J :
76- if min_lines == 0 :
77- return tree
78- return tree .with_prefix (minimum_lines_for_space (tree .prefix , min_lines ))
82+ def _adjusted_lines_for_tree (tree : J , min_lines : int , max_lines : int ) -> J :
83+ return tree .with_prefix (_adjusted_lines_for_space (tree .prefix , min_lines , max_lines ))
7984
8085
81- def minimum_lines_for_space (prefix : Space , min_lines ) -> Space :
82- if min_lines == 0 :
83- return prefix
86+ def _adjusted_lines_for_space (prefix : Space , min_lines : int , max_lines : int ) -> Space :
8487 if not prefix .comments or \
8588 '\n ' in prefix .whitespace or \
8689 (prefix .comments [0 ].multiline and '\n ' in prefix .comments [0 ].text ):
87- return prefix .with_whitespace (minimum_lines_for_string (prefix .whitespace , min_lines ))
90+ return prefix .with_whitespace (_adjusted_lines_for_string (prefix .whitespace , min_lines , max_lines ))
8891
8992 # the first comment is a trailing comment on the previous line
90- c0 = prefix .comments [0 ].with_suffix (minimum_lines_for_string (prefix .comments [0 ].suffix , min_lines ))
93+ c0 = prefix .comments [0 ].with_suffix (_adjusted_lines_for_string (prefix .comments [0 ].suffix , min_lines , max_lines ))
9194 return prefix if c0 is prefix .comments [0 ] else prefix .with_comments ([c0 ] + prefix .comments [1 :])
9295
9396
94- def minimum_lines_for_string (whitespace , min_lines ):
95- if min_lines == 0 :
97+ def _adjusted_lines_for_string (whitespace , min_lines : int , max_lines : int ):
98+ existing_blank_lines = max (_count_line_breaks (whitespace ) - 1 , 0 )
99+ max_lines = max (min_lines , max_lines )
100+ if min_lines <= existing_blank_lines <= max_lines :
96101 return whitespace
97-
98- min_whitespace = whitespace
99- for _ in range (min_lines - get_new_line_count (whitespace ) + 1 ):
100- min_whitespace = '\n ' + min_whitespace
101-
102- return min_whitespace
102+ elif existing_blank_lines < min_lines :
103+ return '\n ' * (min_lines - existing_blank_lines ) + whitespace
104+ else :
105+ return '\n ' * max_lines + whitespace [whitespace .rfind ('\n ' ):]
103106
104107
105- def get_new_line_count (whitespace ):
108+ def _count_line_breaks (whitespace ):
106109 return whitespace .count ('\n ' )
0 commit comments