Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions internal/fourslash/fourslash.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func NewFourslash(t *testing.T, capabilities *lsproto.ClientCapabilities, conten
in: inputWriter,
out: outputReader,
testData: &testData,
userPreferences: lsutil.NewDefaultUserPreferences(), // !!! parse default preferences for fourslash case?
userPreferences: lsutil.DefaultUserPreferences,
vfs: fs,
scriptInfos: scriptInfos,
converters: converters,
Expand Down Expand Up @@ -646,7 +646,7 @@ func (f *FourslashTest) Configure(t *testing.T, config *lsutil.UserPreferences)
}

func (f *FourslashTest) ConfigureWithReset(t *testing.T, config *lsutil.UserPreferences) (reset func()) {
originalConfig := f.userPreferences.Copy()
originalConfig := f.userPreferences
f.Configure(t, config)
return func() {
f.Configure(t, originalConfig)
Expand Down Expand Up @@ -1260,7 +1260,7 @@ func (f *FourslashTest) VerifyApplyCodeActionFromCompletion(t *testing.T, marker
userPreferences = options.UserPreferences
} else {
// Default preferences: enables auto-imports
userPreferences = lsutil.NewDefaultUserPreferences()
userPreferences = lsutil.DefaultUserPreferences
}

reset := f.ConfigureWithReset(t, userPreferences)
Expand Down Expand Up @@ -3282,7 +3282,7 @@ func (f *FourslashTest) VerifyBaselineInlayHints(

preferences := testPreferences
if preferences == nil {
preferences = lsutil.NewDefaultUserPreferences()
preferences = lsutil.DefaultUserPreferences
}
reset := f.ConfigureWithReset(t, preferences)
defer reset()
Expand Down Expand Up @@ -3602,11 +3602,11 @@ type VerifyWorkspaceSymbolCase struct {

// `verify.navigateTo` in Strada.
func (f *FourslashTest) VerifyWorkspaceSymbol(t *testing.T, cases []*VerifyWorkspaceSymbolCase) {
originalPreferences := f.userPreferences.Copy()
originalPreferences := f.userPreferences
for _, testCase := range cases {
preferences := testCase.Preferences
if preferences == nil {
preferences = lsutil.NewDefaultUserPreferences()
preferences = lsutil.DefaultUserPreferences
}
f.Configure(t, preferences)
result := sendRequest(t, f, lsproto.WorkspaceSymbolInfo, &lsproto.WorkspaceSymbolParams{Query: testCase.Pattern})
Expand Down
2 changes: 1 addition & 1 deletion internal/ls/autoimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ func isIndexFileName(fileName string) bool {

// returns `-1` if `a` is better than `b`
func compareModuleSpecifierRelativity(a *ImportFix, b *ImportFix, preferences *lsutil.UserPreferences) int {
switch preferences.ImportModuleSpecifierPreference {
switch preferences.ModuleSpecifier.ImportModuleSpecifierPreference {
case modulespecifiers.ImportModuleSpecifierPreferenceNonRelative, modulespecifiers.ImportModuleSpecifierPreferenceProjectRelative:
return core.CompareBooleans(a.moduleSpecifierKind == modulespecifiers.ResultKindRelative, b.moduleSpecifierKind == modulespecifiers.ResultKindRelative)
}
Expand Down
1,004 changes: 521 additions & 483 deletions internal/ls/lsutil/userpreferences.go

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions internal/ls/lsutil/userpreferences_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package lsutil

import (
"reflect"
"testing"

"github.com/go-json-experiment/json"
"github.com/microsoft/typescript-go/internal/modulespecifiers"
"gotest.tools/v3/assert"
)

func fillNonZeroValues(v reflect.Value, path string) {
t := v.Type()
for i := range t.NumField() {
field := v.Field(i)
fieldType := t.Field(i)
if !field.CanSet() {
continue
}
fieldPath := path + "." + fieldType.Name
switch field.Kind() {
case reflect.Bool:
field.SetBool(true)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.SetInt(1)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
field.SetUint(1)
case reflect.String:
val := getValidStringValue(field.Type(), fieldPath)
field.SetString(val)
case reflect.Slice:
if field.Type().Elem().Kind() == reflect.String {
field.Set(reflect.ValueOf([]string{"test"}))
}
case reflect.Struct:
fillNonZeroValues(field, fieldPath)
}
}
}

func getValidStringValue(t reflect.Type, path string) string {
typeName := t.String()
switch typeName {
case "lsutil.QuotePreference":
return string(QuotePreferenceSingle)
case "lsutil.JsxAttributeCompletionStyle":
return string(JsxAttributeCompletionStyleBraces)
case "lsutil.IncludePackageJsonAutoImports":
return string(IncludePackageJsonAutoImportsOn)
case "lsutil.IncludeInlayParameterNameHints":
return string(IncludeInlayParameterNameHintsAll)
case "modulespecifiers.ImportModuleSpecifierPreference":
return string(modulespecifiers.ImportModuleSpecifierPreferenceRelative)
case "modulespecifiers.ImportModuleSpecifierEndingPreference":
return string(modulespecifiers.ImportModuleSpecifierEndingPreferenceJs)
default:
return "test"
}
}

func TestUserPreferencesRoundtrip(t *testing.T) {
t.Parallel()

original := &UserPreferences{}
fillNonZeroValues(reflect.ValueOf(original).Elem(), "UserPreferences")

jsonBytes, err := json.Marshal(original)
assert.NilError(t, err)

t.Run("UnmarshalJSONFrom", func(t *testing.T) {
t.Parallel()
parsed := &UserPreferences{}
err2 := json.Unmarshal(jsonBytes, parsed)
assert.NilError(t, err2)
assert.DeepEqual(t, original, parsed)
})

t.Run("parseWorker", func(t *testing.T) {
t.Parallel()
var config map[string]any
err2 := json.Unmarshal(jsonBytes, &config)
assert.NilError(t, err2)
parsed := &UserPreferences{}
parsed.parseWorker(config)
assert.DeepEqual(t, original, parsed)
})
}
15 changes: 6 additions & 9 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ func (s *Server) RefreshCodeLens(ctx context.Context) error {
func (s *Server) RequestConfiguration(ctx context.Context) (*lsutil.UserPreferences, error) {
caps := lsproto.GetClientCapabilities(ctx)
if !caps.Workspace.Configuration {
// if no configuration request capapbility, return default preferences
return s.session.NewUserPreferences(), nil
// if no configuration request capability, return default preferences
return lsutil.DefaultUserPreferences, nil
}
configs, err := sendClientRequest(ctx, s, lsproto.WorkspaceConfigurationInfo, &lsproto.ConfigurationParams{
Items: []*lsproto.ConfigurationItem{
Expand All @@ -287,13 +287,12 @@ func (s *Server) RequestConfiguration(ctx context.Context) (*lsutil.UserPreferen
return nil, fmt.Errorf("configure request failed: %w", err)
}
s.logger.Infof("configuration: %+v, %T", configs, configs)
userPreferences := s.session.NewUserPreferences()
for _, item := range configs {
if parsed := userPreferences.Parse(item); parsed != nil {
if parsed := lsutil.ParseUserPreferences(item); parsed != nil {
return parsed, nil
}
}
return userPreferences, nil
return lsutil.DefaultUserPreferences, nil
}

func (s *Server) Run(ctx context.Context) error {
Expand Down Expand Up @@ -1101,11 +1100,9 @@ func (s *Server) handleDidChangeWorkspaceConfiguration(ctx context.Context, para
}
// !!! Both the 'javascript' and 'js/ts' scopes need to be checked for settings as well.
tsSettings := settings["typescript"]
userPreferences := s.session.UserPreferences()
if parsed := userPreferences.Parse(tsSettings); parsed != nil {
userPreferences = parsed
if parsed := lsutil.ParseUserPreferences(tsSettings); parsed != nil {
s.session.Configure(parsed)
}
s.session.Configure(userPreferences)
return nil
}

Expand Down
16 changes: 4 additions & 12 deletions internal/project/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ type Session struct {
// released from the parseCache.
programCounter *programCounter

// read-only after initialization
initialPreferences *lsutil.UserPreferences
userPreferences *lsutil.UserPreferences // !!! update to Config
compilerOptionsForInferredProjects *core.CompilerOptions
typingsInstaller *ata.TypingsInstaller
Expand Down Expand Up @@ -190,16 +188,11 @@ func (s *Session) GetCurrentDirectory() string {
return s.options.CurrentDirectory
}

// Gets current UserPreferences, always a copy
// Gets current UserPreferences
func (s *Session) UserPreferences() *lsutil.UserPreferences {
s.configRWMu.Lock()
defer s.configRWMu.Unlock()
return s.userPreferences.Copy()
}

// Gets original UserPreferences of the session
func (s *Session) NewUserPreferences() *lsutil.UserPreferences {
return s.initialPreferences.CopyOrDefault()
return s.userPreferences
}

// Trace implements module.ResolutionHost
Expand All @@ -222,8 +215,7 @@ func (s *Session) Configure(userPreferences *lsutil.UserPreferences) {
}

func (s *Session) InitializeWithConfig(userPreferences *lsutil.UserPreferences) {
s.initialPreferences = userPreferences.CopyOrDefault()
s.Configure(s.initialPreferences)
s.Configure(userPreferences)
}

func (s *Session) DidOpenFile(ctx context.Context, uri lsproto.DocumentUri, version int32, content string, languageKind lsproto.LanguageKind) {
Expand Down Expand Up @@ -706,7 +698,7 @@ func (s *Session) flushChanges(ctx context.Context) (FileChangeSummary, map[tspa
var newConfig *Config
if s.pendingConfigChanges {
newConfig = &Config{
tsUserPreferences: s.userPreferences.Copy(),
tsUserPreferences: s.userPreferences,
}
}
s.pendingConfigChanges = false
Expand Down
2 changes: 1 addition & 1 deletion internal/project/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ func TestSession(t *testing.T) {
session.Configure(&lsutil.UserPreferences{})

// Change user preferences for code lens and inlay hints.
newPrefs := session.UserPreferences()
newPrefs := session.UserPreferences().Copy()
newPrefs.CodeLens.ReferencesCodeLensEnabled = !newPrefs.CodeLens.ReferencesCodeLensEnabled
newPrefs.InlayHints.IncludeInlayFunctionLikeReturnTypeHints = !newPrefs.InlayHints.IncludeInlayFunctionLikeReturnTypeHints
session.Configure(newPrefs)
Expand Down
2 changes: 1 addition & 1 deletion internal/project/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (s *Snapshot) Clone(ctx context.Context, change SnapshotChange, overlays ma
config := s.config
if change.newConfig != nil {
if change.newConfig.tsUserPreferences != nil {
config.tsUserPreferences = change.newConfig.tsUserPreferences.CopyOrDefault()
config.tsUserPreferences = change.newConfig.tsUserPreferences
}
if change.newConfig.formatOptions != nil {
config.formatOptions = change.newConfig.formatOptions
Expand Down