Fork me on GitHub
#emacs
<
2020-04-24
>
dotemacs11:04:31

Hello This is a request for comments about a little utility I’ve been working on. I’ve created a mode (which is not finished just yet), which deals with comments in Clojure(Script) mode. It is very much inspired by comment-dwim-2, in that when you have something like this:

█(ns foo.bar
    (:require [foo.baz]))
and you M-x clojure-comment-dwim it turns the code into:
#_(ns foo.bar
      (:require [foo.baz]))
` then when you invoke the function again, it changes the comment style to:
;;(ns foo.bar
      (:require [foo.baz]))
Yes, that does mess up the parens, but then it’s up to you what particular style of comment is appropriate. And a final tweak is that if you turn off clojure-comment-dwim-ignore-trailing-comment it allows you to have trailing comments:
(ns foo.bar ;
    (:require [foo.baz]))
What I’m wondering is if instead of the behaviour above, which cycles through the different comment types, it can comment out the form following it. For example, with the code like this where the cursor is just after the ns:
(ns█foo.bar
    (:require [foo.baz]))
by invoking clojure-comment-dwim, it should insert #_, so that you get:
(ns #_foo.bar
    (:require [foo.baz]))
And then the subsequent invocations should have the already existing behaviour described above, where the comment is inserted at the beginning of line. What are your thoughts on this? Thanks Repo of the mode so far (which is not yet finished): https://github.com/dotemacs/clojure-comment-dwim.el

yuhan11:04:35

I don't understand the utility of cycling to a ;; comment and unbalancing parens in the process - I imagine most of the time it's a 2-state toggle between inserting and deleting #_ , so having a 3rd state seems like more unneccessary steps

yuhan11:04:32

here's the function I use for that purpose, which also has some other "dwim" behaviour like wrapping in a comment form when a region is selected:

dotemacs12:04:50

Thanks for the feedback @qythium. It was influenced by comment-dwim-2, which acts this way. I like what you’ve done with your gist. I had another idea, after your feedback, for any given point, if the command is invoked, insert #_ even if it’s in a middle of a s-expression. Then the function is invoked again, uncomment it. With an argument supplied, go up a s-expression and (un)comment. And I like your idea of using comment on the region, but I’ll try to borrow from your approach and comment-dwim-2’s, in applying it against a region, but not from just a “starting” & “ending” points, but for the s-expressions within the region. Thanks again

dotemacs08:04:44

I’ve implemented the functionality where you can (un)comment #_, uncomment (comment ...) and when a region is selected, comment with (comment ...), all under a *single function*. With a prefix argument C-u n` you can go up n s-expressions from point, and comment them out. There’s bound to be some edge cases and tests need to be added to prevent regressions, but it works so far. (Yes, there is some duplication and I intend on cleaning that up). See what you think (note, the new changes are in s-expressions-only branch): https://github.com/dotemacs/clojure-comment-dwim.el/blob/s-expressions-only/clojure-comment-dwim.el

yuhan11:04:47

I like being able to comment out multiple forms using the numeric argument, which comes up surprisingly often in clojure due to "pairs" in maps and binding forms: C-u 4 clojure-toggle-ignore-form => {a 1 #_#_#_#_ b 2 c 3 d 4}

yuhan11:04:18

Are you thinking of submitting it to clojure-mode or a MELPA package? I've been planning to contribute my above snippet to clojure-mode too, seeing as Calva added a similar feature recently 🙂

dotemacs13:04:19

Hey @qythium yea, once it’s done, I will. Wasn’t aware that Calva had this, but good to know. I’m mostly going off my own needs plus feedback I’m currently getting.

dotemacs13:04:42

About

C-u 4 clojure-toggle-ignore-form =>
{a 1 #_#_#_#_ b 2 c 3 d 4}
I’m using the prefix argument C-u to clojure-comment-dwim to mean “go up PREFIX s-expressions and comment them out via #_“. I have the following considerations: - Unlike your gist, my setup has only one “entry” function, which is called and then dependent on the situation/prefix argument, it determines how to comment out the code. Whereas you have two different functions. - I could use a negative number to go up, as in back, and a positive number to go forward. But then since I’m using a single function, to what case would the prefix apply? - An alternative to your:
C-u 4 clojure-toggle-ignore-form =>
{a 1 #_#_#_#_ b 2 c 3 d 4}
is to select the b 2 c 3, which would then comment it out as: {a 1 (comment b 2 c 3) d 4} I’m not claiming that either of our approaches are better, just trying to figure out what would be more useful, potentially more frequently used. Which I guess it’s a hard thing to predict. I think that it might be an idea to have some test cases upfront that show the behaviour that can be decided on upfront, rather than implementing the features first and then discussing it...

dotemacs15:04:13

I’ve put a bunch of scenarios in this issue, along with the discussion on the prefix argument behaviour: https://github.com/dotemacs/clojure-comment-dwim.el/issues/2

yuhan15:04:08

Looks like you put a lot of thought into this! I think a lot of this comes down to personal preference and workflow habits - different users will prefer having multiple specific commands vs. having a single command with context-sensitive or cycling behaviour.

yuhan15:04:47

In any case it seems that some sort of #_ command should belong in the main clojure-mode package for everyone's benefit. I'll open an issue on the clojure-mode repo to get feedback, hope you don't mind if I link to your repo in its current state

practicalli-john21:04:36

Just sharing my thoughts and workflow. I would like to see a clojure-mode function that just toggles #_ on the current expression, preferably without having to use C-u argument. I have some code I someone wrote for me a year or so ago, which I just bind to C-# and it does the trick. It's Evil aware so it works with Spacemacs. There are already several ways to toggle ;; style comments for lines and regions (certainly in Spacemacs) I only use one comment block per namespace, so toggling would be of no value to me. I think there is a (comment ) yasnippet (or its really easy to add one.

👍 4
dotemacs14:04:11

Thanks for the feedback @U05254DQM. > I would like to see a clojure-mode function that just toggles #_ on the current expression, preferably without having to use C-u argument. I’ve got that covered right now in clojure-comment-dwim.el. But if you select a region, it figures out if this region is a s-expression, in which case it still uses #_, otherwise if it isn’t a s-expression, it wraps it in (comment ...). > It’s Evil aware so it works with Spacemacs. Not sure how Evil/Spacemacs works, as I don’t use it, but I assume that it just invokes Elisp functions bound to a certain key(combo)? > There are already several ways to toggle ;; style comments for lines and regions (certainly in Spacemacs) comment-dwim as part of stock Emacs does this.

yuhan17:04:06

The main issue I have with your implementation is that #_ and comment forms are semantically very different in the way they're commonly used. Treating them as equivalent and choosing which to use based on some heuristic would encourage bad style.

dotemacs18:04:54

OK, so let me understand that better, how would you comment out a section of the code here:

{:a 1
 :b 2
 :c 3}
If you selected the region containing :b 2, what would be your preference @qythium? Something like: a)
{:a 1
#_ :b #_2
 :c 3}
b)
{:a 1
 #_ #_ :b 2
 :c 3}
c)
{:a 1
 #_(:b 2)
 :c 3}

dotemacs19:04:32

Or just standard comment-dwim style: d)

{:a 1
;; :b 2
:c 3}

yuhan12:04:00

hey sorry for the late reply, I would use option b

yuhan12:04:02

For me comment is reserved mostly for top-level comment forms, ie. temporary test code or things that are meant to be executed interactively, and ;; line comments are for text

practicalli-john13:04:58

In your example @U3SG7RX7A option b would be the result I expect to see (although comments in maps is not an approach I would typically use). The only time I seem to use the #_ comment within an expression is in a threading macro, where I would expect a single #_ would comment out the part of the execution pipeline I wanted to exclude. I guess it could also be useful in cond too, but don't really use that much.

dotemacs15:04:52

Thank you both for the feedback!

dotemacs13:04:19
replied to a thread:Hello This is a request for comments about a little utility I’ve been working on. I’ve created a mode (which is not finished just yet), which deals with comments in Clojure(Script) mode. It is very much inspired by `comment-dwim-2`, in that when you have something like this: █(ns foo.bar (:require [foo.baz])) and you `M-x clojure-comment-dwim` it turns the code into: #_(ns foo.bar (:require [foo.baz])) ` then when you invoke the function again, it changes the comment style to: ;;(ns foo.bar (:require [foo.baz])) Yes, that does mess up the parens, but then it’s up to you what particular style of comment is appropriate. And a final tweak is that if you turn off `clojure-comment-dwim-ignore-trailing-comment` it allows you to have trailing comments: (ns foo.bar ; (:require [foo.baz])) What I’m wondering is if instead of the behaviour above, which cycles through the different comment types, it can comment out the form following it. For example, with the code like this where the cursor is just after the `ns`: (ns█foo.bar (:require [foo.baz])) by invoking `clojure-comment-dwim`, it should insert `#_`, so that you get: (ns #_foo.bar (:require [foo.baz])) And then the subsequent invocations should have the already existing behaviour described above, where the comment is inserted at the beginning of line. What are your thoughts on this? Thanks Repo of the mode so far (which is not yet finished): https://github.com/dotemacs/clojure-comment-dwim.el

Hey @qythium yea, once it’s done, I will. Wasn’t aware that Calva had this, but good to know. I’m mostly going off my own needs plus feedback I’m currently getting.

dotemacs13:04:42
replied to a thread:Hello This is a request for comments about a little utility I’ve been working on. I’ve created a mode (which is not finished just yet), which deals with comments in Clojure(Script) mode. It is very much inspired by `comment-dwim-2`, in that when you have something like this: █(ns foo.bar (:require [foo.baz])) and you `M-x clojure-comment-dwim` it turns the code into: #_(ns foo.bar (:require [foo.baz])) ` then when you invoke the function again, it changes the comment style to: ;;(ns foo.bar (:require [foo.baz])) Yes, that does mess up the parens, but then it’s up to you what particular style of comment is appropriate. And a final tweak is that if you turn off `clojure-comment-dwim-ignore-trailing-comment` it allows you to have trailing comments: (ns foo.bar ; (:require [foo.baz])) What I’m wondering is if instead of the behaviour above, which cycles through the different comment types, it can comment out the form following it. For example, with the code like this where the cursor is just after the `ns`: (ns█foo.bar (:require [foo.baz])) by invoking `clojure-comment-dwim`, it should insert `#_`, so that you get: (ns #_foo.bar (:require [foo.baz])) And then the subsequent invocations should have the already existing behaviour described above, where the comment is inserted at the beginning of line. What are your thoughts on this? Thanks Repo of the mode so far (which is not yet finished): https://github.com/dotemacs/clojure-comment-dwim.el

About

C-u 4 clojure-toggle-ignore-form =>
{a 1 #_#_#_#_ b 2 c 3 d 4}
I’m using the prefix argument C-u to clojure-comment-dwim to mean “go up PREFIX s-expressions and comment them out via #_“. I have the following considerations: - Unlike your gist, my setup has only one “entry” function, which is called and then dependent on the situation/prefix argument, it determines how to comment out the code. Whereas you have two different functions. - I could use a negative number to go up, as in back, and a positive number to go forward. But then since I’m using a single function, to what case would the prefix apply? - An alternative to your:
C-u 4 clojure-toggle-ignore-form =>
{a 1 #_#_#_#_ b 2 c 3 d 4}
is to select the b 2 c 3, which would then comment it out as: {a 1 (comment b 2 c 3) d 4} I’m not claiming that either of our approaches are better, just trying to figure out what would be more useful, potentially more frequently used. Which I guess it’s a hard thing to predict. I think that it might be an idea to have some test cases upfront that show the behaviour that can be decided on upfront, rather than implementing the features first and then discussing it...