mirror of
https://github.com/neovim/neovim.git
synced 2024-12-20 03:05:11 -07:00
feat(api): support getting abbreviations (#26868)
This commit is contained in:
parent
d3a8e9217f
commit
98a4ed0a11
@ -159,7 +159,7 @@ The following new APIs and features were added.
|
|||||||
• |nvim_win_text_height()| computes the number of screen lines occupied
|
• |nvim_win_text_height()| computes the number of screen lines occupied
|
||||||
by a range of text in a given window.
|
by a range of text in a given window.
|
||||||
|
|
||||||
• |nvim_set_keymap()| and |nvim_del_keymap()| now support abbreviations.
|
• Mapping APIs now support abbreviations when mode short-name has suffix "a".
|
||||||
|
|
||||||
• Better cmdline completion for string option value. |complete-set-option|
|
• Better cmdline completion for string option value. |complete-set-option|
|
||||||
|
|
||||||
|
@ -36,6 +36,7 @@
|
|||||||
#include "nvim/lua/executor.h"
|
#include "nvim/lua/executor.h"
|
||||||
#include "nvim/macros_defs.h"
|
#include "nvim/macros_defs.h"
|
||||||
#include "nvim/mapping.h"
|
#include "nvim/mapping.h"
|
||||||
|
#include "nvim/mapping_defs.h"
|
||||||
#include "nvim/mbyte.h"
|
#include "nvim/mbyte.h"
|
||||||
#include "nvim/mbyte_defs.h"
|
#include "nvim/mbyte_defs.h"
|
||||||
#include "nvim/memory.h"
|
#include "nvim/memory.h"
|
||||||
@ -142,20 +143,6 @@ mapblock_T *get_buf_maphash_list(int state, int c)
|
|||||||
return curbuf->b_maphash[MAP_HASH(state, c)];
|
return curbuf->b_maphash[MAP_HASH(state, c)];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieve the mapblock at the index either globally or for a certain buffer
|
|
||||||
///
|
|
||||||
/// @param index The index in the maphash[]
|
|
||||||
/// @param buf The buffer to get the maphash from. NULL for global
|
|
||||||
mapblock_T *get_maphash(int index, buf_T *buf)
|
|
||||||
FUNC_ATTR_PURE
|
|
||||||
{
|
|
||||||
if (index >= MAX_MAPHASH) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (buf == NULL) ? maphash[index] : buf->b_maphash[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Delete one entry from the abbrlist or maphash[].
|
/// Delete one entry from the abbrlist or maphash[].
|
||||||
/// "mpp" is a pointer to the m_next field of the PREVIOUS entry!
|
/// "mpp" is a pointer to the m_next field of the PREVIOUS entry!
|
||||||
static void mapblock_free(mapblock_T **mpp)
|
static void mapblock_free(mapblock_T **mpp)
|
||||||
@ -2820,16 +2807,23 @@ ArrayOf(Dictionary) keymap_array(String mode, buf_T *buf)
|
|||||||
{
|
{
|
||||||
Array mappings = ARRAY_DICT_INIT;
|
Array mappings = ARRAY_DICT_INIT;
|
||||||
|
|
||||||
// Convert the string mode to the integer mode
|
char *p = mode.size > 0 ? mode.data : "m";
|
||||||
// that is stored within each mapblock
|
bool forceit = *p == '!';
|
||||||
char *p = mode.data;
|
// Convert the string mode to the integer mode stored within each mapblock.
|
||||||
int int_mode = get_map_mode(&p, 0);
|
int int_mode = get_map_mode(&p, forceit);
|
||||||
|
if (forceit) {
|
||||||
|
assert(p == mode.data);
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
bool is_abbrev = (int_mode & (MODE_INSERT | MODE_CMDLINE)) != 0 && *p == 'a';
|
||||||
|
|
||||||
// Determine the desired buffer value
|
// Determine the desired buffer value
|
||||||
int buffer_value = (buf == NULL) ? 0 : buf->handle;
|
int buffer_value = (buf == NULL) ? 0 : buf->handle;
|
||||||
|
|
||||||
for (int i = 0; i < MAX_MAPHASH; i++) {
|
for (int i = 0; i < (is_abbrev ? 1 : MAX_MAPHASH); i++) {
|
||||||
for (const mapblock_T *current_maphash = get_maphash(i, buf);
|
for (const mapblock_T *current_maphash = is_abbrev
|
||||||
|
? (buf ? buf->b_first_abbr : first_abbr)
|
||||||
|
: (buf ? buf->b_maphash[i] : maphash[i]);
|
||||||
current_maphash;
|
current_maphash;
|
||||||
current_maphash = current_maphash->m_next) {
|
current_maphash = current_maphash->m_next) {
|
||||||
if (current_maphash->m_simplified) {
|
if (current_maphash->m_simplified) {
|
||||||
@ -2839,7 +2833,7 @@ ArrayOf(Dictionary) keymap_array(String mode, buf_T *buf)
|
|||||||
if (int_mode & current_maphash->m_mode) {
|
if (int_mode & current_maphash->m_mode) {
|
||||||
ADD(mappings,
|
ADD(mappings,
|
||||||
DICTIONARY_OBJ(mapblock_fill_dict(current_maphash, NULL,
|
DICTIONARY_OBJ(mapblock_fill_dict(current_maphash, NULL,
|
||||||
buffer_value, false, false)));
|
buffer_value, is_abbrev, false)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -460,6 +460,57 @@ describe('nvim_get_keymap', function()
|
|||||||
desc = 'map description',
|
desc = 'map description',
|
||||||
}, api.nvim_get_keymap('n')[1])
|
}, api.nvim_get_keymap('n')[1])
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('can get abbreviations', function()
|
||||||
|
command('inoreabbr foo bar')
|
||||||
|
command('cnoreabbr <buffer> foo baz')
|
||||||
|
|
||||||
|
local mapargs_i = {
|
||||||
|
abbr = 1,
|
||||||
|
buffer = 0,
|
||||||
|
expr = 0,
|
||||||
|
lhs = 'foo',
|
||||||
|
lhsraw = 'foo',
|
||||||
|
lnum = 0,
|
||||||
|
mode = 'i',
|
||||||
|
mode_bits = 0x10,
|
||||||
|
noremap = 1,
|
||||||
|
nowait = 0,
|
||||||
|
rhs = 'bar',
|
||||||
|
script = 0,
|
||||||
|
scriptversion = 1,
|
||||||
|
sid = 0,
|
||||||
|
silent = 0,
|
||||||
|
}
|
||||||
|
local mapargs_c = {
|
||||||
|
abbr = 1,
|
||||||
|
buffer = 1,
|
||||||
|
expr = 0,
|
||||||
|
lhs = 'foo',
|
||||||
|
lhsraw = 'foo',
|
||||||
|
lnum = 0,
|
||||||
|
mode = 'c',
|
||||||
|
mode_bits = 0x08,
|
||||||
|
noremap = 1,
|
||||||
|
nowait = 0,
|
||||||
|
rhs = 'baz',
|
||||||
|
script = 0,
|
||||||
|
scriptversion = 1,
|
||||||
|
sid = 0,
|
||||||
|
silent = 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
local curbuf = api.nvim_get_current_buf()
|
||||||
|
|
||||||
|
eq({ mapargs_i }, api.nvim_get_keymap('ia'))
|
||||||
|
eq({}, api.nvim_buf_get_keymap(curbuf, 'ia'))
|
||||||
|
|
||||||
|
eq({}, api.nvim_get_keymap('ca'))
|
||||||
|
eq({ mapargs_c }, api.nvim_buf_get_keymap(curbuf, 'ca'))
|
||||||
|
|
||||||
|
eq({ mapargs_i }, api.nvim_get_keymap('!a'))
|
||||||
|
eq({ mapargs_c }, api.nvim_buf_get_keymap(curbuf, '!a'))
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('nvim_set_keymap, nvim_del_keymap', function()
|
describe('nvim_set_keymap, nvim_del_keymap', function()
|
||||||
|
Loading…
Reference in New Issue
Block a user