Skip to content

Commit 5e262b1

Browse files
committed
docs(tips-and-tricks): LSP setup guide for Neovim
1 parent 6c86914 commit 5e262b1

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# Configuring RobotCode as Language Server for Neovim
2+
3+
[Neovim](https://neovim.io/) is an extensible Vim-based text editor.
4+
While there is no fully featured plugin of RobotCode that you can just install
5+
and use "as is" like for VS Code, it is still possible to leverage the
6+
[Language Server](https://microsoft.github.io/language-server-protocol/)
7+
provided by RobotCode to enable static analysis, go-to-definition, and other
8+
useful features.
9+
10+
This guide shows two alternatives to set up and configure your Neovim
11+
installation to use the RobotCode language server properly.
12+
13+
## Common Prerequisites
14+
15+
To follow this guide, the reader is expected to already know the basics of
16+
17+
* installing and configuring Neovim
18+
* adding plugins to Neovim
19+
* Python virtual environments and how to create them
20+
21+
Regardless of the option you choose, using a language server in Neovim
22+
always requires to
23+
24+
* **install** the language server
25+
* **configure** the language server
26+
* **enable** the language server
27+
28+
This guide assumes a Neovim version >= 0.11 and uses the built-in LSP API.
29+
30+
## The Common Pitfall When Using Mason and nvim-lspconfig
31+
32+
Two plugins are commonly used to install and configure LSP servers for Neovim,
33+
and are included in Neovim starter distributions like [LazyVim](https://www.lazyvim.org/):
34+
35+
* [mason.nvim](https://github.com/mason-org/mason.nvim):
36+
a package manager that lets you install and manage LSP servers, including RobotCode
37+
* [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig):
38+
quickstart configurations for LSP servers, including a configuration for RobotCode
39+
40+
While this combination sounds ideal, you will quickly experience import errors for
41+
third party libraries and custom keywords. Understanding why this happens is important
42+
to understand the differences of the alternatives discussed below.
43+
44+
![Import Errors in Neovim](./images/neovim-mason-import-errors.png)
45+
46+
`Mason` installs every package in a dedicated virtual environment, and makes the
47+
corresponding binary available globally from within Neovim.
48+
49+
`nvim-lspconfig` provides the following configuration:
50+
51+
```lua
52+
return {
53+
cmd = { 'robotcode', 'language-server' }, -- [!code focus]
54+
filetypes = { 'robot', 'resource' },
55+
root_markers = { 'robot.toml', 'pyproject.toml', 'Pipfile', '.git' },
56+
get_language_id = function(_, _)
57+
return 'robotframework'
58+
end,
59+
}
60+
```
61+
62+
This will start the language server by using the first `robotcode` binary found
63+
in `PATH`, which most likely is the one installed via `Mason`.
64+
65+
In this situation, RobotCode can only "see" the packages that are available in the
66+
virtual environment created by `Mason`, lacking all third party keyword libraries
67+
you may have installed in your project's virtual environment.
68+
69+
## Setup Alternatives
70+
71+
### Option 1: Use Local Installation of RobotCode
72+
73+
The easiest way to run RobotCode in the context of your project's virtual environment
74+
is to add `robotcode[languageserver]` (or simply `robotcode[all]`) to your dependencies.
75+
76+
To configure the RobotCode language server, install `nvim-lspconfig`, or create
77+
the file manually under `~/.config/nvim/lsp/robotcode.lua`:
78+
79+
```lua
80+
---@brief
81+
---
82+
--- https://robotcode.io
83+
---
84+
--- RobotCode - Language Server Protocol implementation for Robot Framework.
85+
return {
86+
cmd = { 'robotcode', 'language-server' },
87+
filetypes = { 'robot', 'resource' },
88+
root_markers = { 'robot.toml', 'pyproject.toml', 'Pipfile', '.git' },
89+
get_language_id = function(_, _)
90+
return 'robotframework'
91+
end,
92+
}
93+
```
94+
95+
Enable the LSP server by adding the following line to `~/.config/nvim/init.lua`:
96+
97+
```lua
98+
vim.lsp.enable("robotcode")
99+
```
100+
101+
Before starting Neovim, make sure to first activate the virtual environment.
102+
If your virtual environment is created in the folder `.venv`:
103+
104+
::: code-group
105+
106+
``` shell [Mac/Linux]
107+
source .venv/bin/activate
108+
nvim
109+
```
110+
111+
``` ps [Windows PowerShell/pwsh]
112+
.venv\Scripts\activate.ps1
113+
nvim
114+
```
115+
116+
``` cmd [Windows CMD]
117+
.venv\Scripts\activate.cmd
118+
nvim
119+
```
120+
121+
:::
122+
123+
This ensures the `robotcode` binary from your project's environment is the first
124+
in `PATH`.
125+
126+
### Option 2: Use Globally Installed RobotCode and Set PYTHONPATH
127+
128+
With this approach it is not necessary to install RobotCode in each and
129+
every project.
130+
You use `Mason` to install and update RobotCode globally for Neovim. The LSP configuration
131+
provides a helper function to set the PYTHONPATH variable so the globally installed
132+
RobotCode can import all your project specific libraries.
133+
134+
First, install RobotCode by executing `:MasonInstall robotcode` from within
135+
Neovim, or by using the `Mason` UI (`:Mason`).
136+
137+
Next, create the LSP configuration under
138+
`~/.config/nvim/lsp/robotcode.lua`:
139+
140+
```lua
141+
---@brief
142+
---
143+
--- https://robotcode.io
144+
---
145+
--- RobotCode - Language Server Protocol implementation for Robot Framework.
146+
local function get_python_path()
147+
local project_site_packages = vim.fn.glob(vim.loop.cwd() .. "/.venv/lib/python*/site-packages", true, true)[1]
148+
local pythonpath = project_site_packages
149+
if vim.env.PYTHONPATH then
150+
pythonpath = project_site_packages .. ":" .. vim.env.PYTHONPATH
151+
end
152+
return pythonpath
153+
end
154+
155+
---@type vim.lsp.Config
156+
return {
157+
cmd = { 'robotcode', 'language-server' },
158+
cmd_env = {
159+
PYTHONPATH = get_python_path(),
160+
},
161+
filetypes = { 'robot', 'resource' },
162+
root_markers = { 'robot.toml', 'pyproject.toml', 'Pipfile', '.git' },
163+
get_language_id = function(_, _)
164+
return 'robotframework'
165+
end,
166+
}
167+
```
168+
169+
Note that `get_python_path` assumes that your virtual environment is created
170+
inside your project folder in a folder called `.venv`, which is a
171+
widespread standard but not necessarily true for some tools (e.g. `pyenv`).
172+
173+
Finally, enable the LSP server in `~/.config/nvim/init.lua`:
174+
175+
```lua
176+
vim.lsp.enable("robotcode")
177+
```
178+
179+
This solution also works if you have some projects that have RobotCode installed
180+
locally. The downside is that you may have to tweak `get_python_path` if you don't
181+
follow the `.venv` folder convention.
182+
183+
## Final Notes
184+
185+
Be aware that this setup only enables the features provided by the language server,
186+
i.e. diagnostics, completions, go-to-definition etc.
187+
Unlike the VS Code plugin this setup does not enable you to run or debug robot tests
188+
from within Neovim.
60.9 KB
Loading

0 commit comments

Comments
 (0)