more stuff

This commit is contained in:
elkowar 2021-05-11 19:21:48 +02:00
parent c9c69c5ca9
commit ee5ba5034a
No known key found for this signature in database
GPG key ID: E321AD71B1D1F27F
5 changed files with 119 additions and 40 deletions

View file

@ -7,25 +7,61 @@
ts nvim-treesitter} ts nvim-treesitter}
require-macros [macros]}) require-macros [macros]})
(defn in-range [lines r1 c1 r2 c2]
(var res [])
(for [i r1 r2]
(let [line (. lines (+ i 1))
trimmed-line (line:sub (if (= r1 i) (+ c1 1) 1)
(if (= r2 i) (+ c2 1) -1))]
(table.insert res trimmed-line)))
res)
(def- query-module-header
(vim.treesitter.parse_query
"fennel"
"(function_call
name: (identifier) @module-header-name (#eq? @module-header-name \"module\")
(identifier) @module-name
(table ((identifier) @import-type
(table ((identifier) @key (_) @value)*)
)*
)
)"))
(defn read-module-imports-fnl [bufnr]
(let [parser (vim.treesitter.get_parser bufnr "fennel")
[tstree] (parser:parse)
tsnode (tstree:root)]
(var last-module nil)
(var modules {})
(each [id node metadata (query-module-header:iter_captures tsnode bufnr 0 -1)]
(let [name (. query-module-header.captures id)
(r1 c1 r2 c2) (node:range)
file-content (vim.api.nvim_buf_get_lines 0 0 -1 false)
node-text (str.join "\n" (in-range file-content r1 c1 r2 (- c2 1)))]
(match name
:key (set last-module node-text)
:value (tset modules last-module node-text))))
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)]
(.. (vim.fn.matchstr (string.sub line 1 (+ col 1)) (.. (vim.fn.matchstr (line:sub 1 (+ col 1))
"\\k*$") "\\k*$")
(string.sub ( vim.fn.matchstr (string.sub line (+ col 1)) (string.sub (vim.fn.matchstr (line:sub (+ col 1))
"^\\k*") "^\\k*")
2)))) 2))))
(def helpfiles-path (str.join "/" (a.butlast (str.split vim.o.helpfile "/")))) (def helpfiles-path (str.join "/" (a.butlast (str.split vim.o.helpfile "/"))))
(def tags (def tags
(let [entries {}] (var entries {})
(each [line _ (io.lines (.. helpfiles-path "/tags"))] (each [line _ (io.lines (.. helpfiles-path "/tags"))]
(let [[key file address] (str.split line "\t")] (let [[key file address] (str.split line "\t")]
(tset entries key {:file (.. helpfiles-path "/" file) :address address}))) (tset entries key {:file (.. helpfiles-path "/" file) :address address})))
entries)) entries)
(defn find-help-tag-for [topic] (defn find-help-tag-for [topic]
(or (. tags topic) (or (. tags topic)
@ -53,10 +89,10 @@
(lua "return data"))))) (lua "return data")))))
(defn pop [text ft] (defn pop [text ft]
(var width 0) "Open a popup with the given text and filetype"
(var width 20)
(each [_ line (ipairs text)] (each [_ line (ipairs text)]
(when (> (length line) width) (set width (math.max width (length line))))
(set width (length line))))
(let [bufnr (vim.api.nvim_create_buf false true)] (let [bufnr (vim.api.nvim_create_buf false true)]
(vim.api.nvim_buf_set_option bufnr :bufhidden "wipe") (vim.api.nvim_buf_set_option bufnr :bufhidden "wipe")
(vim.api.nvim_buf_set_option bufnr :filetype ft) (vim.api.nvim_buf_set_option bufnr :filetype ft)
@ -65,15 +101,15 @@
(fn _G.get_help [] (fn _G.get_help []
(let [help-tag (find-help-tag-for (get-current-word))] (if-let [help-tag (find-help-tag-for (get-current-word))]
(when help-tag (pop (help-for-tag help-tag) :help)))
(pop (help-for-tag help-tag) :help))))
(def all-module-paths (def all-module-paths
(let [paths (str.split package.path ";")] (do
(var paths (str.split package.path ";"))
(each [_ path (ipairs (str.split vim.o.runtimepath ","))] (each [_ path (ipairs (str.split vim.o.runtimepath ","))]
(table.insert paths (.. path "/fnl/?.fnl")) (table.insert paths (.. path "/fnl/?.fnl"))
(table.insert paths (.. path "/fnl/?/init.fnl")) (table.insert paths (.. path "/fnl/?/init.fnl"))
@ -95,25 +131,29 @@
all-module-paths))) all-module-paths)))
(defn get-filetype [filename]
"Return the filetype given a files name"
(match (utils.split-last filename ".")
[_ :fnl] "fennel"
[_ :lua] "lua"))
(defn read-module-file [module-name] (defn read-module-file [module-name]
(let [path (find-module-path module-name)] "Given the name of a module, returns two values:
(when path the lines of the file that matched a given module
(let [ft (match (string.gsub path ".+%.(%w+)" "%1") and the filetype of that module"
:fnl "fennel" (if-let [path (find-module-path module-name)]
:lua "lua") (let [ft (get-filetype path)
result (icollect [line _ (io.lines path)] result (icollect [line _ (io.lines path)] line)]
line)] (values result ft))))
(values result ft)))))
(defn gib-definition [mod word] (defn find-definition-fnl [lines symbol]
(let [(file-lines filetype) (read-module-file mod) (let [query (vim.treesitter.parse_query
query (vim.treesitter.parse_query "fennel"
filetype (.. "((identifier) @symbol-name (#contains? @symbol-name \"" symbol "\"))"))
(.. "((identifier) @fuck (#contains? @fuck \"" word "\"))"))
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 file-lines) (vim.api.nvim_buf_set_lines bufnr 0 -1 true lines)
(let [parser (vim.treesitter.get_parser bufnr filetype) (let [parser (vim.treesitter.get_parser bufnr "fennel")
[tstree] (parser:parse) [tstree] (parser:parse)
tsnode (tstree:root) tsnode (tstree:root)
code-lines []] code-lines []]
@ -121,21 +161,25 @@
(let [parent (node:parent) (let [parent (node:parent)
(r1 c1 r2 c2) (parent:range)] (r1 c1 r2 c2) (parent:range)]
(for [i (+ r1 1) r2] (for [i (+ r1 1) r2]
(table.insert code-lines (. file-lines i))))) (table.insert code-lines (. lines i)))))
(pop code-lines filetype)))) code-lines)))
(defn gib-definition [mod word]
(let [imports (read-module-imports-fnl 0)
actual-mod (or (. imports mod) mod)
(module-lines module-ft) (read-module-file actual-mod)
definition-lines (find-definition-fnl module-lines word)]
(pop definition-lines module-ft)))
(fn _G.gib_def [] (fn _G.gib_def []
(let [word (get-current-word) (let [word (get-current-word)
segs (str.split word "%.")] segs (utils.split-last word ".")]
(match segs (match segs
[mod ident] [mod ident]
(gib-definition mod ident) (gib-definition mod ident)
[ident] [ident]
(let [[current-file] (str.split (vim.fn.expand "%:t") "%.")] (let [[current-file] (utils.split-last (vim.fn.expand "%:t") ".")]
(gib-definition current-file ident))))) (gib-definition current-file ident)))))

View file

@ -52,6 +52,11 @@
,r# (. data# (+ i# 1))] ,r# (. data# (+ i# 1))]
,...))))) ,...)))))
:if-let
(fn [[name value] ...]
`(let [,name ,value]
(when ,name ,...)))
:packer-use :packer-use
(fn [...] (fn [...]
(let [a (require "aniseed.core") (let [a (require "aniseed.core")

View file

@ -17,6 +17,7 @@
:JoosepAlviste/nvim-ts-context-commentstring {} :JoosepAlviste/nvim-ts-context-commentstring {}
:nvim-treesitter/nvim-treesitter {:mod "plugins.treesitter" :nvim-treesitter/nvim-treesitter {:mod "plugins.treesitter"
:run ":TSUpdate"} :run ":TSUpdate"}
:nvim-treesitter/playground {}
; :code-biscuits/nvim-biscuits {} ; show opening line after closing curly ; :code-biscuits/nvim-biscuits {} ; show opening line after closing curly

View file

@ -18,7 +18,26 @@
; Might fuck with gitsigns ; Might fuck with gitsigns
;:rainbow {:enable true ;:rainbow {:enable true
;:extended_mode true} ;:extended_mode true}
:context_commentstring {:enable true}}) :context_commentstring {:enable true}
:playground
{:enable true
:disable {}
:updatetime 25 ; Debounced time for highlighting nodes in the playground from source code
:persist_queries false ; Whether the query persists across vim sessions
:keybindings
{:toggle_query_editor "o"
:toggle_hl_groups "i"
:toggle_injected_languages "t"
:toggle_anonymous_nodes "a"
:toggle_language_display "I"
:focus_language "f"
:unfocus_language "F"
:update "R"
:goto_node "<cr>"
:show_help "?"}}})
;:indent {:enable true} ;:indent {:enable true}
;:disable ["lua"] ;:disable ["lua"]

View file

@ -24,6 +24,16 @@
(values k v)))) (values k v))))
(defn split-last [s sep]
"split a string at the last occurrence of a separator"
(for [i (length s) 1 -1]
(let [c (s:sub i i)]
(when (= sep c)
(let [left (s:sub 1 (- i 1))
right (s:sub (+ i 1))]
(lua "return { left, right }")))))
[s])
(defn find-where [pred xs] (defn find-where [pred xs]
(each [_ x (ipairs xs)] (each [_ x (ipairs xs)]
(when (pred x) (when (pred x)