@@ -591,12 +591,23 @@ impl Generate for UserCommand {
591591 }
592592}
593593
594+ fn get_local ( state : & State , name : & Identifier ) -> String {
595+ if state. is_top_level ( ) || !name. is_valid_local ( ) {
596+ ""
597+ } else {
598+ "local"
599+ }
600+ . to_string ( )
601+ }
602+
594603impl Generate for DeclCommand {
595604 fn write_default ( & self , state : & mut State , output : & mut Output ) {
605+ let local = get_local ( state, & self . name ) ;
606+
596607 // TODO: default value should not be nil for everything here
597608 // i think?
598609 output. write_lua ( & format ! (
599- "local {} = {}" ,
610+ "{ local} {} = {}" ,
600611 self . name. gen ( state) ,
601612 match & self . ty {
602613 Some ( ty) => match ty {
@@ -730,12 +741,7 @@ impl Generate for DefCommand {
730741
731742 let ( signature, sig_statements) = gen_signature ( state, & self . args ) ;
732743
733- let local = if state. is_top_level ( ) || !self . name . is_valid_local ( ) {
734- ""
735- } else {
736- "local"
737- } ;
738-
744+ let local = get_local ( state, & self . name ) ;
739745 let result = if scope. deferred > 0 {
740746 // TODO: Should probably handle errors in default statements or body
741747 format ! (
@@ -1120,12 +1126,13 @@ impl Generate for VarCommand {
11201126 _ => self . expr . gen ( state) ,
11211127 } ;
11221128
1129+ let local = get_local ( state, & self . name ) ;
11231130 match & self . name {
11241131 Identifier :: Unpacked ( unpacked) => {
11251132 let identifiers: String = identifier_list ( state, & unpacked) ;
1126- output. write_lua ( & format ! ( "local {identifiers} = unpack({expr})" ) )
1133+ output. write_lua ( & format ! ( "{ local} {identifiers} = unpack({expr})" ) )
11271134 }
1128- _ => output. write_lua ( & format ! ( "local {} = {}" , self . name. gen ( state) , expr) ) ,
1135+ _ => output. write_lua ( & format ! ( "{ local} {} = {}" , self . name. gen ( state) , expr) ) ,
11291136 }
11301137 }
11311138}
@@ -1601,7 +1608,7 @@ fn toplevel_ident(s: &mut State, command: &ExCommand) -> Option<String> {
16011608 ExCommand :: ExportCommand ( e) => toplevel_ident ( s, e. command . as_ref ( ) ) ,
16021609 ExCommand :: Heredoc ( here) => Some ( here. name . gen ( s) ) ,
16031610 // This might make sense, but I don't think it allows you to do this?
1604- ExCommand :: Var ( _ ) => None ,
1611+ ExCommand :: Var ( var ) => Some ( var . name . gen ( s ) ) ,
16051612 // These make sense
16061613 ExCommand :: Vim9Script ( _) => None ,
16071614 ExCommand :: Echo ( _) => None ,
0 commit comments