LightTable / Clojure

Light Table Clojure language plugin
MIT License
99 stars 47 forks source link

Add behavior to select word or form under cursor #60

Closed kenny-evitt closed 8 years ago

kenny-evitt commented 8 years ago

See LightTable/LightTable#744; the original comment:

Idea has this great feature where you can hit a hotkey and select the word underneath the cursor. If you have a selection, then the same hotkey will 'grow' the selection to be the parent word or form of the current selection. In this way, you can very quickly select and manipulate code in terms of forms rather than by line. This works really well when combined with the eval hotkey (cmd-enter in LT) and would be a great fit in LT IMHO.

The following video shows how this works in Idea: http://www.youtube.com/watch?v=VE6go04EZbw

A deeper textual example:

Given the following clojure code with my cursor is where the | is:

(defn- param? [params k]
  (and
    (contains? params k)
    (not (blank-o|r-nil? (get params k)))))

Pressing the hotkey in this state, selects the entire symbol

blank-or-nil? 

Pressing the same hotkey again, selects the entire enclosing form for that symbol

(blank-or-nil? (get params k))

..and again, the next enclosing form...

(not (blank-or-nil? (get params k)))

...and so on, until the entire outer form is selected

(defn- param? [params k]
  (and
    (contains? params k)
    (not (blank-or-nil? (get params k)))))

This comment also links to code that could be used to implement this:

Cool to see enthusiasm for a feature. Unfortunately, we have higher priority items to fix and improve (including paredit). Happy to discuss a pull with anyone interested in developing this. Fwiw, I currently highlight the current clojure word with this command. It needs to be improved though since it highlights differently than :paredit.select.parent. Probably would be fixed with the right selection call to codemirror.

The code linked-to in the above quoted comment:

(defn current-word []
  (:string (clojure/find-symbol-at-cursor (pool/last-active))))

(defn find-next-clojure-word []
  (let [word (current-word)]
    (find/set-val find/bar word) ;; necessary for find.next to work
    (object/raise find/bar :search! word)))

(cmd/command {:command :ltfiles.find-next-clojure-word
              :desc "ltfiles: Finds next clojure word"
              :exec find-next-clojure-word})
rundis commented 8 years ago

Parembrace allready has this feature :)

kenny-evitt commented 8 years ago

@rundis Of course it does! :smile:

kenny-evitt commented 8 years ago

Closing as this is already available in another plugin (that's also awesome).