refactor: use LspAttach instead of calling on_attach

This commit is contained in:
Steven Arcangeli 2022-10-20 19:53:38 -07:00
parent 66f6939c7c
commit 0d4c6010f8
4 changed files with 55 additions and 62 deletions

View file

@ -71,7 +71,7 @@ def update_treesitter_languages():
languages = sorted(os.listdir(os.path.join(ROOT, "queries")))
language_lines = ["\n"] + [f"- {l}\n" for l in languages] + ["\n"]
replace_section(
README, r"^\s*<summary>Supported languages", r"^[^\s\-]", language_lines
README, r"^\s*<summary>Supported treesitter languages", r"^[^\s\-]", language_lines
)

View file

@ -5,9 +5,6 @@ A code outline window for skimming and quick navigation
- [Requirements](#requirements)
- [Installation](#installation)
- [Setup](#setup)
- [Treesitter](#treesitter)
- [LSP](#lsp)
- [Markdown](#markdown)
- [Keymaps](#keymaps)
- [Commands](#commands)
- [Options](#options)
@ -103,20 +100,10 @@ Somewhere in your init.lua you will need to call `aerial.setup()`. See below for
require('aerial').setup({})
```
In addition, you will need to follow the setup steps for at least one of the
symbol sources listed below. You can configure your preferred source(s) with the
`backends` option (see [Options](#options)). The default is to prefer Treesitter
when it's available and fall back to LSP.
### Treesitter
First ensure you have
[nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) installed
and configured for all languages you want to support. That's all! Aerial will
automatically fetch symbols from treesitter.
In addition, you will need to have either Treesitter or a working LSP client. You can configure your preferred source(s) with the `backends` option (see [Options](#options)). The default is to prefer Treesitter when it's available and fall back to LSP.
<details>
<summary>Supported languages</summary>
<summary>Supported treesitter languages</summary>
- bash
- c
@ -153,40 +140,6 @@ it](https://github.com/stevearc/aerial.nvim/issues/new?assignees=stevearc&labels
</details>
### LSP
First ensure you have a functioning LSP setup (see
[nvim-lspconfig](https://github.com/neovim/nvim-lspconfig)). Once complete, add
the aerial `on_attach` callback to your config:
```lua
-- Set up your LSP clients here, using the aerial on_attach method
require("lspconfig").vimls.setup{
on_attach = require("aerial").on_attach,
}
-- Repeat this for each language server you have configured
```
If you have your own custom `on_attach` function, call aerial's `on_attach` from
inside it:
```lua
local function my_custom_attach(client, bufnr)
-- your code here
require("aerial").on_attach(client, bufnr)
end
```
### Markdown
There is a simple custom backend that does rudimentary parsing of markdown
headers. It should work well enough in most cases, but does not parse the full
markdown spec.
There is now an experimental [treesitter parser for
markdown](https://github.com/nvim-treesitter/nvim-treesitter/issues/872), so you
can install that and try the treesitter backend instead.
### Keymaps
While not required, you may want to add some keymaps for aerial. The best way to

View file

@ -71,26 +71,39 @@ M.fetch_symbols_sync = function(bufnr, opts)
end
end
local function mark_lsp_attached(bufnr)
vim.api.nvim_buf_set_var(bufnr, "_aerial_lsp_attached", true)
local function mark_lsp_attached(bufnr, attached)
vim.api.nvim_buf_set_var(bufnr, "_aerial_lsp_attached", attached)
end
---@param bufnr integer
---@return boolean
local function is_lsp_attached(bufnr)
local ok, attached = pcall(vim.api.nvim_buf_get_var, bufnr, "_aerial_lsp_attached")
return ok and attached
end
---@param bufnr integer
---@param exclude_id nil|integer Client ID to exclude from calculation
---@return boolean
local function has_lsp_client(bufnr, exclude_id)
for _, client in pairs(vim.lsp.buf_get_clients(bufnr)) do
if client.id ~= exclude_id and client.server_capabilities.documentSymbolProvider then
return true
end
end
return false
end
M.is_supported = function(bufnr)
if not bufnr or bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
end
if not is_lsp_attached(bufnr) then
for _, client in pairs(vim.lsp.buf_get_clients(bufnr)) do
if client.server_capabilities.documentSymbolProvider then
return false, "LSP client not attached (did you call aerial.on_attach?)"
end
if has_lsp_client(bufnr) then
return false, "LSP client available, but not attached"
else
return false, "No LSP client found"
end
return false, "LSP client not attached"
end
return true, nil
end
@ -105,7 +118,14 @@ M.on_attach = function(client, bufnr, opts)
opts = opts or {}
if client.server_capabilities.documentSymbolProvider then
hook_handlers(opts.preserve_callback)
mark_lsp_attached(bufnr)
mark_lsp_attached(bufnr, true)
backends.attach(bufnr, true)
end
end
M.on_detach = function(client_id, bufnr)
if not has_lsp_client(bufnr, client_id) then
mark_lsp_attached(bufnr, false)
backends.attach(bufnr, true)
end
end

View file

@ -33,6 +33,23 @@ M.setup = function(opts)
require("aerial.autocommands").on_enter_buffer()
end,
})
vim.api.nvim_create_autocmd("LspAttach", {
desc = "Aerial mark LSP backend as available",
pattern = "*",
group = group,
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
require("aerial.backends.lsp").on_attach(client, args.buf)
end,
})
vim.api.nvim_create_autocmd("LspDetach", {
desc = "Aerial mark LSP backend as unavailable",
pattern = "*",
group = group,
callback = function(args)
require("aerial.backends.lsp").on_detach(args.data.client_id, args.buf)
end,
})
command.create_commands()
highlight.create_highlight_groups()
end
@ -127,9 +144,12 @@ M.up = function(direction, count)
nav.up(direction, count)
end
-- This LSP on_attach function must be called in order to use the LSP backend
---@deprecated
M.on_attach = function(...)
require("aerial.backends.lsp").on_attach(...)
vim.notify_once(
"Deprecated(aerial.on_attach): you no longer need to call this function\nThis function will be removed on 2023-02-01",
vim.log.levels.WARN
)
end
-- Returns a list representing the symbol path to the current location.
@ -293,8 +313,8 @@ end
-- Register a callback to be called when aerial is attached to a buffer.
M.register_attach_cb = function(callback)
vim.notify(
"Deprecated(register_attach_cb): pass `on_attach` to aerial.setup() instead (see :help aerial)",
vim.notify_once(
"Deprecated(aerial.register_attach_cb): pass `on_attach` to aerial.setup() instead (see :help aerial)\nThis function will be removed on 2023-02-01",
vim.log.levels.WARN
)
config.on_attach = callback