From e2144bd62b703c1fa298b9e154296caeef389553 Mon Sep 17 00:00:00 2001 From: Jay Patel <36803168+jay-babu@users.noreply.github.com> Date: Sun, 11 Dec 2022 20:34:55 -0500 Subject: [PATCH] feat!: source.lua is not required for package names following normal conventions (#40) Co-authored-by: Jay Patel <36803168+jayp0521@users.noreply.github.com> --- lua/mason-null-ls/api/command.lua | 14 +-- lua/mason-null-ls/automatic_installation.lua | 2 +- lua/mason-null-ls/ensure_installed.lua | 2 +- lua/mason-null-ls/init.lua | 8 +- lua/mason-null-ls/mappings/source.lua | 102 +++---------------- 5 files changed, 28 insertions(+), 100 deletions(-) diff --git a/lua/mason-null-ls/api/command.lua b/lua/mason-null-ls/api/command.lua index 6f5b3a4..27547ea 100644 --- a/lua/mason-null-ls/api/command.lua +++ b/lua/mason-null-ls/api/command.lua @@ -15,12 +15,12 @@ local function parse_packages_from_user_args(user_args) local source_name, version = Package.Parse(source_specifier) -- 1. first see if the provided arg is an actual null-ls source name return Optional - .of_nilable(source_mappings.null_ls_to_package[source_name]) + .of_nilable(source_mappings.getPackageFromNullLs(source_name)) -- 2. if not, check if it's a language specifier (e.g., "typescript" or "java") :or_(function() - return Optional.of_nilable(language_mappings[source_name]):map(function(package_names) + return Optional.of_nilable(language_mappings(source_name)):map(function(package_names) local package_names = _.filter(function(package_name) - return source_mappings.null_ls_to_package[package_name] ~= nil + return source_mappings.getPackageFromNullLs(package_name) ~= nil end, package_names) if #package_names == 0 then @@ -32,7 +32,7 @@ local function parse_packages_from_user_args(user_args) source_name ), format_item = function(package_name) - local source_name = source_mappings.null_ls_to_package[package_name] + local source_name = source_mappings.getPackageFromNullLs(package_name) if registry.is_installed(package_name) then return ('%s (installed)'):format(source_name) else @@ -64,7 +64,7 @@ local function parse_packages_from_heuristics() return a.promisify(vim.ui.select)(source_names, { prompt = ('Please select which source you want to install for filetype %q:'):format(current_ft), format_item = function(source_name) - if registry.is_installed(source_mappings.null_ls_to_package[source_name]) then + if registry.is_installed(source_mappings.getPackageFromNullLs(source_name)) then return ('%s (installed)'):format(source_name) else return source_name @@ -73,7 +73,7 @@ local function parse_packages_from_heuristics() }) end) :map(function(source_name) - local package_name = source_mappings.null_ls_to_package[source_name] + local package_name = source_mappings.getPackageFromNullLs(source_name) return { { package = package_name, version = nil } } end) :or_else_get(function() @@ -119,7 +119,7 @@ local function NullLsUninstall(sources) local registry = require('mason-registry') local source_mappings = require('mason-null-ls.mappings.source') for _, source_specifier in ipairs(sources) do - local package_name = source_mappings.null_ls_to_package[source_specifier] + local package_name = source_mappings.getPackageFromNullLs(source_specifier) local pkg = registry.get_package(package_name) pkg:uninstall() end diff --git a/lua/mason-null-ls/automatic_installation.lua b/lua/mason-null-ls/automatic_installation.lua index 0265d2e..625683a 100644 --- a/lua/mason-null-ls/automatic_installation.lua +++ b/lua/mason-null-ls/automatic_installation.lua @@ -18,7 +18,7 @@ local function resolve_package(null_ls_source_name) local Optional = require('mason-core.optional') local source_mappings = require('mason-null-ls.mappings.source') - return Optional.of_nilable(source_mappings.null_ls_to_package[null_ls_source_name]):map(function(package_name) + return Optional.of_nilable(source_mappings.getPackageFromNullLs(null_ls_source_name)):map(function(package_name) local ok, pkg = pcall(registry.get_package, package_name) if ok then return pkg diff --git a/lua/mason-null-ls/ensure_installed.lua b/lua/mason-null-ls/ensure_installed.lua index 56e84c2..63defde 100644 --- a/lua/mason-null-ls/ensure_installed.lua +++ b/lua/mason-null-ls/ensure_installed.lua @@ -6,7 +6,7 @@ local function resolve_package(null_ls_source_name) local Optional = require('mason-core.optional') local source_mappings = require('mason-null-ls.mappings.source') - return Optional.of_nilable(source_mappings.null_ls_to_package[null_ls_source_name]):map(function(package_name) + return Optional.of_nilable(source_mappings.getPackageFromNullLs(null_ls_source_name)):map(function(package_name) local ok, pkg = pcall(registry.get_package, package_name) if ok then return pkg diff --git a/lua/mason-null-ls/init.lua b/lua/mason-null-ls/init.lua index d30b576..fd8e65f 100644 --- a/lua/mason-null-ls/init.lua +++ b/lua/mason-null-ls/init.lua @@ -53,7 +53,7 @@ function M.setup_handlers(handlers) local default_handler = Optional.of_nilable(handlers[1]):or_(_.always(Optional.of_nilable(default_setup))) _.each(function(handler) - if type(handler) == 'string' and not source_mappings.null_ls_to_package[handler] then + if type(handler) == 'string' and not source_mappings.getPackageFromNullLs(handler) then notify( ('mason-null-ls.setup_handlers: Received handler for unknown null-ls source name: %s.'):format(handler), vim.log.levels.WARN @@ -63,7 +63,7 @@ function M.setup_handlers(handlers) ---@param pkg_name string local function get_source_name(pkg_name) - return Optional.of_nilable(source_mappings.package_to_null_ls[pkg_name]) + return Optional.of_nilable(source_mappings.getNullLsFromPackage(pkg_name)) end local function call_handler(source_name) @@ -104,7 +104,7 @@ function M.get_installed_sources() local source_mappings = require('mason-null-ls.mappings.source') return _.filter_map(function(pkg_name) - return Optional.of_nilable(source_mappings.package_to_null_ls[pkg_name]) + return Optional.of_nilable(source_mappings.getNullLsFromPackage(pkg_name)) end, registry.get_installed_package_names()) end @@ -149,7 +149,7 @@ function M.get_available_sources(filter) end return _.filter_map(function(pkg_name) - return Optional.of_nilable(source_mappings.package_to_null_ls[pkg_name]):map(function(source_name) + return Optional.of_nilable(source_mappings.getNullLsFromPackage(pkg_name)):map(function(source_name) if #predicates == 0 or _.all_pass(predicates, source_name) then return source_name end diff --git a/lua/mason-null-ls/mappings/source.lua b/lua/mason-null-ls/mappings/source.lua index 19236e5..fceb8dc 100644 --- a/lua/mason-null-ls/mappings/source.lua +++ b/lua/mason-null-ls/mappings/source.lua @@ -1,101 +1,29 @@ local _ = require('mason-core.functional') +local Optional = require('mason-core.optional') local M = {} ---Maps null_ls source name to its corresponding package name. -M.null_ls_to_package = { - ['actionlint'] = 'actionlint', - ['alex'] = 'alex', - ['autopep8'] = 'autopep8', - ['beautysh'] = 'beautysh', - ['black'] = 'black', - ['blade_formatter'] = 'blade-formatter', - ['blue'] = 'blue', - ['buf'] = 'buf', - ['buildifier'] = 'buildifier', - ['cbfmt'] = 'cbfmt', - ['cfn_lint'] = 'cfn-lint', - ['clang_format'] = 'clang-format', +local null_ls_to_package = { ['cmake_format'] = 'cmakelang', - ['codespell'] = 'codespell', - ['commitlint'] = 'commitlint', - ['cpplint'] = 'cpplint', - ['csharpier'] = 'csharpier', - ['cspell'] = 'cspell', - ['curlylint'] = 'curlylint', - ['djlint'] = 'djlint', - ['dprint'] = 'dprint', - ['editorconfig_checker'] = 'editorconfig-checker', - ['elm_format'] = 'elm-format', - ['erb_lint'] = 'erb-lint', ['eslint_d'] = 'eslint_d', - ['fixjson'] = 'fixjson', - ['flake8'] = 'flake8', - ['gersemi'] = 'gersemi', - ['gitlint'] = 'gitlint', - ['gofumpt'] = 'gofumpt', - ['goimports'] = 'goimports', ['goimports_reviser'] = 'goimports_reviser', - ['golangci_lint'] = 'golangci-lint', - ['golines'] = 'golines', - ['hadolint'] = 'hadolint', - ['haml_lint'] = 'haml-lint', - ['isort'] = 'isort', - ['joker'] = 'joker', - ['jq'] = 'jq', - ['jsonlint'] = 'jsonlint', - ['ktlint'] = 'ktlint', - ['luacheck'] = 'luacheck', - ['markdownlint'] = 'markdownlint', - ['misspell'] = 'misspell', - ['mypy'] = 'mypy', - ['phpcbf'] = 'phpcbf', - ['phpcs'] = 'phpcs', ['phpcsfixer'] = 'php-cs-fixer', - ['phpmd'] = 'phpmd', - ['phpstan'] = 'phpstan', - ['pint'] = 'pint', - ['prettier'] = 'prettier', - ['prettierd'] = 'prettierd', - ['proselint'] = 'proselint', - ['protolint'] = 'protolint', - ['psalm'] = 'psalm', - ['pydocstyle'] = 'pydocstyle', - ['pylama'] = 'pylama', - ['pylint'] = 'pylint', - ['pyproject_flake8'] = 'pyproject-flake8', - ['reorder_python_imports'] = 'reorder-python-imports', - ['revive'] = 'revive', - ['rome'] = 'rome', - ['rstcheck'] = 'rstcheck', - ['rubocop'] = 'rubocop', - ['ruff'] = 'ruff', - ['rustfmt'] = 'rustfmt', - ['selene'] = 'selene', - ['shellcheck'] = 'shellcheck', - ['shellharden'] = 'shellharden', - ['shfmt'] = 'shfmt', - ['solhint'] = 'solhint', - ['sql_formatter'] = 'sql-formatter', - ['sqlfluff'] = 'sqlfluff', - ['standardrb'] = 'standardrb', - ['staticcheck'] = 'staticcheck', - ['stylua'] = 'stylua', - ['taplo'] = 'taplo', - ['textlint'] = 'textlint', - ['twigcs'] = 'twigcs', - ['usort'] = 'usort', - ['vale'] = 'vale', ['verible_verilog_format'] = 'verible', - ['vint'] = 'vint', - ['vulture'] = 'vulture', - ['write_good'] = 'write-good', - ['xo'] = 'xo', - ['yamlfmt'] = 'yamlfmt', - ['yamllint'] = 'yamllint', - ['yapf'] = 'yapf', } -M.package_to_null_ls = _.invert(M.null_ls_to_package) +local package_to_null_ls = _.invert(null_ls_to_package) + +---@param source string: Source Name from NullLs +---@return string: Package Name from Mason +M.getPackageFromNullLs = function(source) + return Optional.of_nilable(null_ls_to_package[source]):or_else_get(_.always(source:gsub('%_', '-'))) +end + +---@param package string: Package Name from Mason +---@return string: NullLs Source Name +M.getNullLsFromPackage = function(package) + return Optional.of_nilable(package_to_null_ls[package]):or_else_get(_.always(package:gsub('%-', '_'))) +end return M