Skip to content

Commit

Permalink
Merge pull request #9 from ranjithshegde/enable_codelens
Browse files Browse the repository at this point in the history
feat(codelens): Setup and configure autocmds
  • Loading branch information
ranjithshegde authored Nov 7, 2022
2 parents 360b17a + e1d615b commit 3a58ddd
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Features include:
- Setup lsp either via `lspconfig` or built-in `vim.lsp.start()`
- Use treesitter to highlight NodeTree window
- Update tagstack on jump to new node
- Setup codelens autocmds

Soon to be added:
Add floating previews for each nodes
Expand Down Expand Up @@ -153,8 +154,6 @@ The default values are:

```lua
defaults = {
-- Lsp is not setup by default to avoid overriding user's personal configurations.
-- Look ahead for instructions on using this plugin for ccls setup
win_config = {
-- Sidebar configuration
sidebar = {
Expand All @@ -176,6 +175,15 @@ defaults = {
},
},
filetypes = {"c", "cpp", "objc", "objcpp"},

-- Lsp is not setup by default to avoid overriding user's personal configurations.
-- Look ahead for instructions on using this plugin for ccls setup
lsp = {
codelens = {
enabled = false,
events = {"BufEnter", "BufWritePost"}
}
}
}
```

Expand Down Expand Up @@ -278,6 +286,31 @@ If neither `use_defaults`, `lspconfig` nor `server` are set,
then the plugin assumes you have setup ccls LSP elsewhere in your config.
This is the default behaviour.

#### Codelens

ccls has minimal codelens capabilites. If you are not familiar with codenels, see [Lsp spec
documentation](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_codeLens).
According to ccls server capabilities tree, ccls supports `resolveProvider`
option of codelens.

To enable codelens, set `lsp = { codelens = {enable = true}}` in the config.
It is necessary to setup autocmds to refresh codelens. The default events are
`BufEnter` and `BufWritePost`. You can customize it this way:

```lua
require('ccls').setup({
lsp = {
codelens = {
enable = true,
events = {"BufWritePost", "InsertLeave"}
}
}
})
```

_Note:_ Setting up codelens using this plugin requires neovim >= 0.8 as
`LspAttach` autocmd is only avaialble from version 0.8

### Coexistence with clangd

If you wish to use clangd alongside ccls and want to avoid conflicting parallel
Expand Down Expand Up @@ -357,6 +390,7 @@ descried earlier.
},
disable_diagnostics = true,
disable_signature = true,
codelens = { enable = true }
},
}
```
Expand Down
21 changes: 21 additions & 0 deletions lua/ccls/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ local ccls = {
lsp = {
nil_handlers = nil,
disable_capabilities = nil,
codelens = {
enable = false,
events = { "BufEnter", "BufWritePost" },
},
},
}

Expand All @@ -46,6 +50,23 @@ function ccls.setup(config)
if utils.assert_table(config.lsp) then
local nil_handlers = {}

if utils.assert_table(config.lsp.codelens) then
if config.lsp.codelens.enable then
if vim.fn.has "nvim-0.8" ~= 1 then
vim.notify(
[[Attempting to configure codelens events. This feature requires nvim>= 0.8]],
vim.log.levels.WARN,
{ title = "ccls.nvim" }
)
else
ccls.lsp.codelens.enable = true
if utils.assert_table(config.lsp.codelens.events) then
ccls.lsp.codelens.events = config.lsp.codelens.events
end
end
end
end

if utils.tbl_haskey(config.lsp, false, "use_defaults") and config.lsp.use_defaults == true then
require("ccls.protocol").setup_lsp "lspconfig"
return
Expand Down
38 changes: 38 additions & 0 deletions lua/ccls/protocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ local default_config = {
single_file_support = false,
}

local config_aug = vim.api.nvim_create_augroup("ccls_lsp_setup", { clear = true })

local function disable_capabilities(rules)
local on_init = function(client)
local sc = client.server_capabilities
Expand Down Expand Up @@ -52,6 +54,38 @@ local function set_root_dir(type)
end
end

local function enable_codelens(events)
local codelens_aug = vim.api.nvim_create_augroup("ccls_codelens", { clear = true })

vim.api.nvim_create_autocmd("LspAttach", {
group = config_aug,
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client.name == "ccls" then
vim.api.nvim_create_autocmd(events, {
group = codelens_aug,
buffer = args.buf,
callback = vim.lsp.codelens.refresh,
desc = "Refresh ccls codelens",
})
vim.lsp.codelens.refresh()
end
end,
desc = "Create codelens autocmd on Lsp Attach",
})

vim.api.nvim_create_autocmd("LspDetach", {
group = config_aug,
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client.name == "ccls" then
vim.api.nvim_clear_autocmds { group = codelens_aug, buffer = args.buf }
end
end,
desc = "Clear codelens autocmd on Lsp Detach",
})
end

local function qfRequest(params, method, bufnr, name)
local util = require "lspconfig.util"
bufnr = util.validate_bufnr(bufnr)
Expand Down Expand Up @@ -171,6 +205,10 @@ function protocol.setup_lsp(type, config)
set_root_dir(type)
end

if lsp_config.codelens.enable then
enable_codelens(lsp_config.codelens.events)
end

if utils.assert_table(config) then
default_config = vim.tbl_extend("force", default_config, config)
else
Expand Down

0 comments on commit 3a58ddd

Please sign in to comment.