# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "External Systems"
        IDE["IDE Client<br/>(VS Code, NeoVim)"]
        FS["File System"]
    end

    subgraph "lua-language-server"
        subgraph "Protocol Layer"
            PROTO["proto/proto.lua<br/>JSON-RPC Handler"]
            JSONRPC["jsonrpc.lua<br/>Protocol Encoding"]
            SERVICE["service/service.lua<br/>Event Loop"]
            NET["service/net.lua<br/>Network Handler"]
        end

        subgraph "Provider Layer"
            PROVIDER["provider/provider.lua<br/>LSP Method Registry"]
            CLIENT["client.lua<br/>Client Manager"]
            CAP["provider/capability.lua<br/>Capability Manager"]
        end

        subgraph "Core Layer"
            COMPLETION["core/completion/<br/>Code Completion"]
            DEFINITION["core/definition.lua<br/>Go to Definition"]
            REFERENCE["core/reference.lua<br/>Find References"]
            HOVER["core/hover/<br/>Hover Information"]
            DIAGNOSTICS["core/diagnostics/<br/>Diagnostics (65+)"]
            RENAME["core/rename.lua<br/>Symbol Rename"]
            SEMANTIC["core/semantic-tokens.lua<br/>Semantic Tokens"]
        end

        subgraph "VM Layer"
            COMPILER["vm/compiler.lua<br/>AST Compiler"]
            INFER["vm/infer.lua<br/>Type Inference"]
            NODE["vm/node.lua<br/>Node Manager"]
            TYPE["vm/type.lua<br/>Type System"]
            GLOBAL["vm/global.lua<br/>Global Index"]
        end

        subgraph "Parser Layer"
            COMPILE["parser/compile.lua<br/>Lua Parser"]
            GUIDE["parser/guide.lua<br/>AST Guide"]
            LUADOC["parser/luadoc.lua<br/>LuaDoc Parser"]
        end

        subgraph "Infrastructure Layer"
            FILES["files.lua<br/>File Manager"]
            WORKSPACE["workspace/workspace.lua<br/>Workspace Manager"]
            CONFIG["config/config.lua<br/>Configuration"]
            SCOPE["workspace/scope.lua<br/>Scope Manager"]
        end

        subgraph "Foundation Layer"
            AWAIT["await.lua<br/>Async/Await"]
            PUB["pub/pub.lua<br/>Publisher"]
            BRAVE["brave/<br/>Worker Threads"]
            LOG["log.lua<br/>Logger"]
            UTIL["utility.lua<br/>Utilities"]
            TIMER["timer.lua<br/>Timer"]
        end
    end

    IDE <-->|"LSP (stdio/socket)"| PROTO
    FS <-->|"bee.filesystem"| FILES

    PROTO --> JSONRPC
    SERVICE --> PROTO
    SERVICE --> NET
    NET --> PROTO

    PROTO --> PROVIDER
    PROVIDER --> CLIENT
    PROVIDER --> CAP

    PROVIDER --> COMPLETION
    PROVIDER --> DEFINITION
    PROVIDER --> REFERENCE
    PROVIDER --> HOVER
    PROVIDER --> DIAGNOSTICS
    PROVIDER --> RENAME
    PROVIDER --> SEMANTIC

    COMPLETION --> COMPILER
    DEFINITION --> COMPILER
    REFERENCE --> COMPILER
    HOVER --> COMPILER
    DIAGNOSTICS --> COMPILER

    COMPILER --> INFER
    COMPILER --> NODE
    COMPILER --> TYPE
    COMPILER --> GLOBAL

    INFER --> COMPILE
    NODE --> GUIDE
    TYPE --> LUADOC

    FILES --> SCOPE
    WORKSPACE --> FILES
    WORKSPACE --> SCOPE
    CONFIG --> WORKSPACE

    AWAIT --> TIMER
    PUB --> BRAVE
    SERVICE --> PUB
    SERVICE --> AWAIT
```

## レイヤー構成図

```mermaid
graph TB
    subgraph "Layer Architecture"
        L1["Protocol Layer<br/>LSP/JSON-RPC Communication"]
        L2["Provider Layer<br/>LSP Method Registration"]
        L3["Core Layer<br/>Language Features"]
        L4["VM Layer<br/>Type Inference Engine"]
        L5["Parser Layer<br/>Lua Source Parsing"]
        L6["Infrastructure Layer<br/>File & Workspace Management"]
        L7["Foundation Layer<br/>Utilities & Threading"]
    end

    L1 --> L2
    L2 --> L3
    L3 --> L4
    L4 --> L5
    L3 --> L6
    L4 --> L6
    L5 --> L6
    L1 --> L7
    L2 --> L7
    L3 --> L7
    L4 --> L7
    L5 --> L7
    L6 --> L7

    style L1 fill:#e1f5fe
    style L2 fill:#b3e5fc
    style L3 fill:#81d4fa
    style L4 fill:#4fc3f7
    style L5 fill:#29b6f6
    style L6 fill:#03a9f4
    style L7 fill:#0288d1
```

## データフロー図

```mermaid
sequenceDiagram
    participant Client as IDE Client
    participant Proto as proto/proto.lua
    participant Provider as provider/provider.lua
    participant Core as core/*
    participant VM as vm/*
    participant Parser as parser/*
    participant Files as files.lua

    Client->>Proto: LSP Request (JSON-RPC)
    Proto->>Proto: doMethod (queue request)
    Proto->>Provider: applyMethod (execute handler)
    Provider->>Core: Call language feature
    Core->>VM: Request type info
    VM->>Parser: Get AST
    Parser->>Files: Get file content
    Files-->>Parser: Return content
    Parser-->>VM: Return AST
    VM-->>Core: Return type info
    Core-->>Provider: Return result
    Provider-->>Proto: Return response
    Proto-->>Client: LSP Response (JSON-RPC)
```

## 並行処理アーキテクチャ図

```mermaid
graph LR
    subgraph "Main Thread"
        SERVICE["service.lua<br/>Event Loop"]
        AWAIT["await.lua<br/>Coroutine Manager"]
        PUB["pub.lua<br/>Publisher"]
    end

    subgraph "Worker Threads (Braves)"
        BRAVE1["Brave #1"]
        BRAVE2["Brave #2"]
        BRAVE3["Brave #3"]
        BRAVE4["Brave #4"]
    end

    subgraph "Channels"
        TASK1["Task Channel #1"]
        TASK2["Task Channel #2"]
        TASK3["Task Channel #3"]
        TASK4["Task Channel #4"]
        REPLY1["Reply Channel #1"]
        REPLY2["Reply Channel #2"]
        REPLY3["Reply Channel #3"]
        REPLY4["Reply Channel #4"]
    end

    PUB -->|"pushTask"| TASK1
    PUB -->|"pushTask"| TASK2
    PUB -->|"pushTask"| TASK3
    PUB -->|"pushTask"| TASK4

    TASK1 --> BRAVE1
    TASK2 --> BRAVE2
    TASK3 --> BRAVE3
    TASK4 --> BRAVE4

    BRAVE1 -->|"result"| REPLY1
    BRAVE2 -->|"result"| REPLY2
    BRAVE3 -->|"result"| REPLY3
    BRAVE4 -->|"result"| REPLY4

    REPLY1 -->|"popTask"| PUB
    REPLY2 -->|"popTask"| PUB
    REPLY3 -->|"popTask"| PUB
    REPLY4 -->|"popTask"| PUB

    SERVICE --> AWAIT
    SERVICE --> PUB
    AWAIT -->|"coroutine"| SERVICE
```

## モジュール依存関係図

```mermaid
graph TD
    subgraph "Entry Point"
        MAIN["main.lua"]
    end

    subgraph "Service"
        SERVICE["service/service.lua"]
        NET["service/net.lua"]
    end

    subgraph "Protocol"
        PROTO["proto/proto.lua"]
        DEFINE["proto/define.lua"]
        CONVERTER["proto/converter.lua"]
    end

    subgraph "Provider"
        PROVIDER["provider/provider.lua"]
        CAPABILITY["provider/capability.lua"]
        DIAGNOSTIC_P["provider/diagnostic.lua"]
    end

    subgraph "Core Features"
        COMPLETION["core/completion/"]
        DEFINITION["core/definition.lua"]
        REFERENCE["core/reference.lua"]
        HOVER["core/hover/"]
        DIAGNOSTIC_C["core/diagnostics/"]
        RENAME["core/rename.lua"]
    end

    subgraph "VM"
        VM_INIT["vm/init.lua"]
        COMPILER["vm/compiler.lua"]
        INFER["vm/infer.lua"]
        NODE["vm/node.lua"]
    end

    subgraph "Parser"
        PARSER_INIT["parser/init.lua"]
        COMPILE["parser/compile.lua"]
        GUIDE["parser/guide.lua"]
    end

    subgraph "Workspace"
        WORKSPACE["workspace/workspace.lua"]
        SCOPE["workspace/scope.lua"]
        LOADING["workspace/loading.lua"]
    end

    subgraph "Infrastructure"
        FILES["files.lua"]
        CONFIG["config/config.lua"]
        CLIENT["client.lua"]
    end

    subgraph "Foundation"
        AWAIT["await.lua"]
        PUB["pub/pub.lua"]
        LOG["log.lua"]
        UTIL["utility.lua"]
    end

    MAIN --> SERVICE
    SERVICE --> NET
    SERVICE --> PROTO
    SERVICE --> PUB
    SERVICE --> PROVIDER

    PROTO --> DEFINE
    PROTO --> CONVERTER

    PROVIDER --> CAPABILITY
    PROVIDER --> DIAGNOSTIC_P
    PROVIDER --> COMPLETION
    PROVIDER --> DEFINITION
    PROVIDER --> REFERENCE
    PROVIDER --> HOVER
    PROVIDER --> DIAGNOSTIC_C
    PROVIDER --> RENAME

    COMPLETION --> VM_INIT
    DEFINITION --> VM_INIT
    REFERENCE --> VM_INIT
    HOVER --> VM_INIT
    DIAGNOSTIC_C --> VM_INIT

    VM_INIT --> COMPILER
    COMPILER --> INFER
    COMPILER --> NODE
    COMPILER --> PARSER_INIT

    PARSER_INIT --> COMPILE
    PARSER_INIT --> GUIDE

    WORKSPACE --> SCOPE
    WORKSPACE --> LOADING
    WORKSPACE --> FILES

    FILES --> CONFIG
    PROVIDER --> CLIENT

    SERVICE --> AWAIT
    SERVICE --> LOG
    PUB --> UTIL
```

## コンポーネント配置図

```mermaid
graph TB
    subgraph "script/ Directory"
        subgraph "Top Level Modules"
            MAIN["main.lua"]
            FILES["files.lua"]
            CLIENT["client.lua"]
            AWAIT["await.lua"]
            LOG["log.lua"]
            UTILITY["utility.lua"]
        end

        subgraph "service/"
            SERVICE_S["service.lua"]
            NET_S["net.lua"]
        end

        subgraph "proto/"
            PROTO_P["proto.lua"]
            DEFINE_P["define.lua"]
            CONVERTER_P["converter.lua"]
        end

        subgraph "provider/"
            PROVIDER_V["provider.lua"]
            CAPABILITY_V["capability.lua"]
            DIAGNOSTIC_V["diagnostic.lua"]
        end

        subgraph "core/"
            subgraph "completion/"
                COMP_INIT["init.lua"]
            end
            subgraph "diagnostics/"
                DIAG_INIT["init.lua"]
                DIAG_RULES["65+ diagnostic rules"]
            end
            subgraph "hover/"
                HOVER_INIT["init.lua"]
            end
            DEFINITION_C["definition.lua"]
            REFERENCE_C["reference.lua"]
            RENAME_C["rename.lua"]
        end

        subgraph "vm/"
            VM_INIT["init.lua"]
            COMPILER_V["compiler.lua"]
            INFER_V["infer.lua"]
            NODE_V["node.lua"]
            TYPE_V["type.lua"]
        end

        subgraph "parser/"
            PARSER_INIT["init.lua"]
            COMPILE_P["compile.lua"]
            GUIDE_P["guide.lua"]
            LUADOC_P["luadoc.lua"]
        end

        subgraph "workspace/"
            WORKSPACE_W["workspace.lua"]
            SCOPE_W["scope.lua"]
            LOADING_W["loading.lua"]
        end

        subgraph "config/"
            CONFIG_C["config.lua"]
            LOADER_C["loader.lua"]
            TEMPLATE_C["template.lua"]
        end

        subgraph "pub/"
            PUB_P["pub.lua"]
            REPORT_P["report.lua"]
        end

        subgraph "brave/"
            BRAVE_B["brave.lua"]
            LOG_B["log.lua"]
        end
    end
```

## LSP機能一覧図

```mermaid
mindmap
  root((lua-language-server))
    Language Features
      Go to Definition
      Find References
      Hover Information
      Code Completion
      Signature Help
      Document Symbol
      Workspace Symbol
      Rename
      Code Action
      Code Lens
      Folding Range
      Semantic Tokens
    Diagnostics
      Syntax Errors
      Type Errors
      Undefined Variables
      Unused Variables
      Deprecated Usage
      Style Warnings
      Custom Rules
    Document Operations
      Formatting
      Range Formatting
      On Type Formatting
      Document Highlight
      Document Color
    Workspace Operations
      File Watch
      Configuration
      Multi-root Support
    Extensions
      Plugin System
      Custom Annotations
      Library Support
```
