Skip to content

Commit 2985d98

Browse files
authored
Beta (#4)
* remove doc/tags * use cmd instead of alias for clarity * use alias and proper checking of existing command installation * mason pkg name override * we still want to enforce local by default * remove unused on_attach since we have event to control it * fallback to using existing docker language servers * add yaml language server + remove table setup logging * write comment what symbol we expect * more notes * use mason-lspconfig for getting lsp->mason map and better utilities * use very lazy
1 parent 115243d commit 2985d98

File tree

7 files changed

+113
-107
lines changed

7 files changed

+113
-107
lines changed

doc/tags

Lines changed: 0 additions & 3 deletions
This file was deleted.

init.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,19 @@ local function DockerFileTypeDetectionAutoCommand()
226226
})
227227
end
228228

229-
DockerFileTypeDetectionAutoCommand()
229+
local function AddDockerFilesToFiletype()
230+
vim.filetype.add {
231+
filename = {
232+
['docker-compose.yml'] = 'yaml.docker-compose',
233+
['docker-compose.yaml'] = 'yaml.docker-compose',
234+
['compose.yml'] = 'yaml.docker-compose',
235+
['compose.yaml'] = 'yaml.docker-compose',
236+
},
237+
}
238+
end
239+
240+
-- DockerFileTypeDetectionAutoCommand()
241+
AddDockerFilesToFiletype()
230242

231243
-- [[ Install `lazy.nvim` plugin manager ]]
232244
-- See `:help lazy.nvim.txt` or https://github.com/folke/lazy.nvim for more info

lua/kickstart/plugins/lsp.lua

Lines changed: 27 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ return {
1717
'neovim/nvim-lspconfig',
1818
dependencies = {
1919
-- Automatically install LSPs and related tools to stdpath for Neovim
20-
{ 'williamboman/mason.nvim', config = true }, -- NOTE: Must be loaded before dependants
21-
'williamboman/mason-lspconfig.nvim',
20+
{ 'mason-org/mason.nvim', config = true }, -- NOTE: Must be loaded before dependants
21+
'mason-org/mason-lspconfig.nvim',
2222
'WhoIsSethDaniel/mason-tool-installer.nvim',
2323

2424
-- Useful status updates for LSP.
@@ -61,52 +61,6 @@ return {
6161
-- That is to say, every time a new file is opened that is associated with
6262
-- an lsp (for example, opening `main.rs` is associated with `rust_analyzer`) this
6363
-- function will be executed to configure the current buffer
64-
local on_attach = function(_, buffr)
65-
local kset = function(key, func, buffer, desc)
66-
vim.keymap.set('n', key, func, { buffer = buffer, desc = 'LSP: ' .. desc })
67-
end
68-
69-
-- Jump to the definition of the word under your cursor.
70-
-- This is where a variable was first declared, or where a function is defined, etc.
71-
-- To jump back, press <C-t>.
72-
kset('gd', require('telescope.builtin').lsp_definitions, buffr, '[G]oto [D]efinition')
73-
74-
-- Find references for the word under your cursor.
75-
kset('gr', require('telescope.builtin').lsp_references, buffr, '[G]oto [R]eferences')
76-
77-
-- Jump to the implementation of the word under your cursor.
78-
-- Useful when your language has ways of declaring types without an actual implementation.
79-
kset('gI', require('telescope.builtin').lsp_implementations, buffr, '[G]oto [I]mplementation')
80-
81-
-- Jump to the type of the word under your cursor.
82-
-- Useful when you're not sure what type a variable is and you want to see
83-
-- the definition of its *type*, not where it was *defined*.
84-
kset('<leader>D', require('telescope.builtin').lsp_type_definitions, buffr, 'Type [D]efinition')
85-
86-
-- Fuzzy find all the symbols in your current document.
87-
-- Symbols are things like variables, functions, types, etc.
88-
kset('<leader>ds', require('telescope.builtin').lsp_document_symbols, buffr, '[D]ocument [S]ymbols')
89-
90-
-- Fuzzy find all the symbols in your current workspace.
91-
-- Similar to document symbols, except searches over your entire project.
92-
kset('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, buffr, '[W]orkspace [S]ymbols')
93-
94-
-- Rename the variable under your cursor.
95-
-- Most Language Servers support renaming across files, etc.
96-
kset('<leader>rn', vim.lsp.buf.rename, buffr, '[R]e[n]ame')
97-
98-
-- Execute a code action, usually your cursor needs to be on top of an error
99-
-- or a suggestion from your LSP for this to activate.
100-
kset('<leader>ca', vim.lsp.buf.code_action, buffr, '[C]ode [A]ction')
101-
102-
-- Opens a popup that displays documentation about the word under your cursor
103-
-- See `:help K` for why this keymap.
104-
kset('K', vim.lsp.buf.hover, buffr, 'Hover Documentation')
105-
106-
-- WARN: This is not Goto Definition, this is Goto Declaration.
107-
-- For example, in C this would take you to the header.
108-
kset('gD', vim.lsp.buf.declaration, buffr, '[G]oto [D]eclaration')
109-
end
11064

11165
vim.api.nvim_create_autocmd('LspAttach', {
11266
group = vim.api.nvim_create_augroup('kickstart-lsp-attach', { clear = true }),
@@ -222,14 +176,16 @@ return {
222176
severity_sort = true,
223177
float = { border = 'rounded', source = 'if_many' },
224178
underline = { severity = vim.diagnostic.severity.ERROR },
225-
signs = vim.g.have_nerd_font and {
226-
text = {
227-
[vim.diagnostic.severity.ERROR] = '󰅚 ',
228-
[vim.diagnostic.severity.WARN] = '󰀪 ',
229-
[vim.diagnostic.severity.INFO] = '󰋽 ',
230-
[vim.diagnostic.severity.HINT] = '󰌶 ',
231-
},
232-
} or {},
179+
signs = vim.g.have_nerd_font
180+
and {
181+
text = {
182+
[vim.diagnostic.severity.ERROR] = '󰅚 ', -- circle with cross
183+
[vim.diagnostic.severity.WARN] = '󰀪 ', -- triangle warning
184+
[vim.diagnostic.severity.INFO] = '󰋽 ', -- circle with i
185+
[vim.diagnostic.severity.HINT] = '󰌶 ', -- lightbulb
186+
},
187+
}
188+
or {},
233189
virtual_text = {
234190
source = 'if_many',
235191
spacing = 2,
@@ -268,40 +224,28 @@ return {
268224
-- You can add other tools here that you want Mason to install
269225
-- for you, so that they are available from within Neovim.
270226
-- require('mason-tool-installer').setup { ensure_installed = ensure_installed }
227+
271228
-- INFO: Using my own utils function instead of mason-lspconfig as it checks if the stuff is already installed
272229
-- outside of mason. This is useful for NixOS setup where mason version just doesn't work sometimes due to libc issues.
273-
274230
-- We take the languages configured for a given profile
275231
-- Given the profile we take the LSPs configured for the languages
276232
-- Then we guarantee use or install the LSPs
277-
local languages = require('utils.profile').Languages()
278-
local languageServers = require 'utils.languages'
279-
local tmpTable = {}
280-
for _, lang in ipairs(languages) do
281-
for lsp, config in pairs(languageServers[lang]) do
282-
tmpTable[lsp] = config
283-
end
284-
end
285-
require('utils.mason').install(tmpTable, true)
286-
local lsp = require 'lspconfig'
287-
for server, config in pairs(tmpTable) do
233+
local lsps = require('utils.profile').LanguageServers()
234+
-- print(vim.inspect(lsps))
235+
local missing_lsps = require('utils.mason').missing(lsps) -- find missing lsps
236+
-- print(vim.inspect(missing_lsps))
237+
missing_lsps = {} -- TODO: this is only for NixOS to prefer installing via nixpkgs instead of mason
238+
239+
-- install the executables of the language servers that we don't already have installed locally outside of mason
240+
require('utils.mason').install(missing_lsps)
241+
242+
-- configure nvim lsp via lspconfig package for our list of lsps
243+
local lspconfig = require 'lspconfig'
244+
for server, config in pairs(lsps) do
288245
config.capabilities = vim.tbl_deep_extend('force', {}, capabilities, config.capabilities or {})
289-
config.on_attach = on_attach
290-
lsp[server].setup(config)
246+
-- config.on_attach = on_attach -- we don't need this because of the events
247+
lspconfig[server].setup(config)
291248
end
292-
293-
-- require('mason-lspconfig').setup {
294-
-- handlers = {
295-
-- function(server_name)
296-
-- local server = servers[server_name] or {}
297-
-- -- This handles overriding only values explicitly passed
298-
-- -- by the server configuration above. Useful when disabling
299-
-- -- certain features of an LSP (for example, turning off formatting for tsserver)
300-
-- server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
301-
-- require('lspconfig')[server_name].setup(server)
302-
-- end,
303-
-- },
304-
-- }
305249
end,
306250
},
307251
-- Show LSP explorer of functions and classes etc.

lua/utils/languages.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,31 @@ local Languages = {
105105
alias = 'docker-compose-langserver',
106106
},
107107
},
108+
['yaml'] = {
109+
yamlls = {
110+
alias = 'yaml-language-server',
111+
settings = {
112+
yaml = {
113+
schemas = {
114+
extra = {
115+
-- kubernetes = 'k8s-*.yaml', -- TODO: consider using *.k8s.yaml for kubernetes schemas
116+
['http://json.schemastore.org/github-workflow'] = '.github/workflows/*',
117+
['http://json.schemastore.org/github-action'] = '.github/action.{yml,yaml}',
118+
['http://json.schemastore.org/ansible-stable-2.9'] = 'roles/tasks/**/*.{yml,yaml}',
119+
['http://json.schemastore.org/prettierrc'] = '.prettierrc.{yml,yaml}',
120+
['http://json.schemastore.org/kustomization'] = 'kustomization.{yml,yaml}',
121+
['http://json.schemastore.org/chart'] = 'Chart.{yml,yaml}',
122+
['http://json.schemastore.org/circleciconfig'] = '.circleci/**/*.{yml,yaml}',
123+
},
124+
},
125+
format = {
126+
enable = true,
127+
},
128+
redhat = { telemetry = { enabled = false } },
129+
},
130+
},
131+
},
132+
},
108133
['lua'] = {
109134
lua_ls = {
110135
-- cmd = {...},

lua/utils/mappings/lsp_to_cmd.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- TODO: move lsp_config_name -> executable mapping here instead of being part of languages.lua file

lua/utils/mason.lua

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,43 @@ local name_to_bin = {
77
-- ['docker-compose-language-service'] = 'docker-compose-langserver',
88
}
99

10-
-- We guarantee 'ensure_installed' package is installed locally
11-
-- If enforce_local is false then we install it via mason-registry
12-
-- By default we install LSPs via mason
13-
M.install = function(ensure_installed, enforce_local)
14-
-- Allow for passing in a single string
10+
M.missing = function(ensure_installed)
1511
if type(ensure_installed) == 'string' then
1612
ensure_installed = { ensure_installed }
1713
end
1814

19-
enforce_local = enforce_local == nil and false or enforce_local
15+
local result = {}
16+
for lsp_name, config in pairs(ensure_installed) do
17+
local executable_name = lsp_name
18+
if config['alias'] then
19+
executable_name = config['alias']
20+
end
2021

21-
-- Function to check if the executable exists in the PATH
22-
local function executable_exists(name)
23-
if name_to_bin[name] then
24-
return vim.fn.executable(name_to_bin[name]) == 1
22+
-- check if executable exists
23+
if vim.fn.executable(executable_name) ~= 1 then
24+
result[lsp_name] = config
2525
end
26-
return vim.fn.executable(name) == 1
2726
end
27+
return result
28+
end
29+
30+
-- We guarantee 'ensure_installed' package is installed locally
31+
-- If enforce_local is false then we install it via mason-registry
32+
-- By default we install LSPs via mason
33+
M.install = function(ensure_installed)
34+
-- ensure installed is expected of the form <lspname>: {cmd: "", settings: {...}}
35+
36+
-- ensure_installed = M.missing(ensure_installed, enforce_local)
37+
local lspconfig_to_pkg = require('mason-lspconfig').get_mappings().lspconfig_to_package
2838

2939
local registry = require 'mason-registry'
40+
-- local mason_lspconfig = require 'mason-lspconfig'
3041
registry.refresh(function()
31-
for _, pkg_name in ipairs(ensure_installed) do
32-
if (not executable_exists(pkg_name)) and not enforce_local then
33-
local pkg = registry.get_package(pkg_name)
34-
if not pkg:is_installed() then
35-
pkg:install()
36-
end
42+
for lsp_cfg, _ in pairs(ensure_installed) do
43+
local pkg_name = lspconfig_to_pkg[lsp_cfg] -- get mason package name based on lspconfig name
44+
local pkg = registry.get_package(pkg_name)
45+
if not pkg:is_installed() then
46+
pkg:install()
3747
end
3848
end
3949
end)

lua/utils/profile.lua

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,37 @@ local PROFILES = {
88
'bash',
99
'docker',
1010
'lua',
11+
'yaml',
1112
},
1213
['DEFAULT'] = {
1314
'python',
1415
'markdown',
1516
'bash',
1617
'docker',
1718
'lua',
19+
'yaml',
1820
},
1921
}
2022

2123
local Profile = {}
22-
Profile.Languages = function()
23-
local profile = PROFILES[os.getenv 'NVIM_PROFILE' or 'DEFAULT']
24-
return profile
24+
Profile.Languages = function(profile)
25+
if profile == nil then
26+
profile = os.getenv 'NVIM_PROFILE' or 'DEFAULT'
27+
end
28+
return PROFILES[profile]
29+
end
30+
31+
Profile.LanguageServers = function(profile)
32+
local languages = Profile.Languages(profile)
33+
local language_config = require 'utils.languages'
34+
35+
local result = {} -- <nvim_ls_name> -> {<configuration>}
36+
for _, lang in ipairs(languages) do
37+
for lsp, config in pairs(language_config[lang]) do
38+
result[lsp] = config
39+
end
40+
end
41+
return result
2542
end
2643

2744
return Profile

0 commit comments

Comments
 (0)