Skip to content

Commit

Permalink
feat!: source.lua is not required for package names following normal …
Browse files Browse the repository at this point in the history
…conventions (#40)

Co-authored-by: Jay Patel <36803168+jayp0521@users.noreply.github.com>
  • Loading branch information
jay-babu and jay-babu authored Dec 12, 2022
1 parent ab5d996 commit e2144bd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 100 deletions.
14 changes: 7 additions & 7 deletions lua/mason-null-ls/api/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/mason-null-ls/automatic_installation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/mason-null-ls/ensure_installed.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lua/mason-null-ls/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
102 changes: 15 additions & 87 deletions lua/mason-null-ls/mappings/source.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e2144bd

Please sign in to comment.