Skip to content
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

macrostep like macroexpand #1850

Closed
colonelpanic8 opened this issue Sep 20, 2016 · 27 comments
Closed

macrostep like macroexpand #1850

colonelpanic8 opened this issue Sep 20, 2016 · 27 comments

Comments

@colonelpanic8
Copy link

Is there any reason that cider opted to use a separate buffer for outputting the result of expanding a macro? I quite like the way https://github.com/joddie/macrostep expands macros inline in emacs lisp. Would the maintainers consider an optional change that maybe even reuses some of that code?

@bbatsov
Copy link
Member

bbatsov commented Sep 20, 2016

Legacy. We just did it the way it was done in SLIME. Including inline expansion of macros shouldn't be hard and we discussed it in the past. If someone is willing to implement this we'd be happy to accept it.

@xiongtx
Copy link
Member

xiongtx commented Sep 25, 2016

Inline expansion isn't hard, but "stepping" is a bit trickier. The macroexpansion functions in CIDER do not currently do that.

It'd be necessary to ask the middleware whether the form we're looking at is a macro. See slime-macrostep, for example.

@bbatsov
Copy link
Member

bbatsov commented Sep 26, 2016

You can already get this info from the var-info middleware.

On Monday, 26 September 2016, Tianxiang Xiong [email protected]
wrote:

Inline expansion isn't hard, but "stepping" is a bit trickier. The
macroexpansion functions in CIDER do not currently do that.

It'd be necessary to ask the middleware whether the form we're looking at
is a macro. See slime-macrostep
https://github.com/slime/slime/blob/master/contrib/slime-macrostep.el#L117,
for example.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#1850 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGVykDYL5hFSybrIzYs2cOwlZHgeSLMks5qtuftgaJpZM4KBaVU
.

Best Regards,
Bozhidar Batsov

http://www.batsov.com

@xiongtx
Copy link
Member

xiongtx commented Oct 6, 2016

Here's a proof of concept:

First define some Clojure functions:

(defn macro? [form]
  (not (= (macroexpand-1 form) form)))

(defn expand-once [form]
  (let [expanded? (atom false)]
    (walk/prewalk (fn [x]
                    (if (and (not @expanded?)
                             (macro? x))
                      (do (reset! expanded? true)
                          (macroexpand-1 x))
                      x))
                  form)))

Then set the relevant values for macrostep (these can be improved!):

(setq-default macrostep-sexp-bounds-function
              (lambda ()
                (let ((pos (cider-sexp-at-point t)))
                  (cons (car pos) (cadr pos)))))

(setq-default macrostep-sexp-at-point-function
              (lambda (&rest _ignore)
                (cider-sexp-at-point)))

(setq-default macrostep-expand-1-function
              (lambda (form env)
                (nrepl-dbind-response (cider-nrepl-sync-request:eval (format "(expand-once '%s)" form))
                    (value)
                  value)))

(setq-default macrostep-print-function
              (lambda (sexp env)
                (insert sexp)))

Obviously we should be setting these in a hook, but this is a POC...

Then call macrostep-expand on something like:

(defn foo [x]
  (defn bar [y]
    (defn baz [z]
      (+ 1 2))))

@bbatsov
Copy link
Member

bbatsov commented Oct 6, 2016

@xiongtx I think you forgot to include something. :-)

@xiongtx
Copy link
Member

xiongtx commented Oct 6, 2016

Yes, accidentally hit RET too quickly.

Is there a way to pretty-print a Clojure form in an Emacs buffer? Not clojure.pprint, but like macrostep-pp or Emacs pp, i.e. aligning the text of the form in a nice way.

I don't see anything in the cider- namespace, and results from various CIDER commands don't do this (they just output the result in one long line), so my guess is "no".

@bbatsov
Copy link
Member

bbatsov commented Oct 6, 2016

CIDER's macroexpansion pretty-prints the macro forms (they are processed in the middleware).

I'm not sure it's a good idea to plug something into macrostep - I was thinking more in terms of just implementing the existing macroexpansion we have as on overlay. Seems to me this is pretty much what macrostep does. Integrations with 3rd party packages are notoriosly brittle and users generally don't want to install a ton of add-ons anyways.

@bbatsov
Copy link
Member

bbatsov commented Oct 6, 2016

P.S. I'm fine with developing a macrostep integration if macrostep brings a lot of added functionality to the table.

@xiongtx
Copy link
Member

xiongtx commented Oct 6, 2016

CIDER's macroexpansion pretty-prints the macro forms (they are processed in the middleware).

It does Clojure pprint, not Emacs pp. pp will take a one-liner like

(defn foo [x] (defn bar [y] (defn baz [z] (+ 1 2))))

and output:

(defn foo
  [x]
  (defn bar
    [y]
    (defn baz
      [z]
      (+ 1 2))))

which isn't quite right, but close. Clojure's pprint does not do this.

if macrostep brings a lot of added functionality to the table.

I'd think that the most desirable feature of macrostep is the "stepping" (which you can experiment with using the POC above), not just expanding inline. But I see that expanding inline is what the OP has asked for...does no one care about the stepping then?

@colonelpanic8
Copy link
Author

@xiongtx
You are correct that I was mostly asking for inline expansion, but I think that stepping is also a valuable feature that would certainly be nice to have.

@bbatsov
Copy link
Member

bbatsov commented Oct 7, 2016

@xiongtx I'm guessing clj-fmt or https://github.com/kkinnear/zprint should be able to format the code in this manner

@xiongtx
Copy link
Member

xiongtx commented Oct 7, 2016

Would that need to be pulled into nREPL or something? How do we deal with Clojure dependencies or extra code in CIDER?

@bbatsov
Copy link
Member

bbatsov commented Oct 7, 2016

Would that need to be pulled into nREPL or something? How do we deal with Clojure dependencies or extra code in CIDER?

We simply add them as deps in cider-nrepl. Ideally they should be exposed via some nREPL op instead of using direct evaluation - this gives us the flexibility to replace some dep, but the preserve the nREPL interface. You can see the clj-format code in cider-nrepl as an example.

@xiongtx
Copy link
Member

xiongtx commented Oct 7, 2016

It seems that cljfmt is already a dependency. Is that exposed through Emacs Lisp as an op or something?

@bbatsov
Copy link
Member

bbatsov commented Oct 8, 2016

Sure. See

(defun cider--format-buffer (formatter)

@xiongtx
Copy link
Member

xiongtx commented Oct 15, 2016

Macro-stepping is now part of cider-nrepl.

Do we want cider-macrostep to be part of CIDER or a separate repo? slime-macrostep is a contrib package for SLIME, though SLIME packages all contrib packages within SLIME itself.

@bbatsov
Copy link
Member

bbatsov commented Oct 16, 2016

I guess that depends on whether cider-macrostep depends on macrostep or not.

@bbatsov
Copy link
Member

bbatsov commented Jan 5, 2017

@xiongtx Ping :-)

@bbatsov
Copy link
Member

bbatsov commented Mar 2, 2017

@xiongtx Ping 2 ;-)

@xiongtx
Copy link
Member

xiongtx commented Mar 3, 2017

Trying to work on it, but I'm not satisfied with the current way of hooking into macrostep:

  • Formatting of code is not preserved
  • macrostep-next-macro and macrostep-previous-macro are not working; need to mark macros w/ text properties

Needs more investigation...

@colonelpanic8
Copy link
Author

@xiongtx status?

@colonelpanic8
Copy link
Author

@xiongtx @bbatsov
Stepping would be nice, but not essential to a v1. Since that seems to be the hold up, why not just get inline expansion working for now?

@vspinu
Copy link
Contributor

vspinu commented Oct 26, 2017

What is the added value of this? And does it really work? Macros could expand to something completely unrelated to the original code, so my gut feeling is that this functionality is bound to be broken outside of very basic macros.

Plus, there are benefits of having it in separate buffer. You see the whole expansion and can continue working on your macro at the same time. I personally find the "read-only" mode of elisp debugger quite limiting.

@stale
Copy link

stale bot commented Aug 7, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

@stale stale bot added the stale label Aug 7, 2019
@stale
Copy link

stale bot commented Sep 6, 2019

This issues been automatically closed due to lack of activity. Feel free to re-open it if you ever come back to it.

@stale stale bot closed this as completed Sep 6, 2019
@nbfalcon
Copy link

nbfalcon commented Mar 24, 2021

I have just pushed cider support in macrostep-geiser.

Alternatively, I can offer an adapted version of that package that is cider-specific and could theoretically be merged into cider (at the cost of lots of code/maintanence duplication): gist. Of course such a functionality would (at least at runtime) depend on macrostep.

@bbatsov
Copy link
Member

bbatsov commented Mar 25, 2021

@nbfalcon That's for letting me know. The package is relatively small, so I'm not particularly worried about code duplication. That being said - I'm fine with this being a separate package, although I think with a name with macrostep-geiser the likelihood any CIDER user coming across it would be more or less 0. I can mention it in the docs, but the problem is that no one reads the docs. :D

It'd be nice if there was a package named cider-macrostep/macrostep-cider that people can organically discover.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants