A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/neovim/neovim/commit/be35d3c5ad501abb029279f8e1812d0e4525284f below:

remove Lua autocommand callbacks when they return true (#1… · neovim/neovim@be35d3c · GitHub

File tree Expand file treeCollapse file tree 3 files changed

+45

-6

lines changed

Filter options

Expand file treeCollapse file tree 3 files changed

+45

-6

lines changed Original file line number Diff line number Diff line change

@@ -2013,6 +2013,7 @@ char_u *getnextac(int c, void *cookie, int indent, bool do_concat)

2013 2013

}

2014 2014 2015 2015

AutoCmd *ac = acp->nextcmd;

2016 +

bool oneshot = ac->once;

2016 2017 2017 2018

if (p_verbose >= 9) {

2018 2019

verbose_enter_scroll();

@@ -2024,7 +2025,13 @@ char_u *getnextac(int c, void *cookie, int indent, bool do_concat)

2024 2025

if (ac->exec.type == CALLABLE_CB) {

2025 2026

typval_T argsin = TV_INITIAL_VALUE;

2026 2027

typval_T rettv = TV_INITIAL_VALUE;

2027 -

callback_call(&ac->exec.callable.cb, 0, &argsin, &rettv);

2028 +

if (callback_call(&ac->exec.callable.cb, 0, &argsin, &rettv)) {

2029 +

if (ac->exec.callable.cb.type == kCallbackLua) {

2030 +

// If a Lua callback returns 'true' then the autocommand is removed

2031 +

oneshot = true;

2032 +

}

2033 +

}

2034 + 2028 2035 2029 2036

// TODO(tjdevries):

2030 2037

//

@@ -2042,7 +2049,7 @@ char_u *getnextac(int c, void *cookie, int indent, bool do_concat)

2042 2049

}

2043 2050 2044 2051

// Remove one-shot ("once") autocmd in anticipation of its execution.

2045 -

if (ac->once) {

2052 +

if (oneshot) {

2046 2053

aucmd_del(ac);

2047 2054

}

2048 2055

autocmd_nested = ac->nested;

Original file line number Diff line number Diff line change

@@ -7730,6 +7730,7 @@ bool callback_call(Callback *const callback, const int argcount_in, typval_T *co

7730 7730

partial_T *partial;

7731 7731

char_u *name;

7732 7732

Array args = ARRAY_DICT_INIT;

7733 +

Object rv;

7733 7734

switch (callback->type) {

7734 7735

case kCallbackFuncref:

7735 7736

name = callback->data.funcref;

@@ -7742,10 +7743,13 @@ bool callback_call(Callback *const callback, const int argcount_in, typval_T *co

7742 7743

break;

7743 7744 7744 7745

case kCallbackLua:

7745 -

nlua_call_ref(callback->data.luaref, NULL, args, false, NULL);

7746 - 7747 -

return false;

7748 -

break;

7746 +

rv = nlua_call_ref(callback->data.luaref, NULL, args, true, NULL);

7747 +

switch (rv.type) {

7748 +

case kObjectTypeBoolean:

7749 +

return rv.data.boolean;

7750 +

default:

7751 +

return false;

7752 +

}

7749 7753 7750 7754

case kCallbackNone:

7751 7755

return false;

Original file line number Diff line number Diff line change

@@ -154,6 +154,34 @@ describe('autocmd api', function()

154 154

eq(1, #aus, aus)

155 155

end)

156 156

end)

157 + 158 +

it('removes an autocommand if the callback returns true', function()

159 +

meths.set_var("some_condition", false)

160 + 161 +

exec_lua [[

162 +

vim.api.nvim_create_autocmd("User", {

163 +

pattern = "Test",

164 +

desc = "A test autocommand",

165 +

callback = function()

166 +

return vim.g.some_condition

167 +

end,

168 +

})

169 +

]]

170 + 171 +

meths.do_autocmd("User", {pattern = "Test"})

172 +

eq({{

173 +

buflocal = false,

174 +

command = 'A test autocommand',

175 +

desc = 'A test autocommand',

176 +

event = 'User',

177 +

id = 1,

178 +

once = false,

179 +

pattern = 'Test',

180 +

}}, meths.get_autocmds({event = "User", pattern = "Test"}))

181 +

meths.set_var("some_condition", true)

182 +

meths.do_autocmd("User", {pattern = "Test"})

183 +

eq({}, meths.get_autocmds({event = "User", pattern = "Test"}))

184 +

end)

157 185

end)

158 186 159 187

describe('nvim_get_autocmds', function()

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4