Skip to content

Commit f0aca95

Browse files
committed
fixup: make sure to use the hoisted vars and decls
1 parent 954d878 commit f0aca95

File tree

6 files changed

+35
-21
lines changed

6 files changed

+35
-21
lines changed

crates/vim9-gen/src/lib.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
594603
impl 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,

crates/vim9-gen/testdata/output/busted_indexing.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
----------------------------------------
55

66
local NVIM9 = require('_vim9script')
7+
local l = nil
78
describe('filename', function()
89
-- vim9script
910

10-
local l = { 1, 2, 3 }
11+
l = { 1, 2, 3 }
1112

1213
it('Test_can_index', function()
1314
-- Set errors to empty

crates/vim9-gen/testdata/output/vim9_gen__test__autocmd.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: crates/vim9-gen/src/lib.rs
3-
assertion_line: 1836
3+
assertion_line: 1843
44
expression: "generate(contents, ParserOpts { mode: ParserMode::Standalone }).unwrap().lua"
55
---
66
----------------------------------------
@@ -10,6 +10,7 @@ expression: "generate(contents, ParserOpts { mode: ParserMode::Standalone }).unw
1010

1111
local NVIM9 = require('_vim9script')
1212
local __VIM9_MODULE = {}
13+
local x = nil
1314
-- vim9script
1415

1516
vim.api.nvim_create_augroup('matchparen', { clear = false })
@@ -28,6 +29,6 @@ vim.api.nvim_create_autocmd({ 'WinLeave' }, {
2829
end,
2930
})
3031

31-
local x = NVIM9.fn['len'](vim.api['nvim_get_autocmds']({ ['group'] = 'matchparen' }))
32+
x = NVIM9.fn['len'](vim.api['nvim_get_autocmds']({ ['group'] = 'matchparen' }))
3233
return __VIM9_MODULE
3334

crates/vim9-gen/testdata/output/vim9_gen__test__call.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: crates/vim9-gen/src/lib.rs
3-
assertion_line: 1835
3+
assertion_line: 1842
44
expression: "generate(contents, ParserOpts { mode: ParserMode::Standalone }).unwrap().lua"
55
---
66
----------------------------------------
@@ -11,12 +11,13 @@ expression: "generate(contents, ParserOpts { mode: ParserMode::Standalone }).unw
1111
local NVIM9 = require('_vim9script')
1212
local __VIM9_MODULE = {}
1313
local MyCoolFunc = nil
14+
local x = nil
1415
-- vim9script
1516

1617
MyCoolFunc = function()
1718
return 5
1819
end
1920

20-
local x = NVIM9.ops['Plus'](MyCoolFunc(), 1)
21+
x = NVIM9.ops['Plus'](MyCoolFunc(), 1)
2122
return __VIM9_MODULE
2223

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: crates/vim9-gen/src/lib.rs
3-
assertion_line: 1832
3+
assertion_line: 1839
44
expression: "generate(contents, ParserOpts { mode: ParserMode::Standalone }).unwrap().lua"
55
---
66
----------------------------------------
@@ -10,15 +10,18 @@ expression: "generate(contents, ParserOpts { mode: ParserMode::Standalone }).unw
1010

1111
local NVIM9 = require('_vim9script')
1212
local __VIM9_MODULE = {}
13+
local x = nil
14+
local literal = nil
15+
local func_call = nil
1316
-- vim9script
1417

15-
local x = 1 + 2
18+
x = 1 + 2
1619
print(x)
1720

18-
local literal = NVIM9.fn['charcol']('.') - 1
21+
literal = NVIM9.fn['charcol']('.') - 1
1922
print(literal)
2023

21-
local func_call = NVIM9.ops['Minus'](MyFunc(), 1)
24+
func_call = NVIM9.ops['Minus'](MyFunc(), 1)
2225
print(func_call)
2326
return __VIM9_MODULE
2427

crates/vim9-gen/testdata/output/vim9_gen__test__if.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: crates/vim9-gen/src/lib.rs
3-
assertion_line: 1833
3+
assertion_line: 1840
44
expression: "generate(contents, ParserOpts { mode: ParserMode::Standalone }).unwrap().lua"
55
---
66
----------------------------------------
@@ -10,9 +10,10 @@ expression: "generate(contents, ParserOpts { mode: ParserMode::Standalone }).unw
1010

1111
local NVIM9 = require('_vim9script')
1212
local __VIM9_MODULE = {}
13+
local x = nil
1314
-- vim9script
1415

15-
local x = true
16+
x = true
1617
if NVIM9.bool(x) then
1718
print(x)
1819
end

0 commit comments

Comments
 (0)