Module MountainEnvironment

Module MountainEnvironment 

Source
Expand description

Main DI container struct.

§MountainEnvironment (Environment)

The primary dependency injection (DI) container for the Mountain application. It implements all provider traits defined in the Common crate, acting as the central orchestrator that provides access to all platform services.

§RESPONSIBILITIES

§1. Dependency Injection Container

  • Implements Requires<T> for all 25+ provider traits via macro
  • Enable other components to request dependencies through Require() method
  • Provide lazy initialization with proper lifetime management
  • Support circular dependencies via Arc-wrapped self-references

§2. Application Lifecycle Management

  • Hold references to Tauri AppHandle and ApplicationState
  • Manage the core application context and state
  • Coordinate initialization sequence for all providers
  • Support graceful shutdown and cleanup

§3. Air Integration (Optional)

  • Manage the Air gRPC client for cloud-based services when AirIntegration feature enabled
  • Enable dynamic switching between local and cloud services
  • Provide health checking for Air daemon availability
  • Support fallback to local services when Air unavailable

§4. Extension Management

  • Implement ExtensionManagementService for discovering extensions
  • Scan registered paths for valid extensions
  • Manage extension metadata in ApplicationState
  • Provide extension list to extension host (Cocoon)

§5. Service Orchestration

  • Act as central coordinator between all providers (FileSystem, Document, Command, etc.)
  • Ensure proper initialization order and dependency resolution
  • Facilitate inter-provider communication via IPC or direct calls
  • Provide capability resolution through Requires<T> trait

§ARCHITECTURAL ROLE

MountainEnvironment is the central DI container and service locator for the entire application:

Component ──► Requires<T> ──► MountainEnvironment ──► Arc<dyn T>

§Position in Mountain

  • Environment module: Root of the dependency injection system
  • Implements Common crate’s Environment and Requires traits
  • All providers accessed through capability-based lookups
  • Created early in startup and shared via Arc<MountainEnvironment>

§Dependencies (Incoming)

  • CommonLibrary::* provider traits (25+ interfaces)
  • tauri::AppHandle: UI and platform integration
  • ApplicationState: Shared application state
  • AirServiceClient (optional): Cloud service integration

§Dependents (Outgoing)

  • All command handlers: Require capabilities from environment
  • ApplicationRunTime: Uses environment for effect execution
  • Provider implementations: self-referencing via Arc<dyn Trait>
  • InitializationData: Uses environment to gather extensions and workspace data

§DEPENDENCY INJECTION PATTERN

Mountain uses a service locator pattern with trait-based lookup:

impl Requires<dyn FileSystemReader> for MountainEnvironment {
    fn Require(&self) -> Arc<dyn FileSystemReader> {
        Arc::new(self.clone()) // Self implements FileSystemReader
    }
}

This enables:

  • Loose coupling: Components depend on traits, not concrete types
  • Circular dependencies: All providers can require each other through Environment
  • Testability: Environment can be mocked or stubbed for tests
  • Flexibility: New providers added without changing dependents

§INITIALIZATION SEQUENCE

  1. Create Environment: MountainEnvironment::Create(app_handle, app_state)
  2. Provider Instantiation: Lazy through Requires<T> trait calls
  3. State Access: Each provider can access ApplicationState and AppHandle via self
  4. Inter-Provider Communication: Via trait methods (direct Rust calls) or IPC

Note: Provider implementations are separate modules in Environment/ directory. Each module implements a specific trait and can be included/excluded via feature flags.

§AIR INTEGRATION

When AirIntegration feature is enabled:

  • AirClient field is Option<Arc<AirServiceClient<tonic::transport::Channel>>>
  • CreateWithAir() allows passing pre-configured Air client
  • SetAirClient() allows dynamic switching at runtime
  • IsAirAvailable() performs health check on Air daemon
  • Providers like SecretProvider and UpdateService can delegate to Air

When feature is disabled:

  • AirClient field is absent
  • IsAirAvailable() always returns false
  • Local-only service implementations are used

§VS CODE REFERENCE

Patterns borrowed from VS Code’s service architecture:

  • vs/platform/instantiation/common/instantiation.ts - Service collection and instantiation
  • vs/platform/workspace/common/workspace.ts - Service locator pattern
  • vs/workbench/services/extensions/common/extensions.ts - Extension point management

Key concepts:

  • Service lifetime management through Arc reference counting
  • Trait-based service definitions for loose coupling
  • Lazy initialization on first use
  • Thread-safe service access

§PROVIDER TRAITS IMPLEMENTED

The following 25+ provider traits are implemented using the impl_provider! macro:

  • CommandExecutor - Execute commands and actions
  • ConfigurationProvider - Access application configuration
  • ConfigurationInspector - Inspect configuration values
  • CustomEditorProvider - Custom document editor support
  • DebugService - Debug adapter protocol integration
  • DiagnosticManager - Diagnostic/lint/error management
  • DocumentProvider - Document lifecycle and content access
  • FileSystemReader / FileSystemWriter - File system operations
  • IPCProvider - Inter-process communication
  • KeybindingProvider - Keybinding resolution and execution
  • LanguageFeatureProviderRegistry - LSP and language features
  • OutputChannelManager - Output panel channel management
  • SecretProvider - Credential and secret storage
  • SourceControlManagementProvider - Git/SCM integration
  • StatusBarProvider - Status bar item management
  • StorageProvider - Key-value storage
  • SynchronizationProvider - Remote sync and collaboration
  • TerminalProvider - Integrated terminal management
  • TestController - Test discovery and execution
  • TreeViewProvider - Tree view UI components
  • UserInterfaceProvider - UI dialog and interaction services
  • WebviewProvider - Webview panel management
  • WorkspaceProvider - Workspace and folder management
  • WorkspaceEditApplier - Workspace edit application (text changes)
  • ExtensionManagementService - Extension scanning and metadata
  • SearchProvider - Search and replace functionality

§TODO

  • Add telemetry integration for performance monitoring
  • Implement proper provider health checking
  • Add provider dependency validation on initialization
  • Consider async initialization for providers
  • Add circuit breaker pattern for external service calls (Air)
  • Implement graceful degradation when providers fail
  • Add metrics collection for provider usage
  • Consider provider initialization order dependencies

Structs§

MountainEnvironment
The concrete Environment for the Mountain application.