Skip to content

Commit 70edf61

Browse files
authored
bundle lua for upstream PR (#18)
* bundle code via build.rs, much better dx for edit lua files * remove scratch, no longer needed
1 parent d0cc49d commit 70edf61

23 files changed

+1004
-650
lines changed

crates/vim9-gen/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ rmpv = "1.0.0"
2121
[dev-dependencies]
2222
insta = "1.19.0"
2323
pretty_assertions = "1.3.0"
24+
25+
[build-dependencies]
26+
stylua = "0.14.3"
27+
anyhow = "1.0.62"

crates/vim9-gen/build.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
use std::{fmt::Write, fs, path::Path};
2+
3+
fn get_stylua_config() -> stylua_lib::Config {
4+
stylua_lib::Config::new()
5+
.with_indent_type(stylua_lib::IndentType::Spaces)
6+
.with_indent_width(2)
7+
.with_column_width(120)
8+
}
9+
10+
fn main() -> anyhow::Result<()> {
11+
println!("cargo:rerun-if-changed=src/lua/");
12+
13+
let luadir = Path::new(env!("CARGO_MANIFEST_DIR"))
14+
.parent()
15+
.expect("crates_dir")
16+
.parent()
17+
.expect("root_dir")
18+
.join("lua");
19+
20+
if !luadir.is_dir() {
21+
std::fs::create_dir_all(&luadir).expect("to create lua directory");
22+
}
23+
24+
let mut file = String::new();
25+
26+
writeln!(
27+
&mut file,
28+
r#"
29+
-------------------------------------------------------------------------------
30+
-- This file is auto generated by vim9jit. Do not edit by hand.
31+
-- All content is in the source repository.
32+
-- Bugs should be reported to: github.com/tjdevries/vim9jit
33+
--
34+
-- In addition, this file is considered "private" by neovim. You should
35+
-- not expect any of the APIs, functions, etc to be stable. They are subject
36+
-- to change at any time.
37+
-------------------------------------------------------------------------------
38+
39+
"#
40+
)?;
41+
42+
let source_lua = Path::new("src/lua");
43+
let mut entries = Vec::new();
44+
for entry in source_lua.read_dir().unwrap() {
45+
if let Ok(entry) = entry {
46+
entries.push(entry.path());
47+
}
48+
}
49+
50+
// always sort `init` to the top of the list
51+
entries.sort_by(|a, b| {
52+
if a.file_stem().unwrap() == "init" {
53+
return std::cmp::Ordering::Less;
54+
}
55+
56+
if b.file_stem().unwrap() == "init" {
57+
return std::cmp::Ordering::Greater;
58+
}
59+
60+
a.cmp(b)
61+
});
62+
63+
// Iterate over each of the entries and write them to the new file
64+
for entry in entries {
65+
// get entry without the extension
66+
let name = entry
67+
.file_stem()
68+
.expect("file_stem")
69+
.to_str()
70+
.expect("to_str");
71+
72+
let contents = fs::read_to_string(&entry)?;
73+
let contents = contents.replace("require('_vim9script')", "NVIM9");
74+
let contents = contents.replace("require(\"_vim9script\")", "NVIM9");
75+
let contents = contents.replace("require \"_vim9script\"", "NVIM9");
76+
77+
if name == "_" {
78+
writeln!(&mut file, "{}\n", contents)?;
79+
} else if name == "init" {
80+
// init.lua declares our base module and it's guaranteed to be first.
81+
writeln!(&mut file, "local NVIM9 = (function() {} end)()\n", contents)?;
82+
} else {
83+
writeln!(
84+
&mut file,
85+
"NVIM9['{}'] = (function() {} end)()",
86+
name, contents
87+
)?;
88+
}
89+
}
90+
91+
writeln!(&mut file, "")?;
92+
writeln!(&mut file, "return NVIM9")?;
93+
94+
// Format the final lib
95+
let file = stylua_lib::format_code(
96+
&file,
97+
get_stylua_config(),
98+
None,
99+
stylua_lib::OutputVerification::None,
100+
)
101+
.expect(&format!("to format code: {}", file));
102+
103+
let init_lua = luadir.join("_vim9script.lua");
104+
fs::write(init_lua, file)?;
105+
106+
Ok(())
107+
}

crates/vim9-gen/src/lua/_.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
vim.cmd [[
2+
function! _Vim9ScriptFn(name, args) abort
3+
try
4+
let ret = function(a:name, a:args)()
5+
catch
6+
echo "Failed..."
7+
echo a:name
8+
echo a:args
9+
10+
throw v:errmsg
11+
endtry
12+
13+
return [ret, a:args]
14+
endfunction
15+
]]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
return function(path)
2+
return loadfile(path)()
3+
end

crates/vim9-gen/src/lua/bool.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
return function(...)
2+
return require("_vim9script").convert.to_vim_bool(...)
3+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
local M = {}
2+
3+
M.decl_bool = function(val)
4+
if type(val) == "boolean" then
5+
return val
6+
elseif type(val) == "number" then
7+
if val == 0 then
8+
return false
9+
elseif val == 1 then
10+
return true
11+
else
12+
error(string.format("bad number passed to bool declaration: %s", val))
13+
end
14+
end
15+
16+
error(string.format("invalid bool declaration: %s", vim.inspect(val)))
17+
end
18+
19+
M.decl_dict = function(val)
20+
if type(val) == "nil" then
21+
return vim.empty_dict()
22+
elseif type(val) == "table" then
23+
if vim.tbl_isempty(val) then
24+
return vim.empty_dict()
25+
elseif vim.tbl_islist(val) then
26+
error(string.format("Cannot pass list to dictionary? %s", vim.inspect(val)))
27+
else
28+
return val
29+
end
30+
end
31+
32+
error(string.format("invalid dict declaration: %s", vim.inspect(val)))
33+
end
34+
35+
M.to_vim_bool = function(val)
36+
if type(val) == "boolean" then
37+
return val
38+
elseif type(val) == "number" then
39+
return val ~= 0
40+
elseif type(val) == "string" then
41+
return string.len(val) ~= 0
42+
elseif type(val) == "table" then
43+
return not vim.tbl_isempty(val)
44+
elseif val == nil then
45+
return false
46+
end
47+
48+
error("unhandled type: " .. vim.inspect(val))
49+
end
50+
51+
return M

0 commit comments

Comments
 (0)