multilspy

repository·main·Indexed 20 days ago

https://github.com/microsoft/multilspy

A language-agnostic LSP client library in Python designed for building applications like AI coding agents and monitors. It provides both asynchronous (LanguageServer) and synchronous (SyncLanguageServer) interfaces to perform static analysis across multiple languages, including Python, Rust, Java, Go, JavaScript, TypeScript, Ruby, C#, Dart, Kotlin, PHP, and C++. It handles server binaries, JSON-RPC communication, and standard LSP requests such as finding definitions, references, completions, and document symbols.

Tokens
18.6K
Snippets
47
Records
91
Agent score
69%

What's inside multilspy

  1. Install multilspy

    main

    It is recommended to use a virtual environment with python>=3.10. You can install multilspy using pip.

    To set up a conda environment first:

    conda create -n multilspy_env python=3.10
    conda activate multilspy_env

    Then install the package:

    pip install multilspy
  2. How to get help and file issues for multilspy

    main

    If you encounter bugs, have feature requests, or need assistance using multilspy, use GitHub Issues:

    • Bugs and Feature Requests: Search existing issues first to avoid duplicates. If no relevant issue exists, file a new Issue.
    • Questions and Help: For general questions about using the project, create a new issue and apply the question label.
  3. Perform Code Actions

    main

    Code actions represent refactorings or quick fixes.

    Request (CodeActionParams):

    • textDocument: The document identifier.
    • range: The range for which the command was invoked.
    • context: The CodeActionContext.

    Response (CodeAction):

    • title: Human-readable title.
    • kind: CodeActionKind for filtering.
    • edit: A WorkspaceEdit to perform.
    • command: A Command to execute. If both edit and command are provided, the edit is applied first.
    • isPreferred: Marks the action as a preferred choice (e.g., for auto-fix).
  4. Report Progress with WorkDoneProgress

    main

    Servers can report long-running operation progress using three types of notifications:

    1. WorkDoneProgressBegin: Starts an operation. Requires a title (e.g., "Indexing"). Can include cancellable status and percentage (0-100).
    2. WorkDoneProgressReport: Updates an ongoing operation with a new message or percentage.
    3. WorkDoneProgressEnd: Signals completion with an optional final message.
  5. Understand CompletionItem structure

    main

    A CompletionItem represents a code snippet proposed to the user during autocompletion.

    Key fields include:

    • label: The text to be inserted (or the unqualified name).
    • kind: The icon/type of the item (e.g., function, variable).
    • detail: A human-readable string (e.g., type information).
    • documentation: A doc-comment or markup content.
    • insertText: The actual text to be inserted if different from the label.
    • sortText: Used for comparing items in the completion list.
    • filterText: Used for filtering the list of items.
  6. Identify Document Symbols

    main

    Document symbols represent programming constructs (variables, classes, etc.) within a document.

    SymbolInformation:

    • name: The name of the symbol.
    • kind: The SymbolKind.
    • location: The Location of the symbol.
    • containerName: The name of the symbol containing this one (for UI purposes).

    DocumentSymbol (Hierarchical):

    • name: The name of the symbol.
    • detail: Additional info (e.g., function signature).
    • range: The range enclosing the symbol (including comments).
    • selectionRange: The range that should be selected/revealed (e.g., the identifier name).
    • children: A list of nested DocumentSymbol objects.
  7. Handle SignatureHelp requests

    main

    Signature help provides information about callable signatures.

    Request (SignatureHelpParams):

    • context: Contains textDocument and position. Requires client support for textDocument.signatureHelp.contextSupport.

    Response (SignatureHelp):

    • signatures: A list of SignatureInformation.
    • activeSignature: The index of the currently active signature.
    • activeParameter: The index of the currently active parameter within the active signature.
  8. Understand CompletionItem properties

    main

    A CompletionItem represents a single item in a completion list. Key properties include:

    • insertText: The text to be inserted. Note that some clients (like VS Code) may interpret this string (e.g., by only inserting the suffix of a match).
    • insertTextFormat: The format of the insert text (e.g., PlainText). Defaults to InsertTextFormat.PlainText.
    • insertTextMode: How whitespace and indentation are handled.
    • textEdit: A TextEdit or InsertReplaceEdit applied when the item is selected. If provided, insertText is ignored.
    • additionalTextEdits: A list of TextEdit objects applied alongside the main edit. Useful for adding imports at the top of a file.
    • commitCharacters: Characters that, when pressed, accept the completion.
    • command: An optional Command executed after insertion.
    • data: A field preserved between CompletionRequest and CompletionResolveRequest.
  9. Understand Call Hierarchy types

    main

    The LSP protocol defines several types for navigating call hierarchies, which allow users to see what calls a specific function (incoming) or what functions a specific function calls (outgoing).

    • CallHierarchyItem: Represents a symbol in the hierarchy. It includes name, kind, uri, and range. The selectionRange is the specific part of the symbol (like the function name) that should be highlighted.
    • CallHierarchyIncomingCall: Represents a caller. It contains the from item (the caller) and fromRanges (where the calls appear relative to the caller).
    • CallHierarchyOutgoingCall: Represents a callee. It contains the to item (the function being called) and fromRanges (the range at which the item is called, relative to the caller).
    • CallHierarchyRegistrationOptions: Used to register call hierarchy capabilities, allowing for documentSelector scoping and an optional id for deregistration.
  10. Understand Client Capabilities

    main

    The ClientCapabilities object defines what features the client (the application using multilspy) supports. This allows the Language Server to know which requests it can safely send.

    Major capability groups include:

    • workspace: Capabilities related to the workspace (e.g., applyEdit, workspaceFolders, configuration).
    • textDocument: Capabilities related to specific text documents (e.g., completion, hover, definition, references, formatting).
    • notebookDocument: Capabilities for notebook support (e.g., synchronization).
    • window: Window-specific capabilities (e.g., showMessage, showDocument).
    • general: General capabilities like positionEncodings and markdown support.
  11. Use CompletionList for multiple items

    main

    A CompletionList is a collection of CompletionItem objects presented to the editor.

    • isIncomplete: If true, further typing results in recomputing the list (items are replaced, not appended).
    • itemDefaults: Allows defining default values (like commitCharacters or textEdit ranges) for all items in the list to reduce payload size. This requires client support for completionList.itemDefaults.
    • items: The list of CompletionItem objects.
  12. Understand Type Hierarchy types

    main

    Type hierarchy allows navigating the relationship between types (supertypes and subtypes).

    • TypeHierarchyItem: Similar to CallHierarchyItem, it represents a type in the hierarchy with name, kind, uri, and range.
    • TypeHierarchyPrepareParams: Used to prepare the hierarchy at a specific position in a textDocument.
    • TypeHierarchySupertypesParams: Request parameters to find the supertypes of a given item.
    • TypeHierarchySubtypesParams: Request parameters to find the subtypes of a given item.
    • TypeHierarchyRegistrationOptions: Options for registering type hierarchy capabilities.