Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
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
Loading