# openlawlibrary/pygls architecture

> Architecture diagram of the GitHub repository openlawlibrary/pygls, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openlawlibrary/pygls
- Repository: https://github.com/openlawlibrary/pygls

## Overview

##### Main Components

a) Core Framework Components:
- Language Server Base (pygls/lsp/)
- Protocol Handler (pygls/protocol/)
- Workspace Manager (pygls/workspace/)
- Feature Manager (pygls/feature_manager.py)
- I/O Handler (pygls/io_.py)

b) External Interfaces:
- LSP Client (Editor/IDE)
- JSON-RPC Communication Layer
- File System Interface

##### Architectural Patterns to Highlight

- Event-driven architecture for LSP requests/notifications
- Plugin-based feature system (using decorators)
- Asynchronous I/O handling
- Text document synchronization

**1. Client Layer (External Editors/IDEs)**
- ↕ [JSON-RPC over stdin/stdout or TCP]
**2. Server Interface Layer**
- Protocol Handler
- JSON-RPC Handler
**3. Core Processing Layer**
- Language Server Base
- Feature Manager
- Progress Handler
**4. Workspace Layer**
- Document Manager
- Position Codec
- Text Synchronization
**5. File System Layer**

##### Key Relationships

a) Control Flow:
- Client → Protocol Handler → Feature Manager → Feature Implementation
- Workspace Manager ↔ File System
- Server Base → I/O Handler → Client

b) Data Flow:
- Document changes → Workspace Manager → Feature Implementation
- Feature Results → Protocol Handler → Client
- Configuration → Feature Manager

##### Implementation Details

- Feature registration system (@server.feature decorator)
- LSP message handling pipeline
- Document synchronization mechanism
- Capability negotiation flow

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        IDE["Editor/IDE (LSP Client)"]
        click IDE "https://github.com/openlawlibrary/pygls/blob/main/pygls/lsp/_base_client.py"
    end

    subgraph ServerInterface
        PH["Protocol Handler"]:::core
        click PH "https://github.com/openlawlibrary/pygls/tree/main/pygls/protocol/"
        JRPC["JSON-RPC Handler"]:::core
        click JRPC "https://github.com/openlawlibrary/pygls/blob/main/pygls/protocol/json_rpc.py"
        LSP["LSP Protocol Handler"]:::core
        click LSP "https://github.com/openlawlibrary/pygls/blob/main/pygls/protocol/language_server.py"
    end

    subgraph CoreProcessing
        SB["Server Base"]:::core
        click SB "https://github.com/openlawlibrary/pygls/blob/main/pygls/lsp/_base_server.py"
        FM["Feature Manager"]:::core
        click FM "https://github.com/openlawlibrary/pygls/blob/main/pygls/feature_manager.py"
        PG["Progress Handler"]:::core
        click PG "https://github.com/openlawlibrary/pygls/blob/main/pygls/progress.py"
        CH["Capabilities Handler"]:::core
        click CH "https://github.com/openlawlibrary/pygls/blob/main/pygls/capabilities.py"
        SI["Server Implementation"]:::core
        click SI "https://github.com/openlawlibrary/pygls/blob/main/pygls/server.py"
    end

    subgraph WorkspaceLayer
        WM["Workspace Manager"]:::core
        click WM "https://github.com/openlawlibrary/pygls/blob/main/pygls/workspace/workspace.py"
        DM["Document Manager"]:::core
        click DM "https://github.com/openlawlibrary/pygls/blob/main/pygls/workspace/text_document.py"
        PC["Position Codec"]:::core
        click PC "https://github.com/openlawlibrary/pygls/blob/main/pygls/workspace/position_codec.py"
        URI["URI Handler"]:::core
        click URI "https://github.com/openlawlibrary/pygls/blob/main/pygls/uris.py"
    end

    subgraph FileSystem
        FS["File System Interface"]:::external
    end

    %% Connections
    IDE <--> |JSON-RPC| PH
    PH <--> JRPC
    PH <--> LSP
    LSP <--> SB
    SB <--> FM
    FM <--> PG
    FM <--> CH
    FM <--> SI
    SB <--> WM
    WM <--> DM
    WM <--> PC
    WM <--> URI
    WM <--> FS

    %% Styles
    classDef core fill:#a8d1f0,stroke:#333,stroke-width:2px
    classDef external fill:#f9d6d6,stroke:#333,stroke-width:2px
    classDef optional fill:#f5f5f5,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5

    %% Legend
    subgraph Legend
        C1["Core Component"]:::core
        E1["External Interface"]:::external
        O1["Optional Component"]:::optional
    end
```
