Skip to content

Commit ecddfc4

Browse files
committed
format options implementation
1 parent 2459249 commit ecddfc4

23 files changed

+590
-387
lines changed

internal/execute/tsc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/microsoft/typescript-go/internal/execute/tsc"
1616
"github.com/microsoft/typescript-go/internal/format"
1717
"github.com/microsoft/typescript-go/internal/jsonutil"
18+
"github.com/microsoft/typescript-go/internal/ls/lsutil"
1819
"github.com/microsoft/typescript-go/internal/parser"
1920
"github.com/microsoft/typescript-go/internal/pprof"
2021
"github.com/microsoft/typescript-go/internal/tsoptions"
@@ -36,7 +37,7 @@ func CommandLine(sys tsc.System, commandLineArgs []string, testing tsc.CommandLi
3637
}
3738

3839
func fmtMain(sys tsc.System, input, output string) tsc.ExitStatus {
39-
ctx := format.WithFormatCodeSettings(context.Background(), format.GetDefaultFormatCodeSettings("\n"), "\n")
40+
ctx := format.WithFormatCodeSettings(context.Background(), lsutil.GetDefaultFormatCodeSettings(), "\n")
4041
input = string(tspath.ToPath(input, sys.GetCurrentDirectory(), sys.FS().UseCaseSensitiveFileNames()))
4142
output = string(tspath.ToPath(output, sys.GetCurrentDirectory(), sys.FS().UseCaseSensitiveFileNames()))
4243
fileContent, ok := sys.FS().ReadFile(input)

internal/format/api.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/microsoft/typescript-go/internal/ast"
88
"github.com/microsoft/typescript-go/internal/core"
9+
"github.com/microsoft/typescript-go/internal/ls/lsutil"
910
"github.com/microsoft/typescript-go/internal/scanner"
1011
"github.com/microsoft/typescript-go/internal/stringutil"
1112
)
@@ -28,16 +29,18 @@ const (
2829
formatNewlineKey
2930
)
3031

31-
func WithFormatCodeSettings(ctx context.Context, options *FormatCodeSettings, newLine string) context.Context {
32+
func WithFormatCodeSettings(ctx context.Context, options *lsutil.FormatCodeSettings, newLine string) context.Context {
3233
ctx = context.WithValue(ctx, formatOptionsKey, options)
3334
ctx = context.WithValue(ctx, formatNewlineKey, newLine)
3435
// 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.
3536
return ctx
3637
}
3738

38-
func GetFormatCodeSettingsFromContext(ctx context.Context) *FormatCodeSettings {
39-
opt := ctx.Value(formatOptionsKey).(*FormatCodeSettings)
40-
return opt
39+
func GetFormatCodeSettingsFromContext(ctx context.Context) *lsutil.FormatCodeSettings {
40+
if opt := ctx.Value(formatOptionsKey); opt != nil {
41+
return opt.(*lsutil.FormatCodeSettings)
42+
}
43+
return nil
4144
}
4245

4346
func GetNewLineOrDefaultFromContext(ctx context.Context) string { // TODO: Move into broader LS - more than just the formatter uses the newline editor setting/host new line

internal/format/api_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/microsoft/typescript-go/internal/ast"
1010
"github.com/microsoft/typescript-go/internal/core"
1111
"github.com/microsoft/typescript-go/internal/format"
12+
"github.com/microsoft/typescript-go/internal/ls/lsutil"
1213
"github.com/microsoft/typescript-go/internal/parser"
1314
"github.com/microsoft/typescript-go/internal/printer"
1415
"github.com/microsoft/typescript-go/internal/repo"
@@ -38,14 +39,14 @@ func TestFormat(t *testing.T) {
3839

3940
t.Run("format checker.ts", func(t *testing.T) {
4041
t.Parallel()
41-
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
42-
EditorSettings: format.EditorSettings{
42+
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
43+
EditorSettings: lsutil.EditorSettings{
4344
TabSize: 4,
4445
IndentSize: 4,
4546
BaseIndentSize: 4,
4647
NewLineCharacter: "\n",
4748
ConvertTabsToSpaces: true,
48-
IndentStyle: format.IndentStyleSmart,
49+
IndentStyle: lsutil.IndentStyleSmart,
4950
TrimTrailingWhitespace: true,
5051
},
5152
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
@@ -67,14 +68,14 @@ func TestFormat(t *testing.T) {
6768
}
6869

6970
func BenchmarkFormat(b *testing.B) {
70-
ctx := format.WithFormatCodeSettings(b.Context(), &format.FormatCodeSettings{
71-
EditorSettings: format.EditorSettings{
71+
ctx := format.WithFormatCodeSettings(b.Context(), &lsutil.FormatCodeSettings{
72+
EditorSettings: lsutil.EditorSettings{
7273
TabSize: 4,
7374
IndentSize: 4,
7475
BaseIndentSize: 4,
7576
NewLineCharacter: "\n",
7677
ConvertTabsToSpaces: true,
77-
IndentStyle: format.IndentStyleSmart,
78+
IndentStyle: lsutil.IndentStyleSmart,
7879
TrimTrailingWhitespace: true,
7980
},
8081
InsertSpaceBeforeTypeAnnotation: core.TSTrue,

internal/format/comment_test.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/microsoft/typescript-go/internal/ast"
88
"github.com/microsoft/typescript-go/internal/core"
99
"github.com/microsoft/typescript-go/internal/format"
10+
"github.com/microsoft/typescript-go/internal/ls/lsutil"
1011
"github.com/microsoft/typescript-go/internal/parser"
1112
"gotest.tools/v3/assert"
1213
)
@@ -16,14 +17,14 @@ func TestCommentFormatting(t *testing.T) {
1617

1718
t.Run("format comment issue reproduction", func(t *testing.T) {
1819
t.Parallel()
19-
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
20-
EditorSettings: format.EditorSettings{
20+
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
21+
EditorSettings: lsutil.EditorSettings{
2122
TabSize: 4,
2223
IndentSize: 4,
2324
BaseIndentSize: 4,
2425
NewLineCharacter: "\n",
2526
ConvertTabsToSpaces: true,
26-
IndentStyle: format.IndentStyleSmart,
27+
IndentStyle: lsutil.IndentStyleSmart,
2728
TrimTrailingWhitespace: true,
2829
},
2930
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
@@ -67,14 +68,14 @@ func TestCommentFormatting(t *testing.T) {
6768

6869
t.Run("format JSDoc with tab indentation", func(t *testing.T) {
6970
t.Parallel()
70-
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
71-
EditorSettings: format.EditorSettings{
71+
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
72+
EditorSettings: lsutil.EditorSettings{
7273
TabSize: 4,
7374
IndentSize: 4,
7475
BaseIndentSize: 0,
7576
NewLineCharacter: "\n",
7677
ConvertTabsToSpaces: false, // Use tabs
77-
IndentStyle: format.IndentStyleSmart,
78+
IndentStyle: lsutil.IndentStyleSmart,
7879
TrimTrailingWhitespace: true,
7980
},
8081
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
@@ -104,14 +105,14 @@ func TestCommentFormatting(t *testing.T) {
104105

105106
t.Run("format comment inside multi-line argument list", func(t *testing.T) {
106107
t.Parallel()
107-
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
108-
EditorSettings: format.EditorSettings{
108+
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
109+
EditorSettings: lsutil.EditorSettings{
109110
TabSize: 4,
110111
IndentSize: 4,
111112
BaseIndentSize: 0,
112113
NewLineCharacter: "\n",
113114
ConvertTabsToSpaces: false, // Use tabs
114-
IndentStyle: format.IndentStyleSmart,
115+
IndentStyle: lsutil.IndentStyleSmart,
115116
TrimTrailingWhitespace: true,
116117
},
117118
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
@@ -137,14 +138,14 @@ func TestCommentFormatting(t *testing.T) {
137138

138139
t.Run("format comment in chained method calls", func(t *testing.T) {
139140
t.Parallel()
140-
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
141-
EditorSettings: format.EditorSettings{
141+
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
142+
EditorSettings: lsutil.EditorSettings{
142143
TabSize: 4,
143144
IndentSize: 4,
144145
BaseIndentSize: 0,
145146
NewLineCharacter: "\n",
146147
ConvertTabsToSpaces: false, // Use tabs
147-
IndentStyle: format.IndentStyleSmart,
148+
IndentStyle: lsutil.IndentStyleSmart,
148149
TrimTrailingWhitespace: true,
149150
},
150151
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
@@ -171,14 +172,14 @@ func TestCommentFormatting(t *testing.T) {
171172
// Regression test for issue #1928 - panic when formatting chained method call with comment
172173
t.Run("format chained method call with comment (issue #1928)", func(t *testing.T) {
173174
t.Parallel()
174-
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
175-
EditorSettings: format.EditorSettings{
175+
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
176+
EditorSettings: lsutil.EditorSettings{
176177
TabSize: 4,
177178
IndentSize: 4,
178179
BaseIndentSize: 0,
179180
NewLineCharacter: "\n",
180181
ConvertTabsToSpaces: false, // Use tabs
181-
IndentStyle: format.IndentStyleSmart,
182+
IndentStyle: lsutil.IndentStyleSmart,
182183
TrimTrailingWhitespace: true,
183184
},
184185
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
@@ -208,14 +209,14 @@ func TestSliceBoundsPanic(t *testing.T) {
208209

209210
t.Run("format code with trailing semicolon should not panic", func(t *testing.T) {
210211
t.Parallel()
211-
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
212-
EditorSettings: format.EditorSettings{
212+
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
213+
EditorSettings: lsutil.EditorSettings{
213214
TabSize: 4,
214215
IndentSize: 4,
215216
BaseIndentSize: 4,
216217
NewLineCharacter: "\n",
217218
ConvertTabsToSpaces: true,
218-
IndentStyle: format.IndentStyleSmart,
219+
IndentStyle: lsutil.IndentStyleSmart,
219220
TrimTrailingWhitespace: true,
220221
},
221222
InsertSpaceBeforeTypeAnnotation: core.TSTrue,

internal/format/context.go

Lines changed: 3 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,10 @@ package format
33
import (
44
"github.com/microsoft/typescript-go/internal/ast"
55
"github.com/microsoft/typescript-go/internal/core"
6+
"github.com/microsoft/typescript-go/internal/ls/lsutil"
67
"github.com/microsoft/typescript-go/internal/scanner"
78
)
89

9-
type IndentStyle int
10-
11-
const (
12-
IndentStyleNone IndentStyle = iota
13-
IndentStyleBlock
14-
IndentStyleSmart
15-
)
16-
17-
type SemicolonPreference string
18-
19-
const (
20-
SemicolonPreferenceIgnore SemicolonPreference = "ignore"
21-
SemicolonPreferenceInsert SemicolonPreference = "insert"
22-
SemicolonPreferenceRemove SemicolonPreference = "remove"
23-
)
24-
25-
type EditorSettings struct {
26-
BaseIndentSize int
27-
IndentSize int
28-
TabSize int
29-
NewLineCharacter string
30-
ConvertTabsToSpaces bool
31-
IndentStyle IndentStyle
32-
TrimTrailingWhitespace bool
33-
}
34-
35-
type FormatCodeSettings struct {
36-
EditorSettings
37-
InsertSpaceAfterCommaDelimiter core.Tristate
38-
InsertSpaceAfterSemicolonInForStatements core.Tristate
39-
InsertSpaceBeforeAndAfterBinaryOperators core.Tristate
40-
InsertSpaceAfterConstructor core.Tristate
41-
InsertSpaceAfterKeywordsInControlFlowStatements core.Tristate
42-
InsertSpaceAfterFunctionKeywordForAnonymousFunctions core.Tristate
43-
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis core.Tristate
44-
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets core.Tristate
45-
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces core.Tristate
46-
InsertSpaceAfterOpeningAndBeforeClosingEmptyBraces core.Tristate
47-
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces core.Tristate
48-
InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces core.Tristate
49-
InsertSpaceAfterTypeAssertion core.Tristate
50-
InsertSpaceBeforeFunctionParenthesis core.Tristate
51-
PlaceOpenBraceOnNewLineForFunctions core.Tristate
52-
PlaceOpenBraceOnNewLineForControlBlocks core.Tristate
53-
InsertSpaceBeforeTypeAnnotation core.Tristate
54-
IndentMultiLineObjectLiteralBeginningOnBlankLine core.Tristate
55-
Semicolons SemicolonPreference
56-
IndentSwitchCase core.Tristate
57-
}
58-
59-
func GetDefaultFormatCodeSettings(newLineCharacter string) *FormatCodeSettings {
60-
return &FormatCodeSettings{
61-
EditorSettings: EditorSettings{
62-
IndentSize: 4,
63-
TabSize: 4,
64-
NewLineCharacter: newLineCharacter,
65-
ConvertTabsToSpaces: true,
66-
IndentStyle: IndentStyleSmart,
67-
TrimTrailingWhitespace: true,
68-
},
69-
InsertSpaceAfterConstructor: core.TSFalse,
70-
InsertSpaceAfterCommaDelimiter: core.TSTrue,
71-
InsertSpaceAfterSemicolonInForStatements: core.TSTrue,
72-
InsertSpaceBeforeAndAfterBinaryOperators: core.TSTrue,
73-
InsertSpaceAfterKeywordsInControlFlowStatements: core.TSTrue,
74-
InsertSpaceAfterFunctionKeywordForAnonymousFunctions: core.TSFalse,
75-
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: core.TSFalse,
76-
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: core.TSFalse,
77-
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: core.TSTrue,
78-
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: core.TSFalse,
79-
InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: core.TSFalse,
80-
InsertSpaceBeforeFunctionParenthesis: core.TSFalse,
81-
PlaceOpenBraceOnNewLineForFunctions: core.TSFalse,
82-
PlaceOpenBraceOnNewLineForControlBlocks: core.TSFalse,
83-
Semicolons: SemicolonPreferenceIgnore,
84-
IndentSwitchCase: core.TSTrue,
85-
}
86-
}
87-
8810
type FormattingContext struct {
8911
currentTokenSpan TextRangeWithKind
9012
nextTokenSpan TextRangeWithKind
@@ -100,12 +22,12 @@ type FormattingContext struct {
10022

10123
SourceFile *ast.SourceFile
10224
FormattingRequestKind FormatRequestKind
103-
Options *FormatCodeSettings
25+
Options *lsutil.FormatCodeSettings
10426

10527
scanner *scanner.Scanner
10628
}
10729

108-
func NewFormattingContext(file *ast.SourceFile, kind FormatRequestKind, options *FormatCodeSettings) *FormattingContext {
30+
func NewFormattingContext(file *ast.SourceFile, kind FormatRequestKind, options *lsutil.FormatCodeSettings) *FormattingContext {
10931
res := &FormattingContext{
11032
SourceFile: file,
11133
FormattingRequestKind: kind,

internal/format/format_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/microsoft/typescript-go/internal/ast"
88
"github.com/microsoft/typescript-go/internal/core"
99
"github.com/microsoft/typescript-go/internal/format"
10+
"github.com/microsoft/typescript-go/internal/ls/lsutil"
1011
"github.com/microsoft/typescript-go/internal/parser"
1112
"gotest.tools/v3/assert"
1213
)
@@ -30,14 +31,14 @@ func TestFormatNoTrailingNewline(t *testing.T) {
3031
for _, tc := range testCases {
3132
t.Run(tc.name, func(t *testing.T) {
3233
t.Parallel()
33-
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
34-
EditorSettings: format.EditorSettings{
34+
ctx := format.WithFormatCodeSettings(t.Context(), &lsutil.FormatCodeSettings{
35+
EditorSettings: lsutil.EditorSettings{
3536
TabSize: 4,
3637
IndentSize: 4,
3738
BaseIndentSize: 4,
3839
NewLineCharacter: "\n",
3940
ConvertTabsToSpaces: true,
40-
IndentStyle: format.IndentStyleSmart,
41+
IndentStyle: lsutil.IndentStyleSmart,
4142
TrimTrailingWhitespace: true,
4243
},
4344
}, "\n")

0 commit comments

Comments
 (0)