Talks - Chahak Mehta: LSP Demystified - How towrite an LSP server in Python

Learn how to build Python language servers with LSP, enabling IDE features like autocompletion and linting across multiple editors. A practical guide to LSP development.

Key takeaways
  • LSP (Language Server Protocol) provides a standardized way for code editors to communicate with language-specific tooling, introduced by Microsoft in 2016

  • Language servers communicate via JSON-RPC protocol and handle tasks like:

    • Code completion/intellisense
    • Error diagnostics/linting
    • Hover documentation
    • Go to definition
    • Code actions and refactoring
  • The pygls library provides a Python framework for building language servers with minimal boilerplate code

  • Key benefits:

    • One server implementation works with all LSP-compatible editors
    • Easier to add new language features (only server-side changes needed)
    • Decouples language tooling from editor implementation
  • Language server workflow:

    1. Parse code into abstract syntax tree (AST)
    2. Analyze AST for linting/completions/etc
    3. Send diagnostics/suggestions back to client
  • Smart autocompletion is context-aware:

    • Suggests based on cursor position in AST
    • Can include documentation with suggestions
    • Can filter suggestions based on context
  • Performance considerations:

    • Can implement debouncing for keystroke events
    • Clients can control when to show suggestions
    • Heavy processing only needed after relevant changes
  • Distribution is straightforward:

    • Package as Python library
    • Client just needs minimal configuration
    • Communication happens over stdio