@@ -297,26 +297,29 @@ def src_info(self) -> Dict[str, Any]:
297297 get_logger ().debug (e )
298298 return result
299299
300- def format_model (
300+ def format (
301301 self ,
302- save : bool = False ,
302+ overwrite_file : bool = False ,
303303 canonicalize : Union [bool , str , Iterable [str ]] = False ,
304+ max_line_length : int = 78 ,
304305 * ,
305- unsafe : bool = False ,
306+ backup : bool = True ,
306307 ) -> None :
307308 """
308309 Run stanc's auto-formatter on the model code. Either saves directly
309310 back to the file or prints for inspection
310311
311312
312- :param save : If True, save the updated code to disk, rather than
313- printing it. By default False
313+ :param overwrite_file : If True, save the updated code to disk, rather
314+ than printing it. By default False
314315 :param canonicalize: Whether or not the compiler should 'canonicalize'
315316 the Stan model, removing things like deprecated syntax. Default is
316317 False. If True, all canonicalizations are run. If it is a list of
317318 strings, those options are passed to stanc (new in Stan 2.29)
318- :param unsafe: If True, do not create stanfile.bak backups before
319- writing to the file. Only do this if you're sure you have other
319+ :param max_line_length: Set the wrapping point for the formatter. The
320+ default value is 78, which wraps most lines by the 80th character.
321+ :param backup: If True, create a stanfile.bak backup before
322+ writing to the file. Only disable this if you're sure you have other
320323 copies of the file or are using a version control system like Git.
321324 """
322325 if self .stan_file is None or not os .path .isfile (self .stan_file ):
@@ -330,25 +333,32 @@ def format_model(
330333 )
331334
332335 if canonicalize :
333- if isinstance (canonicalize , str ):
336+ if cmdstan_version_before (2 , 29 ) or isinstance (
337+ canonicalize , bool
338+ ):
339+ cmd .append ('--print-canonical' )
340+ elif isinstance (canonicalize , str ):
334341 cmd .append ('--canonicalize=' + canonicalize )
335342 elif isinstance (canonicalize , Iterable ):
336343 cmd .append ('--canonicalize=' + ',' .join (canonicalize ))
337- else :
338- cmd .append ('--print-canonical' )
339344
345+ # before 2.29, having both --print-canonical
346+ # and --auto-format printed twice
340347 if not (cmdstan_version_before (2 , 29 ) and canonicalize ):
341348 cmd .append ('--auto-format' )
342349
350+ if not cmdstan_version_before (2 , 29 ):
351+ cmd .append (f'--max-line-length={ max_line_length } ' )
352+
343353 out = subprocess .run (
344354 cmd , capture_output = True , text = True , check = True
345355 )
346356 if out .stderr :
347357 get_logger ().warning (out .stderr )
348358 result = out .stdout
349- if save :
359+ if overwrite_file :
350360 if result :
351- if not unsafe :
361+ if backup :
352362 shutil .copyfile (self .stan_file , self .stan_file + '.bak' )
353363 with (open (self .stan_file , 'w' )) as file_handle :
354364 file_handle .write (result )
0 commit comments