Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ecddfc4
format options implementation
iisaduan Dec 2, 2025
1e5c2ea
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
iisaduan Dec 2, 2025
48bdad2
merge
iisaduan Dec 2, 2025
4f60eeb
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
iisaduan Dec 3, 2025
ca78105
formatting
iisaduan Dec 12, 2025
ea1ac02
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
iisaduan Dec 12, 2025
c105023
updatefailing
iisaduan Dec 12, 2025
ae84521
fix broken error message and nil formatcodesettings pointer
iisaduan Dec 13, 2025
f003dd9
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
iisaduan Dec 17, 2025
79f20e3
remove extra baseline
iisaduan Dec 17, 2025
08c14bd
include js/ts scope in language service
iisaduan Dec 18, 2025
dbd5231
remove string setting from fourslash.SetFormatOption
iisaduan Dec 19, 2025
a3dccce
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
iisaduan Dec 19, 2025
efcdf12
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
iisaduan Jan 9, 2026
ee89609
merge changes
iisaduan Jan 13, 2026
1f42b62
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
iisaduan Jan 13, 2026
0c34d11
format
iisaduan Jan 13, 2026
9a064af
updatefailing
iisaduan Jan 14, 2026
60a2a35
fix config race
iisaduan Jan 15, 2026
7a50287
config refactor pt1
iisaduan Jan 15, 2026
19a721a
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
iisaduan Jan 15, 2026
1de4120
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
iisaduan Jan 21, 2026
632e36e
completions fourslash bug
iisaduan Jan 22, 2026
6c9c816
rename config
iisaduan Jan 22, 2026
a40906f
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
iisaduan Jan 22, 2026
4c928da
rename config (again)
iisaduan Jan 23, 2026
0004f16
remove "js" and "ts" individual options
iisaduan Jan 23, 2026
1dffc2a
format
iisaduan Jan 23, 2026
e687b29
rename `copyInto`->`merge`
iisaduan Jan 23, 2026
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
6 changes: 3 additions & 3 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (api *API) GetSymbolAtPosition(ctx context.Context, projectId Handle[projec
return nil, errors.New("project not found")
}

languageService := ls.NewLanguageService(project.ConfigFilePath(), project.GetProgram(), snapshot)
languageService := ls.NewLanguageService(project.ConfigFilePath(), project.GetProgram(), snapshot, fileName)
symbol, err := languageService.GetSymbolAtPosition(ctx, fileName, position)
if err != nil || symbol == nil {
return nil, err
Expand Down Expand Up @@ -204,7 +204,7 @@ func (api *API) GetSymbolAtLocation(ctx context.Context, projectId Handle[projec
if node == nil {
return nil, fmt.Errorf("node of kind %s not found at position %d in file %q", kind.String(), pos, sourceFile.FileName())
}
languageService := ls.NewLanguageService(project.ConfigFilePath(), project.GetProgram(), snapshot)
languageService := ls.NewLanguageService(project.ConfigFilePath(), project.GetProgram(), snapshot, sourceFile.FileName())
symbol := languageService.GetSymbolAtLocation(ctx, node)
if symbol == nil {
return nil, nil
Expand Down Expand Up @@ -234,7 +234,7 @@ func (api *API) GetTypeOfSymbol(ctx context.Context, projectId Handle[project.Pr
if !ok {
return nil, fmt.Errorf("symbol %q not found", symbolHandle)
}
languageService := ls.NewLanguageService(project.ConfigFilePath(), project.GetProgram(), snapshot)
languageService := ls.NewLanguageService(project.ConfigFilePath(), project.GetProgram(), snapshot, "")
t := languageService.GetTypeOfSymbol(ctx, symbol)
if t == nil {
return nil, nil
Expand Down
3 changes: 2 additions & 1 deletion internal/execute/tsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/microsoft/typescript-go/internal/format"
"github.com/microsoft/typescript-go/internal/jsonutil"
"github.com/microsoft/typescript-go/internal/locale"
"github.com/microsoft/typescript-go/internal/ls/lsutil"
"github.com/microsoft/typescript-go/internal/parser"
"github.com/microsoft/typescript-go/internal/pprof"
"github.com/microsoft/typescript-go/internal/tsoptions"
Expand All @@ -36,7 +37,7 @@ func CommandLine(sys tsc.System, commandLineArgs []string, testing tsc.CommandLi
}

func fmtMain(sys tsc.System, input, output string) tsc.ExitStatus {
ctx := format.WithFormatCodeSettings(context.Background(), format.GetDefaultFormatCodeSettings("\n"), "\n")
ctx := format.WithFormatCodeSettings(context.Background(), lsutil.GetDefaultFormatCodeSettings(), "\n")
input = string(tspath.ToPath(input, sys.GetCurrentDirectory(), sys.FS().UseCaseSensitiveFileNames()))
output = string(tspath.ToPath(output, sys.GetCurrentDirectory(), sys.FS().UseCaseSensitiveFileNames()))
fileContent, ok := sys.FS().ReadFile(input)
Expand Down
11 changes: 7 additions & 4 deletions internal/format/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/ls/lsutil"
"github.com/microsoft/typescript-go/internal/scanner"
"github.com/microsoft/typescript-go/internal/stringutil"
)
Expand All @@ -28,16 +29,18 @@ const (
formatNewlineKey
)

func WithFormatCodeSettings(ctx context.Context, options *FormatCodeSettings, newLine string) context.Context {
func WithFormatCodeSettings(ctx context.Context, options *lsutil.FormatCodeSettings, newLine string) context.Context {
ctx = context.WithValue(ctx, formatOptionsKey, options)
ctx = context.WithValue(ctx, formatNewlineKey, newLine)
// In strada, the rules map was both globally cached *and* cached into the context, for some reason. We skip that here and just use the global one.
return ctx
}

func GetFormatCodeSettingsFromContext(ctx context.Context) *FormatCodeSettings {
opt := ctx.Value(formatOptionsKey).(*FormatCodeSettings)
return opt
func GetFormatCodeSettingsFromContext(ctx context.Context) *lsutil.FormatCodeSettings {
if opt := ctx.Value(formatOptionsKey); opt != nil {
return opt.(*lsutil.FormatCodeSettings)
}
return nil
}

func GetNewLineOrDefaultFromContext(ctx context.Context) string { // TODO: Move into broader LS - more than just the formatter uses the newline editor setting/host new line
Expand Down
13 changes: 7 additions & 6 deletions internal/format/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/format"
"github.com/microsoft/typescript-go/internal/ls/lsutil"
"github.com/microsoft/typescript-go/internal/parser"
"github.com/microsoft/typescript-go/internal/printer"
"github.com/microsoft/typescript-go/internal/repo"
Expand Down Expand Up @@ -38,14 +39,14 @@ func TestFormat(t *testing.T) {

t.Run("format checker.ts", func(t *testing.T) {
t.Parallel()
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
EditorSettings: format.EditorSettings{
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
EditorSettings: lsutil.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 4,
NewLineCharacter: "\n",
ConvertTabsToSpaces: true,
IndentStyle: format.IndentStyleSmart,
IndentStyle: lsutil.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
Expand All @@ -67,14 +68,14 @@ func TestFormat(t *testing.T) {
}

func BenchmarkFormat(b *testing.B) {
ctx := format.WithFormatCodeSettings(b.Context(), &format.FormatCodeSettings{
EditorSettings: format.EditorSettings{
ctx := format.WithFormatCodeSettings(b.Context(), &lsutil.FormatCodeSettings{
EditorSettings: lsutil.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 4,
NewLineCharacter: "\n",
ConvertTabsToSpaces: true,
IndentStyle: format.IndentStyleSmart,
IndentStyle: lsutil.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
Expand Down
37 changes: 19 additions & 18 deletions internal/format/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/format"
"github.com/microsoft/typescript-go/internal/ls/lsutil"
"github.com/microsoft/typescript-go/internal/parser"
"gotest.tools/v3/assert"
)
Expand All @@ -16,14 +17,14 @@ func TestCommentFormatting(t *testing.T) {

t.Run("format comment issue reproduction", func(t *testing.T) {
t.Parallel()
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
EditorSettings: format.EditorSettings{
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
EditorSettings: lsutil.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 4,
NewLineCharacter: "\n",
ConvertTabsToSpaces: true,
IndentStyle: format.IndentStyleSmart,
IndentStyle: lsutil.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
Expand Down Expand Up @@ -67,14 +68,14 @@ func TestCommentFormatting(t *testing.T) {

t.Run("format JSDoc with tab indentation", func(t *testing.T) {
t.Parallel()
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
EditorSettings: format.EditorSettings{
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
EditorSettings: lsutil.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 0,
NewLineCharacter: "\n",
ConvertTabsToSpaces: false, // Use tabs
IndentStyle: format.IndentStyleSmart,
IndentStyle: lsutil.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
Expand Down Expand Up @@ -104,14 +105,14 @@ func TestCommentFormatting(t *testing.T) {

t.Run("format comment inside multi-line argument list", func(t *testing.T) {
t.Parallel()
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
EditorSettings: format.EditorSettings{
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
EditorSettings: lsutil.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 0,
NewLineCharacter: "\n",
ConvertTabsToSpaces: false, // Use tabs
IndentStyle: format.IndentStyleSmart,
IndentStyle: lsutil.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
Expand All @@ -137,14 +138,14 @@ func TestCommentFormatting(t *testing.T) {

t.Run("format comment in chained method calls", func(t *testing.T) {
t.Parallel()
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
EditorSettings: format.EditorSettings{
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
EditorSettings: lsutil.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 0,
NewLineCharacter: "\n",
ConvertTabsToSpaces: false, // Use tabs
IndentStyle: format.IndentStyleSmart,
IndentStyle: lsutil.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
Expand All @@ -171,14 +172,14 @@ func TestCommentFormatting(t *testing.T) {
// Regression test for issue #1928 - panic when formatting chained method call with comment
t.Run("format chained method call with comment (issue #1928)", func(t *testing.T) {
t.Parallel()
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
EditorSettings: format.EditorSettings{
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
EditorSettings: lsutil.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 0,
NewLineCharacter: "\n",
ConvertTabsToSpaces: false, // Use tabs
IndentStyle: format.IndentStyleSmart,
IndentStyle: lsutil.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
Expand Down Expand Up @@ -208,14 +209,14 @@ func TestSliceBoundsPanic(t *testing.T) {

t.Run("format code with trailing semicolon should not panic", func(t *testing.T) {
t.Parallel()
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
EditorSettings: format.EditorSettings{
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
EditorSettings: lsutil.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 4,
NewLineCharacter: "\n",
ConvertTabsToSpaces: true,
IndentStyle: format.IndentStyleSmart,
IndentStyle: lsutil.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
Expand Down
84 changes: 3 additions & 81 deletions internal/format/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,88 +3,10 @@ package format
import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/ls/lsutil"
"github.com/microsoft/typescript-go/internal/scanner"
)

type IndentStyle int

const (
IndentStyleNone IndentStyle = iota
IndentStyleBlock
IndentStyleSmart
)

type SemicolonPreference string

const (
SemicolonPreferenceIgnore SemicolonPreference = "ignore"
SemicolonPreferenceInsert SemicolonPreference = "insert"
SemicolonPreferenceRemove SemicolonPreference = "remove"
)

type EditorSettings struct {
BaseIndentSize int
IndentSize int
TabSize int
NewLineCharacter string
ConvertTabsToSpaces bool
IndentStyle IndentStyle
TrimTrailingWhitespace bool
}

type FormatCodeSettings struct {
EditorSettings
InsertSpaceAfterCommaDelimiter core.Tristate
InsertSpaceAfterSemicolonInForStatements core.Tristate
InsertSpaceBeforeAndAfterBinaryOperators core.Tristate
InsertSpaceAfterConstructor core.Tristate
InsertSpaceAfterKeywordsInControlFlowStatements core.Tristate
InsertSpaceAfterFunctionKeywordForAnonymousFunctions core.Tristate
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis core.Tristate
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets core.Tristate
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces core.Tristate
InsertSpaceAfterOpeningAndBeforeClosingEmptyBraces core.Tristate
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces core.Tristate
InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces core.Tristate
InsertSpaceAfterTypeAssertion core.Tristate
InsertSpaceBeforeFunctionParenthesis core.Tristate
PlaceOpenBraceOnNewLineForFunctions core.Tristate
PlaceOpenBraceOnNewLineForControlBlocks core.Tristate
InsertSpaceBeforeTypeAnnotation core.Tristate
IndentMultiLineObjectLiteralBeginningOnBlankLine core.Tristate
Semicolons SemicolonPreference
IndentSwitchCase core.Tristate
}

func GetDefaultFormatCodeSettings(newLineCharacter string) *FormatCodeSettings {
return &FormatCodeSettings{
EditorSettings: EditorSettings{
IndentSize: 4,
TabSize: 4,
NewLineCharacter: newLineCharacter,
ConvertTabsToSpaces: true,
IndentStyle: IndentStyleSmart,
TrimTrailingWhitespace: true,
},
InsertSpaceAfterConstructor: core.TSFalse,
InsertSpaceAfterCommaDelimiter: core.TSTrue,
InsertSpaceAfterSemicolonInForStatements: core.TSTrue,
InsertSpaceBeforeAndAfterBinaryOperators: core.TSTrue,
InsertSpaceAfterKeywordsInControlFlowStatements: core.TSTrue,
InsertSpaceAfterFunctionKeywordForAnonymousFunctions: core.TSFalse,
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: core.TSFalse,
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: core.TSFalse,
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: core.TSTrue,
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: core.TSFalse,
InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: core.TSFalse,
InsertSpaceBeforeFunctionParenthesis: core.TSFalse,
PlaceOpenBraceOnNewLineForFunctions: core.TSFalse,
PlaceOpenBraceOnNewLineForControlBlocks: core.TSFalse,
Semicolons: SemicolonPreferenceIgnore,
IndentSwitchCase: core.TSTrue,
}
}

type FormattingContext struct {
currentTokenSpan TextRangeWithKind
nextTokenSpan TextRangeWithKind
Expand All @@ -100,12 +22,12 @@ type FormattingContext struct {

SourceFile *ast.SourceFile
FormattingRequestKind FormatRequestKind
Options *FormatCodeSettings
Options *lsutil.FormatCodeSettings

scanner *scanner.Scanner
}

func NewFormattingContext(file *ast.SourceFile, kind FormatRequestKind, options *FormatCodeSettings) *FormattingContext {
func NewFormattingContext(file *ast.SourceFile, kind FormatRequestKind, options *lsutil.FormatCodeSettings) *FormattingContext {
res := &FormattingContext{
SourceFile: file,
FormattingRequestKind: kind,
Expand Down
7 changes: 4 additions & 3 deletions internal/format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/format"
"github.com/microsoft/typescript-go/internal/ls/lsutil"
"github.com/microsoft/typescript-go/internal/parser"
"gotest.tools/v3/assert"
)
Expand All @@ -30,14 +31,14 @@ func TestFormatNoTrailingNewline(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
EditorSettings: format.EditorSettings{
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
EditorSettings: lsutil.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 4,
NewLineCharacter: "\n",
ConvertTabsToSpaces: true,
IndentStyle: format.IndentStyleSmart,
IndentStyle: lsutil.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
}, "\n")
Expand Down
Loading