From 5f16af48b101db4982042b7c634fe3e9de76addd Mon Sep 17 00:00:00 2001 From: Kujtim Hoxha Date: Sat, 5 Jul 2025 18:02:52 +0200 Subject: [PATCH] chore: pass config to app --- cmd/root.go | 2 +- internal/app/app.go | 7 ++++--- internal/app/lsp.go | 12 ++++-------- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 5747145284fbed25201b58a6b43eccf975db1304..02ebbd0104e88a59b659c322a0605189b01afaaf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -91,7 +91,7 @@ to assist developers in writing, debugging, and understanding code directly from return err } - app, err := app.New(ctx, conn) + app, err := app.New(ctx, conn, cfg) if err != nil { slog.Error(fmt.Sprintf("Failed to create app instance: %v", err)) return err diff --git a/internal/app/app.go b/internal/app/app.go index 616aabc74483cc725e7e4953d8ad876511925fe6..da014df81367665caf1df793760e3d832c223648 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -37,22 +37,23 @@ type App struct { watcherCancelFuncs []context.CancelFunc cancelFuncsMutex sync.Mutex watcherWG sync.WaitGroup + + config *config.Config } -func New(ctx context.Context, conn *sql.DB) (*App, error) { +func New(ctx context.Context, conn *sql.DB, cfg *config.Config) (*App, error) { q := db.New(conn) sessions := session.NewService(q) messages := message.NewService(q) files := history.NewService(q, conn) - cfg := config.Get() - app := &App{ Sessions: sessions, Messages: messages, History: files, Permissions: permission.NewPermissionService(cfg.WorkingDir()), LSPClients: make(map[string]*lsp.Client), + config: cfg, } // Initialize LSP clients in the background diff --git a/internal/app/lsp.go b/internal/app/lsp.go index 3c44b4b761124b91c7590e5ec8cf724dccd718b7..1777a653a4153dc42cc87444f6122df01e82cedd 100644 --- a/internal/app/lsp.go +++ b/internal/app/lsp.go @@ -5,17 +5,14 @@ import ( "log/slog" "time" - "github.com/charmbracelet/crush/internal/config" "github.com/charmbracelet/crush/internal/log" "github.com/charmbracelet/crush/internal/lsp" "github.com/charmbracelet/crush/internal/lsp/watcher" ) func (app *App) initLSPClients(ctx context.Context) { - cfg := config.Get() - // Initialize LSP clients - for name, clientConfig := range cfg.LSP { + for name, clientConfig := range app.config.LSP { // Start each client initialization in its own goroutine go app.createAndStartLSPClient(ctx, name, clientConfig.Command, clientConfig.Args...) } @@ -39,7 +36,7 @@ func (app *App) createAndStartLSPClient(ctx context.Context, name string, comman defer cancel() // Initialize with the initialization context - _, err = lspClient.InitializeLSPClient(initCtx, config.Get().WorkingDir()) + _, err = lspClient.InitializeLSPClient(initCtx, app.config.WorkingDir()) if err != nil { slog.Error("Initialize failed", "name", name, "error", err) // Clean up the client to prevent resource leaks @@ -92,15 +89,14 @@ func (app *App) runWorkspaceWatcher(ctx context.Context, name string, workspaceW app.restartLSPClient(ctx, name) }) - workspaceWatcher.WatchWorkspace(ctx, config.Get().WorkingDir()) + workspaceWatcher.WatchWorkspace(ctx, app.config.WorkingDir()) slog.Info("Workspace watcher stopped", "client", name) } // restartLSPClient attempts to restart a crashed or failed LSP client func (app *App) restartLSPClient(ctx context.Context, name string) { // Get the original configuration - cfg := config.Get() - clientConfig, exists := cfg.LSP[name] + clientConfig, exists := app.config.LSP[name] if !exists { slog.Error("Cannot restart client, configuration not found", "client", name) return