-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
[RFC] Lua error message better formatting #7969
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
_emsg is the same as emsg but has additionnal argument which is a pointer to a function that is used to return the value
src/nvim/message.c
Outdated
* | ||
* return TRUE if wait_return not called | ||
*/ | ||
int emsg(const char_u *s_) |
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 need to have const char *s = (const char *)s_;
line above as well as underscore here: underscore is only because of the existence of the line and line is there only because I did not feel like refactoring lots of emsg
callers which use char_u
.
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.
Ah, and here still is underscore.
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.
Oh god, I must be blind. Sorry for that, I will change it immediately so I will not forget anymore !
src/nvim/message.c
Outdated
* return TRUE if wait_return not called | ||
*/ | ||
int emsg(const char_u *s_) | ||
static int _emsg(const char *s_, int (*msg_attr)(const char *s, const int attr)) |
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 bad idea to mask symbols; also need typedef, AFAIR C declarations generator intentionally cannot swallow that.
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 made the typedef in a commit but I did not understood the "mask symbol" part (I mean, technically what does that mean? I'm sorry, my knowledge of C is not that advanced).
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 means that you should not define a symbol in some inner scope which is the same as a symbol in an outer scope. E.g. in
int i = 0;
{
int i = 1;
printf("%d\n", i);
}
printf("%d\n", i);
second i
masks the first one. msg_attr
is a function defined in compilation unit scope, and at the same time an argument to _emsg
which means that it is redefined in _emsg
functions scope.
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.
Indeed it was not smart, thanks for the explanation!
The added function behaves like the non-echo function but display message in a echo-style way (i.e. tab and newline are preserved)
src/nvim/message.c
Outdated
@@ -54,6 +54,8 @@ struct msgchunk_S { | |||
char_u sb_text[1]; /* text to be displayed, actually longer */ | |||
}; | |||
|
|||
typedef int (*fct_msg_attr)(const char *s, const int attr); |
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.
New types should be CamelCase.
src/nvim/message.c
Outdated
msg_echo_attr_keep( | ||
const char *s, | ||
const int attr, | ||
int keep |
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 must not put arguments completely on separate lines unless you absolutely have to. Neither separate type from function name or closing parenthesis from the last argument.
src/nvim/message.c
Outdated
const int attr, | ||
int keep | ||
) | ||
FUNC_ATTR_NONNULL_ARG(1) |
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.
FUNC_ATTR_NONNULL_ALL may be used when all pointer arguments are non-NULL. Non-pointer ones do not matter.
src/nvim/message.c
Outdated
@@ -577,9 +666,9 @@ static int _emsg(const char *s_, int (*msg_attr)(const char *s, const int attr)) | |||
*/ | |||
int emsg(const char_u *s_) | |||
{ | |||
return _emsg((const char *)s_, &msg_attr); | |||
fct_msg_attr f = &msg_attr; |
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.
Do not create temporary variable here, code should work as it worked.
src/nvim/message.c
Outdated
buf = (char *)msg_strtrunc((char_u *)s, false); | ||
if (buf != NULL) { | ||
s = 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.
Too much copy-paste.
src/nvim/message.c
Outdated
|
||
char * spec_char = "\t\n\r"; | ||
|
||
char * next_spec = (char *)s; |
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.
There should not be space after asterisk.
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 next_spec
must be const char *
. Do not cast to char *
until needed.
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.e. until going to legacy (unrefactored) msg_outtrans[_len]_attr.
src/nvim/message.c
Outdated
s = buf; | ||
} | ||
|
||
char * spec_char = "\t\n\r"; |
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.
Do not make compiler work harder, strpbrk argument better be a literal, either just literal or macros. But since you are using this list in a single place just put literal there.
src/nvim/message.c
Outdated
char * next_spec = (char *)s; | ||
|
||
while (next_spec != NULL) { | ||
next_spec = (char *)vim_strpbrk(s, spec_char); |
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 just added a bunch of useless casts here (some hidden by vim_strpbrk
macros), don’t use vim_strpbrk
, use strpbrk
directly.
src/nvim/message.c
Outdated
@@ -606,6 +713,19 @@ static bool emsgfv(const char *fmt, va_list ap) | |||
return emsg((const char_u *)errbuf); | |||
} | |||
|
|||
static bool emsgfv_echo(const char *fmt, va_list ap) |
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 function is short and not used anywhere, but in emsgf_echo, so should not exist.
src/nvim/message.c
Outdated
@@ -572,7 +653,21 @@ int emsg(const char_u *s_) | |||
|
|||
// Display the error message itself. | |||
msg_nowait = false; // Wait for this msg. | |||
return msg_attr(s, attr); | |||
return ret_fct(s, attr); |
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.
ret_fct
is a strange name. I would go with something like display_msg
.
@ZyX-I I think I've modified everything you pointed out. Still I'm not sure about the "too much copy-paste" comment. Let me know if I need to modify anything :-) |
src/nvim/message.c
Outdated
} | ||
|
||
int | ||
msg_echo_attr_keep(const char *s, const int attr, int keep) |
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 are still separating type from function name. Correct examples of formatting are new files like src/nvim/eval/typval.c, not legacy code 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.
BTW, function returns boolean.
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 it is logical to either have const int keep
or int attr
. I normally prefer putting const
everywhere I can.
src/nvim/message.c
Outdated
@@ -137,6 +139,81 @@ int msg_attr(const char *s, const int attr) FUNC_ATTR_NONNULL_ARG(1) | |||
return msg_attr_keep((char_u *)s, attr, false); | |||
} | |||
|
|||
int msg_echo_attr(const char *s, const int attr) FUNC_ATTR_NONNULL_ARG(1) |
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.
Do not put FUNC_ATTR on the same line as a function name, this variant both visually hides attributes and produces unneccessary line change if something needs to be added (e.g. new argument or another attribute).
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.
BTW, is this a function intended to be used in ex_echo
? It is not good to have two implementations doing essentially the same task; also using function there makes testing a lot easier and allows CI to catch errors before test suite is good enough.
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 did not intend to use it in ex_echo
, but I guess I could modify ex_echo
to use it (if it is the purpose of the question)
src/nvim/message.c
Outdated
msg_echo_attr_keep(const char *s, const int attr, int keep) | ||
FUNC_ATTR_NONNULL_ALL | ||
{ | ||
static int entered = 0; |
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 function contains too many copy-paste from its origin, common code should not be copied if possible.
src/nvim/message.c
Outdated
{ | ||
static int entered = 0; | ||
int retval; | ||
char * buf = NULL; |
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 asterisk is formatted as multiplication, but it is not.
src/nvim/message.c
Outdated
} | ||
|
||
msg_start(); | ||
buf = (char *)msg_strtrunc((char_u *)s, false); |
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 if you do refactor old code, move variable definitions closer to the place where they are actually used. It should be char *const buf
here.
src/nvim/message.c
Outdated
s = buf; | ||
} | ||
|
||
const char *spec_char = "\t\n\r"; |
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 variable still has no reason to exist.
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.
Oh I see, I misunderstood your previous comment
src/nvim/message.c
Outdated
FUNC_ATTR_NONNULL_ALL | ||
{ | ||
static int entered = 0; | ||
int retval; |
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 one should not exist either, need const bool retval = msg_end()
below.
src/nvim/message.c
Outdated
retval = msg_end(); | ||
|
||
if (keep && retval && vim_strsize((char_u *)s) < (int)(Rows - cmdline_row -1) | ||
* Columns + sc_col) { |
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.
Alignment is not correct.
src/nvim/message.c
Outdated
{ | ||
return msg_echo_attr_keep(s, attr, false); | ||
} | ||
|
||
int | ||
msg_echo_attr_keep(const char *s, const int attr, int keep) | ||
bool msg_attr_skip(const char *s, const int attr, int *const call) |
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.
@ZyX-I I move the common code into this function that return when the message doesn't need to be shown (I will add comments, I forgot them). Is the function ok for you?
Also, i didn't put the add_msg_hist
here because (if I understood well the code) it is already done in msg_outtrans_len_attr
and msg_puts_attr_len
. I guess it doesn't need to be done multiple time?
* When terminal not initialized (yet) mch_errmsg(..) is used. | ||
* | ||
* return TRUE if wait_return not called | ||
*/ |
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.
Please run make lint
, it will notify you about lint problems.
What is
|
@justinmk Yep |
I do not see new code used for echo. Also missing tests including outputting following messages:
That list repeated for
As I said I would expect you to write a test generator rather then lots of boilerplate tests. |
Oh yeah, I forget this story about the |
Normally you write tests like |
Ok, thanks for the references, I will look at that. |
Merged by #9520. Would be good if someone implemented the tests mentioned in #7969 (comment) . |
reference #7388