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,42 +144,71 @@
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])
;(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"
(.. "(function_call
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 find-definition-fnl [lines symbol]
(let [query (vim.treesitter.parse_query (defn create-buf-with [lines]
"fennel" "create a buffer and fill it with the given lines"
(.. "((identifier) @symbol-name (#contains? @symbol-name \"" symbol "\"))")) (let [bufnr (vim.api.nvim_create_buf false true)]
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))
[tstree] (parser:parse)
tsnode (tstree:root) (defn find-definition-node-fnl [lines symbol]
code-lines []] (let [query (make-def-query symbol)
(each [id node metadata (query:iter_captures tsnode bufnr 0 -1)] bufnr (create-buf-with lines)
(let [parent (node:parent) parser (vim.treesitter.get_parser bufnr "fennel")
(r1 c1 r2 c2) (parent:range)] [tstree] (parser:parse)
(for [i (+ r1 1) r2] tsnode (tstree:root)]
(table.insert code-lines (. lines i))))) (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)
(r1 c1 r2 c2) (parent:range)]
(var code-lines [])
(for [i (+ r1 1) r2]
(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 []
(let [word (get-current-word) (xpcall
segs (utils.split-last word ".")] (fn []
(match segs (let [word (get-current-word)
[mod ident] segs (utils.split-last word ".")]
(gib-definition mod ident) (match segs
[mod ident]
[ident] (gib-definition mod ident)
(let [[current-file] (utils.split-last (vim.fn.expand "%:t") ".")]
(gib-definition current-file ident)))))
[ident]
(let [[current-file] (utils.split-last (vim.fn.expand "%:t") ".")]
(gib-definition current-file ident)))))
#(print (fennel.traceback $1))))
;(gib-definition "help-thingy" (ident))) ;(gib-definition "help-thingy" (ident)))