@@ -275,6 +275,24 @@ func (*commandHandler) AddTelemetryCounters(_ context.Context, args command.AddT
275
275
return nil
276
276
}
277
277
278
+ func (c * commandHandler ) AddTest (ctx context.Context , loc protocol.Location ) (* protocol.WorkspaceEdit , error ) {
279
+ var result * protocol.WorkspaceEdit
280
+ err := c .run (ctx , commandConfig {
281
+ forURI : loc .URI ,
282
+ }, func (ctx context.Context , deps commandDeps ) error {
283
+ if deps .snapshot .FileKind (deps .fh ) != file .Go {
284
+ return fmt .Errorf ("can't add test for non-Go file" )
285
+ }
286
+ docedits , err := golang .AddTestForFunc (ctx , deps .snapshot , loc )
287
+ if err != nil {
288
+ return err
289
+ }
290
+ return applyChanges (ctx , c .s .client , docedits )
291
+ })
292
+ // TODO(hxjiang): move the cursor to the new test once edits applied.
293
+ return result , err
294
+ }
295
+
278
296
// commandConfig configures common command set-up and execution.
279
297
type commandConfig struct {
280
298
requireSave bool // whether all files must be saved for the command to work
@@ -388,16 +406,7 @@ func (c *commandHandler) ApplyFix(ctx context.Context, args command.ApplyFixArgs
388
406
result = wsedit
389
407
return nil
390
408
}
391
- resp , err := c .s .client .ApplyEdit (ctx , & protocol.ApplyWorkspaceEditParams {
392
- Edit : * wsedit ,
393
- })
394
- if err != nil {
395
- return err
396
- }
397
- if ! resp .Applied {
398
- return errors .New (resp .FailureReason )
399
- }
400
- return nil
409
+ return applyChanges (ctx , c .s .client , changes )
401
410
})
402
411
return result , err
403
412
}
@@ -622,17 +631,7 @@ func (c *commandHandler) RemoveDependency(ctx context.Context, args command.Remo
622
631
if err != nil {
623
632
return err
624
633
}
625
- response , err := c .s .client .ApplyEdit (ctx , & protocol.ApplyWorkspaceEditParams {
626
- Edit : * protocol .NewWorkspaceEdit (
627
- protocol .DocumentChangeEdit (deps .fh , edits )),
628
- })
629
- if err != nil {
630
- return err
631
- }
632
- if ! response .Applied {
633
- return fmt .Errorf ("edits not applied because of %s" , response .FailureReason )
634
- }
635
- return nil
634
+ return applyChanges (ctx , c .s .client , []protocol.DocumentChange {protocol .DocumentChangeEdit (deps .fh , edits )})
636
635
})
637
636
}
638
637
@@ -1107,17 +1106,7 @@ func (c *commandHandler) AddImport(ctx context.Context, args command.AddImportAr
1107
1106
if err != nil {
1108
1107
return fmt .Errorf ("could not add import: %v" , err )
1109
1108
}
1110
- r , err := c .s .client .ApplyEdit (ctx , & protocol.ApplyWorkspaceEditParams {
1111
- Edit : * protocol .NewWorkspaceEdit (
1112
- protocol .DocumentChangeEdit (deps .fh , edits )),
1113
- })
1114
- if err != nil {
1115
- return fmt .Errorf ("could not apply import edits: %v" , err )
1116
- }
1117
- if ! r .Applied {
1118
- return fmt .Errorf ("failed to apply edits: %v" , r .FailureReason )
1119
- }
1120
- return nil
1109
+ return applyChanges (ctx , c .s .client , []protocol.DocumentChange {protocol .DocumentChangeEdit (deps .fh , edits )})
1121
1110
})
1122
1111
}
1123
1112
@@ -1126,18 +1115,11 @@ func (c *commandHandler) ExtractToNewFile(ctx context.Context, args protocol.Loc
1126
1115
progress : "Extract to a new file" ,
1127
1116
forURI : args .URI ,
1128
1117
}, func (ctx context.Context , deps commandDeps ) error {
1129
- edit , err := golang .ExtractToNewFile (ctx , deps .snapshot , deps .fh , args .Range )
1118
+ changes , err := golang .ExtractToNewFile (ctx , deps .snapshot , deps .fh , args .Range )
1130
1119
if err != nil {
1131
1120
return err
1132
1121
}
1133
- resp , err := c .s .client .ApplyEdit (ctx , & protocol.ApplyWorkspaceEditParams {Edit : * edit })
1134
- if err != nil {
1135
- return fmt .Errorf ("could not apply edits: %v" , err )
1136
- }
1137
- if ! resp .Applied {
1138
- return fmt .Errorf ("edits not applied: %s" , resp .FailureReason )
1139
- }
1140
- return nil
1122
+ return applyChanges (ctx , c .s .client , changes )
1141
1123
})
1142
1124
}
1143
1125
@@ -1543,13 +1525,7 @@ func (c *commandHandler) ChangeSignature(ctx context.Context, args command.Chang
1543
1525
result = wsedit
1544
1526
return nil
1545
1527
}
1546
- r , err := c .s .client .ApplyEdit (ctx , & protocol.ApplyWorkspaceEditParams {
1547
- Edit : * wsedit ,
1548
- })
1549
- if ! r .Applied {
1550
- return fmt .Errorf ("failed to apply edits: %v" , r .FailureReason )
1551
- }
1552
- return nil
1528
+ return applyChanges (ctx , c .s .client , docedits )
1553
1529
})
1554
1530
return result , err
1555
1531
}
0 commit comments