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

simple way to evaluate context sensitive expressions #2161

Closed
qq00 opened this issue Jan 11, 2018 · 3 comments
Closed

simple way to evaluate context sensitive expressions #2161

qq00 opened this issue Jan 11, 2018 · 3 comments

Comments

@qq00
Copy link

qq00 commented Jan 11, 2018

motivation

file a ticket and put there our conversation highlights. Then everything is possible. -- @bbatsov 

problem

Consider the following expression:

(let [doge 20]
  (def awesome (fn [x] (* x 3)))
  (let [cider (awesome doge)]
    (+ cider doge) ;; cursor is here
    (stuff we d9n 't care about)
    (more stuff we dont care about)))

Is there a wa yto make C-x C-e work ?

key idea

  1. find 'outermost enclosing sexp' by repeatedly looking up parent element
  2. prematurely end sexp by adding )]} at cursor
  3. evaluate the 'new parent sexp'
  4. in the above case, we end up executing
(let [doge 20]
  (def awesome (fn [x] (* x 3)))
  (let [cider (awesome doge)]
    (+ cider doge) ;; cursor is here))

praise for this approach

That’s actual much simpler as we already have the code that finds the top-level expression and the only thing needed it just terminate the expression after the current sexp. Should be trivial to implement.
-- @bbatsov 
@mallt
Copy link
Contributor

mallt commented Jan 12, 2018

Maybe a workaround, but the scope-capture library might already help you out here, I made a small screencast for your example:

scope-capture-screencast

@qq00
Copy link
Author

qq00 commented Jan 13, 2018

Possible solution:


(defun find-outer ()
  (let ((beg-loc (point))
	(end-loc (point)))
    (save-excursion 
      (lispy-left 1)
      (while (not (= beg-loc (point)))
	(setq beg-loc (point))
	(lispy-left 1)
	(message "looking for start: %s %s" (point) beg-loc ))
      (lispy-right 1)
      (setq end-loc (point))
      (list beg-loc end-loc)))) 

(defun magic-yank ()
  (interactive)
  (let* ((lst (find-outer))
	 (beg-loc (car lst))
	 (mid-loc (point))
	 (end-loc (cadr lst))
	 (ss (buffer-substring beg-loc end-loc)))
    (with-temp-buffer 
      (clojurec-mode)
      (lispy-mode)
      (lispyville-mode)
      (insert ss)
      (message "full expr: %s" ss)
      (lispyville-delete (- mid-loc beg-loc) (point-max))
      (setq ss (buffer-substring (point-min) (point-max)))) 
    (message "sub exp: %s" ss)
    (cider-interactive-eval ss)))

// putting this code in the public domain

@bbatsov
Copy link
Member

bbatsov commented Jan 14, 2018

Check out cider-eval-defun-to-point. I guess you'll like it. 😄

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

No branches or pull requests

3 participants