on main help sign in
chore: pass config to app
commit5f16af48b101db4982042b7c634fe3e9de76addd authorKujtim Hoxha <kujtimii.h@gmail.com> dateSat 5 Jul 2025, 18:02 (1 year ago) parenta07dd6b
3 files changed+9−12
cmd/root.go+1−1 internal/app/app.go+4−3 internal/app/lsp.go+4−8
cmd/root.go+1−1
@@ -91,7 +91,7 @@ to assist developers in writing, debugging, and understanding code directly from
9191 return err
9292 }
9393
94− app, err := app.New(ctx, conn)
94+ app, err := app.New(ctx, conn, cfg)
9595 if err != nil {
9696 slog.Error(fmt.Sprintf("Failed to create app instance: %v", err))
9797 return err
internal/app/app.go+4−3
@@ -37,22 +37,23 @@ type App struct {
3737 watcherCancelFuncs []context.CancelFunc
3838 cancelFuncsMutex sync.Mutex
3939 watcherWG sync.WaitGroup
40+
41+ config *config.Config
4042}
4143
42−func New(ctx context.Context, conn *sql.DB) (*App, error) {
44+func New(ctx context.Context, conn *sql.DB, cfg *config.Config) (*App, error) {
4345 q := db.New(conn)
4446 sessions := session.NewService(q)
4547 messages := message.NewService(q)
4648 files := history.NewService(q, conn)
4749
48− cfg := config.Get()
49−
5050 app := &App{
5151 Sessions: sessions,
5252 Messages: messages,
5353 History: files,
5454 Permissions: permission.NewPermissionService(cfg.WorkingDir()),
5555 LSPClients: make(map[string]*lsp.Client),
56+ config: cfg,
5657 }
5758
5859 // Initialize LSP clients in the background
internal/app/lsp.go+4−8
@@ -5,17 +5,14 @@ import (
55 "log/slog"
66 "time"
77
8− "github.com/charmbracelet/crush/internal/config"
98 "github.com/charmbracelet/crush/internal/log"
109 "github.com/charmbracelet/crush/internal/lsp"
1110 "github.com/charmbracelet/crush/internal/lsp/watcher"
1211)
1312
1413func (app *App) initLSPClients(ctx context.Context) {
15− cfg := config.Get()
16−
1714 // Initialize LSP clients
18− for name, clientConfig := range cfg.LSP {
15+ for name, clientConfig := range app.config.LSP {
1916 // Start each client initialization in its own goroutine
2017 go app.createAndStartLSPClient(ctx, name, clientConfig.Command, clientConfig.Args...)
2118 }
@@ -39,7 +36,7 @@ func (app *App) createAndStartLSPClient(ctx context.Context, name string, comman
3936 defer cancel()
4037
4138 // Initialize with the initialization context
42− _, err = lspClient.InitializeLSPClient(initCtx, config.Get().WorkingDir())
39+ _, err = lspClient.InitializeLSPClient(initCtx, app.config.WorkingDir())
4340 if err != nil {
4441 slog.Error("Initialize failed", "name", name, "error", err)
4542 // Clean up the client to prevent resource leaks
@@ -92,15 +89,14 @@ func (app *App) runWorkspaceWatcher(ctx context.Context, name string, workspaceW
9289 app.restartLSPClient(ctx, name)
9390 })
9491
95− workspaceWatcher.WatchWorkspace(ctx, config.Get().WorkingDir())
92+ workspaceWatcher.WatchWorkspace(ctx, app.config.WorkingDir())
9693 slog.Info("Workspace watcher stopped", "client", name)
9794}
9895
9996// restartLSPClient attempts to restart a crashed or failed LSP client
10097func (app *App) restartLSPClient(ctx context.Context, name string) {
10198 // Get the original configuration
102− cfg := config.Get()
103− clientConfig, exists := cfg.LSP[name]
99+ clientConfig, exists := app.config.LSP[name]
104100 if !exists {
105101 slog.Error("Cannot restart client, configuration not found", "client", name)
106102 return