@@ -88,6 +88,8 @@ type LanguageServer struct {
88
88
workspaceRootURI string
89
89
clientIdentifier clients.Identifier
90
90
91
+ clientInitializationOptions types.InitializationOptions
92
+
91
93
cache * cache.Cache
92
94
regoStore storage.Store
93
95
@@ -352,7 +354,7 @@ func (l *LanguageServer) StartConfigWorker(ctx context.Context) {
352
354
}
353
355
}
354
356
355
- // when a file is 'unignored', we move it's contents to the
357
+ // when a file is 'unignored', we move its contents to the
356
358
// standard file list if missing
357
359
for k , v := range l .cache .GetAllIgnoredFiles () {
358
360
if l .ignoreURI (k ) {
@@ -364,7 +366,7 @@ func (l *LanguageServer) StartConfigWorker(ctx context.Context) {
364
366
if ! ok {
365
367
l .cache .SetFileContents (k , v )
366
368
367
- // updating the parse here will enable things like go-to defn
369
+ // updating the parse here will enable things like go-to definition
368
370
// to start working right away without the need for a file content
369
371
// update to run updateParse.
370
372
_ , err = updateParse (ctx , l .cache , l .regoStore , k )
@@ -529,9 +531,9 @@ func (l *LanguageServer) StartWorkspaceStateWorker(ctx context.Context) {
529
531
continue
530
532
}
531
533
532
- for _ , uri := range changedOrNewURIs {
534
+ for _ , cnURI := range changedOrNewURIs {
533
535
l .diagnosticRequestFile <- fileUpdateEvent {
534
- URI : uri ,
536
+ URI : cnURI ,
535
537
Reason : "internal/workspaceStateWorker/changedOrNewFile" ,
536
538
}
537
539
}
@@ -1296,10 +1298,6 @@ func (l *LanguageServer) handleTextDocumentFormatting(
1296
1298
return nil , fmt .Errorf ("failed to unmarshal params: %w" , err )
1297
1299
}
1298
1300
1299
- if warnings := validateFormattingOptions (params .Options ); len (warnings ) > 0 {
1300
- l .logError (fmt .Errorf ("formatting params validation warnings: %v" , warnings ))
1301
- }
1302
-
1303
1301
var oldContent string
1304
1302
1305
1303
var ok bool
@@ -1315,43 +1313,81 @@ func (l *LanguageServer) handleTextDocumentFormatting(
1315
1313
return nil , fmt .Errorf ("failed to get file contents for uri %q" , params .TextDocument .URI )
1316
1314
}
1317
1315
1318
- // set up an in-memory file provider to pass to the fixer for this one file
1319
- memfp := fileprovider .NewInMemoryFileProvider (map [string ][]byte {
1320
- params .TextDocument .URI : []byte (oldContent ),
1321
- })
1316
+ formatter := "opa-fmt"
1322
1317
1323
- input , err := memfp .ToInput ()
1324
- if err != nil {
1325
- return nil , fmt .Errorf ("failed to create fixer input: %w" , err )
1318
+ if l .clientInitializationOptions .Formatter != nil {
1319
+ formatter = * l .clientInitializationOptions .Formatter
1326
1320
}
1327
1321
1328
- li := linter .NewLinter ().
1329
- WithUserConfig (* l .loadedConfig ).
1330
- WithInputModules (& input )
1322
+ var newContent []byte
1323
+
1324
+ switch formatter {
1325
+ case "opa-fmt" , "opa-fmt-rego-v1" :
1326
+ opts := format.Opts {}
1327
+ if formatter == "opa-fmt-rego-v1" {
1328
+ opts .RegoVersion = ast .RegoV0CompatV1
1329
+ }
1330
+
1331
+ f := & fixes.Fmt {OPAFmtOpts : opts }
1332
+ p := uri .ToPath (l .clientIdentifier , params .TextDocument .URI )
1333
+
1334
+ fixResults , err := f .Fix (& fixes.FixCandidate {Filename : filepath .Base (p ), Contents : []byte (oldContent )}, nil )
1335
+ if err != nil {
1336
+ l .logError (fmt .Errorf ("failed to format file: %w" , err ))
1337
+
1338
+ // return "null" as per the spec
1339
+ return nil , nil
1340
+ }
1341
+
1342
+ if len (fixResults ) == 0 {
1343
+ return []types.TextEdit {}, nil
1344
+ }
1345
+
1346
+ newContent = fixResults [0 ].Contents
1347
+ case "regal-fix" :
1348
+ // set up an in-memory file provider to pass to the fixer for this one file
1349
+ memfp := fileprovider .NewInMemoryFileProvider (map [string ][]byte {
1350
+ params .TextDocument .URI : []byte (oldContent ),
1351
+ })
1352
+
1353
+ input , err := memfp .ToInput ()
1354
+ if err != nil {
1355
+ return nil , fmt .Errorf ("failed to create fixer input: %w" , err )
1356
+ }
1331
1357
1332
- f := fixer .NewFixer ()
1333
- f .RegisterFixes (fixes .NewDefaultFixes ()... )
1334
- f .RegisterMandatoryFixes (
1335
- & fixes.Fmt {
1336
- NameOverride : "use-rego-v1" ,
1337
- OPAFmtOpts : format.Opts {
1338
- RegoVersion : ast .RegoV0CompatV1 ,
1358
+ f := fixer .NewFixer ()
1359
+ f .RegisterFixes (fixes .NewDefaultFixes ()... )
1360
+ f .RegisterMandatoryFixes (
1361
+ & fixes.Fmt {
1362
+ NameOverride : "use-rego-v1" ,
1363
+ OPAFmtOpts : format.Opts {
1364
+ RegoVersion : ast .RegoV0CompatV1 ,
1365
+ },
1339
1366
},
1340
- },
1341
- )
1367
+ )
1342
1368
1343
- fixReport , err := f .Fix (ctx , & li , memfp )
1344
- if err != nil {
1345
- return nil , fmt .Errorf ("failed to format: %w" , err )
1346
- }
1369
+ li := linter .NewLinter ().
1370
+ WithInputModules (& input )
1347
1371
1348
- if fixReport . TotalFixes () == 0 {
1349
- return []types. TextEdit {}, nil
1350
- }
1372
+ if l . loadedConfig != nil {
1373
+ li = li . WithUserConfig ( * l . loadedConfig )
1374
+ }
1351
1375
1352
- newContent , err := memfp .GetFile (params .TextDocument .URI )
1353
- if err != nil {
1354
- return nil , fmt .Errorf ("failed to get formatted contents: %w" , err )
1376
+ fixReport , err := f .Fix (ctx , & li , memfp )
1377
+ if err != nil {
1378
+ return nil , fmt .Errorf ("failed to format: %w" , err )
1379
+ }
1380
+
1381
+ if fixReport .TotalFixes () == 0 {
1382
+ return []types.TextEdit {}, nil
1383
+ }
1384
+
1385
+ newContent , err = memfp .GetFile (params .TextDocument .URI )
1386
+ if err != nil {
1387
+ return nil , fmt .Errorf ("failed to get formatted contents: %w" , err )
1388
+ }
1389
+ default :
1390
+ return nil , fmt .Errorf ("unrecognized formatter %q" , formatter )
1355
1391
}
1356
1392
1357
1393
return ComputeEdits (oldContent , string (newContent )), nil
@@ -1507,6 +1543,10 @@ func (l *LanguageServer) handleInitialize(
1507
1543
)
1508
1544
}
1509
1545
1546
+ if params .InitializationOptions != nil {
1547
+ l .clientInitializationOptions = * params .InitializationOptions
1548
+ }
1549
+
1510
1550
regoFilter := types.FileOperationFilter {
1511
1551
Scheme : "file" ,
1512
1552
Pattern : types.FileOperationPattern {
0 commit comments