Skip to content

Commit d5dc238

Browse files
committed
methods work on weird params
1 parent 63afba6 commit d5dc238

6 files changed

Lines changed: 78 additions & 24 deletions

File tree

crates/vim9-gen/src/call_expr.rs

Lines changed: 14 additions & 6 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::{Generate, State};
5+
use crate::{func_info_for_call, Generate, State};
66

77
#[derive(Debug)]
88
pub struct VimFuncMutability {
@@ -316,7 +316,18 @@ pub fn generate_method(
316316
state: &mut State,
317317
) -> String {
318318
let mut call = *method.right.clone();
319-
call.args.insert(0, *method.left.clone());
319+
320+
// Methods don't always get inserted in the first argument.
321+
// Check if we have func info and use the method arg if applicable
322+
let idx = match func_info_for_call(&call) {
323+
Some(info) => info.method_arg.unwrap_or(1) - 1,
324+
None => 0,
325+
};
326+
327+
match idx == call.args.len() {
328+
true => call.args.push(*method.left.clone()),
329+
false => call.args.insert(idx, *method.left.clone()),
330+
}
320331

321332
let func_data: FunctionData = (&call).into();
322333
if expr_is_func_mutable(&method.left) {
@@ -333,8 +344,5 @@ pub fn generate_method(
333344
}
334345
}
335346

336-
let mut expr = method.right.clone();
337-
expr.args.insert(0, *method.left.clone());
338-
339-
expr.gen(state)
347+
call.gen(state)
340348
}

crates/vim9-gen/src/lib.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -896,28 +896,30 @@ fn vim_to_type(ret: &vimfuncs::FuncReturnType) -> Type {
896896
}
897897
}
898898

899+
pub fn func_info_for_call(c: &CallExpression) -> Option<vimfuncs::FuncInfo> {
900+
match c.name() {
901+
Some(ident) => {
902+
match ident {
903+
// TODO: Use our very cool new generated stuff here
904+
Identifier::Raw(raw) => {
905+
return vimfuncs::get_func_info(&raw.name);
906+
}
907+
_ => None,
908+
}
909+
}
910+
_ => None,
911+
}
912+
}
913+
899914
fn guess_type_of_expr(state: &State, expr: &Expression) -> Type {
900915
match expr {
901916
Expression::Number(_) => Type::Number,
902917
Expression::String(_) => Type::String,
903918
Expression::Boolean(_) => Type::Bool,
904-
Expression::Call(c) => {
905-
match c.name() {
906-
Some(ident) => {
907-
match ident {
908-
// TODO: Use our very cool new generated stuff here
909-
Identifier::Raw(raw) => {
910-
match vimfuncs::get_func_info(&raw.name) {
911-
Some(info) => vim_to_type(&info.return_type),
912-
_ => Type::Any,
913-
}
914-
}
915-
_ => Type::Any,
916-
}
917-
}
918-
_ => Type::Any,
919-
}
920-
}
919+
Expression::Call(c) => match func_info_for_call(&c) {
920+
Some(info) => vim_to_type(&info.return_type),
921+
_ => Type::Any,
922+
},
921923
Expression::Infix(infix) => {
922924
if infix.operator.is_comparison() {
923925
return Type::Bool;
@@ -936,7 +938,7 @@ fn guess_type_of_expr(state: &State, expr: &Expression) -> Type {
936938
&& right_ty == Type::Number
937939
&& infix.operator.is_math()
938940
{
939-
return dbg!(Type::Number);
941+
return Type::Number;
940942
}
941943

942944
Type::Any
@@ -1580,6 +1582,10 @@ mod test {
15801582
busted!(busted_methods_1, "../testdata/busted/methods_1.vim");
15811583
busted!(busted_methods_2, "../testdata/busted/methods_2.vim");
15821584
busted!(busted_megamethods, "../testdata/busted/megamethods.vim");
1585+
busted!(
1586+
busted_methods_shifted,
1587+
"../testdata/busted/methods_shifted.vim"
1588+
);
15831589

15841590
snapshot!(test_expr, "../testdata/snapshots/expr.vim");
15851591
snapshot!(test_if, "../testdata/snapshots/if.vim");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def Test_settabvar_method()
2+
call settabvar(1, "testing", 0)
3+
assert_equal(0, t:testing)
4+
5+
25->settabvar(1, "testing")
6+
assert_equal(25, t:testing)
7+
enddef
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
local NVIM9 = require("vim9script")
2+
local __VIM9_MODULE = {}
3+
describe("filename", function()
4+
local Test_settabvar_method = nil
5+
6+
it("Test_settabvar_method", function()
7+
-- Set errors to empty
8+
vim.v.errors = {}
9+
10+
-- Actual test
11+
NVIM9.fn["settabvar"](1, "testing", 0)
12+
NVIM9.fn["assert_equal"](0, vim.t["testing"])
13+
14+
NVIM9.fn["settabvar"](1, "testing", 25)
15+
NVIM9.fn["assert_equal"](25, vim.t["testing"])
16+
17+
-- Assert that errors is still empty
18+
assert.are.same({}, vim.v.errors)
19+
end)
20+
end)
21+
return __VIM9_MODULE

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@ local __VIM9_MODULE = {}
99

1010
local x = 1 + 2
1111
print(x)
12+
13+
local literal = NVIM9.fn["charcol"](".") - 1
14+
print(literal)
15+
16+
local func_call = NVIM9.ops["Minus"](MyFunc(), 1)
17+
print(func_call)
1218
return __VIM9_MODULE
1319

crates/vim9-gen/testdata/snapshots/expr.vim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ vim9script
22

33
var x = 1 + 2
44
echo x
5+
6+
var literal = charcol('.') - 1
7+
echo literal
8+
9+
var func_call = MyFunc() - 1
10+
echo func_call

0 commit comments

Comments
 (0)