Skip to content

Support for neovim/nvim-lspconfig #21

Open
@powerman

Description

@powerman

While recommended configuration for Neovim at https://termux-language-server.readthedocs.io/en/latest/resources/configure.html#neovim works it's inconvenient to use together with lspconfig. I propose to add lspconfig support for termux-language-server.

I've already implemented and tested most of it (I've no idea is it exist and if yes then how to detect a "root dir" for Android Termux and ArchLinux projects, so I leave a TODO comment there). Probably needs some polishing and then you can make a couple PRs:

  1. Add server configuration file to https://github.com/neovim/nvim-lspconfig/tree/master/lua/lspconfig/server_configurations
  2. Add mapping between lspconfig's name "termux_ls" and mason's name "termux-language-server" to https://github.com/williamboman/mason-lspconfig.nvim/blob/main/lua/mason-lspconfig/mappings/server.lua

Here is setup I've used for testing.

  1. ~/.config/nvim/lua/lspconfig/server_configurations/termux_ls.lua:
local util = require 'lspconfig.util'

return {
    default_config = {
        cmd = { 'termux-language-server' },
        filetypes = {
            -- Android Termux
            'sh.build', -- build.sh
            'sh.subpackage', -- *.subpackage.sh
            -- ArchLinux/Windows Msys2
            'sh.PKGBUILD', -- PKGBUILD
            'sh.install', -- *.install
            'sh.makepkg.conf', -- makepkg.conf
            -- Gentoo
            'sh.ebuild', -- *.ebuild
            'sh.eclass', -- *.eclass
            'sh.make.conf', -- /etc/make.conf, /etc/portage/make.conf
            'sh.color.map', -- /etc/portage/color.map
            -- Zsh
            'sh.mdd', -- *.mdd
        },
        root_dir = function(fname)
            local gentoo_repo = util.root_pattern 'profiles/repo_name'(fname)
            -- TODO: Root detection for Termux and ArchLinux?
            return gentoo_repo or util.find_git_ancestor(fname)
        end,
        single_file_support = true,
    },
    docs = {
        description = [[
Termux is a language server for some specific bash scripts.

You can install termux-language-server using mason or follow the instructions here: https://termux-language-server.readthedocs.io/en/latest/resources/install.html

The file types are not detected automatically, you can register them manually (see below) or override the filetypes:

```lua
vim.filetype.add {
  extension = {
    -- ArchLinux/Windows Msys2
    install = 'sh.install',
    -- Gentoo
    ebuild = 'sh.ebuild',
    eclass = 'sh.eclass',
    -- Zsh
    mdd = 'sh.mdd',
  },
  filename = {
    -- Android Termux
    ['build.sh'] = 'sh.build',
    -- ArchLinux/Windows Msys2
    ['PKGBUILD'] = 'sh.PKGBUILD',
    ['makepkg.conf'] = 'sh.makepkg.conf',
  },
  pattern = {
    -- Android Termux
    ['.*%.subpackage%.sh'] = 'sh.subpackage',
    -- Gentoo
    ['.*/etc/make%.conf'] = 'sh.make.conf',
    ['.*/etc/portage/make%.conf'] = 'sh.make.conf',
    ['.*/etc/portage/color%.map'] = 'sh.color.map',
  },
}
```
]],
    },
}
  1. Somewhere else in my configs:
    vim.filetype.add {
        extension = {
            -- ArchLinux/Windows Msys2
            install = 'sh.install',
            -- Gentoo
            ebuild = 'sh.ebuild',
            eclass = 'sh.eclass',
            -- Zsh
            mdd = 'sh.mdd',
        },
        filename = {
            -- Android Termux
            ['build.sh'] = 'sh.build',
            -- ArchLinux/Windows Msys2
            ['PKGBUILD'] = 'sh.PKGBUILD',
            ['makepkg.conf'] = 'sh.makepkg.conf',
        },
        pattern = {
            -- Android Termux
            ['.*%.subpackage%.sh'] = 'sh.subpackage',
            -- Gentoo
            ['.*/etc/make%.conf'] = 'sh.make.conf',
            ['.*/etc/portage/make%.conf'] = 'sh.make.conf',
            ['.*/etc/portage/color%.map'] = 'sh.color.map',
        },
    }
  1. At beginning of lspconfig setup (adding our config because it's not supported officially yet):
            local configs = require 'lspconfig.configs'
            if not configs['termux_ls'] then
                configs['termux_ls'] = require 'lspconfig/server_configurations/termux_ls'
            end
            local server = require 'mason-lspconfig.mappings.server'
            server.lspconfig_to_package['termux_ls'] = 'termux-language-server'
            server.package_to_lspconfig['termux-language-server'] = 'termux_ls'

That's it, the rest of setup is usual for lspconfig.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions