Skip to content

Commit afa1d1b

Browse files
authored
Move location of LSP storage helpers (#1993)
The `lsp` namespace is bloated enough as it is, and the store did not depend on it. Signed-off-by: Anders Eknert <anders.eknert@apple.com>
1 parent 934ff4e commit afa1d1b

6 files changed

Lines changed: 21 additions & 18 deletions

File tree

internal/lsp/lint.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/open-policy-agent/regal/internal/lsp/cache"
1414
"github.com/open-policy-agent/regal/internal/lsp/clients"
1515
"github.com/open-policy-agent/regal/internal/lsp/completions/refs"
16+
"github.com/open-policy-agent/regal/internal/lsp/store"
1617
"github.com/open-policy-agent/regal/internal/lsp/types"
1718
"github.com/open-policy-agent/regal/internal/lsp/uri"
1819
rparse "github.com/open-policy-agent/regal/internal/parse"
@@ -79,7 +80,7 @@ func updateParse(ctx context.Context, opts updateParseOpts) (bool, error) {
7980
opts.Cache.SetModule(opts.FileURI, module)
8081
opts.Cache.SetSuccessfulParseLineCount(opts.FileURI, numLines)
8182

82-
if err := PutFileMod(ctx, opts.Store, opts.FileURI, module); err != nil {
83+
if err := store.PutFileMod(ctx, opts.Store, opts.FileURI, module); err != nil {
8384
return false, fmt.Errorf("failed to update rego store with parsed module: %w", err)
8485
}
8586

@@ -92,7 +93,7 @@ func updateParse(ctx context.Context, opts updateParseOpts) (bool, error) {
9293
}
9394
}
9495

95-
if err = PutFileRefs(ctx, opts.Store, opts.FileURI, ruleRefs); err != nil {
96+
if err = store.PutFileRefs(ctx, opts.Store, opts.FileURI, ruleRefs); err != nil {
9697
return false, fmt.Errorf("failed to update rego store with defined refs: %w", err)
9798
}
9899

internal/lsp/lint_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/open-policy-agent/regal/internal/lsp/cache"
99
"github.com/open-policy-agent/regal/internal/lsp/clients"
10+
"github.com/open-policy-agent/regal/internal/lsp/store"
1011
"github.com/open-policy-agent/regal/internal/lsp/types"
1112
"github.com/open-policy-agent/regal/internal/test/assert"
1213
"github.com/open-policy-agent/regal/internal/test/must"
@@ -96,7 +97,7 @@ allow[msg] { 1 == 1; msg := "hello" }
9697

9798
success := must.Return(updateParse(t.Context(), updateParseOpts{
9899
Cache: c,
99-
Store: NewRegalStore(),
100+
Store: store.NewRegalStore(),
100101
FileURI: testData.fileURI,
101102
Builtins: ast.BuiltinMap,
102103
RegoVersion: testData.regoVersion,

internal/lsp/rego/router_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import (
1919
"github.com/open-policy-agent/opa/v1/storage"
2020
"github.com/open-policy-agent/opa/v1/storage/inmem"
2121

22-
"github.com/open-policy-agent/regal/internal/lsp"
2322
"github.com/open-policy-agent/regal/internal/lsp/rego"
2423
"github.com/open-policy-agent/regal/internal/lsp/rego/query"
2524
"github.com/open-policy-agent/regal/internal/lsp/semantictokens"
25+
"github.com/open-policy-agent/regal/internal/lsp/store"
2626
"github.com/open-policy-agent/regal/internal/lsp/types"
2727
"github.com/open-policy-agent/regal/internal/parse"
2828
"github.com/open-policy-agent/regal/internal/roast/transforms/module"
@@ -167,15 +167,15 @@ func storeForDocument(tb testing.TB, doc document, jsonData []byte) storage.Stor
167167
}
168168
}
169169

170-
store := inmem.NewWithOpts(inmem.OptReturnASTValuesOnRead(true))
171-
if err := storage.WriteOne(tb.Context(), store, storage.AddOp, storage.RootPath, data); err != nil {
170+
stg := inmem.NewWithOpts(inmem.OptReturnASTValuesOnRead(true))
171+
if err := storage.WriteOne(tb.Context(), stg, storage.AddOp, storage.RootPath, data); err != nil {
172172
tb.Fatalf("failed to write to store: %v", err)
173173
}
174174

175175
bis := rego.BuiltinsForCapabilities(ast.CapabilitiesForThisVersion())
176-
must.Equal(tb, nil, lsp.PutBuiltins(tb.Context(), store, bis), "failed to update builtins in storage")
176+
must.Equal(tb, nil, store.PutBuiltins(tb.Context(), stg, bis), "failed to update builtins in storage")
177177

178-
return store
178+
return stg
179179
}
180180

181181
func handlerTests(tb testing.TB) iter.Seq2[string, testCase] {

internal/lsp/server.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/open-policy-agent/regal/internal/lsp/rego"
3939
"github.com/open-policy-agent/regal/internal/lsp/rego/query"
4040
"github.com/open-policy-agent/regal/internal/lsp/semantictokens"
41+
"github.com/open-policy-agent/regal/internal/lsp/store"
4142
"github.com/open-policy-agent/regal/internal/lsp/types"
4243
"github.com/open-policy-agent/regal/internal/lsp/uri"
4344
"github.com/open-policy-agent/regal/internal/update"
@@ -192,17 +193,17 @@ func NewLanguageServer(ctx context.Context, opts *LanguageServerOptions) *Langua
192193
func NewLanguageServerMinimal(ctx context.Context, opts *LanguageServerOptions, cfg *config.Config) *LanguageServer {
193194
c := cache.NewCache()
194195
qc := query.NewCache()
195-
store := NewRegalStore()
196+
rstore := store.NewRegalStore()
196197

197198
featureFlags := util.Or(opts.FeatureFlags, DefaultServerFeatureFlags)
198199

199-
_ = PutServer(ctx, store, types.ServerContext{FeatureFlags: *featureFlags, Version: version.Version})
200+
_ = store.PutServer(ctx, rstore, types.ServerContext{FeatureFlags: *featureFlags, Version: version.Version})
200201

201202
ls := &LanguageServer{
202203
cache: c,
203204
queryCache: qc,
204205
loadedConfig: cfg,
205-
regoStore: store,
206+
regoStore: rstore,
206207
log: opts.Logger,
207208
featureFlags: *featureFlags,
208209
initializationGate: make(chan struct{}),
@@ -220,7 +221,7 @@ func NewLanguageServerMinimal(ctx context.Context, opts *LanguageServerOptions,
220221
loadedConfigAllRegoVersions: concurrent.MapOf(make(map[string]ast.RegoVersion)),
221222
}
222223

223-
ls.regoRouter = rego.NewRegoRouter(ctx, store, qc, rego.Providers{
224+
ls.regoRouter = rego.NewRegoRouter(ctx, rstore, qc, rego.Providers{
224225
ContextProvider: ls.regalContext,
225226
IgnoredProvider: ls.ignoreURI,
226227
ContentProvider: ls.cache.GetFileContents,
@@ -613,7 +614,7 @@ func (l *LanguageServer) setClient(ctx context.Context, client types.Client) {
613614
l.clientLock.Lock()
614615
l.client = client
615616

616-
if err := PutClient(ctx, l.regoStore, client); err != nil {
617+
if err := store.PutClient(ctx, l.regoStore, client); err != nil {
617618
l.clientLock.Unlock()
618619
panic(fmt.Sprintf("failed to store client in rego store: %s", err))
619620
}
@@ -636,7 +637,7 @@ func (l *LanguageServer) loadConfig(ctx context.Context, conf config.Config) {
636637
l.loadedConfig = &conf
637638
l.loadedConfigLock.Unlock()
638639

639-
if err := PutConfig(ctx, l.regoStore, &conf); err != nil {
640+
if err := store.PutConfig(ctx, l.regoStore, &conf); err != nil {
640641
l.log.Message("failed to update config in storage: %v", err)
641642
}
642643

@@ -668,7 +669,7 @@ func (l *LanguageServer) loadConfig(ctx context.Context, conf config.Config) {
668669

669670
l.loadedBuiltins.Set(capsURL, bis)
670671

671-
if err := PutBuiltins(ctx, l.regoStore, bis); err != nil {
672+
if err := store.PutBuiltins(ctx, l.regoStore, bis); err != nil {
672673
l.log.Message("failed to update builtins in storage: %v", err)
673674
}
674675

@@ -687,7 +688,7 @@ func (l *LanguageServer) loadConfig(ctx context.Context, conf config.Config) {
687688
l.cache.SetIgnoredFileContents(k, contents)
688689
}
689690

690-
if err := RemoveFileMod(ctx, l.regoStore, k); err != nil {
691+
if err := store.RemoveFileMod(ctx, l.regoStore, k); err != nil {
691692
l.log.Message("failed to remove mod from store: %s", err)
692693
}
693694
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package lsp
1+
package store
22

33
import (
44
"context"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package lsp
1+
package store
22

33
import (
44
"encoding/json"

0 commit comments

Comments
 (0)