MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

分析:creativenull/efmls-configs-nvim: 构建 Neovim 的语言服务配置集

项目背景与痛点

在现在的编程环境中,保持代码质量是衡量代码健康的重要指标之一。语言服务是实现这一目标的利器,它不仅能够在开发过程中提供实时的代码检查、语法高亮、自动补全等特性,还能帮助开发者通过集成各式各样的代码检查工具(如 linters 和 formatters)来规范代码风格,提高代码质量。尽管 Neovim 提供了强大的插件支持,但要将这些语言服务工具与 efm-langserver 集成仍充满挑战,特别是需要针对不同的编程语言定制配置。而 efmls-configs-nvim 恰逢其时,它集合了这些配置,为开发者省去了大量的配置工作,让 Neovim 用户能够快速、高效地享受到语言服务带来的便利。

核心技术揭秘

efmls-configs-nvim 的精髓在于它为 efm-langserver 提供了一套全面且灵活的配置方案。通过使用 Lua 脚本编写配置文件,efmls-configs-nvim 能够根据用户的指定语言自动加载相应的 linters 和 formatters,并将这些工具的输出体现在编辑器中。这些配置脚本能够方便地将多种不同类型的工具集成到一起,从而为用户提供一个一体化的语言服务解决方案。

Data Flow

graph TD A[用户编辑代码] --> B[efm-langserver] B --> C{"linter/formatter选择"} C -->|"linter"| D[执行 linter] C -->|"formatter"| E[执行 formatter] D --> F[返回错误信息] E --> F

在图中,用户在 Neovim 中编辑代码时,efm-langserver 会根据当前语言自动选择对应的 linter 或 formatter 进行代码分析。随后,分析结果包含错误信息等形式将返回给用户。

功能亮点与差异

efmls-configs-nvim 的真正亮点在于它的灵活性和可扩展性。它提供了一套清晰的模板,帮助开发者方便地添加支持新的 linters 或 formatters。与同类库相比,efmls-configs-nvim 独特之处在于它专注于提供一个统一且易用的配置框架,从而简化了 Neovim 用户在使用 efm-langserver 时的配置过程。此外,它还提供了遵循语言社区标准的最佳实践,为用户提供了一套高质量的配置。

应用场景与落地建议

在实际使用中,efmls-configs-nvim 适用于所有 Neovim 用户,尤其是那些需要处理多种编程语言代码的开发者。使用该库时,最需要注意的是确保所有依赖的 linters 和 formatters 工具已经正确安装,并且根据需要调整相关配置以适应特定的代码风格规范。此外,在生产环境中使用时,建议开发者定期检查配置更新,以确保始终使用最新、最佳的工具组合来保持代码质量。

综合评价

efmls-configs-nvim 在 Neovim 的 efm-langserver 集成中发挥着不可或缺的作用,其强大的配置能力、灵活的可扩展性和与 efm-langserver 的无缝结合能力使其成为 Neovim 用户实现高质量代码编辑的最佳选择之一。不过,单一的外部工具集成总有一定局限性,例如在跨平台支持、工具版本兼容性等方面可能存在不足,用户需要根据具体情况评估并适当调整配置以适用于特定的开发场景。

本文通过深入分析 efmls-configs-nvim,揭示了它在提高 Neovim 用户代码编辑体验中的价值,为 Neovim 开发者的工具链选择提供了有价值的参考。

关于

GitHub 原始描述(英文)

An unofficial collection of linters and formatters configured for efm-langserver for neovim.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
423
价值分
206
活跃状态
活跃

语言占比

Lua
TypeScript

Release

README

efmls-configs-nvim

Configs on dotfyle

An unofficial collection of linters and formatters configured for efm-langserver for neovim.

Supported linters and formatters

doc/SUPPORTED_LIST.md

Features

  • Out-of-box configurations for 90+ tools (linters and formatters)
  • Intelligently detect tools installed project-wide or system-wide - works only for node/npm, php/composer and ruby/bundler (additional support for other build tools coming soon, welcome any contributions)
  • :checkhealth integration for status check

Documentation

Documentation can be accessed via :help efmls-configs or further below.

Requirements

Installation

Install with your favorite plugin manager or just use builtin packages.

lazy.nvim

{
  'creativenull/efmls-configs-nvim',
  version = 'v1.x.x', -- version is optional, but recommended
  dependencies = { 'neovim/nvim-lspconfig' }, -- not required if using nvim >= 0.11
}

vim-plug

Plug 'neovim/nvim-lspconfig' " not required if using nvim >= 0.11
Plug 'creativenull/efmls-configs-nvim', { 'tag': 'v1.*' } " tag is optional, but recommended

Setup

See also :help efmls-configs-setup to view docs inside neovim.

To get started, make a languages table that will define configurations for the language of your choice. Or use the defaults provided by this plugin.

-- Register linters and formatters per language
local eslint = require('efmls-configs.linters.eslint')
local prettier = require('efmls-configs.formatters.prettier')
local stylua = require('efmls-configs.formatters.stylua')
local languages = {
  typescript = { eslint, prettier },
  lua = { stylua },
}

-- Or use the defaults provided by this plugin
-- check doc/SUPPORTED_LIST.md for the supported languages
--
-- local languages = require('efmls-configs.defaults').languages()

local efmls_config = {
  filetypes = vim.tbl_keys(languages),
  settings = {
    rootMarkers = { '.git/' },
    languages = languages,
  },
  init_options = {
    documentFormatting = true,
    documentRangeFormatting = true,
  },
}

-- If using nvim >= 0.11 then use the following
vim.lsp.config('efm', vim.tbl_extend('force', efmls_config, {
  cmd = { 'efm-langserver' },

  -- Pass your custom lsp config below like on_attach and capabilities
  --
  -- on_attach = on_attach,
  -- capabilities = capabilities,
}))

-- If using nvim <= 0.10 then use the following
require('lspconfig').efm.setup(vim.tbl_extend('force', efmls_config, {
  -- Pass your custom lsp config below like on_attach and capabilities
  --
  -- on_attach = on_attach,
  -- capabilities = capabilities,
}))

Default Configurations

See also :help efmls-configs-defaults to view docs inside neovim.

Default configurations are an opt-in feature. To see all the configurations provided by default go to doc/SUPPORTED_LIST.md.

You can use a list of defaults provided by this plugin, in-case you don’t want to specify configuration for each language.

local languages = require('efmls-configs.defaults').languages()

To add additional tools which are not provided by default you can extend via vim.tbl_extend(). This can also be used as a way to override defaults.

local languages = require('efmls-configs.defaults').languages()
languages = vim.tbl_extend('force', languages, {
  -- Custom languages, or override existing ones
  html = {
    require('efmls-configs.formatters.prettier'),
  },
})

Tips and Tricks

Format on save

There are couple ways you can format your code on save.

  1. Register an autocmd to run format on save:
local lsp_fmt_group = vim.api.nvim_create_augroup('LspFormattingGroup', {})
vim.api.nvim_create_autocmd('BufWritePost', {
  group = lsp_fmt_group,
  callback = function(ev)
    local efm = vim.lsp.get_active_clients({ name = 'efm', bufnr = ev.buf })

    if vim.tbl_isempty(efm) then
      return
    end

    vim.lsp.buf.format({ name = 'efm' })
  end,
})
  1. If you do not want to write and maintain the code above, then you can rely on a plugin like lukas-reineke/lsp-format.nvim which makes it easier to format on save.

Troubleshooting

See also :help efmls-configs-issues to view docs inside neovim.

Always run :checkhealth to see if there are any issue, when you get no response from the linter or formatter as you expected.

If you get “no executable found” issues in :checkhealth, this means that the linter or formatter was not found in the provided filepath. Ensure that it is installed globally or in a valid filepath.

For nodejs/npm, php/composer, ruby/bundler: check if the linter or formatter is installed in your node_modules (npm), vendor (composer/bundler) project folder, or installed globally.

Mason setup issue (:help efmls-configs-mason)

If you use mason, then you don’t need to add anything extra for efmls-configs for it to work. But if you are having problems setting up then check the following:

  1. Make sure you call mason setup() before vim.lsp.config('efm', {...}) (if using nvim 0.10 and below, then make sure it’s before lspconfig setup())
  2. Run :!which <tool> to check if the path is provided by mason

Alternatives

Credits

Credits goes to the following projects for inspiration:

  • efm-langserver for this awesome language server to provide an interface to run linters/formatters through a LSP protocol
  • ALE for a huge list of linters/formatters to look through and add them in here

评论

暂无评论