Skip to content

Port formatter, enable in LSP #1052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions internal/ast/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,6 @@ const (
KindFirstContextualKeyword = KindAbstractKeyword
KindLastContextualKeyword = KindOfKeyword
KindComment = KindSingleLineCommentTrivia | KindMultiLineCommentTrivia
KindFirstTriviaToken = KindSingleLineCommentTrivia
KindLastTriviaToken = KindConflictMarkerTrivia
)
12 changes: 12 additions & 0 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func IsModifier(node *Node) bool {
return IsModifierKind(node.Kind)
}

func IsModifierLike(node *Node) bool {
return IsModifier(node) || IsDecorator(node)
}

func IsKeywordKind(token Kind) bool {
return KindFirstKeyword <= token && token <= KindLastKeyword
}
Expand Down Expand Up @@ -3471,3 +3475,11 @@ func IsInitializedProperty(member *ClassElement) bool {
return member.Kind == KindPropertyDeclaration &&
member.Initializer() != nil
}

func IsTrivia(token Kind) bool {
return KindFirstTriviaToken <= token && token <= KindLastTriviaToken
}

func HasDecorators(node *Node) bool {
return HasSyntacticModifier(node, ModifierFlagsDecorator)
}
6 changes: 3 additions & 3 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5654,7 +5654,7 @@ func (c *Checker) checkVarDeclaredNamesNotShadowed(node *ast.Node) {
func (c *Checker) checkDecorators(node *ast.Node) {
// skip this check for nodes that cannot have decorators. These should have already had an error reported by
// checkGrammarModifiers.
if !ast.CanHaveDecorators(node) || !hasDecorators(node) || !nodeCanBeDecorated(c.legacyDecorators, node, node.Parent, node.Parent.Parent) {
if !ast.CanHaveDecorators(node) || !ast.HasDecorators(node) || !nodeCanBeDecorated(c.legacyDecorators, node, node.Parent, node.Parent.Parent) {
return
}
firstDecorator := core.Find(node.ModifierNodes(), ast.IsDecorator)
Expand Down Expand Up @@ -11618,7 +11618,7 @@ func (c *Checker) getThisTypeOfDeclaration(declaration *ast.Node) *Type {
func (c *Checker) checkThisInStaticClassFieldInitializerInDecoratedClass(thisExpression *ast.Node, container *ast.Node) {
if ast.IsPropertyDeclaration(container) && ast.HasStaticModifier(container) && c.legacyDecorators {
initializer := container.Initializer()
if initializer != nil && initializer.Loc.ContainsInclusive(thisExpression.Pos()) && hasDecorators(container.Parent) {
if initializer != nil && initializer.Loc.ContainsInclusive(thisExpression.Pos()) && ast.HasDecorators(container.Parent) {
c.error(thisExpression, diagnostics.Cannot_use_this_in_a_static_property_initializer_of_a_decorated_class)
}
}
Expand Down Expand Up @@ -26786,7 +26786,7 @@ func (c *Checker) markLinkedReferences(location *ast.Node, hint ReferenceHint, p
if !c.compilerOptions.EmitDecoratorMetadata.IsTrue() {
return
}
if !ast.CanHaveDecorators(location) || !hasDecorators(location) || location.Modifiers() == nil || !nodeCanBeDecorated(c.legacyDecorators, location, location.Parent, location.Parent.Parent) {
if !ast.CanHaveDecorators(location) || !ast.HasDecorators(location) || location.Modifiers() == nil || !nodeCanBeDecorated(c.legacyDecorators, location, location.Parent, location.Parent.Parent) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion internal/checker/grammarchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (c *Checker) checkGrammarModifiers(node *ast.Node /*Union[HasModifiers, Has
}
} else if c.legacyDecorators && (node.Kind == ast.KindGetAccessor || node.Kind == ast.KindSetAccessor) {
accessors := c.getAllAccessorDeclarationsForDeclaration(node)
if hasDecorators(accessors.firstAccessor) && node == accessors.secondAccessor {
if ast.HasDecorators(accessors.firstAccessor) && node == accessors.secondAccessor {
return c.grammarErrorOnFirstToken(node, diagnostics.Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name)
}
}
Expand Down
4 changes: 0 additions & 4 deletions internal/checker/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ func hasAsyncModifier(node *ast.Node) bool {
return ast.HasSyntacticModifier(node, ast.ModifierFlagsAsync)
}

func hasDecorators(node *ast.Node) bool {
return ast.HasSyntacticModifier(node, ast.ModifierFlagsDecorator)
}

func getSelectedModifierFlags(node *ast.Node, flags ast.ModifierFlags) ast.ModifierFlags {
return node.ModifierFlags() & flags
}
Expand Down
10 changes: 10 additions & 0 deletions internal/core/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,13 @@ func (t TextRange) WithPos(pos int) TextRange {
func (t TextRange) WithEnd(end int) TextRange {
return TextRange{pos: t.pos, end: TextPos(end)}
}

func (t TextRange) ContainedBy(t2 TextRange) bool {
return t2.pos <= t.pos && t2.end >= t.end
}

func (t TextRange) Overlaps(t2 TextRange) bool {
start := max(t.pos, t2.pos)
end := min(t.end, t2.end)
return start < end
}
10 changes: 10 additions & 0 deletions internal/core/textchange.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core

type TextChange struct {
TextRange
NewText string
}

func (t TextChange) ApplyTo(text string) string {
return text[:t.Pos()] + t.NewText + text[t.End():]
}
26 changes: 26 additions & 0 deletions internal/format/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# How does TypeScript formatting work?

To format code you need to have a formatting context and a `SourceFile`. The formatting context contains
all user settings like tab size, newline character, etc.

The end result of formatting is represented by TextChange objects which hold the new string content, and
the text to replace it with.

## Internals

Most of the exposed APIs internally are `Format*` and they all set up and configure `FormatSpan` which could be considered the root call for formatting. Span in this case refers to the range of
the sourcefile which should be formatted.

The formatSpan then uses a scanner (either with or without JSX support) which starts at the highest
node the covers the span of text and recurses down through the node's children.

As it recurses, `processNode` is called on the children setting the indentation is decided and passed
through into each of that node's children.

The meat of formatting decisions is made via `processPair`, the pair here being the current node and the previous node. `processPair` which mutates the formatting context to represent the current place in the scanner and requests a set of rules which can be applied to the items via `createRulesMap`.

There are a lot of rules, which you can find in [rules.ts](./rules.ts) each one has a left and right reference to nodes or token ranges and note of what action should be applied by the formatter.

### Where is this used?

The formatter is used mainly from any language service operation that inserts or modifies code. The formatter is not exported publicly, and so all usage can only come through the language server.
161 changes: 161 additions & 0 deletions internal/format/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package format

import (
"context"
"unicode/utf8"

"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/scanner"
"github.com/microsoft/typescript-go/internal/stringutil"
)

type FormatRequestKind int

const (
FormatRequestKindFormatDocument FormatRequestKind = iota
FormatRequestKindFormatSelection
FormatRequestKindFormatOnEnter
FormatRequestKindFormatOnSemicolon
FormatRequestKindFormatOnOpeningCurlyBrace
FormatRequestKindFormatOnClosingCurlyBrace
)

type formatContextKey int

const (
formatOptionsKey formatContextKey = iota
formatNewlineKey
)

func NewContext(ctx context.Context, options *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 getNewLineOrDefaultFromContext(ctx context.Context) string { // TODO: Move into broader LS - more than just the formatter uses the newline editor setting/host new line
opt := ctx.Value(formatOptionsKey).(*FormatCodeSettings)
if opt != nil && len(opt.NewLineCharacter) > 0 {
return opt.NewLineCharacter
}
host := ctx.Value(formatNewlineKey).(string)
if len(host) > 0 {
return host
}
return "\n"
}

func FormatSpan(ctx context.Context, span core.TextRange, file *ast.SourceFile, kind FormatRequestKind) []core.TextChange {
// find the smallest node that fully wraps the range and compute the initial indentation for the node
enclosingNode := findEnclosingNode(span, file)
opts := ctx.Value(formatOptionsKey).(*FormatCodeSettings)

return newFormattingScanner(
file.Text(),
file.LanguageVariant,
getScanStartPosition(enclosingNode, span, file),
span.End(),
newFormatSpanWorker(
ctx,
span,
enclosingNode,
GetIndentationForNode(enclosingNode, &span, file, opts),
getOwnOrInheritedDelta(enclosingNode, opts, file),
kind,
prepareRangeContainsErrorFunction(file.Diagnostics(), span),
file,
),
)
}

func formatNodeLines(ctx context.Context, sourceFile *ast.SourceFile, node *ast.Node, requestKind FormatRequestKind) []core.TextChange {
if node == nil {
return nil
}
tokenStart := scanner.GetTokenPosOfNode(node, sourceFile, false)
lineStart := getLineStartPositionForPosition(tokenStart, sourceFile)
span := core.NewTextRange(lineStart, node.End())
return FormatSpan(ctx, span, sourceFile, requestKind)
}

func FormatDocument(ctx context.Context, sourceFile *ast.SourceFile) []core.TextChange {
return FormatSpan(ctx, core.NewTextRange(0, sourceFile.End()), sourceFile, FormatRequestKindFormatDocument)
}

func FormatSelection(ctx context.Context, sourceFile *ast.SourceFile, start int, end int) []core.TextChange {
return FormatSpan(ctx, core.NewTextRange(getLineStartPositionForPosition(start, sourceFile), end), sourceFile, FormatRequestKindFormatSelection)
}

func FormatOnOpeningCurly(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
openingCurly := findImmediatelyPrecedingTokenOfKind(position, ast.KindOpenBraceToken, sourceFile)
if openingCurly == nil {
return nil
}
curlyBraceRange := openingCurly.Parent
outermostNode := findOutermostNodeWithinListLevel(curlyBraceRange)
/**
* We limit the span to end at the opening curly to handle the case where
* the brace matched to that just typed will be incorrect after further edits.
* For example, we could type the opening curly for the following method
* body without brace-matching activated:
* ```
* class C {
* foo()
* }
* ```
* and we wouldn't want to move the closing brace.
*/
textRange := core.NewTextRange(getLineStartPositionForPosition(scanner.GetTokenPosOfNode(outermostNode, sourceFile, false), sourceFile), position)
return FormatSpan(ctx, textRange, sourceFile, FormatRequestKindFormatOnOpeningCurlyBrace)
}

func FormatOnClosingCurly(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
precedingToken := findImmediatelyPrecedingTokenOfKind(position, ast.KindCloseBraceToken, sourceFile)
return formatNodeLines(ctx, sourceFile, findOutermostNodeWithinListLevel(precedingToken), FormatRequestKindFormatOnClosingCurlyBrace)
}

func FormatOnSemicolon(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
semicolon := findImmediatelyPrecedingTokenOfKind(position, ast.KindSemicolonToken, sourceFile)
return formatNodeLines(ctx, sourceFile, findOutermostNodeWithinListLevel(semicolon), FormatRequestKindFormatOnSemicolon)
}

func FormatOnEnter(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
line, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, position)
if line == 0 {
return nil
}
// get start position for the previous line
startPos := int(scanner.GetLineStarts(sourceFile)[line-1])
// After the enter key, the cursor is now at a new line. The new line may or may not contain non-whitespace characters.
// If the new line has only whitespaces, we won't want to format this line, because that would remove the indentation as
// trailing whitespaces. So the end of the formatting span should be the later one between:
// 1. the end of the previous line
// 2. the last non-whitespace character in the current line
endOfFormatSpan := scanner.GetEndLinePosition(sourceFile, line)
for endOfFormatSpan > startPos {
ch, s := utf8.DecodeRuneInString(sourceFile.Text()[endOfFormatSpan:])
if s == 0 || stringutil.IsWhiteSpaceSingleLine(ch) { // on multibyte character keep backing up
endOfFormatSpan--
continue
}
break
}

// if the character at the end of the span is a line break, we shouldn't include it, because it indicates we don't want to
// touch the current line at all. Also, on some OSes the line break consists of two characters (\r\n), we should test if the
// previous character before the end of format span is line break character as well.
ch, _ := utf8.DecodeRuneInString(sourceFile.Text()[endOfFormatSpan:])
if stringutil.IsLineBreak(ch) {
endOfFormatSpan--
}

span := core.NewTextRange(
startPos,
// end value is exclusive so add 1 to the result
endOfFormatSpan+1,
)

return FormatSpan(ctx, span, sourceFile, FormatRequestKindFormatOnEnter)
}
Loading
Loading