Skip to content

Refactor: Consolidate document state management in Workspace #645

@jade-codes

Description

@jade-codes

Problem

The LSP layer duplicates state that should live in Workspace:

  • document_texts: HashMap<PathBuf, String> - tracked separately from workspace files
  • parse_errors: HashMap<PathBuf, Vec<ParseError>> - tracked separately from workspace files
  • Direct std::fs::read_to_string calls bypassing core abstractions

This violates separation of concerns - LSP should be a thin protocol adapter, not a state manager.

Current Architecture

LspServer (syster-lsp)
├── workspace: Workspace<SyntaxFile>
├── document_texts: HashMap<PathBuf, String>    ← DUPLICATED STATE
├── parse_errors: HashMap<PathBuf, Vec<ParseError>>  ← DUPLICATED STATE
└── sync_document_texts_from_workspace()        ← SYNCING DUPLICATED STATE

Proposed Architecture

LspServer (syster-lsp)
└── workspace: Workspace<SyntaxFile>
    └── files: HashMap<PathBuf, WorkspaceFile<T>>
        ├── source_text: String       ← SINGLE SOURCE OF TRUTH
        ├── errors: Vec<ParseError>   ← SINGLE SOURCE OF TRUTH
        └── content: T (parsed AST)

Implementation Plan

  • Step 1: Add source_text: String to WorkspaceFile
  • Step 2: Add errors: Vec<ParseError> to WorkspaceFile
  • Step 3: Create FileLoader trait for abstraction (disk/memory/virtual sources)
  • Step 4: Add update_from_text(path, text) method to Workspace
  • Step 5: Remove document_texts from LspServer, use workspace.get_text(path)
  • Step 6: Remove parse_errors from LspServer, use workspace.get_errors(path)
  • Step 7: Remove direct std::fs calls from LSP layer

Benefits

  • Single source of truth for document state
  • LSP becomes a thin protocol adapter
  • Easier testing (mock FileLoader)
  • Better separation of concerns
  • Reduces bugs from state synchronization issues

Files Affected

syster-base:

  • crates/syster-base/src/semantic/workspace/file.rs
  • crates/syster-base/src/semantic/workspace/core.rs
  • crates/syster-base/src/core/ (new FileLoader trait)

syster-lsp:

  • crates/syster-lsp/src/server/core.rs
  • crates/syster-lsp/src/server/document.rs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions