API: nvim_get_commands()

This commit is contained in:
Nimit Bhardwaj 2018-02-24 15:44:51 +05:30 committed by Justin M. Keyes
parent 273d2cd5d5
commit 25b6304840
6 changed files with 274 additions and 62 deletions

View File

@ -24,6 +24,7 @@
#include "nvim/syntax.h"
#include "nvim/window.h"
#include "nvim/undo.h"
#include "nvim/ex_docmd.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "api/buffer.c.generated.h"
@ -478,6 +479,26 @@ ArrayOf(Dictionary) nvim_buf_get_keymap(Buffer buffer, String mode, Error *err)
return keymap_array(mode, buf);
}
/// Gets a list of dictionaries describing buffer-local commands.
/// The "buffer" key in the returned dictionary reflects the buffer
/// handle where the command is present.
///
/// @param buffer Buffer handle.
/// @param opts Optional parameters, currently always
/// @param[out] err Error details, if any.
///
/// @returns Array of dictionaries describing commands.
ArrayOf(Dictionary) nvim_buf_get_commands(Buffer buffer, Dictionary opts,
Error *err)
FUNC_API_SINCE(4)
{
buf_T *buf = find_buffer_by_handle(buffer, err);
if (!buf) {
return (Array)ARRAY_DICT_INIT;
}
return commands_array(buf);
}
/// Sets a buffer-scoped (b:) variable
///
/// @param buffer Buffer handle

View File

@ -701,7 +701,7 @@ String cchar_to_string(char c)
String cstr_to_string(const char *str)
{
if (str == NULL) {
return (String) STRING_INIT;
return (String) STRING_INIT;
}
size_t len = strlen(str);
@ -1149,7 +1149,7 @@ void api_set_error(Error *err, ErrorType errType, const char *format, ...)
///
/// @param mode The abbreviation for the mode
/// @param buf The buffer to get the mapping array. NULL for global
/// @returns An array of maparg() like dictionaries describing mappings
/// @returns Array of maparg()-like dictionaries describing mappings
ArrayOf(Dictionary) keymap_array(String mode, buf_T *buf)
{
Array mappings = ARRAY_DICT_INIT;

View File

@ -959,6 +959,19 @@ ArrayOf(Dictionary) nvim_get_keymap(String mode)
return keymap_array(mode, NULL);
}
/// Gets a list of dictionaries describing global(non-buffer) commands.
///
/// @param opts Holds the API Metadata describing what type of commands
/// are needed.
/// @param[out] err Error details, if any.
///
/// @returns Array of dictionaries describing commands.
ArrayOf(Dictionary) nvim_get_commands(Dictionary opts, Error *err)
FUNC_API_SINCE(4)
{
return commands_array(NULL);
}
/// Returns a 2-tuple (Array), where item 0 is the current channel id and item
/// 1 is the |api-metadata| map (Dictionary).
///

View File

@ -73,6 +73,7 @@
#include "nvim/shada.h"
#include "nvim/lua/executor.h"
#include "nvim/globals.h"
#include "nvim/api/private/helpers.h"
static int quitmore = 0;
static int ex_pressedreturn = FALSE;
@ -4862,51 +4863,56 @@ static struct {
* List of names for completion for ":command" with the EXPAND_ flag.
* Must be alphabetical for completion.
*/
static struct {
int expand;
char *name;
} command_complete[] =
static const char *command_complete[] =
{
{ EXPAND_AUGROUP, "augroup" },
{ EXPAND_BEHAVE, "behave" },
{ EXPAND_BUFFERS, "buffer" },
{ EXPAND_CHECKHEALTH, "checkhealth" },
{ EXPAND_COLORS, "color" },
{ EXPAND_COMMANDS, "command" },
{ EXPAND_COMPILER, "compiler" },
{ EXPAND_CSCOPE, "cscope" },
{ EXPAND_USER_DEFINED, "custom" },
{ EXPAND_USER_LIST, "customlist" },
{ EXPAND_DIRECTORIES, "dir" },
{ EXPAND_ENV_VARS, "environment" },
{ EXPAND_EVENTS, "event" },
{ EXPAND_EXPRESSION, "expression" },
{ EXPAND_FILES, "file" },
{ EXPAND_FILES_IN_PATH, "file_in_path" },
{ EXPAND_FILETYPE, "filetype" },
{ EXPAND_FUNCTIONS, "function" },
{ EXPAND_HELP, "help" },
{ EXPAND_HIGHLIGHT, "highlight" },
{ EXPAND_HISTORY, "history" },
[EXPAND_AUGROUP] = "augroup",
[EXPAND_BEHAVE] = "behave",
[EXPAND_BUFFERS] = "buffer",
[EXPAND_CHECKHEALTH] = "checkhealth",
[EXPAND_COLORS] = "color",
[EXPAND_COMMANDS] = "command",
[EXPAND_COMPILER] = "compiler",
[EXPAND_CSCOPE] = "cscope",
[EXPAND_USER_DEFINED] = "custom",
[EXPAND_USER_LIST] = "customlist",
[EXPAND_DIRECTORIES] = "dir",
[EXPAND_ENV_VARS] = "environment",
[EXPAND_EVENTS] = "event",
[EXPAND_EXPRESSION] = "expression",
[EXPAND_FILES] = "file",
[EXPAND_FILES_IN_PATH] = "file_in_path",
[EXPAND_FILETYPE] = "filetype",
[EXPAND_FUNCTIONS] = "function",
[EXPAND_HELP] = "help",
[EXPAND_HIGHLIGHT] = "highlight",
[EXPAND_HISTORY] = "history",
#ifdef HAVE_WORKING_LIBINTL
{ EXPAND_LOCALES, "locale" },
[EXPAND_LOCALES] = "locale",
#endif
{ EXPAND_MAPPINGS, "mapping" },
{ EXPAND_MENUS, "menu" },
{ EXPAND_MESSAGES, "messages" },
{ EXPAND_OWNSYNTAX, "syntax" },
{ EXPAND_SYNTIME, "syntime" },
{ EXPAND_SETTINGS, "option" },
{ EXPAND_PACKADD, "packadd" },
{ EXPAND_SHELLCMD, "shellcmd" },
{ EXPAND_SIGN, "sign" },
{ EXPAND_TAGS, "tag" },
{ EXPAND_TAGS_LISTFILES, "tag_listfiles" },
{ EXPAND_USER, "user" },
{ EXPAND_USER_VARS, "var" },
{ 0, NULL }
[EXPAND_MAPPINGS] = "mapping",
[EXPAND_MENUS] = "menu",
[EXPAND_MESSAGES] = "messages",
[EXPAND_OWNSYNTAX] = "syntax",
[EXPAND_SYNTIME] = "syntime",
[EXPAND_SETTINGS] = "option",
[EXPAND_PACKADD] = "packadd",
[EXPAND_SHELLCMD] = "shellcmd",
[EXPAND_SIGN] = "sign",
[EXPAND_TAGS] = "tag",
[EXPAND_TAGS_LISTFILES] = "tag_listfiles",
[EXPAND_USER] = "user",
[EXPAND_USER_VARS] = "var",
};
static char *get_command_complete(int arg)
{
if (arg >= (int)(ARRAY_SIZE(command_complete))) {
return NULL;
} else {
return (char *)command_complete[arg];
}
}
static void uc_list(char_u *name, size_t name_len)
{
int i, j;
@ -5000,13 +5006,12 @@ static void uc_list(char_u *name, size_t name_len)
IObuff[len++] = ' ';
} while (len < 21);
/* Completion */
for (j = 0; command_complete[j].expand != 0; ++j)
if (command_complete[j].expand == cmd->uc_compl) {
STRCPY(IObuff + len, command_complete[j].name);
len += (int)STRLEN(IObuff + len);
break;
}
// Completion
char *cmd_compl = get_command_complete(cmd->uc_compl);
if (cmd_compl != NULL) {
STRCPY(IObuff + len, get_command_complete(cmd->uc_compl));
len += (int)STRLEN(IObuff + len);
}
do {
IObuff[len++] = ' ';
@ -5797,7 +5802,15 @@ char_u *get_user_cmd_nargs(expand_T *xp, int idx)
*/
char_u *get_user_cmd_complete(expand_T *xp, int idx)
{
return (char_u *)command_complete[idx].name;
if (idx >= (int)ARRAY_SIZE(command_complete)) {
return NULL;
}
char *cmd_compl = get_command_complete(idx);
if (cmd_compl == NULL) {
return (char_u *)"";
} else {
return (char_u *)cmd_compl;
}
}
/*
@ -5857,20 +5870,23 @@ int parse_compl_arg(const char_u *value, int vallen, int *complp,
}
}
for (i = 0; command_complete[i].expand != 0; ++i) {
if ((int)STRLEN(command_complete[i].name) == valend
&& STRNCMP(value, command_complete[i].name, valend) == 0) {
*complp = command_complete[i].expand;
if (command_complete[i].expand == EXPAND_BUFFERS)
for (i = 0; i < (int)ARRAY_SIZE(command_complete); i++) {
if (get_command_complete(i) == NULL) {
continue;
}
if ((int)STRLEN(command_complete[i]) == valend
&& STRNCMP(value, command_complete[i], valend) == 0) {
*complp = i;
if (i == EXPAND_BUFFERS) {
*argt |= BUFNAME;
else if (command_complete[i].expand == EXPAND_DIRECTORIES
|| command_complete[i].expand == EXPAND_FILES)
} else if (i == EXPAND_DIRECTORIES || i == EXPAND_FILES) {
*argt |= XFILE;
}
break;
}
}
if (command_complete[i].expand == 0) {
if (i == (int)ARRAY_SIZE(command_complete)) {
EMSG2(_("E180: Invalid complete value: %s"), value);
return FAIL;
}
@ -5894,9 +5910,13 @@ int parse_compl_arg(const char_u *value, int vallen, int *complp,
int cmdcomplete_str_to_type(char_u *complete_str)
{
for (int i = 0; command_complete[i].expand != 0; i++) {
if (STRCMP(complete_str, command_complete[i].name) == 0) {
return command_complete[i].expand;
for (int i = 0; i < (int)(ARRAY_SIZE(command_complete)); i++) {
char *cmd_compl = get_command_complete(i);
if (cmd_compl == NULL) {
continue;
}
if (STRCMP(complete_str, command_complete[i]) == 0) {
return i;
}
}
@ -9948,3 +9968,87 @@ bool cmd_can_preview(char_u *cmd)
return false;
}
/// Gets a list of maps describing user-commands defined for buffer `buf`
/// or defined globally if `buf` is NULL.
///
/// @param buf Buffer to inspect, or NULL to get global commands.
///
/// @return Array of dictionaries describing commands
ArrayOf(Dictionary) commands_array(buf_T *buf)
{
Array rv = ARRAY_DICT_INIT;
garray_T *gap;
if (buf == NULL) {
gap = &ucmds;
} else {
gap = &buf->b_ucmds;
}
for (int i = 0; i < gap->ga_len; i++) {
char arg[2] = { 0, 0 };
Dictionary d = ARRAY_DICT_INIT;
char Range[10] = "";
ucmd_T *cmd = USER_CMD_GA(gap, i);
// Name
PUT(d, "name", STRING_OBJ(cstr_to_string((char *)cmd->uc_name)));
// Argument
switch (cmd->uc_argt & (EXTRA|NOSPC|NEEDARG)) {
case 0: arg[0] = '0'; break;
case(EXTRA): arg[0] = '*'; break;
case(EXTRA|NOSPC): arg[0] = '?'; break;
case(EXTRA|NEEDARG): arg[0] = '+'; break;
case(EXTRA|NOSPC|NEEDARG): arg[0] = '1'; break;
}
PUT(d, "nargs", STRING_OBJ(cstr_to_string((char *)arg)));
// Definition
PUT(d, "definition", STRING_OBJ(cstr_to_string((char *)cmd->uc_rep)));
// Complete
char *cmd_compl = get_command_complete(cmd->uc_compl);
if (cmd_compl != NULL) {
PUT(d, "complete", STRING_OBJ(cstr_to_string(cmd_compl)));
}
// Complete Arg
if (cmd->uc_compl_arg != NULL) {
PUT(d, "complete_arg",
STRING_OBJ(cstr_to_string((char *)cmd->uc_compl_arg)));
}
// Range
if (cmd->uc_argt & (RANGE|COUNT)) {
if (cmd->uc_argt & COUNT) {
// -count=N
snprintf((char *)Range, sizeof(Range), "%" PRId64 "c",
(int64_t)cmd->uc_def);
} else if (cmd->uc_argt & DFLALL) {
Range[0] = '%';
} else if (cmd->uc_def >= 0) {
// -range=N
snprintf((char *)Range, sizeof(Range), "%" PRId64 "",
(int64_t)cmd->uc_def);
} else {
Range[0] = '.';
}
PUT(d, "range", STRING_OBJ(cstr_to_string((char *)Range)));
}
// Address
for (int j = 0; addr_type_complete[j].expand != -1; j++) {
if (addr_type_complete[j].expand != ADDR_LINES
&& addr_type_complete[j].expand == cmd->uc_addr_type) {
PUT(d, "addr", STRING_OBJ(cstr_to_string(addr_type_complete[j].name)));
break;
}
}
// ScriptID
PUT(d, "script_id", INTEGER_OBJ(cmd->uc_scriptID));
ADD(rv, DICTIONARY_OBJ(d));
}
return rv;
}

View File

@ -0,0 +1,74 @@
local helpers = require('test.functional.helpers')(after_each)
local clear = helpers.clear
local command = helpers.command
local curbufmeths = helpers.curbufmeths
local eq = helpers.eq
local meths = helpers.meths
describe('nvim_get_commands', function()
local dummy_dict = {dummy=''}
local cmd_string = 'command Hello echo "Hello World"'
local cmd_string2 = 'command Pwd pwd'
local cmd_dict = {
name='Hello',
nargs='0',
script_id=0,
definition='echo "Hello World"',
}
local cmd_dict2 = {
name='Pwd',
nargs='0',
script_id=0,
definition='pwd',
}
before_each(clear)
it('gets empty list if no commands were defined', function()
eq({}, meths.get_commands(dummy_dict))
end)
it('gets user-def commands', function()
-- Insert a command
command(cmd_string)
eq({cmd_dict}, meths.get_commands(dummy_dict))
-- Insert a another command
command(cmd_string2);
eq({cmd_dict, cmd_dict2}, meths.get_commands(dummy_dict))
-- Delete a command
command('delcommand Pwd')
eq({cmd_dict}, meths.get_commands(dummy_dict))
end)
it('considers different buffers', function()
-- Insert a command
command('command -buffer Hello echo "Hello World"')
eq({cmd_dict}, curbufmeths.get_commands(dummy_dict))
-- Insert a another command
command('command -buffer Pwd pwd')
eq({cmd_dict, cmd_dict2}, curbufmeths.get_commands(dummy_dict))
-- Delete a command
command('delcommand Pwd')
eq({cmd_dict}, curbufmeths.get_commands(dummy_dict))
end)
it('gets different attributes of different commands', function()
local cmd1 = {
complete='custom',
nargs='1',
name='Finger',
script_id=0,
complete_arg='ListUsers',
definition='!finger <args>',
}
local cmd2 = {
complete='dir',
nargs='0',
name='TestCmd',
range='10c',
addr='arguments',
script_id=0,
definition='pwd <args>',
}
command('command -complete=custom,ListUsers -nargs=1 Finger !finger <args>')
eq({cmd1}, meths.get_commands(dummy_dict))
command('command -complete=dir -addr=arguments -count=10 TestCmd pwd <args>')
eq({cmd1, cmd2}, meths.get_commands(dummy_dict))
end)
end)

View File

@ -11,7 +11,7 @@ local source = helpers.source
local shallowcopy = global_helpers.shallowcopy
describe('get_keymap', function()
describe('nvim_get_keymap', function()
before_each(clear)
-- Basic mapping and table to be used to describe results