dots-of-war/files/.config/nvim/fnl/help-thingy.fnl
2021-05-11 11:07:17 +02:00

82 lines
2.4 KiB
Fennel

(module help-thingy
{autoload {utils utils
a aniseed.core
str aniseed.string
fennel aniseed.fennel
popup popup}
require-macros [macros]})
(defn get-current-word []
(let [col (. (vim.api.nvim_win_get_cursor 0) 2)
line (vim.api.nvim_get_current_line)]
(.. (vim.fn.matchstr (string.sub line 1 (+ col 1))
"\\k*$")
(string.sub ( vim.fn.matchstr (string.sub line (+ col 1))
"^\\k*")
2))))
(def helpfiles-path (str.join "/" (a.butlast (str.split vim.o.helpfile "/"))))
(def tags
(let [entries {}]
(each [line _ (io.lines (.. helpfiles-path "/tags"))]
(let [[key file address] (str.split line "\t")]
(tset entries key {:file (.. helpfiles-path "/" file) :address address})))
entries))
(defn find-help-tag-for [topic]
(or (. tags topic)
(. tags (.. topic "()"))
(. tags (.. (string.gsub topic "vim.api." "") "()"))
(. tags (.. (string.gsub topic "vim.fn." "") "()"))
(. tags (.. (string.gsub topic "fn." "") "()"))
(. tags (.. (string.gsub topic "vim.o." "") "()"))
(. tags (.. (string.gsub topic "vim.b." "") "()"))
(. tags (.. (string.gsub topic "vim.g." "") "()"))))
(defn help-for-tag [tag]
(var data nil)
(each [line _ (io.lines tag.file)]
(if (= nil data)
(when (~= -1 (vim.fn.match line (tag.address:sub 2)))
(set data [line]))
(if (or (> 2 (length data))
(= "" line)
(= " " (line:sub 1 1))
(= "\t" (line:sub 1 1))
(= "<" (line:sub 1 1)))
(table.insert data line)
(lua "return data")))))
(defn pop [text]
(var width 0)
(each [_ line (ipairs text)]
(when (> (length line) width)
(set width (length line))))
(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 :filetype "help")
(vim.api.nvim_buf_set_lines bufnr 0 -1 true text)
(popup.create bufnr {:padding [1 1 1 1] :width width})))
(fn _G.get_help []
(let [help-tag (find-help-tag-for (get-current-word))]
(when help-tag
(pop (help-for-tag help-tag)))))
(utils.keymap :n :L ":call v:lua.get_help()<CR>")
(def rtp vim.o.runtimepath)
(str.split rtp ",")
; vim.api.nvim_buf_get_name
; vim.api.nvim_buf_call
; vim.api.nvim_buf_set_text
; vim.api.nvim_buf_set_var
; vim.fn.bufnr