Skip to content
This repository was archived by the owner on Jul 7, 2022. It is now read-only.

Commit 7dec4af

Browse files
authored
feat(dart): Add support for dart closing labels (#6)
* Add support for dart closing labels * Remove up-value for opts and wrap draw_labels in a closure getter * Add example autocommand that draws the closing labels * Adding back the defaults if they weren't passed in * Refactor callback to make it a lot simpler * Update README to include closing labels docs
1 parent 7c3f907 commit 7dec4af

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Repo to hold a bunch of info & extension callbacks for built-in LSP. Use at
77
Requires Built-in LSP, [Neovim Nightly](https://github.com/neovim/neovim/releases/tag/nightly), [nvim-lsp](https://github.com/neovim/nvim-lsp)
88

99
```vimscript
10-
" LSP Extensions (inlay-hints)
10+
" LSP Extensions
1111
Plug 'tjdevries/lsp_extensions.nvim'
1212
```
1313

@@ -56,12 +56,20 @@ require'lsp_extensions'.inlay_hints{
5656
autocmd InsertLeave,BufEnter,BufWinEnter,TabEnter,BufWritePost *.rs :lua require'lsp_extensions'.inlay_hints{ prefix = ' » ', highlight = "NonText" }
5757
```
5858

59+
## Closing Labels (dartls)
60+
![closing-labels](https://raw.githubusercontent.com/tjdevries/media.repo/b4a4a20d0c31a4905e42e219cf854c9aa104edbd/lsp_extensions/dart-closingLabels.png)
61+
62+
[Closing Labels Documentation](https://github.com/dart-lang/sdk/blob/master/pkg/analysis_server/tool/lsp_spec/README.md#darttextdocumentpublishclosinglabels-notification)
63+
64+
Check out the [example file](examples/dart/closing_labels.lua) for setup
65+
5966
## Clips
6067

6168
- Showing Line Diagnostics: https://clips.twitch.tv/ProductiveBoxyPastaCoolStoryBro
6269

6370
- This Plugin:
6471

6572
- Lined up hints: https://clips.twitch.tv/DaintyCorrectMarjoramKeepo
73+
- [Closing Labels Demo](https://github.com/tjdevries/media.repo/blob/b4a4a20d0c31a4905e42e219cf854c9aa104edbd/lsp_extensions/dart-closingLabels.mp4)
6674

6775
- N E O V I M: https://clips.twitch.tv/SmoothGoodTurnipCmonBruh

examples/dart/closing_labels.lua

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
local nvim_lsp = require('nvim_lsp')
2+
3+
nvim_lsp.dartls.setup{
4+
init_options = {
5+
closingLabels = true,
6+
},
7+
callbacks = {
8+
-- get_callback can be called with or without arguments
9+
['dart/textDocument/publishClosingLabels'] = require('lsp_extensions.dart.closing_labels').get_callback({highlight = "Special", prefix = " >> "}),
10+
},
11+
}
12+
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.get_callback = 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
51+
draw_labels(opts, labels)
52+
end
53+
end
54+
end
55+
56+
return M

0 commit comments

Comments
 (0)