termux / termux-language-server

🛠️ A language server for some specific bash scripts
https://termux-language-server.readthedocs.io/
GNU General Public License v3.0
67 stars 7 forks source link

Support for neovim/nvim-lspconfig #21

Open powerman opened 1 month ago

powerman commented 1 month ago

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:

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',
  },
}

]], }, }


2. Somewhere else in my configs:
```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. 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.

Freed-Wu commented 1 month ago

It looks like related to https://github.com/termux/termux-language-server/discussions/10?

powerman commented 1 month ago

It looks like related to #10?

Yes, it is! I was happy to see it's already supported by Mason, it was a pleasant surprise. In theory Mason support is not required to use lspconfig, but in practice everyone depends on Mason, so without it we'll need some hacks and non-standard setup.

TomJo2000 commented 1 month ago

Mason also does not work particularly well on Termux itself, so it would be nice to have official instuctions for setting up termux-language-server with lspconfig.

powerman commented 1 month ago

Mason also does not work particularly well on Termux itself

Not sure I understand what you mean. If there are issues with using Mason itself then all you need to use lspconfig (as shown above) without Mason is to make sure your nvim config won't call some mason plugin (e.g mason itself or 'williamboman/mason-lspconfig.nvim' or 'WhoIsSethDaniel/mason-tool-installer.nvim') to install termux-language-server (e.g. with 'termux_ls' item inside 'ensure_installed' list).

Of course, in this case you'll have to install termux-language-server manually.

TomJo2000 commented 1 month ago

If there are issues with using Mason itself then all you need to use lspconfig (as shown above) without Mason is to make sure your nvim config won't call some mason plugin

Am aware. https://github.com/TomJo2000/.dotfiles/blob/478ea851ddde95861388111b1f4f034336588399/.config/nvim/lua/plugins/init.lua#L82-L122