@@ -68,6 +68,13 @@ you may have installed in your project's virtual environment.
6868
6969## Setup Alternatives
7070
71+ If you want a simple LSP configuration and have RobotCode installed in your
72+ project anyway (or don't mind adding it), [ Option 1] ( #option-1-use-local-installation-of-robotcode )
73+ is ideal.
74+
75+ If you prefer to install RobotCode globally via ` Mason ` , you need to tweak
76+ the LSP configuration a bit - see [ Option 2] ( #option-2-use-globally-installed-robotcode-and-set-pythonpath ) .
77+
7178### Option 1: Use Local Installation of RobotCode
7279
7380The easiest way to run RobotCode in the context of your project's virtual environment
@@ -143,26 +150,45 @@ Next, create the LSP configuration under
143150--- https://robotcode.io
144151---
145152--- RobotCode - Language Server Protocol implementation for Robot Framework.
153+
154+ --- @return string | nil
146155local 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
156+ -- Search for the site-packages directory in the .venv folder.
157+ -- The folder structure differs between Windows and Unix,
158+ -- but this function handles both.
159+ local cwd = vim .uv .cwd ()
160+ local project_site_packages = vim .fs .find (" site-packages" , {
161+ path = cwd .. " /.venv" ,
162+ type = " directory" ,
163+ limit = 1 ,
164+ })[1 ]
165+
166+ if not project_site_packages then
167+ -- If the site-packages were not found, RobotCode will still work,
168+ -- but import errors will appear for third party libraries.
169+ vim .notify (" RobotCode: project virtual environment not found." )
170+ return nil
171+ end
172+
173+ local pythonpath = project_site_packages
174+ if vim .env .PYTHONPATH then
175+ -- Preserve original PYTHONPATH if already set by some other plugin
176+ pythonpath = project_site_packages .. " :" .. vim .env .PYTHONPATH
177+ end
178+ return pythonpath
153179end
154180
181+ local python_path = get_python_path ()
182+
155183--- @type vim.lsp.Config
156184return {
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 ,
185+ cmd = { " robotcode" , " language-server" },
186+ cmd_env = python_path and { PYTHONPATH = python_path } or nil ,
187+ filetypes = { " robot" , " resource" },
188+ root_markers = { " robot.toml" , " pyproject.toml" , " Pipfile" , " .git" },
189+ get_language_id = function (_ , _ )
190+ return " robotframework"
191+ end ,
166192}
167193```
168194
0 commit comments