Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for neovim/nvim-lspconfig #21

Open
powerman opened this issue Jul 15, 2024 · 10 comments
Open

Support for neovim/nvim-lspconfig #21

powerman opened this issue Jul 15, 2024 · 10 comments
Labels
enhancement New feature or request

Comments

@powerman
Copy link

powerman commented Jul 15, 2024

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.

@Freed-Wu
Copy link
Collaborator

It looks like related to #10?

@Freed-Wu Freed-Wu added the enhancement New feature or request label Jul 15, 2024
@powerman
Copy link
Author

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
Copy link
Member

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
Copy link
Author

powerman commented Jul 15, 2024

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
Copy link
Member

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

@txtsd
Copy link

txtsd commented Nov 5, 2024

3. 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'

@powerman What file do I add this segment to? I'm trying to get this to work for me. I'm an AUR maintainer, looking to get initiated as an official Arch packager.

@powerman
Copy link
Author

powerman commented Nov 5, 2024

@txtsd You can see my complete config with this snippet here: https://github.com/search?q=repo%3Apowerman%2Fconfig.nvim+termux&type=code

@txtsd
Copy link

txtsd commented Nov 5, 2024

@powerman I used the relevant bits and incorporated it in my lazyvim setup, but it shows my entire PKGBUILD as an error.

2024-11-05-16:10:39:44:327516431-960x1052_grim

Is this something you're familiar with?

@powerman
Copy link
Author

powerman commented Nov 5, 2024

Nope. If Neovim LSP works but show unexpected issues in your sources then open a separate issue about this.

@gwuen
Copy link

gwuen commented Nov 17, 2024

Thanks for this example config!

I'm just wondering if it wouldn't be a better approach to check with nvim-lspconfig's contribution guidelines and then just make a pull request directly over there. This server seems pretty stable by now, and even Mason supports it.

On another note, does this server even need a root_dir? It doesn't seem like workspaceFolders, rootUri or rootPath are ever utilized. Or is such a feature on the roadmap? Maybe @Freed-Wu can answer this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants