# ocaml/ocaml-lsp architecture

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

- Interactive diagram: https://gitdiagram.com/ocaml/ocaml-lsp
- Repository: https://github.com/ocaml/ocaml-lsp

## Overview

a. Client Communication Flow:
- Editor/Client <---> LSP Protocol Handler <---> Request/Response Manager

b. Document Processing Flow:
- Document Store ---> Language Processing Components ---> Feature Modules

c. Build System Integration:
- Dune RPC <---> Workspace Manager <---> Diagnostics Engine

a. Code Intelligence:
- Type Inference
- Code Completion
- Symbol Resolution
- Reference Finding

b. Document Management:
- File Tracking
- Buffer Management
- Change Synchronization
- Version Control

- OCamlFormat
- Merlin
- Dune Build System
- Fiber Runtime

##### Specific architectural patterns

a. Component Layering:
- LSP Protocol Layer
- ↓
- Core Services Layer
- ↓
- Language Processing Layer
- ↓
- Tool Integration Layer

b. Request Processing Pipeline:
- Request Reception → Validation → Processing → Response Formation → Delivery

##### Special Features (highlight with distinct markers)
- Typed Holes Support
- Destructuring Generation
- Value Construction
- Semantic Highlighting
- Custom LSP Extensions

##### The configuration management system

Keep in mind:
- Maintain clear boundaries between components
- Both synchronous and asynchronous communication paths
- Where extensibility points exist
- Critical paths for main features
- Error handling flows
- Configuration impact on components

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef core fill:#b3d1ff,stroke:#333
    classDef lang fill:#b3ffb3,stroke:#333
    classDef feature fill:#fff2b3,stroke:#333
    classDef infra fill:#e6e6e6,stroke:#333
    classDef external fill:none,stroke:#666,stroke-dasharray: 5 5
    
    %% Client/Editor
    Client["Editor/Client"]

    %% Core LSP Components
    subgraph CoreLSP["Core LSP Layer"]
        Protocol["LSP Protocol Handler"]:::core
        ReqResp["Request/Response Manager"]:::core
        DocStore["Document Store"]:::core
        Workspace["Workspace Manager"]:::core
    end

    %% Language Processing
    subgraph LangProc["Language Processing Layer"]
        MerlinInt["Merlin Integration"]:::lang
        TypeInf["Type Inference Engine"]:::lang
        CodeAnal["Code Analysis Engine"]:::lang
        DocSymbol["Document Symbol Provider"]:::lang
    end

    %% Feature Modules
    subgraph Features["Feature Modules Layer"]
        Completion["Completion Provider"]:::feature
        CodeActions["Code Actions Provider"]:::feature
        Diagnostics["Diagnostics Engine"]:::feature
        Semantic["Semantic Highlighting"]:::feature
        Formatting["Formatting Service"]:::feature
    end

    %% Infrastructure
    subgraph Infra["Infrastructure Layer"]
        Fiber["Fiber Concurrency"]:::infra
        Config["Configuration Manager"]:::infra
        FileSystem["File System Interface"]:::infra
        DuneRPC["Dune RPC Integration"]:::infra
    end

    %% External Dependencies
    subgraph External["External Tools"]
        OCamlFormat["OCamlFormat"]:::external
        Merlin["Merlin"]:::external
        Dune["Dune Build System"]:::external
    end

    %% Custom Features
    subgraph Custom["Custom Features"]
        TypedHoles["Typed Holes Support"]:::feature
        LSPExt["Custom LSP Extensions"]:::feature
    end

    %% Connections
    Client <--> Protocol
    Protocol <--> ReqResp
    ReqResp --> DocStore
    DocStore --> Workspace
    
    DocStore --> MerlinInt
    MerlinInt --> TypeInf
    TypeInf --> CodeAnal
    CodeAnal --> DocSymbol
    
    DocStore --> Features
    MerlinInt --> Features
    
    DuneRPC <--> Workspace
    DuneRPC <--> Diagnostics
    
    Formatting --> OCamlFormat
    MerlinInt --> Merlin
    DuneRPC --> Dune

    %% Click events
    click Protocol "https://github.com/ocaml/ocaml-lsp/blob/master/jsonrpc-fiber/src/jsonrpc_fiber.ml"
    click ReqResp "https://github.com/ocaml/ocaml-lsp/blob/master/lsp/src/server_request.ml"
    click DocStore "https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/src/document_store.ml"
    click Workspace "https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/src/workspaces.ml"
    click DocSymbol "https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/src/document_symbol.ml"
    click TypeInf "https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/src/inference.ml"
    click Completion "https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/src/compl.ml"
    click CodeActions "https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/src/code_actions/code_action.ml"
    click Diagnostics "https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/src/diagnostics.ml"
    click Semantic "https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/src/semantic_highlighting.ml"
    click Formatting "https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/src/ocamlformat.ml"
    click Fiber "https://github.com/ocaml/ocaml-lsp/blob/master/lsp-fiber/src/fiber_io.ml"
    click Config "https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/src/configuration.ml"
    click FileSystem "https://github.com/ocaml/ocaml-lsp/blob/master/lsp/src/io.ml"
    click DuneRPC "https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/src/dune.ml"
    click TypedHoles "https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/src/typed_hole.ml"
    click LSPExt "https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/src/custom_requests/custom_request.ml"
```
