Skip to content

Commit

Permalink
feat(lsp)!: refactor server setup
Browse files Browse the repository at this point in the history
- decouple go, json, and yaml to extras/lang/
- move nvim-ufo to extras/editor/
- /init.lua loads extras/lang/{go,json,yaml}.lua
  • Loading branch information
rafi committed Jul 1, 2023
1 parent 801e445 commit cc8c1c3
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 225 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Please read "[Extending](#extending)" to learn how to customize and modify.
* Robust, yet light-weight
* Plugin management with [folke/lazy.nvim]. Use with `:Lazy` or <kbd>Space</kbd>+<kbd>l</kbd>
* Install LSP, DAP, linters, and formatters. Use with `:Mason` or <kbd>Space</kbd>+<kbd>mm</kbd>
* LSP configuration with [nvim-lspconfig] and [folke/neoconf.nvim] (see [neoconf.json])
* LSP configuration with [nvim-lspconfig]
* [telescope.nvim] centric work-flow with lists (try <kbd>;</kbd>+<kbd>f</kbd>…)
* Custom context-menu (try it! <kbd>;</kbd>+<kbd>c</kbd>)
* Auto-complete extensive setup with [nvim-cmp]
Expand Down Expand Up @@ -151,8 +151,8 @@ You'll need utilities like `npm` and `curl` to install some extensions, see
[requirements](https://github.com/williamboman/mason.nvim#requirements)
(or `:h mason-requirements`) for more information.

See [neoconf.json] and [lua/rafi/plugins/lsp/init.lua] for custom key-mappings
and configuration for some language-servers.
See [lua/rafi/plugins/lsp/init.lua] for custom key-mappings and configuration
for some language-servers.

### Recommended LSP

Expand Down Expand Up @@ -409,7 +409,7 @@ package.loaded['rafi.config.options'] = true

To override **LSP configurations**, you can do either:

1. Customize `neoconf.json` or per project's `.neoconf.json`
1. Customize per project's `.neoconf.json`

2. Or, override server options with nvim-lspconfig plugin, for example:

Expand Down Expand Up @@ -586,7 +586,6 @@ _Note_ that 95% of the plugins are **lazy-loaded**.
| [RRethy/nvim-treesitter-endwise] | Wisely add "end" in various filetypes
| [windwp/nvim-ts-autotag] | Use treesitter to auto close and auto rename html tag
| [andymass/vim-matchup] | Modern matchit and matchparen
| [kevinhwang91/nvim-ufo] | Make folds look modern and keep a high performance
| [iloginow/vim-stylus] | Better vim plugin for stylus
| [chrisbra/csv.vim] | Handling column separated data
| [towolf/vim-helm] | Syntax for Helm templates (yaml + gotmpl + sprig)
Expand Down Expand Up @@ -710,7 +709,6 @@ _Note_ that 95% of the plugins are **lazy-loaded**.
[RRethy/nvim-treesitter-endwise]: https://github.com/RRethy/nvim-treesitter-endwise
[windwp/nvim-ts-autotag]: https://github.com/windwp/nvim-ts-autotag
[andymass/vim-matchup]: https://github.com/andymass/vim-matchup
[kevinhwang91/nvim-ufo]: https://github.com/kevinhwang91/nvim-ufo
[iloginow/vim-stylus]: https://github.com/iloginow/vim-stylus
[chrisbra/csv.vim]: https://github.com/chrisbra/csv.vim
[towolf/vim-helm]: https://github.com/towolf/vim-helm
Expand Down Expand Up @@ -788,6 +786,7 @@ Spec: `rafi.plugins.extras.editor.<name>`
| `anyjump` | [pechorin/any-jump.vim] | Jump to any definition and references without overhead
| `flybuf` | [glepnir/flybuf.nvim] | List buffers in a float window
| `sidebar` | [sidebar-nvim/sidebar.nvim] | Generic and modular lua sidebar
| `ufo` | [kevinhwang91/nvim-ufo] | Make folds look modern and keep a high performance

### Extra Plugins: Formatting

Expand Down Expand Up @@ -848,6 +847,7 @@ Spec: `rafi.plugins.extras.ui.<name>`
[pechorin/any-jump.vim]: https://github.com/pechorin/any-jump.vim
[glepnir/flybuf.nvim]: https://github.com/glepnir/flybuf.nvim
[sidebar-nvim/sidebar.nvim]: https://github.com/sidebar-nvim/sidebar.nvim
[kevinhwang91/nvim-ufo]: https://github.com/kevinhwang91/nvim-ufo
[hrsh7th/nvim-gtd]: https://github.com/hrsh7th/nvim-gtd
[lvimuser/lsp-inlayhints.nvim]: https://github.com/lvimuser/lsp-inlayhints.nvim
[kosayoda/nvim-lightbulb]: https://github.com/kosayoda/nvim-lightbulb
Expand Down Expand Up @@ -1310,7 +1310,6 @@ See [chentau/marks.nvim] for more mappings and usage information.
</details>

[Neovim]: https://github.com/neovim/neovim
[neoconf.json]: ./neoconf.json
[lazy.nvim]: https://github.com/folke/lazy.nvim
[LazyVim/starter]: https://github.com/LazyVim/starter
[lua/rafi/plugins/lsp/init.lua]: ./lua/rafi/plugins/lsp/init.lua
Expand Down
5 changes: 5 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ config.setup()
require('lazy').setup(vim.tbl_extend('keep', config.user_lazy_opts(), {
spec = {
{ import = 'rafi.plugins' },
{ import = 'rafi.plugins.extras.lang.go' },
{ import = 'rafi.plugins.extras.lang.json' },
{ import = 'rafi.plugins.extras.lang.yaml' },

-- This will load a custom user lua/plugins.lua or lua/plugins/*
config.has_user_plugins() and { import = 'plugins' } or nil,
},
defaults = { lazy = true, version = false },
Expand Down
110 changes: 110 additions & 0 deletions lua/rafi/plugins/extras/editor/ufo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
return {
{
'neovim/nvim-lspconfig',
opts = {
capabilities = {
textDocument = {
foldingRange = {
dynamicRegistration = false,
lineFoldingOnly = true,
},
},
},
},
},
{
'kevinhwang91/nvim-ufo',
event = { 'BufReadPost', 'BufNewFile' },
-- stylua: ignore
keys = {
{ 'zR', function() require('ufo').openAllFolds() end },
{ 'zM', function() require('ufo').closeAllFolds() end },
-- { 'zr', function() require('ufo').openFoldsExceptKinds() end },
-- { 'zm', function() require('ufo').closeFoldsWith() end },
},
dependencies = {
'kevinhwang91/promise-async',
'nvim-treesitter/nvim-treesitter',
'neovim/nvim-lspconfig',
},
opts = function()
-- lsp->treesitter->indent
---@param bufnr number
---@return table
local function customizeSelector(bufnr)
local function handleFallbackException(err, providerName)
if type(err) == 'string' and err:match('UfoFallbackException') then
return require('ufo').getFolds(bufnr, providerName)
else
return require('promise').reject(err)
end
end

return require('ufo')
.getFolds(bufnr, 'lsp')
:catch(function(err)
return handleFallbackException(err, 'treesitter')
end)
:catch(function(err)
return handleFallbackException(err, 'indent')
end)
end

local ft_providers = {
vim = 'indent',
python = { 'indent' },
git = '',
help = '',
qf = '',
fugitive = '',
fugitiveblame = '',
['neo-tree'] = '',
}

return {
open_fold_hl_timeout = 0,
preview = {
win_config = {
border = { '', '', '', '', '', '', '', '' },
winhighlight = 'Normal:Folded',
winblend = 10,
},
mappings = {
scrollU = '<C-u>',
scrollD = '<C-d>',
jumpTop = '[',
jumpBot = ']',
},
},

-- Select the fold provider.
provider_selector = function(_, filetype, _)
return ft_providers[filetype] or customizeSelector
end,

-- Display text for folded lines.
---@param text table
---@param lnum integer
---@param endLnum integer
---@param width integer
---@return table
fold_virt_text_handler = function(text, lnum, endLnum, width)
local suffix = ' 󰇘 '
local lines = (' 󰁂 %d '):format(endLnum - lnum)

local cur_width = 0
for _, section in ipairs(text) do
cur_width = cur_width + vim.fn.strdisplaywidth(section[1])
end

suffix = suffix
.. (' '):rep(width - cur_width - vim.fn.strdisplaywidth(lines) - 3)

table.insert(text, { suffix, 'UfoFoldedEllipsis' })
table.insert(text, { lines, 'Folded' })
return text
end,
}
end,
},
}
147 changes: 147 additions & 0 deletions lua/rafi/plugins/extras/lang/go.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
-- This is part of LazyVim's code, with my modifications.
-- See: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/plugins/extras/lang/go.lua

return {

{
'nvim-treesitter/nvim-treesitter',
opts = function(_, opts)
if type(opts.ensure_installed) == 'table' then
vim.list_extend(opts.ensure_installed, {
'go',
'gomod',
'gosum',
'gowork',
})
end
end,
},

{
'neovim/nvim-lspconfig',
opts = {
servers = {
gopls = {
settings = {
gopls = {
semanticTokens = true,
},
},
},
},
setup = {
gopls = function(_, opts)
-- workaround for gopls not supporting semanticTokensProvider
-- https://github.com/golang/go/issues/54531#issuecomment-1464982242
require('rafi.config').on_attach(function(client, _)
if client.name == 'gopls' then
if not client.server_capabilities.semanticTokensProvider then
local semantic = client.config.capabilities.textDocument.semanticTokens
client.server_capabilities.semanticTokensProvider = {
full = true,
legend = {
tokenTypes = semantic.tokenTypes,
tokenModifiers = semantic.tokenModifiers,
},
range = true,
}
end
end
end)
-- end workaround
opts.settings = {
-- https://github.com/golang/tools/blob/master/gopls/doc/settings.md
gopls = {
gofumpt = true,
usePlaceholders = true,
completeUnimported = true,
staticcheck = true,
directoryFilters = { '-.git', '-.vscode', '-.idea', '-.vscode-test', '-node_modules' },
semanticTokens = true,
codelenses = {
gc_details = false,
generate = true,
regenerate_cgo = true,
run_govulncheck = true,
test = true,
tidy = true,
upgrade_dependency = true,
vendor = true,
},
hints = {
assignVariableTypes = true,
compositeLiteralFields = true,
compositeLiteralTypes = true,
constantValues = true,
functionTypeParameters = true,
parameterNames = true,
rangeVariableTypes = true,
},
-- https://github.com/golang/tools/blob/master/gopls/doc/analyzers.md
analyses = {
fieldalignment = true,
nilness = true,
unusedparams = true,
unusedwrite = true,
useany = true,
-- fillreturns = true,
-- nonewvars = true,
-- shadow = true,
-- undeclaredname = true,
-- unusedvariable = true,
-- ST1000 = false,
-- ST1005 = false,
},
},
}
end,
},
},
},

{
'jose-elias-alvarez/null-ls.nvim',
opts = function(_, opts)
local nls = require('null-ls')
local sources = {
nls.builtins.code_actions.gomodifytags,
nls.builtins.code_actions.impl,
nls.builtins.formatting.gofumpt,
-- nls.builtins.formatting.goimports_reviser,
}
for _, source in ipairs(sources) do
table.insert(opts.sources, source)
end
end,
},

{
'mfussenegger/nvim-dap',
optional = true,
dependencies = {
{
'mason.nvim',
opts = function(_, opts)
opts.ensure_installed = opts.ensure_installed or {}
table.insert(opts.ensure_installed, 'delve')
end,
},
},
},

{
'nvim-neotest/neotest',
optional = true,
dependencies = {
'nvim-neotest/neotest-go',
},
opts = {
adapters = {
['neotest-go'] = {
-- Here we can set options for neotest-go, e.g.
-- args = { '-tags=integration' }
},
},
},
},
}
40 changes: 40 additions & 0 deletions lua/rafi/plugins/extras/lang/json.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- This is part of LazyVim's code, with my modifications.
-- See: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/plugins/extras/lang/json.lua

return {

{
'nvim-treesitter/nvim-treesitter',
opts = function(_, opts)
if type(opts.ensure_installed) == 'table' then
vim.list_extend(opts.ensure_installed, { 'json', 'json5', 'jsonc' })
end
end,
},

{
'neovim/nvim-lspconfig',
dependencies = { 'b0o/SchemaStore.nvim', version = false },
opts = {
servers = {
jsonls = {
-- lazy-load schemastore when needed
on_new_config = function(new_config)
new_config.settings.json.schemas = new_config.settings.json.schemas
or {}
vim.list_extend(
new_config.settings.json.schemas,
require('schemastore').json.schemas()
)
end,
settings = {
json = {
format = { enable = true },
validate = { enable = true },
},
},
},
},
},
},
}
Loading

0 comments on commit cc8c1c3

Please sign in to comment.