bomberpac/editor/init.fnl

78 lines
2.7 KiB
Fennel

(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 10000000)
(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 (<= 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 10000000)
(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))
doc (when (> (length filename) 0) (core.open_doc filename))]
(when doc
(core.root_view:open_doc doc)
(doc:set_selection line 1 line 1)
true))))
(core.log (.. "Unable to find symbol " symbol))))
(: (replsession.session) :submit (.. ",find " symbol) jump-to-find-result))
(command.add :core.docview {
"repl:eval" #(submit nil (selected-form))
"repl:go-to-definition" #(go-to-definition (selected-symbol))
})
(keymap.add {
:return "repl:submit"
"alt+e" "repl:eval"
"alt+d" "repl:go-to-definition"
})
{: inline-eval : symbol-pattern}