Robot Language Server - Vim

Hi all,

i’m looking for a way how to integrate LSP into VIM editor. I found that there are lsp implementation in VIM. But I’m doing this first time, and I have no idea how to run robot lsp itself to configure vim lsp to ask for an keywords and stuff around

Does anybody tried to do something like this? There are few plugins with robot framework syntax highlighting, but lsp would be great.

I found in some presentation that plugin is working with neovim, but I do not find any link to download neovim plugin.

Thanks

2 Likes

I am looking for the same: Syntax Highlight and LSP Server.
Using Neovim 0.7.

1 Like

For lsp I managed to get it up and running with just these:

nvim-lspconfig/server_configurations.md at master · neovim/nvim-lspconfig · GitHub

and installed the lsp with pip3 install robotframework-lsp

Syntax highlighting was ok with GitHub - mfukar/robotframework-vim: Some vim scripts for use with the Robot framework.

1 Like

I got this working mostly as per the above.

For syntax highlighting I guess it’s not supported by nvim-treesitter. So copy files from the repo as follows:

git clone https://github.com/mfukar/robotframework-vim
cd robotframework-vim
cp ftdetect/robot.vim .config/nvim/ftdetect/
cp after/syntax/robot.vim .config/nvim/syntax/robot.vim

For LSP support add the following lines to init.lua to bootstrap with Packer:

-- Bootstrap packer.nvim
-- from https://github.com/wbthomason/packer.nvim#bootstrapping

local ensure_packer = function()
  local fn = vim.fn
  local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim'
  if fn.empty(fn.glob(install_path)) > 0 then
    fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path})
    vim.cmd [[packadd packer.nvim]]
    return true
  end
  return false
end

local packer_bootstrap = ensure_packer()

return require('packer').startup(function(use)
  use 'wbthomason/packer.nvim'
  -- My plugins here

  -- LSP nvim-cmp
  use 'neovim/nvim-lspconfig' -- Collection of configurations for built-in LSP client
  use 'hrsh7th/nvim-cmp' -- Autocompletion plugin
  use 'hrsh7th/cmp-nvim-lsp' -- LSP source for nvim-cmp
  use 'saadparwaiz1/cmp_luasnip' -- Snippets source for nvim-cmp
  use 'L3MON4D3/LuaSnip' -- Snippets plugin
  use 'williamboman/mason.nvim' -- manage LSP and DAP servers, linters and formatters

  -- Automatically set up your configuration after cloning packer.nvim
  -- Put this at the end after all plugins
  if packer_bootstrap then
    require('packer').sync()
  end
end)

Then add in init.lua or a submodule within .config/nvim/lua/ the following to setup Mason:

require("mason").setup()

And then add the following lines to setup nvim-cmp (auto complete) for C, python and robot framework:

-- Add additional capabilities supported by nvim-cmp
local capabilities = require("cmp_nvim_lsp").default_capabilities()

local lspconfig = require('lspconfig')

-- Enable some language servers with the additional completion capabilities offered by nvim-cmp
local servers = { 'clangd', 'pylsp', 'robotframework_ls' }
for _, lsp in ipairs(servers) do
  lspconfig[lsp].setup {
    -- on_attach = my_custom_on_attach,
    capabilities = capabilities,
  }
end

require('lspconfig')['pylsp'].setup {
    capabilities = capabilities,
    settings = {
        pylsp = {
            plugins = {
                jedi_completion = {
                    include_params = true,
                },
            },
        },
    },
}

-- luasnip setup
local luasnip = require 'luasnip'

-- nvim-cmp setup
local cmp = require 'cmp'
cmp.setup {
  snippet = {
    expand = function(args)
      luasnip.lsp_expand(args.body)
    end,
  },
  mapping = cmp.mapping.preset.insert({
    ['<C-d>'] = cmp.mapping.scroll_docs(-4),
    ['<C-f>'] = cmp.mapping.scroll_docs(4),
    ['<C-Space>'] = cmp.mapping.complete(),
    ['<CR>'] = cmp.mapping.confirm {
      behavior = cmp.ConfirmBehavior.Replace,
      select = true,
    },
    ['<Tab>'] = cmp.mapping(function(fallback)
      if cmp.visible() then
        cmp.select_next_item()
      elseif luasnip.expand_or_jumpable() then
        luasnip.expand_or_jump()
      else
        fallback()
      end
    end, { 'i', 's' }),
    ['<S-Tab>'] = cmp.mapping(function(fallback)
      if cmp.visible() then
        cmp.select_prev_item()
      elseif luasnip.jumpable(-1) then
        luasnip.jump(-1)
      else
        fallback()
      end
    end, { 'i', 's' }),
  }),
  sources = {
    { name = 'nvim_lsp' },
    { name = 'luasnip' },
  },
}

At this stage I exited neovim and restarted it, then ran commands:

:PackerSync

to install the new plugins and

:Mason

to launch the Mason interface, where I scrolled down to robotframework-lsp and pressed the i key to install it. Also installed the python-lsp-server.

Used pylsp instead of the default python language server because it supports function argument signatures and shows docstrings.

For robot files when I ran nvim command :LspInfo I was getting the following error for root directory: Not found:

I noticed here, it is looking for either robotidy.toml or pyproject.toml to indocate the root directory, or a git repo. I just ran git init and then it started working. You can also just create one of those two files.

Still not quite as nice as VS Code robocorp plugin as it’s missing the docstrings in the autocomplete for the robot framework keywords. Also missing is the DAP protcol for the robotframework_debug_adapter for debugging support. Also integration of robocop linter and robotidy formatter would be nice. Should all be doable I’d imagine. Though robocop/robotide can all be done at the CLI easily enough.

Perhaps a neovim plugin that pulls all of this together is in order.

2 Likes