(local util (require :lib.util)) (local core (require :core)) (local command (require :core.command)) (local keymap (require :core.keymap)) (local common (require :core.common)) (fn selected-form [] (let [ldoc core.active_view.doc (aline acol bline bcol) (ldoc:get_selection)] (if (and (= aline bline) (= acol bcol)) (ldoc:get_text aline 1 aline math.huge) (ldoc:get_text aline acol bline bcol)))) (fn find-closest [s pattern i-target] (var (start end) nil) (set (start end) (s:find pattern)) (while (and start (< end (- i-target 1))) (set (start end) (s:find pattern (+ end 1)))) (if (and start (<= start i-target)) (values start end) (values 1 0))) (local symbol-pattern "[a-zA-Z%!%@%#%$%%%^%&%*%<%>%?%/%~%-%_%=%+][a-zA-Z%!%@%#%$%%%^%&%*%<%>%?%/%~%-%_%=%+0-9%.%:]*") (fn selected-symbol [] (let [ldoc core.active_view.doc (aline acol bline bcol) (ldoc:get_selection)] (if (and (= aline bline) (= acol bcol)) (let [line (ldoc:get_text aline 1 aline math.huge) (start end) (find-closest line symbol-pattern acol)] (ldoc:get_text aline start aline (+ end 1))) (ldoc:get_text aline acol bline bcol)))) (fn inline-eval [eval] (let [ldoc core.active_view.doc (aline acol bline bcol) (ldoc:get_selection)] (ldoc:insert bline bcol (eval (selected-form))))) (require :editor.editmode) (command.add :editor.replview { "repl:submit" #(core.active_view:submit) }) (local {: show : submit} (util.require :inspector.debug)) (local replsession (require :editor.replsession)) (command.add nil { "repl:create" #(show) }) (fn go-to-definition [symbol] (fn jump-to-find-result [result] (when (not (match result {:vals [loc]} (let [(filename line) (when loc (loc:match "(.*):([0-9]+)")) filename (or filename "") filename (if (filename:find "^%.%.%.") "" (or (filename:match "^%./(.*)") filename)) line (tonumber (or line 0)) ldoc (when (> (length filename) 0) (core.open_doc filename))] (when ldoc (core.root_view:open_doc ldoc) (ldoc:set_selection line 1 line 1) true)))) (core.log (.. "Unable to find symbol " symbol)))) (: (replsession.session) :submit (.. ",find " symbol) jump-to-find-result)) (fn autocomplete-results [text] (var result []) (: (replsession.session) :submit (.. ",complete " text) #(set result (or $1.vals []))) result) (fn autocomplete-symbol [callback ?starting-text] (fn fixup-result [text] (let [[completed-result] (autocomplete-results text)] (callback (or completed-result text)))) (core.command_view:enter "Symbol" fixup-result autocomplete-results) (when ?starting-text (core.command_view:set_text ?starting-text))) (command.add :core.docview { "fennel:eval" #(submit nil (selected-form)) "fennel:go-to-definition-under-cursor" #(go-to-definition (selected-symbol)) "fennel:go-to-definition" #(autocomplete-symbol #(go-to-definition $1) (selected-symbol)) }) (keymap.add { :return "repl:submit" "alt+e" "fennel:eval" "alt+d" "fennel:go-to-definition-under-cursor" }) {: inline-eval : symbol-pattern}