This commit is contained in:
elkowar 2021-05-11 21:16:17 +02:00
parent ee5ba5034a
commit a936d626bf
No known key found for this signature in database
GPG key ID: E321AD71B1D1F27F

View file

@ -37,14 +37,12 @@
(each [id node metadata (query-module-header:iter_captures tsnode bufnr 0 -1)] (each [id node metadata (query-module-header:iter_captures tsnode bufnr 0 -1)]
(let [name (. query-module-header.captures id) (let [name (. query-module-header.captures id)
(r1 c1 r2 c2) (node:range) (r1 c1 r2 c2) (node:range)
file-content (vim.api.nvim_buf_get_lines 0 0 -1 false) node-text (vim.treesitter.get_node_text node 0)]
node-text (str.join "\n" (in-range file-content r1 c1 r2 (- c2 1)))]
(match name (match name
:key (set last-module node-text) :key (set last-module node-text)
:value (tset modules last-module node-text)))) :value (tset modules last-module node-text))))
modules)) modules))
(defn get-current-word [] (defn get-current-word []
(let [col (. (vim.api.nvim_win_get_cursor 0) 2) (let [col (. (vim.api.nvim_win_get_cursor 0) 2)
line (vim.api.nvim_get_current_line)] line (vim.api.nvim_get_current_line)]
@ -146,32 +144,61 @@
result (icollect [line _ (io.lines path)] line)] result (icollect [line _ (io.lines path)] line)]
(values result ft)))) (values result ft))))
;(defn make-def-query [symbol])
(defn find-definition-fnl [lines symbol] ;(vim.treesitter.parse_query
(let [query (vim.treesitter.parse_query ;"fennel"
;(.. "(function_call
;name: (identifier) @fn-name (#eq? @fn-name \"defn\")
;(identifier) @symbol-name (#contains? @symbol-name \"" symbol "\"))")))
(defn make-def-query [symbol]
(vim.treesitter.parse_query
"fennel" "fennel"
(.. "((identifier) @symbol-name (#contains? @symbol-name \"" symbol "\"))")) (.. "(function_call
bufnr (vim.api.nvim_create_buf false true)] name: (identifier)
(identifier) @symbol-name (#contains? @symbol-name \"" symbol "\"))")))
;(defn make-def-query [symbol]
;(vim.treesitter.parse_query
;"fennel"
;(.. "((identifier) @symbol-name (#contains? @symbol-name \"" symbol "\"))")))
(defn create-buf-with [lines]
"create a buffer and fill it with the given lines"
(let [bufnr (vim.api.nvim_create_buf false true)]
(vim.api.nvim_buf_set_lines bufnr 0 -1 true lines) (vim.api.nvim_buf_set_lines bufnr 0 -1 true lines)
(let [parser (vim.treesitter.get_parser bufnr "fennel") bufnr))
(defn find-definition-node-fnl [lines symbol]
(let [query (make-def-query symbol)
bufnr (create-buf-with lines)
parser (vim.treesitter.get_parser bufnr "fennel")
[tstree] (parser:parse) [tstree] (parser:parse)
tsnode (tstree:root) tsnode (tstree:root)]
code-lines []]
(each [id node metadata (query:iter_captures tsnode bufnr 0 -1)] (each [id node metadata (query:iter_captures tsnode bufnr 0 -1)]
(let [name (. query.captures id)]
(when (= name "symbol-name")
(lua "return node"))))))
(defn find-definition-str-fnl [lines symbol]
(if-let [node (find-definition-node-fnl lines symbol)]
(let [parent (node:parent) (let [parent (node:parent)
(r1 c1 r2 c2) (parent:range)] (r1 c1 r2 c2) (parent:range)]
(var code-lines [])
(for [i (+ r1 1) r2] (for [i (+ r1 1) r2]
(table.insert code-lines (. lines i))))) (table.insert code-lines (. lines i)))
code-lines))) code-lines)))
(defn gib-definition [mod word] (defn gib-definition [mod word]
(let [imports (read-module-imports-fnl 0) (let [imports (read-module-imports-fnl 0)
actual-mod (or (. imports mod) mod) actual-mod (or (. imports mod) mod)
(module-lines module-ft) (read-module-file actual-mod) (module-lines module-ft) (read-module-file actual-mod)]
definition-lines (find-definition-fnl module-lines word)] (if-let [definition-lines (find-definition-str-fnl module-lines word)]
(pop definition-lines module-ft))) (pop definition-lines module-ft))))
(fn _G.gib_def [] (fn _G.gib_def []
(xpcall
(fn []
(let [word (get-current-word) (let [word (get-current-word)
segs (utils.split-last word ".")] segs (utils.split-last word ".")]
(match segs (match segs
@ -181,7 +208,7 @@
[ident] [ident]
(let [[current-file] (utils.split-last (vim.fn.expand "%:t") ".")] (let [[current-file] (utils.split-last (vim.fn.expand "%:t") ".")]
(gib-definition current-file ident))))) (gib-definition current-file ident)))))
#(print (fennel.traceback $1))))
;(gib-definition "help-thingy" (ident))) ;(gib-definition "help-thingy" (ident)))