Skip to content

Commit ff140e9

Browse files
committed
fourslash updates
1 parent dbfff26 commit ff140e9

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

internal/fourslash/fourslash.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ type FourslashTest struct {
3838
testData *TestData // !!! consolidate test files from test data and script info
3939
baselines map[string]*strings.Builder
4040
rangesByText *collections.MultiMap[string, *RangeMarker]
41-
config *ls.UserPreferences
4241

4342
scriptInfos map[string]*scriptInfo
4443
converters *ls.Converters
4544

45+
userPreferences *ls.UserPreferences
4646
currentCaretPosition lsproto.Position
4747
lastKnownMarkerName *string
4848
activeFilename string
@@ -282,7 +282,7 @@ func sendRequest[Params, Resp any](t *testing.T, f *FourslashTest, info lsproto.
282282
req := lsproto.ResponseMessage{
283283
ID: req.ID,
284284
JSONRPC: req.JSONRPC,
285-
Result: []any{&f.config},
285+
Result: []any{&f.userPreferences},
286286
}
287287
f.writeMsg(t, req.Message())
288288
resp = f.readMsg(t)
@@ -324,13 +324,21 @@ func (f *FourslashTest) readMsg(t *testing.T) *lsproto.Message {
324324
return msg
325325
}
326326

327-
func (f *FourslashTest) configure(t *testing.T, config *ls.UserPreferences) {
328-
f.config = config
327+
func (f *FourslashTest) Configure(t *testing.T, config *ls.UserPreferences) {
328+
f.userPreferences = config
329329
sendNotification(t, f, lsproto.WorkspaceDidChangeConfigurationInfo, &lsproto.DidChangeConfigurationParams{
330330
Settings: config,
331331
})
332332
}
333333

334+
func (f *FourslashTest) ConfigureWithReset(t *testing.T, config *ls.UserPreferences) (reset func()) {
335+
originalConfig := f.userPreferences.Copy()
336+
f.Configure(t, config)
337+
return func() {
338+
f.Configure(t, originalConfig)
339+
}
340+
}
341+
334342
func (f *FourslashTest) GoToMarkerOrRange(t *testing.T, markerOrRange MarkerOrRange) {
335343
f.goToMarker(t, markerOrRange)
336344
}
@@ -572,10 +580,9 @@ func (f *FourslashTest) verifyCompletionsWorker(t *testing.T, expected *Completi
572580
Position: f.currentCaretPosition,
573581
Context: &lsproto.CompletionContext{},
574582
}
575-
if expected == nil {
576-
f.configure(t, nil)
577-
} else {
578-
f.configure(t, expected.UserPreferences)
583+
if expected != nil && expected.UserPreferences != nil {
584+
reset := f.ConfigureWithReset(t, expected.UserPreferences)
585+
defer reset()
579586
}
580587
resMsg, result, resultOk := sendRequest(t, f, lsproto.TextDocumentCompletionInfo, params)
581588
if resMsg == nil {
@@ -1408,6 +1415,12 @@ func (f *FourslashTest) getCurrentPositionPrefix() string {
14081415
}
14091416

14101417
func (f *FourslashTest) BaselineAutoImportsCompletions(t *testing.T, markerNames []string) {
1418+
reset := f.ConfigureWithReset(t, &ls.UserPreferences{
1419+
IncludeCompletionsForModuleExports: ptrTo(true),
1420+
IncludeCompletionsForImportStatements: ptrTo(true),
1421+
})
1422+
defer reset()
1423+
14111424
for _, markerName := range markerNames {
14121425
f.GoToMarker(t, markerName)
14131426
params := &lsproto.CompletionParams{

internal/ls/languageservice.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ func (l *LanguageService) GetProgram() *compiler.Program {
2424
return l.host.GetProgram()
2525
}
2626

27-
func (l *LanguageService) UpdateUserPreferences(preferences *UserPreferences) {
28-
l.userPreferences = preferences
29-
}
30-
3127
func (l *LanguageService) UserPreferences() *UserPreferences {
3228
return l.userPreferences
3329
}

internal/ls/types.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,20 @@ type UserPreferences struct {
6969
InteractiveInlayHints *bool
7070
}
7171

72-
func (p *UserPreferences) GetOrDefault() UserPreferences {
72+
func (p *UserPreferences) Copy() *UserPreferences {
73+
// not a true deep copy
7374
if p == nil {
74-
return UserPreferences{}
75+
return nil
7576
}
76-
return *p
77+
copy := *p
78+
return &copy
79+
}
80+
81+
func (p *UserPreferences) CopyOrDefault() *UserPreferences {
82+
if p == nil {
83+
return &UserPreferences{}
84+
}
85+
return p.Copy()
7786
}
7887

7988
func (p *UserPreferences) ModuleSpecifierPreferences() modulespecifiers.UserPreferences {

internal/project/snapshot.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Snapshot struct {
3232
ProjectCollection *ProjectCollection
3333
ConfigFileRegistry *ConfigFileRegistry
3434
compilerOptionsForInferredProjects *core.CompilerOptions
35-
userPreferences ls.UserPreferences
35+
userPreferences *ls.UserPreferences
3636

3737
builderLogs *logging.LogTree
3838
apiError error
@@ -60,7 +60,7 @@ func NewSnapshot(
6060
ConfigFileRegistry: configFileRegistry,
6161
ProjectCollection: &ProjectCollection{toPath: toPath},
6262
compilerOptionsForInferredProjects: compilerOptionsForInferredProjects,
63-
userPreferences: userPreferences.GetOrDefault(),
63+
userPreferences: userPreferences.CopyOrDefault(),
6464
}
6565
s.converters = ls.NewConverters(s.sessionOptions.PositionEncoding, s.LineMap)
6666
s.refCount.Store(1)
@@ -85,7 +85,7 @@ func (s *Snapshot) LineMap(fileName string) *ls.LineMap {
8585
}
8686

8787
func (s *Snapshot) UserPreferences() *ls.UserPreferences {
88-
return &s.userPreferences
88+
return s.userPreferences
8989
}
9090

9191
func (s *Snapshot) Converters() *ls.Converters {

0 commit comments

Comments
 (0)