fix(api): do not switch win/buf if getting option in current win/buf (#19383)

This commit is contained in:
zeertzjq
2022-07-16 09:31:05 +08:00
committed by GitHub
parent 33da7d83e8
commit 73526abbbd
2 changed files with 31 additions and 11 deletions

View File

@@ -504,6 +504,7 @@ static int access_option_value(char *key, long *numval, char **stringval, int op
static int access_option_value_for(char *key, long *numval, char **stringval, int opt_flags, static int access_option_value_for(char *key, long *numval, char **stringval, int opt_flags,
int opt_type, void *from, bool get, Error *err) int opt_type, void *from, bool get, Error *err)
{ {
bool need_switch = false;
switchwin_T switchwin; switchwin_T switchwin;
aco_save_T aco; aco_save_T aco;
int result = 0; int result = 0;
@@ -511,24 +512,32 @@ static int access_option_value_for(char *key, long *numval, char **stringval, in
try_start(); try_start();
switch (opt_type) { switch (opt_type) {
case SREQ_WIN: case SREQ_WIN:
if (switch_win_noblock(&switchwin, (win_T *)from, win_find_tabpage((win_T *)from), true) need_switch = (win_T *)from != curwin;
== FAIL) { if (need_switch) {
restore_win_noblock(&switchwin, true); if (switch_win_noblock(&switchwin, (win_T *)from, win_find_tabpage((win_T *)from), true)
if (try_end(err)) { == FAIL) {
restore_win_noblock(&switchwin, true);
if (try_end(err)) {
return result;
}
api_set_error(err, kErrorTypeException, "Problem while switching windows");
return result; return result;
} }
api_set_error(err,
kErrorTypeException,
"Problem while switching windows");
return result;
} }
result = access_option_value(key, numval, stringval, opt_flags, get, err); result = access_option_value(key, numval, stringval, opt_flags, get, err);
restore_win_noblock(&switchwin, true); if (need_switch) {
restore_win_noblock(&switchwin, true);
}
break; break;
case SREQ_BUF: case SREQ_BUF:
aucmd_prepbuf(&aco, (buf_T *)from); need_switch = (buf_T *)from != curbuf;
if (need_switch) {
aucmd_prepbuf(&aco, (buf_T *)from);
}
result = access_option_value(key, numval, stringval, opt_flags, get, err); result = access_option_value(key, numval, stringval, opt_flags, get, err);
aucmd_restbuf(&aco); if (need_switch) {
aucmd_restbuf(&aco);
}
break; break;
case SREQ_GLOBAL: case SREQ_GLOBAL:
result = access_option_value(key, numval, stringval, opt_flags, get, err); result = access_option_value(key, numval, stringval, opt_flags, get, err);

View File

@@ -1469,6 +1469,17 @@ describe('API', function()
nvim('win_set_option', win, 'number', true) nvim('win_set_option', win, 'number', true)
eq(true, nvim('get_option_value', 'number', {win = win})) eq(true, nvim('get_option_value', 'number', {win = win}))
end) end)
it('getting current buffer option does not adjust cursor #19381', function()
nvim('command', 'new')
local buf = nvim('get_current_buf').id
local win = nvim('get_current_win').id
insert('some text')
feed('0v$')
eq({1, 9}, nvim('win_get_cursor', win))
nvim('get_option_value', 'filetype', {buf = buf})
eq({1, 9}, nvim('win_get_cursor', win))
end)
end) end)
describe('nvim_{get,set}_current_buf, nvim_list_bufs', function() describe('nvim_{get,set}_current_buf, nvim_list_bufs', function()