@@ -163,6 +163,7 @@ impl Generate for ExCommand {
163163 ExCommand :: Break ( b) => b. gen ( state) ,
164164 ExCommand :: Continue ( c) => c. gen ( state) ,
165165 ExCommand :: Defer ( defer) => defer. gen ( state) ,
166+ ExCommand :: Execute ( exec) => exec. gen ( state) ,
166167 _ => todo ! ( "Have not yet handled: {:?}" , self ) ,
167168 }
168169 }
@@ -358,7 +359,8 @@ impl Generate for ExportCommand {
358359
359360impl Generate for SharedCommand {
360361 fn gen ( & self , _: & mut State ) -> String {
361- format ! ( "vim.cmd [[ {} ]]" , self . contents. trim( ) )
362+ // Temp
363+ format ! ( "pcall(vim.cmd, [[ {} ]])" , self . contents. trim( ) )
362364 }
363365}
364366
@@ -379,6 +381,15 @@ impl Generate for UserCommand {
379381 state. command_depth += 1 ;
380382
381383 let complete = to_str_or_nil ( & self . command_complete ) ;
384+ let nargs = match & self . command_nargs {
385+ Some ( nargs) => match nargs. as_str ( ) {
386+ "0" => "0" . to_string ( ) ,
387+ "1" => "1" . to_string ( ) ,
388+ _ => format ! ( "'{}'" , nargs) ,
389+ } ,
390+ None => "nil" . to_string ( ) ,
391+ } ;
392+
382393 let result = format ! (
383394 r#"
384395 vim.api.nvim_create_user_command(
@@ -387,15 +398,14 @@ impl Generate for UserCommand {
387398 {}
388399 end,
389400 {{
390- nargs = '{}',
391401 bang = {},
402+ nargs = {nargs},
392403 complete = {complete},
393404 }}
394405 )"# ,
395406 self . name,
396407 make_user_command_arg( state) ,
397408 self . command. gen ( state) ,
398- self . command_nargs. clone( ) . unwrap_or( "0" . to_string( ) ) ,
399409 self . command_bang,
400410 ) ;
401411
@@ -658,7 +668,7 @@ impl Generate for AssignStatement {
658668 let left = match & self . left {
659669 Expression :: Index ( idx) => {
660670 format ! (
661- "{}[{} + 1 ]" ,
671+ "{}[NVIM9.index_expr({}) ]" ,
662672 idx. container. gen ( state) ,
663673 match idx. index. as_ref( ) {
664674 IndexType :: Item ( item) => item. gen ( state) ,
@@ -795,6 +805,10 @@ impl Generate for VarCommand {
795805 inner : InnerType :: Bool ,
796806 ..
797807 } ) => format ! ( "NVIM9.convert.decl_bool({})" , self . expr. gen ( state) ) ,
808+ Some ( Type {
809+ inner : InnerType :: Dict { .. } ,
810+ ..
811+ } ) => format ! ( "NVIM9.convert.decl_dict({})" , self . expr. gen ( state) ) ,
798812 _ => self . expr . gen ( state) ,
799813 } ;
800814
@@ -839,7 +853,13 @@ impl Generate for ScopedIdentifier {
839853
840854impl Generate for RawIdentifier {
841855 fn gen ( & self , _: & mut State ) -> String {
842- self . name . clone ( )
856+ // There are a variety of keywords we have to make sure
857+ // that we don't use when creating identifiers
858+ match self . name . as_str ( ) {
859+ "end" => "__end__" ,
860+ _ => & self . name ,
861+ }
862+ . to_string ( )
843863 }
844864}
845865
@@ -1059,7 +1079,9 @@ impl Generate for Literal {
10591079impl Generate for VimKey {
10601080 fn gen ( & self , state : & mut State ) -> String {
10611081 match self {
1062- VimKey :: Literal ( literal) => literal. token . text . to_string ( ) ,
1082+ // TODO: For literals, we could simplify this to do
1083+ // direct key access if it doesn't contain any illegal characters
1084+ VimKey :: Literal ( literal) => format ! ( "['{}']" , literal. token. text) ,
10631085 VimKey :: Expression ( expr) => format ! ( "[{}]" , expr. gen ( state) ) ,
10641086 }
10651087 }
@@ -1123,10 +1145,10 @@ impl Generate for VimString {
11231145 format ! ( "\" {}\" " , s)
11241146 }
11251147 VimString :: Interpolated ( interp) => {
1126- format ! ( "string.format('{}' )" , interp)
1148+ format ! ( "string.format([=[{}]=] )" , interp)
11271149 }
11281150 VimString :: InterpolatedLit ( interp) => {
1129- format ! ( "string.format(' {}')" , interp)
1151+ format ! ( "string.format([=[ {}']=] )" , interp)
11301152 }
11311153 VimString :: EnvironmentVariable ( env) => {
11321154 format ! ( "vim.env['{}']" , env)
@@ -1274,13 +1296,16 @@ pub fn eval(program: parser::Program, is_test: bool) -> String {
12741296 output
12751297}
12761298
1277- pub fn generate ( contents : & str , is_test : bool ) -> String {
1299+ pub fn generate (
1300+ contents : & str ,
1301+ is_test : bool ,
1302+ ) -> Result < String , ( String , String ) > {
12781303 let lexer = Lexer :: new ( contents) ;
12791304 let parser = new_parser ( & lexer) ;
12801305 let program = parser. parse_program ( ) ;
12811306
12821307 let result = eval ( program, is_test) ;
1283- println ! ( "{}" , result) ;
1308+ // println!("{}", result);
12841309
12851310 let config = stylua_lib:: Config :: new ( )
12861311 . with_indent_type ( stylua_lib:: IndentType :: Spaces )
@@ -1293,8 +1318,8 @@ pub fn generate(contents: &str, is_test: bool) -> String {
12931318 None ,
12941319 stylua_lib:: OutputVerification :: None ,
12951320 ) {
1296- Ok ( res) => res,
1297- Err ( err) => format ! ( "{} \n \n {}" , result, err) ,
1321+ Ok ( res) => Ok ( res) ,
1322+ Err ( err) => Err ( ( result, err. to_string ( ) ) ) ,
12981323 }
12991324}
13001325
@@ -1315,7 +1340,7 @@ mod test {
13151340 let mut settings = insta:: Settings :: clone_current( ) ;
13161341 settings. set_snapshot_path( "../testdata/output/" ) ;
13171342 settings. bind( || {
1318- insta:: assert_snapshot!( generate( contents, false ) ) ;
1343+ insta:: assert_snapshot!( generate( contents, false ) . unwrap ( ) ) ;
13191344 } ) ;
13201345 }
13211346 } ;
@@ -1326,7 +1351,7 @@ mod test {
13261351 #[ test]
13271352 fn $name( ) {
13281353 let vim_contents = include_str!( $path) ;
1329- let lua_contents = generate( vim_contents, true ) ;
1354+ let lua_contents = generate( vim_contents, true ) . unwrap ( ) ;
13301355
13311356 let filepath = concat!(
13321357 env!( "CARGO_MANIFEST_DIR" ) ,
@@ -1377,7 +1402,7 @@ mod test {
13771402 export var x = MyCoolFunc() + 1
13781403 "# ;
13791404
1380- let generated = generate ( contents, false ) ;
1405+ let generated = generate ( contents, false ) . unwrap ( ) ;
13811406 let eval = exec_lua ( & generated) . unwrap ( ) ;
13821407 assert_eq ! ( eval[ "x" ] , 6 . into( ) ) ;
13831408 }
@@ -1390,7 +1415,7 @@ mod test {
13901415 export var x = len("hello")
13911416 "# ;
13921417
1393- let generated = generate ( contents, false ) ;
1418+ let generated = generate ( contents, false ) . unwrap ( ) ;
13941419 let eval = exec_lua ( & generated) . unwrap ( ) ;
13951420 assert_eq ! ( eval[ "x" ] , 5 . into( ) ) ;
13961421 }
@@ -1412,7 +1437,7 @@ mod test {
14121437 export var x = len(nvim_get_autocmds({group: "matchparen"}))
14131438 "# ;
14141439
1415- let generated = generate ( contents, false ) ;
1440+ let generated = generate ( contents, false ) . unwrap ( ) ;
14161441 let eval = exec_lua ( & generated) . unwrap ( ) ;
14171442 assert_eq ! ( eval[ "x" ] , 4 . into( ) ) ;
14181443 }
0 commit comments