@@ -93,6 +93,43 @@ def wrapper(self: SQLMeshMagics, *args: t.Any, **kwargs: t.Any) -> None:
9393 return wrapper
9494
9595
96+ def format_arguments (func : t .Callable ) -> t .Callable :
97+ """Decorator to add common format arguments to magic commands."""
98+ func = argument (
99+ "--normalize" ,
100+ action = "store_true" ,
101+ help = "Whether or not to normalize identifiers to lowercase." ,
102+ default = None ,
103+ )(func )
104+ func = argument (
105+ "--pad" ,
106+ type = int ,
107+ help = "Determines the pad size in a formatted string." ,
108+ )(func )
109+ func = argument (
110+ "--indent" ,
111+ type = int ,
112+ help = "Determines the indentation size in a formatted string." ,
113+ )(func )
114+ func = argument (
115+ "--normalize-functions" ,
116+ type = str ,
117+ help = "Whether or not to normalize all function names. Possible values are: 'upper', 'lower'" ,
118+ )(func )
119+ func = argument (
120+ "--leading-comma" ,
121+ action = "store_true" ,
122+ help = "Determines whether or not the comma is leading or trailing in select expressions. Default is trailing." ,
123+ default = None ,
124+ )(func )
125+ func = argument (
126+ "--max-text-width" ,
127+ type = int ,
128+ help = "The max number of characters in a segment before creating new lines in pretty mode." ,
129+ )(func )
130+ return func
131+
132+
96133@magics_class
97134class SQLMeshMagics (Magics ):
98135 @property
@@ -579,23 +616,39 @@ def evaluate(self, context: Context, line: str) -> None:
579616 )
580617 @argument ("--dialect" , type = str , help = "SQL dialect to render." )
581618 @argument ("--no-format" , action = "store_true" , help = "Disable fancy formatting of the query." )
619+ @format_arguments
582620 @line_magic
583621 @pass_sqlmesh_context
584622 def render (self , context : Context , line : str ) -> None :
585623 """Renders a model's query, optionally expanding referenced models."""
586624 context .refresh ()
587- args = parse_argstring (self .render , line )
625+ render_opts = vars (parse_argstring (self .render , line ))
626+ model = render_opts .pop ("model" )
627+ dialect = render_opts .pop ("dialect" , None )
588628
589629 query = context .render (
590- args . model ,
591- start = args . start ,
592- end = args . end ,
593- execution_time = args . execution_time ,
594- expand = args . expand ,
630+ model ,
631+ start = render_opts . pop ( " start" , None ) ,
632+ end = render_opts . pop ( " end" , None ) ,
633+ execution_time = render_opts . pop ( " execution_time" , None ) ,
634+ expand = render_opts . pop ( " expand" , False ) ,
595635 )
596636
597- sql = query .sql (pretty = True , dialect = args .dialect or context .config .dialect )
598- if args .no_format :
637+ no_format = render_opts .pop ("no_format" , False )
638+
639+ format_config = context .config_for_node (model ).format
640+ format_options = {
641+ ** format_config .generator_options ,
642+ ** {k : v for k , v in render_opts .items () if v is not None },
643+ }
644+
645+ sql = query .sql (
646+ pretty = True ,
647+ dialect = context .config .dialect if dialect is None else dialect ,
648+ ** format_options ,
649+ )
650+
651+ if no_format :
599652 context .console .log_status_update (sql )
600653 else :
601654 context .console .show_sql (sql )
@@ -853,55 +906,24 @@ def rewrite(self, context: Context, line: str, sql: str) -> None:
853906 help = "Transpile project models to the specified dialect." ,
854907 )
855908 @argument (
856- "--append-newline" ,
857- action = "store_true" ,
858- help = "Whether or not to append a newline to the end of the file." ,
859- default = None ,
860- )
861- @argument (
862- "--no-rewrite-casts" ,
863- action = "store_true" ,
864- help = "Whether or not to preserve the existing casts, without rewriting them to use the :: syntax." ,
865- default = None ,
866- )
867- @argument (
868- "--normalize" ,
909+ "--check" ,
869910 action = "store_true" ,
870- help = "Whether or not to normalize identifiers to lowercase ." ,
911+ help = "Whether or not to check formatting (but not actually format anything) ." ,
871912 default = None ,
872913 )
873914 @argument (
874- "--pad" ,
875- type = int ,
876- help = "Determines the pad size in a formatted string." ,
877- )
878- @argument (
879- "--indent" ,
880- type = int ,
881- help = "Determines the indentation size in a formatted string." ,
882- )
883- @argument (
884- "--normalize-functions" ,
885- type = str ,
886- help = "Whether or not to normalize all function names. Possible values are: 'upper', 'lower'" ,
887- )
888- @argument (
889- "--leading-comma" ,
915+ "--append-newline" ,
890916 action = "store_true" ,
891- help = "Determines whether or not the comma is leading or trailing in select expressions. Default is trailing ." ,
917+ help = "Include a newline at the end of the output ." ,
892918 default = None ,
893919 )
894920 @argument (
895- "--max-text-width" ,
896- type = int ,
897- help = "The max number of characters in a segment before creating new lines in pretty mode." ,
898- )
899- @argument (
900- "--check" ,
921+ "--no-rewrite-casts" ,
901922 action = "store_true" ,
902- help = "Whether or not to check formatting (but not actually format anything) ." ,
923+ help = "Preserve the existing casts, without rewriting them to use the :: syntax ." ,
903924 default = None ,
904925 )
926+ @format_arguments
905927 @line_magic
906928 @pass_sqlmesh_context
907929 def format (self , context : Context , line : str ) -> bool :
0 commit comments