# swiftlang/sourcekit-lsp architecture

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

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

## Overview

##### Main Components

a. Core Components:
- SourceKit-LSP Server
- sourcekitd Integration
- clangd Integration
- Build System Integration
- Language Services Layer
- Protocol Layer
- Index Management

b. External Components:
- Editor Clients
- Swift Package Manager
- Build Systems (CMake, etc.)
- Source Code Files

##### Component Relationships

**Editor Layer**

**Protocol Layer**

**Service Layer**

**Core Layer**

**Build/Index Layer**

##### Detailed Component Breakdown

a. Protocol Layer (LanguageServerProtocol folder):
- LSP message handling
- JSON-RPC communication
- Connection management

b. Service Layer:
- Swift Language Service
- Clang Language Service
- Documentation Service
- Semantic Index Service

c. Core Components:
- SourceKitD Integration
- Build System Integration
- Completion Scoring
- Diagnostic Management

d. Build/Index Layer:
- Build System Manager
- Index Store Integration
- Compilation Database Handler

##### Key Interactions to Highlight

- Editor ←→ LSP Server communication
- Language Service ←→ SourceKitD/Clangd interaction
- Build System ←→ Index Store communication
- Cross-language support paths

##### Implementation Details

- Message flow for main operations (completion, navigation, etc.)
- Component initialization sequence
- Error handling paths
- Configuration flow

##### Specific Features to Highlight

- Code completion pipeline
- Jump-to-definition paths
- Index query flow
- Build system integration points
- Documentation generation

##### Notes

- Version compatibility indicators
- Experimental features
- Extension points
- Configuration dependencies

- Nested components
- Grouping
- Layer separation

Remember to maintain a balance between showing the complexity of the system while keeping it understandable. Focus on the main architectural components while providing enough detail to understand the system's operation.

This design should reflect the modular nature of SourceKit-LSP, its integration with multiple build systems, and its support for both Swift and C-based languages, while showing how it maintains high-fidelity language support through sourcekitd and clangd.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Editor
        EC[Editor Clients]
    end

    subgraph Protocol
        LSP["Language Server Protocol"]:::protocol
        JSONRPC["JSON-RPC Communication"]:::protocol
        click JSONRPC "https://github.com/swiftlang/sourcekit-lsp/tree/main/Sources/LanguageServerProtocolJSONRPC"
        click LSP "https://github.com/swiftlang/sourcekit-lsp/tree/main/Sources/LanguageServerProtocol"
    end

    subgraph Server
        SKLSP["SourceKit-LSP Server"]:::server
        WM["Workspace Management"]:::server
        DM["Document Management"]:::server
        click SKLSP "https://github.com/swiftlang/sourcekit-lsp/blob/main/Sources/SourceKitLSP/SourceKitLSPServer.swift"
        click WM "https://github.com/swiftlang/sourcekit-lsp/blob/main/Sources/SourceKitLSP/Workspace.swift"
        click DM "https://github.com/swiftlang/sourcekit-lsp/blob/main/Sources/SourceKitLSP/DocumentManager.swift"
    end

    subgraph "Language Services"
        SLS["Swift Language Service"]:::service
        CLS["Clang Language Service"]:::service
        DS["Documentation Service"]:::service
        CS["Completion Scoring"]:::service
        click SLS "https://github.com/swiftlang/sourcekit-lsp/tree/main/Sources/SourceKitLSP/Swift"
        click CLS "https://github.com/swiftlang/sourcekit-lsp/tree/main/Sources/SourceKitLSP/Clang"
        click DS "https://github.com/swiftlang/sourcekit-lsp/tree/main/Sources/SourceKitLSP/Documentation"
        click CS "https://github.com/swiftlang/sourcekit-lsp/tree/main/Sources/CompletionScoring"
    end

    subgraph "Core Integration"
        SKD["sourcekitd Integration"]:::core
        CLANGD["clangd Integration"]:::core
        click SKD "https://github.com/swiftlang/sourcekit-lsp/tree/main/Sources/SourceKitD"
        click CLANGD "https://github.com/swiftlang/sourcekit-lsp/tree/main/Sources/SourceKitLSP/Clang"
    end

    subgraph "Build and Index"
        BSM["Build System Manager"]:::build
        BSI["Build System Integration"]:::build
        BSS["Build Settings Management"]:::build
        SIS["Semantic Index Service"]:::index
        IM["Index Management"]:::index
        click BSM "https://github.com/swiftlang/sourcekit-lsp/blob/main/Sources/BuildSystemIntegration/BuildSystemManager.swift"
        click BSI "https://github.com/swiftlang/sourcekit-lsp/tree/main/Sources/BuildSystemIntegration"
        click BSS "https://github.com/swiftlang/sourcekit-lsp/blob/main/Sources/BuildSystemIntegration/FileBuildSettings.swift"
        click SIS "https://github.com/swiftlang/sourcekit-lsp/blob/main/Sources/SemanticIndex/SemanticIndexManager.swift"
        click IM "https://github.com/swiftlang/sourcekit-lsp/tree/main/Sources/SemanticIndex"
    end

    EC <--> LSP
    LSP <--> JSONRPC
    JSONRPC <--> SKLSP
    
    SKLSP --> WM
    SKLSP --> DM
    
    SKLSP --> SLS
    SKLSP --> CLS
    SKLSP --> DS
    
    SLS & CLS --> CS
    
    SLS --> SKD
    CLS --> CLANGD
    
    SLS & CLS --> BSM
    BSM --> BSI
    BSM --> BSS
    
    BSI --> SIS
    SIS --> IM

    classDef default fill:#f9f9f9,stroke:#333,stroke-width:1px
    classDef protocol fill:#e1f5fe,stroke:#0288d1,stroke-width:2px
    classDef server fill:#e8f5e9,stroke:#388e3c,stroke-width:2px
    classDef service fill:#fff3e0,stroke:#f57c00,stroke-width:2px
    classDef core fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px
    classDef build fill:#fbe9e7,stroke:#d84315,stroke-width:2px
    classDef index fill:#fff8e1,stroke:#ffa000,stroke-width:2px
```
