closing_labels.lua (2021B)
1 --[[ 2 ## Closing Labels 3 4 ** Method: 'dart/textDocument/publishClosingLabels** 5 Direction: Server -> Client Params: { uri: string, labels: { label: string, range: Range }[] } 6 This notifies the client when closing label information is available (or updated) for a file. 7 8 Since this is a notification, the callback needs to be registered in the client's callbacks table. 9 This can be achieved with nvim_lspconfig with this minimal config. 10 ```lua 11 nvim_lsp.dartls.setup{ 12 init_options = { 13 closingLabels = true, 14 }, 15 callbacks = { 16 ['dart/textDocument/publishClosingLabels'] = require('lsp_extensions.dart.closing_labels').get_callback{}, 17 }, 18 } 19 ``` 20 https://github.com/dart-lang/sdk/blob/master/pkg/analysis_server/tool/lsp_spec/README.md#darttextdocumentpublishclosinglabels-notification 21 --]] -- 22 local M = {} 23 24 -- Namespace for the virtual text 25 local closing_labels_ns = vim.api.nvim_create_namespace('lsp_extensions.dart.closing_labels') 26 27 -- Draws the newly published labels in the current buffer 28 -- @tparam table a table of options: highlight, prefix 29 -- @tparam table a table of labels for the current buffer 30 local draw_labels = function(opts, labels) 31 opts = opts or {} 32 local highlight = opts.highlight or 'Comment' 33 local prefix = opts.prefix or '// ' 34 vim.api.nvim_buf_clear_namespace(0, closing_labels_ns, 0, -1) 35 for _, label in pairs(labels) do 36 local end_line = label.range['end'].line 37 local text = prefix .. label.label 38 vim.api.nvim_buf_set_virtual_text(0, closing_labels_ns, end_line, {{text, highlight}}, {}) 39 end 40 end 41 42 -- Gets a callback to register to the dartls publishClosingLabels notification. 43 -- @tparam table a table of options: highlight, prefix 44 M.handle = function(opts) 45 return function(_, _, result, _, _) 46 local uri = result.uri 47 local labels = result.labels 48 -- This check is meant to prevent stray events from over-writing labels that 49 -- don't match the current buffer. 50 if uri == vim.uri_from_bufnr(0) then draw_labels(opts, labels) end 51 end 52 end 53 54 return M