-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
[RDY] API: nvim_get_commands #8060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Presently just a prototype, means it works list the command successfully, but some necessary things like documentation, tests are omitted, will be added soon, It first needs to be reviewed |
src/nvim/ex_docmd.c
Outdated
return ucmds.ga_len; | ||
} | ||
|
||
Dictionary get_dict_command(int i) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can have the function return an Array directly, then separate n_get_commands
is not needed.
src/nvim/ex_docmd.c
Outdated
Dictionary get_dict_command(int i) { | ||
Dictionary dic = ARRAY_DICT_INIT; | ||
ucmd_T *cmd = USER_CMD(i); | ||
PUT(dic, "uc_name", STRING_OBJ(cstr_to_string((const char *)cmd->uc_name))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are the internal names, we don't want them in external API. Better to use terms mentioned in documentation :help command
.
522c18a
to
54a16f5
Compare
Made the necessary adjustments by seeing the uc_list, but if there is a room for adjustments I would be happy to do so, and after that my work is adding documentation and testing |
And during testing, I want to know how to do its testing in lua, means we apply command and get output? |
Is it right or something to be done. |
check Also just like |
OK, this will be done |
Now I make the tests |
src/nvim/ex_docmd.c
Outdated
PUT(dic, "ScriptID", INTEGER_OBJ(cmd->uc_scriptID)); | ||
|
||
// Compl Arg | ||
PUT(dic, "Compl Arg", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
key names never have whitespace and are never capitalized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was thinking of Compl Arg same, didn't get the feel of a coder here, I change it
src/nvim/ex_docmd.c
Outdated
/// @param[in] buf the specified buffer, if NULL then global commands required | ||
/// | ||
/// @return The array of the commands and the information of each command | ||
ArrayOf(Dictionary) get_commands(buf_T *buf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's the best convention, but our current convention (for internal wrappers that convert internal structures to Array
) is to name this commands_array
.
Existing examples: mode_style_array
, keymap_array
. Until a better convention is found, we should always do the same thing, to avoid having numerous inconsistent patterns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK got it
Made the basic tests, by understanding the test of get_keymap, may be more things are to be added after it, but its basic of the tests I made |
src/nvim/ex_docmd.c
Outdated
|
||
// Defination | ||
PUT(dic, "Defination", | ||
PUT(dic, "defination", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo, "definition" (also the comment above it, which probably should just be deleted)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okk, so, I think my english is sooo bad, I generally used defination, it was wrong, 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And yes, I have to delete the comments?
I thought they are useful to see the code tidy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Today, I am laughing on myself, I didn't even know the spelling of Definition
, how poor, haha haha 🤣
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nimitbhardwaj No worries!
And yes, I have to delete the comments?
Trivial comments that restate the obvious aren't needed. But if they describe something non-obvious, or if they describe a group of lines, then they might be useful.
I edited the typo, but should I remove all comments, that were added, I added them because I thought they provide the info and somewhat beautify the code. |
src/nvim/ex_docmd.c
Outdated
|
||
// Complete | ||
for (j = 0; command_complete[j].expand != 0; j++) { | ||
if (cmd->uc_compl == command_complete[j].expand) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This operation can be made more fast, command_complete is just a static array, we manually inserted the items in it, if the items there are added in sorted order then by using binary search O(log(n)) complexity can be achieved instead of O(n), so possibliity can be just arange the items in array, replace linear search with bin search
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the proper refactor would be to define the complete strings next to the constants, as a dense array of string constants (with NULL if some constant lacks a string). Probably, move EXPAND_COMMANDS
etc enum from vim.c and command_complete
array both to ex_cmds_defs.h
. Compare to typedef enum hlf_T
and hlf_names
in highlight_defs.h
. Maybe an accessor function should be used, to do bounds checking (negative index should return NULL string).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this possibility, for array command_complete, I add {INT_MIN, NULL} at the end, this element can be used to serve the end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You were asking for buffer?, means if negetive buffer number is sent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't get it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is used by ExpandGeneric
which sorts the results anyway, so reordering them wouldn't be an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, ExpandGeneric uses it, I also found it, so true, reordering is best way here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reorder them according to the value of the EXPAND_
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I sorted the list of commands according to the values of EXPAND_, now I want to test if it will be correct, will get_user_cmd_complete
function pose no difficulty, where is its test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorting the lines in reverse sorted is better, so then we have not to care for the 0 as the last element which acts as comparator for end, and also useful in binary search
src/nvim/ex_docmd.c
Outdated
// Address | ||
for (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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too is possibility
I edited the array |
Sorry, but binary search of enum value in static array doesn't make any sense. Enum lookup array is both simpler and faster. |
Okk, but how about that function, that will not work, because loop is used there, what about it |
what function? |
get_user_cmd_complete, I reordered the array, so it don't pose issue in the function. |
As I said on the line comment, that function can be supported by making the |
I completely understand this, I didn't use much of lookup array in C before, it is C99 addition, now I am implementing it, yes, its easier. |
The syntax might be new, but the pattern is not. For instance an "X macro" (with two args) could be used in older version on C to keep enum and string order matching. |
3ee987b
to
390f7f2
Compare
I made it, the lookup method, this works correctly, some mistakes may be there, some concept of NULL pointers, so I request for a review. |
src/nvim/ex_docmd.c
Outdated
@@ -5815,7 +5815,7 @@ char_u *get_user_cmd_complete(expand_T *xp, int idx) | |||
} | |||
char *cmd_compl = get_command_complete(idx); | |||
if (cmd_compl == NULL) { | |||
return "\000"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just NULL. Thus the if
can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, in ExpandGeneric function, there is a check for NUL,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as I investigated, NUL is '\000', so
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which is same as "", thus made the small change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NUL and NULL are used differently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right, it should work.
src/nvim/ex_docmd.c
Outdated
case(EXTRA|NEEDARG): Arg[0] = '+'; break; | ||
case(EXTRA|NOSPC|NEEDARG): Arg[0] = '1'; break; | ||
} | ||
PUT(dic, "argument", STRING_OBJ(cstr_to_string((const char *)Arg))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"nargs"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okk
src/nvim/ex_docmd.c
Outdated
PUT(dic, "script_id", INTEGER_OBJ(cmd->uc_scriptID)); | ||
|
||
// Compl Arg | ||
PUT(dic, "compl_arg", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this key should also only be present if the argument was not NULL. Also it could be called "complete_arg", and this code could be moved just below the "complete" code above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/nvim/ex_docmd.c
Outdated
for (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(dic, "address", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"addr" (we typically prefer spelled out form when we add new keywords, but addr
is already established in :command -addr
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok this is better
src/nvim/ex_docmd.c
Outdated
break; | ||
} | ||
} | ||
if (!ck) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can remove
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
test/functional/api/command_spec.lua
Outdated
|
||
describe('get_commands', function() | ||
local cmd_string = 'command Hello echo "Hello World"' | ||
local cmd_string2 = 'command Pwd pwd' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not add a bit more attributes like complete,address etc here? (they don't need to make sense, just to exercise the code)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this too done, added 2 more tests for this, exploiting all features of command
So I did it |
88570a2
to
967d3e4
Compare
I rebased it too |
Does this return builtin commands or only user-defined commands? If only user-defined commands, we should add a parameter to allow enhancement later without needing to add a new API function (or break the existing one; see
and only |
It displays perhaps user defined commands, commands like And for the case of adding a parameter |
An alternative is to later on include builtin commands in metadata. Yes, it is called "API metadata" for the moment, but I see no reason not to generalize it to other compile-time constant data of similar kind. |
Yes, generalization of this seems messy, because I thought that there would be 3 options, so adding just boolean is not so good, 3 options are, builtin, user-def and both builtin and user-def. |
967d3e4
to
566a5eb
Compare
Although I have done the changes, but I would like to add the Please have a check if its correct. |
For now, let it take a dictionary which currently is ignored:
Although I agree with @bfredl , I think we should plan to support returning builtin commands from nvim_get_command in the future, for "ergonomics". |
OK, we need to take the dictionary which will be ignored completely. I got it. The dictionary will hold the metadata in future, its good too. |
566a5eb
to
4c9673c
Compare
👍 |
Merged in #8375 |
Add the nvim_get_commands API function,
Returns the dictionary of commands.