1
0
Fork 0

nvim: LSP utility functions

This commit is contained in:
Daniel Kempkens 2022-05-04 15:03:51 +02:00
parent 674e1bda01
commit a6065908bd
6 changed files with 134 additions and 32 deletions

View file

@ -11,7 +11,8 @@
npairs (require :nvim-autopairs)
gitsigns (require :gitsigns)
repl (require :nifoc.repl)
formatting (require :nifoc.formatting)]
formatting (require :nifoc.formatting)
nifoc-lsp (require :nifoc.lsp)]
(fn map-entry [key cmd opts]
(vim.tbl_extend :keep {1 key 2 cmd} opts))
@ -108,7 +109,9 @@
#(telescope-builtin.lsp_implementations telescope-dropdown)
{:buffer bufnr :desc "Find Implementations"})
(keymap.set :n :K vim.lsp.buf.hover
{:buffer bufnr :desc "Show Documentation"}))
{:buffer bufnr :desc "Show Documentation"})
(keymap.set :n :<leader>ddsc nifoc-lsp.symbols-under-cursor
{:buffer bufnr :desc "Document Symbols Under Cursor"}))
(fn mod.terminal-open [bufnr]
(let [map-opts {:buffer bufnr}]

View file

@ -1,18 +1,29 @@
;; Based on https://github.com/nvim-lua/lsp-status.nvim
(let [mod {}
current-function-symbols {:Class ""
:Enum "ﴳ"
:Function ""
:Interface ""
:Method ""
:Module ""
:Namespace ""
:Package ""
:Struct "ﴳ"}
current-function-symbols {:Class " "
:Enum "ﴳ "
:Function " "
:Interface " "
:Method " "
:Module " "
:Namespace " "
:Package " "
:Struct "ﴳ "}
spinner-frames ["⣾" "⣽" "⣻" "⢿" "⡿" "⣟" "⣯" "⣷"]
client-notifications {}
lsp-proto vim.lsp.protocol
set-bufvar vim.api.nvim_buf_set_var
augroup (vim.api.nvim_create_augroup :NifocLsp {:clear true})
aucmd vim.api.nvim_create_autocmd]
(fn filter-sorted-table [test list]
(let [results []]
(each [_ item (ipairs list)]
(when (test item)
(table.insert results item)))
results))
;; Current LSP Context
;; Based on https://github.com/nvim-lua/lsp-status.nvim
(fn extract-symbols [acc items]
(if (= items nil)
acc
@ -28,7 +39,7 @@
(set sym-range.end.line (+ sym-range.end.line 1))))
(table.insert acc {:range sym-range : kind :text item.name})
(when item.children
(extract-symbols item.children acc)))
(extract-symbols acc item.children)))
acc)))
(fn current-function-symbol? [item]
@ -42,24 +53,112 @@
(and (= line sym.end.line) (> char sym.end.character)) false
true)))
(fn handle-symbols-under-cursor [err result ctx config]
(when (and (= err nil) (= (type result) :table))
(let [symbols (extract-symbols [] result)
cursor-pos (vim.api.nvim_win_get_cursor 0)]
(print (.. "Symbols at under current position (" (. cursor-pos 1) ":"
(. cursor-pos 2) ")"))
(each [_ sym (ipairs symbols)]
(when (cursor-in-range? cursor-pos sym.range)
(print (.. sym.kind ": " sym.text " (" sym.range.start.line ":"
sym.range.start.character " - " sym.range.end.line ":"
sym.range.end.character ")")))))))
(fn handle-update-current-context [err result ctx config]
(set-bufvar ctx.bufnr :nifoc_lsp_current_context "")
(var context-levels [])
(when (and (= err nil) (= (type result) :table))
(let [filtered-symbols (->> result (extract-symbols [])
(vim.tbl_filter current-function-symbol?))
(filter-sorted-table current-function-symbol?))
cursor-pos (vim.api.nvim_win_get_cursor 0)]
(for [i (length filtered-symbols) 1 -1]
(local sym (. filtered-symbols i))
(each [_ sym (ipairs filtered-symbols)]
(when (cursor-in-range? cursor-pos sym.range)
(let [current-context (.. (. current-function-symbols sym.kind) " "
(let [current-context (.. (. current-function-symbols sym.kind)
sym.text)]
(set-bufvar ctx.bufnr :nifoc_lsp_current_context current-context)))))))
(table.insert context-levels current-context))))))
(set-bufvar ctx.bufnr :nifoc_lsp_current_context
(table.concat context-levels "  "))
(vim.api.nvim_command :redrawstatus))
(fn update-current-context [bufnr]
(let [params {:textDocument (vim.lsp.util.make_text_document_params bufnr)}]
(vim.lsp.buf_request bufnr :textDocument/documentSymbol params
handle-update-current-context)))
;; Progress Notifications
;; Based on: https://github.com/rcarriga/nvim-notify/wiki/Usage-Recipes#progress-updates
(fn get-notification-data [client-id token]
(when (= (. client-notifications client-id) nil)
(tset client-notifications client-id {}))
(when (= (. client-notifications client-id token) nil)
(tset client-notifications client-id token {}))
(. client-notifications client-id token))
(fn update-notifcation-spinner [client-id token]
(let [notification-data (get-notification-data client-id token)]
(when notification-data.spinner
(let [new-spinner (% (+ notification-data.spinner 1)
(length spinner-frames))]
(set notification-data.spinner new-spinner)
(set notification-data.notification
(vim.notify nil nil
{:hide_from_history true
:icon (. spinner-frames new-spinner)
:replace notification-data.notification}))
(vim.defer_fn #(update-notifcation-spinner client-id token))))))
(fn format-notifcation-title [title client-name]
(.. client-name (if (> (length title) 0) (.. ": " title) "")))
(fn format-notifcation-message [message percentage]
(.. (if percentage (.. percentage "%\t") "") (if message message "")))
(fn handle-progress [err result ctx]
(let [client-id ctx.client_id
val result.value
kind val.kind
lsp-client (vim.lsp.get_client_by_id client-id)
client-name lsp-client.name
notification-data (get-notification-data client-id result.token)]
(if (= kind :begin)
(let [message (format-notifcation-message val.message val.percentage)]
(set notification-data.notification
(vim.notify message :info
{:title (format-notifcation-title val.title
client-name)
:icon (. spinner-frames 1)
:timeout false
:hide_from_history false}))
(set notification-data.spinner 1)
(update-notifcation-spinner client-id result.token))
(and (= kind :report) notification-data)
(let [message (format-notifcation-message val.message val.percentage)]
(set notification-data.notification
(vim.notify message :info
{:replace notification-data.notification
:hide_from_history false})))
(and (= kind :end) notification-data)
(let [message (if val.message
(format-notifcation-message val.message)
:Complete)]
(set notification-data.notification
(vim.notify message :info
{:icon ""
:replace notification-data.notification
:timeout 3000}))
(set notification-data.spinner nil)))))
;; Public Interface
(fn mod.register-progress-handler []
(tset vim.lsp.handlers :$/progress handle-progress))
(fn mod.symbols-under-cursor []
(let [params {:textDocument (vim.lsp.util.make_text_document_params 0)}]
(vim.lsp.buf_request 0 :textDocument/documentSymbol params
handle-symbols-under-cursor)))
(fn mod.on-attach [client bufnr]
(when client.server_capabilities.documentSymbolProvider
(aucmd [:CursorHold]

View file

@ -31,15 +31,10 @@
(.. ftype " " icon))
"no ft")))
(fn mod.gitsigns-formatter [status]
(let [{: added : changed : removed} status
result {}]
(maybe-insert-git-status "%%#GitSignsStatuslineAdd# %s" added result)
(maybe-insert-git-status "%%#GitSignsStatuslineChange# %s" changed
result)
(maybe-insert-git-status "%%#GitSignsStatuslineDelete# %s" removed
result)
(table.concat result " ")))
(fn mod.gitsigns-diff-source []
(let [signs vim.b.gitsigns_status_dict]
(when signs
{:added signs.added :modified signs.changed :removed signs.removed})))
(fn mod.current-function []
(let [ctx vim.b.nifoc_lsp_current_context]

View file

@ -1,5 +1,4 @@
(let [gitsigns (require :gitsigns)
ns (require :nifoc.statusline)]
(let [gitsigns (require :gitsigns)]
(gitsigns.setup {:signs {:add {:hl :GitSignsAdd
:text "│"
:numhl :GitSignsAddNr
@ -22,7 +21,6 @@
:linehl :GitSignsChangeLn}}
:numhl false
:linehl false
:status_formatter ns.gitsigns-formatter
:diff_opts {:internal true}
:preview_config {:border :rounded}}))

View file

@ -21,6 +21,7 @@
(vim.lsp.with vim.lsp.handlers.hover {:border :rounded}))
(tset vim.lsp.handlers :textDocument/signatureHelp
(vim.lsp.with vim.lsp.handlers.signature_help {:border :rounded}))
(nifoc-lsp.register-progress-handler)
;; Servers
(let [default-capabilities (vim.lsp.protocol.make_client_capabilities)
capabilities (cmp.update_capabilities default-capabilities)
@ -37,6 +38,7 @@
:html
:rnix
:sqls
:svelte
:taplo
:yamlls]]
;; Default

View file

@ -7,7 +7,11 @@
:icons_enabled true}
:sections {; Left
:lualine_a [:mode]
:lualine_b ["b:gitsigns_status"
:lualine_b [{1 :diff
:source ns.gitsigns-diff-source
:symbols {:added " "
:modified " "
:removed " "}}
{1 :diagnostics
:sources [:nvim_diagnostic]
:symbols {:error " "
@ -23,3 +27,4 @@
ns.spell-enabled?]
:lualine_y [ns.current-line-percent]
:lualine_z [ns.line-column]}}))