2021-02-07 21:56:19 +00:00
|
|
|
(local util (require :lib.util))
|
|
|
|
(local core (require :core))
|
|
|
|
(local command (require :core.command))
|
|
|
|
(local keymap (require :core.keymap))
|
|
|
|
(local common (require :core.common))
|
|
|
|
|
2022-04-10 04:20:18 +00:00
|
|
|
(fn selected-form []
|
2021-02-07 21:56:19 +00:00
|
|
|
(let [ldoc core.active_view.doc
|
2022-04-10 04:20:18 +00:00
|
|
|
(aline acol bline bcol) (ldoc:get_selection)]
|
2021-02-07 21:56:19 +00:00
|
|
|
(if (and (= aline bline) (= acol bcol))
|
2022-04-10 16:47:44 +00:00
|
|
|
(ldoc:get_text aline 1 aline math.huge)
|
2022-04-10 04:20:18 +00:00
|
|
|
(ldoc:get_text aline acol bline bcol))))
|
|
|
|
|
2022-04-10 16:03:16 +00:00
|
|
|
(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))))
|
2022-04-10 16:47:44 +00:00
|
|
|
(if (and start (<= start i-target)) (values start end)
|
2022-04-10 16:03:16 +00:00
|
|
|
(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))
|
2022-04-10 16:47:44 +00:00
|
|
|
(let [line (ldoc:get_text aline 1 aline math.huge)
|
2022-04-10 16:03:16 +00:00
|
|
|
(start end) (find-closest line symbol-pattern acol)]
|
|
|
|
(ldoc:get_text aline start aline (+ end 1)))
|
|
|
|
(ldoc:get_text aline acol bline bcol))))
|
|
|
|
|
2022-04-10 04:20:18 +00:00
|
|
|
(fn inline-eval [eval]
|
|
|
|
(let [ldoc core.active_view.doc
|
|
|
|
(aline acol bline bcol) (ldoc:get_selection)]
|
|
|
|
(ldoc:insert bline bcol (eval (selected-form)))))
|
2021-02-07 21:56:19 +00:00
|
|
|
|
|
|
|
(require :editor.editmode)
|
|
|
|
|
2021-03-29 05:03:43 +00:00
|
|
|
(command.add :editor.replview {
|
|
|
|
"repl:submit" #(core.active_view:submit)
|
|
|
|
})
|
|
|
|
|
2022-04-10 04:20:18 +00:00
|
|
|
(local {: show : submit} (util.require :inspector.debug))
|
2022-04-10 16:03:16 +00:00
|
|
|
(local replsession (require :editor.replsession))
|
2021-03-29 05:03:43 +00:00
|
|
|
(command.add nil {
|
2022-04-10 04:20:18 +00:00
|
|
|
"repl:create" #(show)
|
|
|
|
})
|
2022-04-10 16:03:16 +00:00
|
|
|
|
|
|
|
(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))
|
2022-04-10 16:47:44 +00:00
|
|
|
ldoc (when (> (length filename) 0) (core.open_doc filename))]
|
|
|
|
(when ldoc
|
|
|
|
(core.root_view:open_doc ldoc)
|
|
|
|
(ldoc:set_selection line 1 line 1)
|
2022-04-10 16:03:16 +00:00
|
|
|
true))))
|
|
|
|
(core.log (.. "Unable to find symbol " symbol))))
|
|
|
|
(: (replsession.session) :submit (.. ",find " symbol) jump-to-find-result))
|
|
|
|
|
2022-04-10 16:47:44 +00:00
|
|
|
(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)))
|
|
|
|
|
2022-04-10 04:20:18 +00:00
|
|
|
(command.add :core.docview {
|
2022-04-10 16:47:44 +00:00
|
|
|
"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))
|
2021-03-29 05:03:43 +00:00
|
|
|
})
|
2022-04-10 16:03:16 +00:00
|
|
|
|
2021-03-29 05:03:43 +00:00
|
|
|
(keymap.add {
|
|
|
|
:return "repl:submit"
|
2022-04-10 16:47:44 +00:00
|
|
|
"alt+e" "fennel:eval"
|
|
|
|
"alt+d" "fennel:go-to-definition-under-cursor"
|
2021-03-29 05:03:43 +00:00
|
|
|
})
|
|
|
|
|
2022-04-10 16:03:16 +00:00
|
|
|
{: inline-eval : symbol-pattern}
|
|
|
|
|