diff options
| author | William Boman <william@redwill.se> | 2025-05-19 07:56:31 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-19 07:56:31 +0200 |
| commit | 1d6730459c42f591602500da994f01ae43a97dbc (patch) | |
| tree | c78417dd383456d2ef8a00600375534d17a9cd45 /scripts/lua/mason-scripts | |
| parent | chore: fix references to williamboman/mason.nvim (#542) (diff) | |
| download | mason-lspconfig-1d6730459c42f591602500da994f01ae43a97dbc.tar mason-lspconfig-1d6730459c42f591602500da994f01ae43a97dbc.tar.gz mason-lspconfig-1d6730459c42f591602500da994f01ae43a97dbc.tar.bz2 mason-lspconfig-1d6730459c42f591602500da994f01ae43a97dbc.tar.lz mason-lspconfig-1d6730459c42f591602500da994f01ae43a97dbc.tar.xz mason-lspconfig-1d6730459c42f591602500da994f01ae43a97dbc.tar.zst mason-lspconfig-1d6730459c42f591602500da994f01ae43a97dbc.zip | |
perf: host pre-compiled filetype mappings (#555)
Generating the filetype mappings by accessing `vim.lsp.config` turns out to be a bad idea because:
1) performance
2) some `lsp/` configurations in nvim-lspconfig execute code immediately (see angularls)
3) accessing `vim.lsp.config[server_name]` seems to populate `:checkhealth vim.lsp`
Diffstat (limited to 'scripts/lua/mason-scripts')
| -rw-r--r-- | scripts/lua/mason-scripts/mason-lspconfig/generate.lua | 42 | ||||
| -rw-r--r-- | scripts/lua/mason-scripts/utils.lua | 40 |
2 files changed, 82 insertions, 0 deletions
diff --git a/scripts/lua/mason-scripts/mason-lspconfig/generate.lua b/scripts/lua/mason-scripts/mason-lspconfig/generate.lua new file mode 100644 index 0000000..75584aa --- /dev/null +++ b/scripts/lua/mason-scripts/mason-lspconfig/generate.lua @@ -0,0 +1,42 @@ +local Optional = require "mason-core.optional" + +local _ = require "mason-core.functional" +local a = require "mason-core.async" +local path = require "mason-core.path" +local script_utils = require "mason-scripts.utils" + +local MASON_LSPCONFIG_DIR = path.concat { "lua", "mason-lspconfig" } + +require("mason").setup() +local registry = require "mason-registry" +registry.refresh() + +---@async +local function create_lspconfig_filetype_map() + local lspconfig_servers = + _.filter_map(_.compose(Optional.of_nilable, _.path { "neovim", "lspconfig" }), registry.get_all_package_specs()) + ---@type table<string, string[]> + local filetype_map = {} + + for _, server_name in ipairs(lspconfig_servers) do + local filetypes = vim.tbl_get(vim.lsp.config, server_name, "filetypes") + if filetypes then + for _, filetype in ipairs(filetypes) do + if not filetype_map[filetype] then + filetype_map[filetype] = {} + end + table.insert(filetype_map[filetype], server_name) + end + end + end + + script_utils.write_file( + path.concat { MASON_LSPCONFIG_DIR, "filetype_mappings.lua" }, + "return " .. vim.inspect(filetype_map), + "w" + ) +end + +a.run_blocking(function() + create_lspconfig_filetype_map() +end) diff --git a/scripts/lua/mason-scripts/utils.lua b/scripts/lua/mason-scripts/utils.lua new file mode 100644 index 0000000..ee6ab10 --- /dev/null +++ b/scripts/lua/mason-scripts/utils.lua @@ -0,0 +1,40 @@ +local _ = require "mason-core.functional" +local fs = require "mason-core.fs" + +local M = {} + +---@async +---@param path string +---@param contents string +---@param flags string? +function M.write_file(path, contents, flags) + local header = _.cond({ + { + _.matches "%.lua$", + _.always { + "-- THIS FILE IS GENERATED. DO NOT EDIT MANUALLY.", + "-- stylua: ignore start", + }, + }, + { + _.matches "%.md$", + _.always { + "<!--- THIS FILE IS GENERATED. DO NOT EDIT MANUALLY. -->", + }, + }, + { + _.matches "doc/.+%.txt$", + _.always {}, + }, + { + _.T, + _.always { + "# THIS FILE IS GENERATED. DO NOT EDIT MANUALLY.", + }, + }, + }, path) + + fs.async.write_file(path, table.concat(_.concat(header, { contents }), "\n"), flags) +end + +return M |
