2024-08-30

Jedi Python LSP for VIM

The Ruff Language Server Protocol (LSP) is intended to show Ruff diagnostics and provide code actions to address them. However, it should be used alongside another Python LSP to access features like navigation and autocompletion.

Jedi Language Server is a Python language server that leverages the capabilities of the Jedi library. Jedi is a static analysis tool for Python, known for its powerful autocompletion and code navigation features.

As a Temu Affiliate I earn from qualifying purchases

In the video below, we showcase some of the main Jedi LSP functionalities, including Go to Definition, help on hover, and autocompletion for class methods.

LSP Client Configuration

Configuring the Vim LSP client is straightforward by defining the variable lspServers in the .vimrc file, as also demonstrated in our previous blog posts.

let lspServers = [#{
            \    name: 'jedi',
            \    filetype: ['python'],
            \    path: 'jedi-language-server.exe',
            \    args: [''],
            \ },
            \#{
            \    name: 'py-ruff',
            \    filetype: ['python'],
            \    path: 'C:\\Users\Seve\\workspace\\toolbox\\bin\\ruff.exe',
            \    args: ['server'],
            \ }]

Note that the newest version of ruff.exe (0.6.3 at the time of writing) does not require the preview option, as the LSP is now fully integrated into the command line executable.

What is pyproject.toml?

pyproject.toml is a configuration file used in Python projects to define build system requirements and tool-specific settings. It was introduced in PEP 518 and expanded in subsequent PEPs. This file helps standardize project configurations and can be used by various tools such as linters, type checkers, and build systems.

The configuration below ensures that Ruff is used for linting with specific rules and exclusions, while Jedi is enabled as the language server with optimized settings for certain modules.


    [tool.ruff]
    line-length = 80
    exclude = ["build", "dist", "venv", "__pycache__"]
    # Enable Ruff as the linter
    lint.select = ["E", "F", "W", "C", "N", "B", "Q"]

    [tool.jedi]
    # Enable Jedi as the language server
    enabled = true

    [tool.jedi.settings]
    # Jedi settings for better performance and module resolution
    autoImportModules = ["numpy", "pandas", "holidays"]

The following vim9script is designed to set up a custom :make command for Python scripts in Vim, allowing you to run Python files easily and see the output in the terminal.

    vim9script
    echom 'Setting :make for python scripts'
    def SetMakeprg(): void
        var &makeprg='py ' . expand( '% : p' )
    enddef

    autocmd BufEnter *.py call SetMakeprg()


    set errorformat=%A%f:%l:\ %m,%C%\ %m 

    call determined#command('Py', 'py', {
        'background': 0,
        'autoclose': 0,
        'reuse': 1,
        'expand': 1 })

Moreover, after installing the plugin Vim-Determined it's possible to register a new command named :Py that runs the current buffer via the Python interpreter. The options provided in the dictionary configure how, for example, the command:

   :Py %

behaves.