Skip to content

Commit e1b39c3

Browse files
authored
fixes for first upstream PR (#19)
* fixup: stylua * try updating to newer version of stylua, still can't reproduce * fixup: justin comments * lul, run stylua twice * moved format to separate crate * __NVIM9_MODULE -> M * NVIM9 -> vim9 * special case vim9 * string escaping issues
1 parent f0aca95 commit e1b39c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+678
-525
lines changed

Cargo.lock

Lines changed: 44 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ members = [
2222
"crates/vim9-gen",
2323
"crates/macros",
2424
"crates/vimfuncs",
25+
"crates/format/",
2526
]

crates/format/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "format"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
stylua = "0.15.3"

crates/format/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use stylua_lib::OutputVerification;
2+
3+
pub fn add(left: usize, right: usize) -> usize {
4+
left + right
5+
}
6+
7+
fn get_stylua_config() -> stylua_lib::Config {
8+
stylua_lib::Config::new()
9+
.with_column_width(100)
10+
.with_line_endings(stylua_lib::LineEndings::Unix)
11+
.with_indent_type(stylua_lib::IndentType::Spaces)
12+
.with_indent_width(2)
13+
.with_quote_style(stylua_lib::QuoteStyle::AutoPreferSingle)
14+
.with_call_parentheses(stylua_lib::CallParenType::Always)
15+
.with_collapse_simple_statement(stylua_lib::CollapseSimpleStatement::Never)
16+
}
17+
18+
pub fn lua(code: &str) -> Result<String, (String, String)> {
19+
let conf = get_stylua_config();
20+
let code = match stylua_lib::format_code(code, conf, None, OutputVerification::None) {
21+
Ok(res) => res,
22+
Err(err) => return Err((code.to_string(), err.to_string())),
23+
};
24+
25+
let code = match stylua_lib::format_code(&code, conf, None, OutputVerification::None) {
26+
Ok(res) => res,
27+
Err(err) => return Err((code.to_string(), err.to_string())),
28+
};
29+
30+
Ok(code)
31+
}

crates/vim9-gen/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ edition = "2021"
99
lexer = { path = "../vim9-lexer", package = "vim9-lexer" }
1010
parser = { path = "../vim9-parser", package = "vim9-parser" }
1111
vimfuncs = { path = "../vimfuncs" }
12+
format = { path = "../format" }
1213

1314
anyhow = "1.0.62"
14-
stylua = "0.14.3"
1515

1616
rmp = "0.8.11"
1717
rmpv = "1.0.0"
@@ -23,5 +23,5 @@ insta = "1.19.0"
2323
pretty_assertions = "1.3.0"
2424

2525
[build-dependencies]
26-
stylua = "0.14.3"
2726
anyhow = "1.0.62"
27+
format = { path = "../format" }

crates/vim9-gen/build.rs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
use std::{fmt::Write, fs, path::Path};
22

3-
fn get_stylua_config() -> stylua_lib::Config {
4-
stylua_lib::Config::new()
5-
.with_column_width(100)
6-
.with_line_endings(stylua_lib::LineEndings::Unix)
7-
.with_indent_type(stylua_lib::IndentType::Spaces)
8-
.with_indent_width(2)
9-
.with_quote_style(stylua_lib::QuoteStyle::AutoPreferSingle)
10-
.with_call_parentheses(stylua_lib::CallParenType::Always)
11-
}
12-
133
fn main() -> anyhow::Result<()> {
144
println!("cargo:rerun-if-changed=src/lua/");
155

@@ -73,35 +63,28 @@ fn main() -> anyhow::Result<()> {
7363
.expect("to_str");
7464

7565
let contents = fs::read_to_string(&entry)?;
76-
let contents = contents.replace("require('_vim9script')", "NVIM9");
77-
let contents = contents.replace("require(\"_vim9script\")", "NVIM9");
78-
let contents = contents.replace("require \"_vim9script\"", "NVIM9");
66+
let contents = contents.replace("require('_vim9script')", "vim9");
67+
let contents = contents.replace("require(\"_vim9script\")", "vim9");
68+
let contents = contents.replace("require \"_vim9script\"", "vim9");
7969

8070
if name == "_" {
8171
writeln!(&mut file, "{}\n", contents)?;
8272
} else if name == "init" {
8373
// init.lua declares our base module and it's guaranteed to be first.
84-
writeln!(&mut file, "local NVIM9 = (function() {} end)()\n", contents)?;
74+
writeln!(&mut file, "local vim9 = (function() {} end)()\n", contents)?;
8575
} else {
8676
writeln!(
8777
&mut file,
88-
"NVIM9['{}'] = (function() {} end)()",
78+
"vim9['{}'] = (function() {} end)()",
8979
name, contents
9080
)?;
9181
}
9282
}
9383

9484
writeln!(&mut file, "")?;
95-
writeln!(&mut file, "return NVIM9")?;
96-
97-
// Format the final lib
98-
let file = stylua_lib::format_code(
99-
&file,
100-
get_stylua_config(),
101-
None,
102-
stylua_lib::OutputVerification::None,
103-
)
104-
.expect(&format!("to format code: {}", file));
85+
writeln!(&mut file, "return vim9")?;
86+
87+
let file = format::lua(&file).expect(&format!("to format code: {}", file));
10588

10689
let init_lua = luadir.join("_vim9script.lua");
10790
fs::write(init_lua, file)?;

crates/vim9-gen/src/call_expr.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,18 @@ pub fn generate(call: &CallExpression, state: &mut State) -> String {
248248

249249
match func_data {
250250
FunctionData::ApiFunc { name, args } => {
251-
format!("vim.api['{}']({})", name, args.gen(state))
251+
if crate::ident::is_safe_str(&name) {
252+
format!("vim.api.{}({})", name, args.gen(state))
253+
} else {
254+
format!("vim.api['{}']({})", name, args.gen(state))
255+
}
252256
}
253257
FunctionData::VimFunc(VimFunc { name, args }) => {
254-
format!("NVIM9.fn['{}']({})", name, args.gen(state))
258+
if crate::ident::is_safe_str(&name) {
259+
format!("vim9.fn.{}({})", name, args.gen(state))
260+
} else {
261+
format!("vim9.fn['{}']({})", name, args.gen(state))
262+
}
255263
}
256264
FunctionData::VimFuncRef { name, arglist, .. } => match arglist {
257265
Some(arglist) => {
@@ -294,7 +302,7 @@ pub fn generate(call: &CallExpression, state: &mut State) -> String {
294302
}
295303

296304
fn generate_mutable_fn_call(name: &str, args: &str, replace: &str) -> String {
297-
return format!("NVIM9.fn_mut('{name}', {{ {args} }}, {{ replace = {replace} }})");
305+
return format!("vim9.fn_mut('{name}', {{ {args} }}, {{ replace = {replace} }})");
298306
}
299307

300308
pub fn generate_method(method: &parser::MethodCall, state: &mut State) -> String {

0 commit comments

Comments
 (0)