Skip to content

Commit 9224c10

Browse files
authored
autoload: humble beginnings (#17)
generates separate `*.vim` and `*.lua` file for autoload files
1 parent fe0abef commit 9224c10

31 files changed

+686
-767
lines changed

.rust-toolchain

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[toolchain]
2+
channel = "nightly"

crates/vim9-gen/src/call_expr.rs

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashSet;
22

33
use parser::{CallExpression, Expression, Identifier};
44

5-
use crate::{func_info_for_call, Generate, State};
5+
use crate::{func_info_for_call, Generate, Output, State};
66

77
#[derive(Debug)]
88
pub struct VimFuncMutability {
@@ -24,8 +24,7 @@ fn expr_is_func_mutable(arg: &Expression) -> bool {
2424
Expression::Infix(infix) => expr_is_func_mutable(&infix.left),
2525
Expression::MethodCall(meth) => expr_is_func_mutable(&meth.left),
2626
Expression::Ternary(tern) => {
27-
expr_is_func_mutable(&tern.if_true)
28-
|| expr_is_func_mutable(&tern.if_false)
27+
expr_is_func_mutable(&tern.if_true) || expr_is_func_mutable(&tern.if_false)
2928
}
3029
Expression::Number(_) => false,
3130
Expression::String(_) => false,
@@ -51,10 +50,7 @@ fn expr_is_func_mutable(arg: &Expression) -> bool {
5150
}
5251
}
5352

54-
pub fn mutates(
55-
expr: &CallExpression,
56-
data: &FunctionData,
57-
) -> Option<VimFuncMutability> {
53+
pub fn mutates(expr: &CallExpression, data: &FunctionData) -> Option<VimFuncMutability> {
5854
match data {
5955
// FunctionData::VimFunc { .. } => return None,
6056
_ => {}
@@ -92,10 +88,7 @@ pub fn mutates(
9288
}
9389
}
9490

95-
pub fn args_to_generated_list(
96-
state: &mut State,
97-
args: &[Expression],
98-
) -> String {
91+
pub fn args_to_generated_list(state: &mut State, args: &[Expression]) -> String {
9992
args.iter()
10093
.map(|e| e.gen(state))
10194
.collect::<Vec<String>>()
@@ -151,11 +144,7 @@ pub struct VimFunc {
151144
}
152145

153146
impl VimFunc {
154-
fn inplace(
155-
&self,
156-
mutability: &VimFuncMutability,
157-
state: &mut State,
158-
) -> String {
147+
fn inplace(&self, mutability: &VimFuncMutability, state: &mut State) -> String {
159148
let name = &self.name;
160149
let args = self.args.gen(state);
161150
let replaced = match mutability.returned {
@@ -180,15 +169,11 @@ impl VimFunc {
180169
impl From<&CallExpression> for FunctionData {
181170
fn from(expr: &CallExpression) -> Self {
182171
match expr.expr.as_ref() {
183-
Expression::Identifier(id) => {
184-
ident_to_func_data(expr.clone(), id.clone())
185-
}
186-
Expression::DictAccess(_) | Expression::Index(_) => {
187-
FunctionData::ExprFunc {
188-
caller: *expr.expr.clone(),
189-
args: expr.args.clone(),
190-
}
191-
}
172+
Expression::Identifier(id) => ident_to_func_data(expr.clone(), id.clone()),
173+
Expression::DictAccess(_) | Expression::Index(_) => FunctionData::ExprFunc {
174+
caller: *expr.expr.clone(),
175+
args: expr.args.clone(),
176+
},
192177
_ => todo!("{:#?}", expr),
193178
}
194179
}
@@ -231,11 +216,14 @@ fn ident_to_func_data(call: CallExpression, ident: Identifier) -> FunctionData {
231216
}
232217

233218
impl Generate for Vec<Expression> {
234-
fn gen(&self, state: &mut State) -> String {
235-
self.iter()
236-
.map(|e| e.gen(state))
237-
.collect::<Vec<String>>()
238-
.join(", ")
219+
fn write_default(&self, state: &mut State, output: &mut Output) {
220+
output.write_lua(
221+
&self
222+
.iter()
223+
.map(|e| e.gen(state))
224+
.collect::<Vec<String>>()
225+
.join(", "),
226+
)
239227
}
240228
}
241229

@@ -306,15 +294,10 @@ pub fn generate(call: &CallExpression, state: &mut State) -> String {
306294
}
307295

308296
fn generate_mutable_fn_call(name: &str, args: &str, replace: &str) -> String {
309-
return format!(
310-
"NVIM9.fn_mut('{name}', {{ {args} }}, {{ replace = {replace} }})"
311-
);
297+
return format!("NVIM9.fn_mut('{name}', {{ {args} }}, {{ replace = {replace} }})");
312298
}
313299

314-
pub fn generate_method(
315-
method: &parser::MethodCall,
316-
state: &mut State,
317-
) -> String {
300+
pub fn generate_method(method: &parser::MethodCall, state: &mut State) -> String {
318301
let mut call = *method.right.clone();
319302

320303
// Methods don't always get inserted in the first argument.
@@ -333,9 +316,7 @@ pub fn generate_method(
333316
if expr_is_func_mutable(&method.left) {
334317
let mutability = mutates(&call, &func_data);
335318
if let Some(mutability) = mutability {
336-
if mutability.returned == Some(0)
337-
&& mutability.modified_args.contains(&0)
338-
{
319+
if mutability.returned == Some(0) && mutability.modified_args.contains(&0) {
339320
let name = func_data.name();
340321
let args = call.args.gen(state);
341322
let replace = mutability.returned.unwrap().to_string();

0 commit comments

Comments
 (0)