Installation¶
You’ll need a Python installation and a Sphinx project to start with Sphinx-LuaLs.
If you’re new to Python and Sphinx
Installing Python
We recommend using PyEnv to manage python installations on your system.
Similar to LuaRocks, it creates executables for Python, Pip (Package Installer for Python), and other tools. When run, these executables determine which Python environment to use, and invoke the appropriate command.
Following its installation guide, install PyEnv, configure your shell, and install build dependencies.
Make sure that PyEnv shims are in your path by checking them with
which:$ which python /home/user/.pyenv/shims/python $ which pip /home/user/.pyenv/shims/pip
Once ready, you can install Python 3.12:
$ pyenv install 3.12.2
To avoid installing Python packages globally, we recommend using virtual environments. It is a good practice to create a separate environment for each project:
$ pyenv virtualenv 3.12.2 my-project
Once a new environment is created, you’ll need to activate it. Navigate to your project directory and run the following command:
$ pyenv local my-project
This will create a file called
.python-version. Every time you run an executable that was installed by PyEnv, it will look for a.python-versionfile to determine which environment to use. Thus, when running Python from your project’s directory, it will always use the right virtual environment.Installing Sphinx
You can now install Sphinx using Pip:
$ pip install sphinx
To make your life easier, you can create a file named
requirements.txt, and list all of the dependencies there:$ echo "sphinx~=8.0" > requirements.txt
Now, you can install all dependencies at once:
$ pip install -r requirements.txt
Creating a Sphinx project
Sphinx comes with a tool for creating new projects. Make a directory for your documentation and run the
sphinx-quickstartcommand in it:$ mkdir docs/ $ cd docs/ $ sphinx-quickstart
Once finished, you’ll see several files generated.
Makefilecontains commands to build documentation,conf.pycontains configuration, andindex.rstis the main document.Try building documentation and see the results:
$ make html $ open build/html/index.html
Install
sphinx-lua-lsusing Pip:$ pip install sphinx-lua-ls
Add it to the
extensionslist in yourconf.py, and specify the location of your Lua project:extensions = [ "sphinx_lua_ls", ] # Path to the folder containing the `.emmyrc.json`/`.luarc.json` file, # relative to the directory with `conf.py`. lua_ls_project_root = "../"
Configure which language analyzer you want to use:
Set
lua_ls_backendto"emmylua"in yourconf.py:lua_ls_backend = "emmylua"
Tip
Add the following settings to your
.emmyrc.jsonto enable syntax highlighting and Go To Definition for comments:{ "diagnostics": { "enables": ["unknown-doc-tag"] }, "doc": { "knownTags": ["doctype", "doc"], "syntax": "rst" } }
{ "diagnostics": { "enables": ["unknown-doc-tag"] }, "doc": { "knownTags": ["doctype", "doc"], "syntax": "myst" } }
Set
lua_ls_backendto"luals"in yourconf.py:lua_ls_backend = "luals"
If you plan to use Markdown in code comments, install the MySt plugin for Sphinx and add it to the
extensionslist in yourconf.py.