# アーキテクチャ構成図

## 全体アーキテクチャ

```mermaid
graph TB
    subgraph "LSP Clients"
        Emacs[Emacs<br/>lsp-mode]
        VSCode[VSCode<br/>Calva]
        Vim[Vim/Neovim<br/>coc.nvim]
        OtherEditor[Other Editors]
    end

    subgraph "clojure-lsp"
        subgraph "CLI Module"
            Main[main.clj<br/>Entry Point]
            Server[server.clj<br/>LSP Server]
            NREPL[nrepl.clj<br/>Dev REPL]
        end

        subgraph "Core Library"
            subgraph "Application Layer"
                Handlers[handlers.clj<br/>Request Handlers]
                Startup[startup.clj<br/>Initialization]
                InternalAPI[internal-api.clj<br/>Internal API]
                PublicAPI[api.clj<br/>Public API]
            end

            subgraph "Feature Layer"
                Completion[completion.clj]
                Hover[hover.clj]
                Rename[rename.clj]
                CodeActions[code-actions.clj]
                Diagnostics[diagnostics.clj]
                Format[format.clj]
                CallHierarchy[call-hierarchy.clj]
                SemanticTokens[semantic-tokens.clj]
            end

            subgraph "Refactoring"
                Transform[transform.clj]
                Edit[edit.clj]
            end

            subgraph "Infrastructure Layer"
                DB[(db.clj<br/>State Management)]
                Queries[queries.clj<br/>Query Engine]
                Kondo[kondo.clj<br/>clj-kondo Integration]
                DepGraph[dep-graph.clj<br/>Dependency Graph]
                Classpath[classpath.clj<br/>Classpath Analysis]
            end

            subgraph "Cross-Cutting"
                Logger[logger.clj<br/>ILogger Protocol]
                Producer[producer.clj<br/>IProducer Protocol]
                Settings[settings.clj<br/>Configuration]
                Shared[shared.clj<br/>Utilities]
            end
        end
    end

    subgraph "External Dependencies"
        CljKondo[clj-kondo<br/>Static Analysis]
        Cljfmt[cljfmt<br/>Formatting]
        CljDepend[clj-depend<br/>Dependency Analysis]
        RewriteClj[rewrite-clj<br/>Code Manipulation]
        LSP4Clj[lsp4clj<br/>LSP Protocol]
    end

    subgraph "File System"
        SourceFiles[Source Files<br/>.clj, .cljs, .cljc]
        Cache[Cache Files<br/>.lsp/.cache/]
        Config[Config Files<br/>.clj-kondo/, .lsp/]
    end

    %% Client to Server connections
    Emacs -.->|JSON-RPC| Server
    VSCode -.->|JSON-RPC| Server
    Vim -.->|JSON-RPC| Server
    OtherEditor -.->|JSON-RPC| Server

    %% CLI Module connections
    Main --> Server
    Server --> Handlers
    Server --> NREPL

    %% Application Layer connections
    Handlers --> Completion
    Handlers --> Hover
    Handlers --> Rename
    Handlers --> CodeActions
    Handlers --> Diagnostics
    Handlers --> Format
    Handlers --> Startup
    Startup --> Kondo
    Startup --> Classpath
    InternalAPI --> Handlers
    PublicAPI --> InternalAPI

    %% Feature to Infrastructure
    Completion --> Queries
    Hover --> Queries
    Rename --> Queries
    Rename --> Transform
    CodeActions --> Transform
    Diagnostics --> Kondo
    Format --> Cljfmt

    %% Refactoring
    Transform --> Edit
    Transform --> Queries

    %% Infrastructure connections
    Queries --> DepGraph
    Queries --> DB
    Kondo --> DepGraph
    Kondo --> CljKondo
    Classpath --> Config

    %% Cross-cutting
    Handlers --> Producer
    Server --> Logger
    Startup --> Settings
    Kondo --> Settings

    %% External Dependencies
    Kondo --> CljKondo
    DepGraph --> DB
    Transform --> RewriteClj
    Server --> LSP4Clj

    %% File System
    Kondo --> SourceFiles
    DB --> Cache
    Settings --> Config
```

## レイヤー構成図

```mermaid
graph TB
    subgraph "Presentation Layer"
        CLI[CLI / LSP Server]
        Pod[Babashka Pod]
    end

    subgraph "Application Layer"
        Handlers[Request Handlers]
        API[Public/Internal API]
        Startup[Initialization]
    end

    subgraph "Domain Layer"
        Features[Feature Modules]
        Refactor[Refactoring]
    end

    subgraph "Infrastructure Layer"
        DB[Database / State]
        Queries[Query Engine]
        External[External Integrations]
    end

    CLI --> Handlers
    Pod --> API
    Handlers --> Features
    API --> Features
    Startup --> External
    Features --> Refactor
    Features --> Queries
    Refactor --> Queries
    Queries --> DB
    External --> DB

    style CLI fill:#e1f5fe
    style Pod fill:#e1f5fe
    style Handlers fill:#fff3e0
    style API fill:#fff3e0
    style Startup fill:#fff3e0
    style Features fill:#e8f5e9
    style Refactor fill:#e8f5e9
    style DB fill:#fce4ec
    style Queries fill:#fce4ec
    style External fill:#fce4ec
```

## コンポーネント依存関係

```mermaid
graph LR
    subgraph "CLI"
        main[main.clj]
        server[server.clj]
        nrepl[nrepl.clj]
        coercer[clojure-coercer.clj]
    end

    subgraph "Handlers"
        handlers[handlers.clj]
        internal_api[internal-api.clj]
        api[api.clj]
    end

    subgraph "Features"
        completion[completion]
        hover[hover]
        rename[rename]
        code_actions[code-actions]
        diagnostics[diagnostics]
        format[format]
        file_mgmt[file-management]
    end

    subgraph "Infrastructure"
        db[db.clj]
        queries[queries.clj]
        kondo[kondo.clj]
        dep_graph[dep-graph.clj]
        startup[startup.clj]
    end

    subgraph "Cross-Cutting"
        producer[producer.clj]
        logger[logger.clj]
        settings[settings.clj]
        shared[shared.clj]
    end

    main --> server
    main --> internal_api
    server --> handlers
    server --> producer
    server --> logger

    handlers --> completion
    handlers --> hover
    handlers --> rename
    handlers --> code_actions
    handlers --> diagnostics
    handlers --> format
    handlers --> file_mgmt
    handlers --> queries
    handlers --> startup

    api --> internal_api
    internal_api --> handlers

    completion --> queries
    hover --> queries
    rename --> queries
    code_actions --> queries
    diagnostics --> kondo
    file_mgmt --> kondo

    queries --> dep_graph
    queries --> db
    kondo --> dep_graph
    kondo --> db
    dep_graph --> db
    startup --> kondo
    startup --> db

    settings --> shared
    producer --> shared
    logger --> shared
```

## データフロー図

```mermaid
sequenceDiagram
    participant Client as LSP Client
    participant Server as server.clj
    participant Handler as handlers.clj
    participant Feature as feature/*
    participant Queries as queries.clj
    participant DB as db.clj (Atom)
    participant Kondo as clj-kondo

    Note over Client,Kondo: Request Flow (e.g., textDocument/definition)

    Client->>Server: JSON-RPC Request
    Server->>Server: normalize-doc-uri
    Server->>Handler: handler/definition
    Handler->>Queries: find-definition-from-cursor
    Queries->>DB: get analysis data
    DB-->>Queries: analysis map
    Queries-->>Handler: definition element
    Handler-->>Server: location response
    Server->>Server: conform-or-log
    Server-->>Client: JSON-RPC Response

    Note over Client,Kondo: Document Change Flow

    Client->>Server: textDocument/didChange
    Server->>Handler: handler/did-change
    Handler->>Handler: current-changes-chan
    Note right of Handler: debounce 50ms
    Handler->>Kondo: run-kondo-on-text!
    Kondo->>Kondo: analyze
    Kondo-->>Handler: analysis result
    Handler->>DB: swap! update analysis
    Handler->>Handler: diagnostics-chan
    Note right of Handler: debounce 25ms
    Handler->>Server: publish-diagnostic
    Server-->>Client: textDocument/publishDiagnostics
```

## 状態管理図

```mermaid
graph TB
    subgraph "Runtime State (Atom)"
        DB[(db*)]

        subgraph "DB Contents"
            Analysis[analysis<br/>URI -> Buckets]
            Documents[documents<br/>URI -> Doc Info]
            DepGraphState[dep-graph<br/>NS -> Dependencies]
            DiagnosticsState[diagnostics<br/>Source -> URI -> Findings]
            Settings[settings<br/>Configuration]
            Classpath[classpath<br/>JAR Paths]
        end
    end

    subgraph "Persistent Cache"
        TransitCache[.lsp/.cache/db.transit.json]
        KondoCache[.clj-kondo/.cache/]
    end

    subgraph "Analysis Buckets"
        VarDefs[var-definitions]
        VarUsages[var-usages]
        NSDefs[namespace-definitions]
        NSUsages[namespace-usages]
        Locals[locals]
        LocalUsages[local-usages]
        Keywords[keyword-definitions/usages]
        JavaClasses[java-class-definitions/usages]
    end

    DB --> Analysis
    DB --> Documents
    DB --> DepGraphState
    DB --> DiagnosticsState
    DB --> Settings
    DB --> Classpath

    Analysis --> VarDefs
    Analysis --> VarUsages
    Analysis --> NSDefs
    Analysis --> NSUsages
    Analysis --> Locals
    Analysis --> LocalUsages
    Analysis --> Keywords
    Analysis --> JavaClasses

    DB -.->|upsert| TransitCache
    TransitCache -.->|load| DB
    KondoCache -.->|read| Analysis
```

## 非同期処理フロー

```mermaid
graph LR
    subgraph "Channels"
        ChangeCh[current-changes-chan]
        DiagCh[diagnostics-chan]
        WatchedCh[watched-files-chan]
        EditsCh[edits-chan]
    end

    subgraph "Debounce"
        ChangeDebounce[50ms debounce]
        DiagDebounce[25ms debounce]
        WatchedDebounce[1000ms debounce]
    end

    subgraph "Handlers"
        AnalyzeChanges[analyze-changes]
        PublishDiag[publish-diagnostic]
        AnalyzeWatched[analyze-watched-files!]
        PublishEdit[publish-workspace-edit]
    end

    ChangeCh --> ChangeDebounce
    DiagCh --> DiagDebounce
    WatchedCh --> WatchedDebounce

    ChangeDebounce --> AnalyzeChanges
    DiagDebounce --> PublishDiag
    WatchedDebounce --> AnalyzeWatched
    EditsCh --> PublishEdit

    AnalyzeChanges --> DiagCh
```
