I’m printing some text to stderr during a macro expansion, and I expected it might show up at the cursor when I evaluate code in cider which causes the macro to expand. But instead it is printed in the repl in RED. Is there a way to get it to show up somehow as text annotation at the site where the macro expansion occurs? Here is the code which prints the diagnostic message
(defn print-unreachable-warning [code-exprs]
(binding [*out* *err*]
(printf "Unreachable code: ")
(if (sequential? code-exprs)
(doseq [word (interpose " " (map str code-exprs))]
(printf "%s" word))
(printf "%s" code-exprs))
(printf "\n")))
it would be great for the message to print at the cursor or in the emacs mini-buffer when I press C-M-x
anyway, shouldn’t C-M-x capture the stderr of the evaluation and display it in the minibuffer?
You may want to look into writing a clj-kondo hook for your macro. That way you'll get the red squigglies for common clojure dev setups.
I agree that what is printed to stderr is not necessarily an error. I cannot throw an exception, because it is not an error, as you alluded to.
I didn’t understand your suggestion. What do I need to do to get the squiggly message?
Are you saying that I just need to print a line somewhere in my output, in my 100s of lines of output, that looks like
something warning, /path/to/file:line:column — message
I don’t know how to compute the line and column number. How can I compute the line and column of the macro being evaluated?
Here’s what I tried.
(defmacro xyzzy [x]
(print "something warning, /i/dont/know/the/path/to/file:0:0 -- message\n")
`(list ~x))
(defn abc []
(xyzzy 100))
when I evaluate the defun abc… It prints the message into the repl, but doesn’t display any squigglesalso tried the following
(defmacro xyzzy [x]
(binding [*out* *err*]
(print "something warning, /i/dont/know/the/path/to/file:0:0 -- message\n"))
`(list ~x))
(defn abc []
(xyzzy 100))
it prints to stderr in red in the repl, but no squiggles in the editor bufferAt macroexpansion time you can use *file* and take meta from the the form you are macroexpanding, there will be line and column.
Try an LLM, it should help you with that.
ok, I can do that. but if I know the file name, do I simply need to print a string of that format?
Yep
OK, I think I’m doing what you suggested. It doesn’t seem to work. The message is still being printed into the REPL, and not displayed at the evaluation point, and no squiggles. Any suggestions?
(defmacro xyzzy [x]
;; Desktop/algebra-1-grader
(let [m (meta &form)]
(printf "something warning, %s:%d:%d - message\n"
*file* (:line m) (:column m) m))
`(list ~x))
(defn abc []
(xyzzy 100))
when I evaluate the abc definition, the message is printed in the REPL, not in squiggles
Why would it display in the minibuffer or inline? We don't do that for any output. You are confusing this with the error message displayed in the minibuffer when exception is thrown.
thanks for your comment. But what is the logic of this? If I evaluate an expression in an editor buffer which prints messages to stderr. The message is printed in the repl window, and nothing notifies me that an error message has been printed.
It seems to me that error messages should be displayed at the place where I pressed C-M-x. right?
The value returned is displayed in the editor buffer, right? why not also the error message?
is there a way I could write my own cider evaluate function which wraps cider-evaluate-defun-at-point and not only displays the return value, but also displays the error message, or better yet, decorates the expression having been evaluated with squiggly underline whose hover text is the error message?
Writing to stderr does not strictly mean there was an error. For example, reflection warnings print to stderr but they don't prevent code from working. Stderr is just an output stream. If you want to convey that something erroneous happened, it is better to throw the actual exception.
I understand your issue that you want the stderr messages for your particular case to be more visible. After all, we do the same with aforementioned reflection warnings – we parse the output and present it with squigly lines.
Actually, you can use that.
If you format your warning in this way: anyword warning, /path/to/file:line:column - arbitrary text and keep it on the same line, the form will be squigly-highlighted with an on-hover warning