Skip to content

Commit a21a470

Browse files
Merge pull request #63 from cmoog/initconfig
allow LSP client to set database connection config
2 parents 01d9f6f + 2ec7993 commit a21a470

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

internal/handler/handler.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ type Server struct {
3030
curDBName string
3131
curConnectionIndex int
3232

33+
// The initOptionDBConfig is an optional param
34+
// sent by the client as part of the LSP InitializationOptions
35+
// payload. If non-nil, the server will ignore all
36+
// other configuration sources (workspace and user).
37+
initOptionDBConfig *database.DBConfig
38+
3339
worker *database.Worker
3440
files map[string]*File
3541
}
@@ -159,6 +165,8 @@ func (s *Server) handleInitialize(ctx context.Context, conn *jsonrpc2.Conn, req
159165
},
160166
}
161167

168+
s.initOptionDBConfig = params.InitializationOptions.ConnectionConfig
169+
162170
// Initialize database database connection
163171
// NOTE: If no connection is found at this point, it is possible that the connection settings are sent to workspace config, so don't make an error
164172
messenger := lsp.NewLspMessenger(conn)
@@ -378,6 +386,11 @@ func (s *Server) newDBRepository(ctx context.Context) (database.DBRepository, er
378386
}
379387

380388
func (s *Server) topConnection() *database.DBConfig {
389+
// if the init config is set, ignore all other connection configs
390+
if s.initOptionDBConfig != nil {
391+
return s.initOptionDBConfig
392+
}
393+
381394
cfg := s.getConfig()
382395
if cfg == nil || len(cfg.Connections) == 0 {
383396
return nil

internal/lsp/lsp.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package lsp
22

3-
import "github.com/lighttiger2505/sqls/internal/config"
3+
import (
4+
"github.com/lighttiger2505/sqls/internal/config"
5+
"github.com/lighttiger2505/sqls/internal/database"
6+
)
47

58
// https://microsoft.github.io/language-server-protocol/specifications/specification-3-14/#initialize
69

@@ -14,6 +17,10 @@ type InitializeParams struct {
1417
}
1518

1619
type InitializeOptions struct {
20+
// Allow the LSP client to choose a database configuration.
21+
// If set, the LSP server will ignore all other configuration
22+
// sources, including the workspace and user configuration files.
23+
ConnectionConfig *database.DBConfig `json:"connectionConfig,omitempty"`
1724
}
1825

1926
type ClientCapabilities struct {

0 commit comments

Comments
 (0)