LSP for Sublime Text

repository·main·Indexed 23 days ago

https://github.com/sublimelsp/lsp

A plugin for Sublime Text that implements the Language Server Protocol (LSP) to provide IDE-like features including autocompletion, go-to-definition, diagnostics, and semantic highlighting. It provides the client infrastructure to communicate with various language servers, supporting both STDIO and TCP transports, and allows for global or project-specific server configurations.

Tokens
17.9K
Snippets
56
Records
92
Agent score
83%

What's inside LSP

  1. Use Call and Type Hierarchies

    main

    LSP provides hierarchical views for navigating code structure:

    • Call Hierarchy: A tree-based view of all callers and callees of a function. It can be viewed in a side-by-side view or as a structured view of outgoing calls. Accessible via the right-click context menu or command palette.
    • Type Hierarchy: Similar to call hierarchy, but used for navigating super/subtypes or parent/child classes.
  2. Handle settings changes and workspace configuration

    main

    Since on_settings_changed and on_workspace_configuration have been removed, use these patterns instead:

    1. One-time setup at startup: Use on_initialized_async (called after the initialized notification).
    2. Dynamic workspace/configuration responses: Override on_pre_send_response_async and intercept the workspace/configuration method to mutate response['result'].
    3. Reacting to client setting changes: Intercept the workspace/didChangeConfiguration notification in on_pre_send_notification_async.
    # Dynamic configuration via on_pre_send_response_async
    def on_pre_send_response_async(self, response: ClientResponse) -> None:
        if response['method'] == 'workspace/configuration':
            for item in response['result']:
                item['myKey'] = 'myValue'
  3. Understanding Windows, Workspaces, and Folders

    main

    LSP uses concepts similar to VS Code to manage project context:

    • Window/Workspace: An LSP server instance is bound to a single Sublime Text window. Moving a file tab to a different window may start a new server instance or attach it to an existing one in that window.
    • Workspace Folders: These are the folders opened in your Sublime Text sidebar. Servers use these folders to locate project configuration files (e.g., Cargo.toml for Rust or pyproject.toml for Python).
    • Files outside Workspace: If you open a file not contained within a workspace folder, most LSP features will still work, but diagnostic messages (errors/warnings) are filtered out by default unless configured otherwise in the server settings.
  4. How LSP works as a client

    main

    LSP (Language Server Protocol) is a specification for communication between text editors (the client) and tools that provide language intelligence (the server).

    sublimelsp acts as the LSP client for Sublime Text. It does not provide language intelligence itself; instead, it interfaces with language servers which can be:

    • Standalone executables.
    • Programs requiring a runtime environment (e.g., Node.js or Python).

    You can install servers in two ways:

    1. Helper Packages: Search Package Control for packages prefixed with LSP- (e.g., LSP-pyright). These automate installation, updates, and default configurations.
    2. Manual Installation: Install the server yourself and configure it manually in the LSP.sublime-settings file.
  5. Restrict key bindings using LSP capabilities

    main

    To prevent key bindings from conflicting with other features, you can restrict them so they only trigger when a language server with a specific capability is active. Use the lsp.session_with_capability context for this purpose.

    For example, to bind Ctrl+R to lsp_document_symbols only when the current file is JavaScript/TypeScript and the server supports documentSymbolProvider, use the following configuration in your .sublime-keymap file:

    {
        "command": "lsp_document_symbols",
        "keys": [
            "ctrl+r"
        ],
        "context": [
            {
                "key": "lsp.session_with_capability",
                "operator": "equal",
                "operand": "documentSymbolProvider"
            },
            {
                "key": "selector",
                "operator": "equal",
                "operand": "source.ts, source.js"
            }
        ]
    },
  6. Format Code

    main
    Formatting can be triggered from the command palette. You can configure it to run automatically on save or on paste. Formatting behavior is often controlled by the language server's own configuration or a project-specific configuration file.
  7. Rename Symbols Semantically

    main

    Instead of using multiple cursors, you can use the language server's semantic knowledge to rename identifiers. This is available via the hover popup, context menu, or top menu bar.

    If a server supports global rename (spanning multiple files), LSP will present a modal dialog to confirm or preview the changes before applying them.

  8. Understand the Zensical project layout

    main

    The project structure follows a standard documentation layout managed by MkDocs:

    • mkdocs.yml: The primary configuration file for the documentation site.
    • docs/: The directory containing all documentation content.
      • index.md: The homepage of the documentation.
      • ...: Other markdown files, images, and assets used within the site.
  9. Configure Server Settings and Initialization Options

    main

    Language servers can be customized using two types of parameters:

    • Server Settings: Dynamic settings used to customize behavior like linting or formatting. If using an LSP-* helper package, you can edit these via Preferences > Package Settings > LSP > Servers or via the command palette.
    • Initialization Options: Static settings that are passed to the server when it starts. Unlike server settings, these cannot be changed once the server subprocess has started.
  10. Navigate Symbols (File and Project)

    main

    LSP provides enhanced symbol navigation:

    • Goto Symbol: Displays all symbols in the active file via the command palette. You can filter by symbol kind by pressing <kbd>Backspace</kbd> in the input field.
    • Goto Symbol in Project: Accesses symbols from all files in the project, with results updating dynamically as you type.

    Note: LSP does not replace the default <kbd>Ctrl</kbd> + <kbd>R</kbd> binding for the built-in Sublime Text command.

  11. Use Code Actions (Quick Fixes and Refactorings)

    main

    Code actions include "Quick Fixes" (to resolve diagnostics) and "Refactorings" (like extracting methods).

    • Quick Fixes: Shown as clickable annotations to the right of the viewport or as a lightbulb icon in the gutter.
    • Refactorings: Accessible via the right-click context menu or the Edit menu.
    • Styling: Use the markup.accent.codeaction scope to control the annotation accent color.

    Automatic Code Actions

    You can configure certain actions to run automatically via these settings:

    • lsp_code_actions_on_save: Run actions when the file is saved (e.g., sorting imports).
    • lsp_code_actions_on_format: Run actions when formatting is triggered.