Skip to content

Commit bba6379

Browse files
authored
Port formatter, enable in LSP (#1052)
1 parent ddb1ebb commit bba6379

30 files changed

+4734
-274
lines changed

internal/ast/kind.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,6 @@ const (
423423
KindFirstContextualKeyword = KindAbstractKeyword
424424
KindLastContextualKeyword = KindOfKeyword
425425
KindComment = KindSingleLineCommentTrivia | KindMultiLineCommentTrivia
426+
KindFirstTriviaToken = KindSingleLineCommentTrivia
427+
KindLastTriviaToken = KindConflictMarkerTrivia
426428
)

internal/ast/utilities.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ func IsModifier(node *Node) bool {
126126
return IsModifierKind(node.Kind)
127127
}
128128

129+
func IsModifierLike(node *Node) bool {
130+
return IsModifier(node) || IsDecorator(node)
131+
}
132+
129133
func IsKeywordKind(token Kind) bool {
130134
return KindFirstKeyword <= token && token <= KindLastKeyword
131135
}
@@ -3491,3 +3495,11 @@ func IsInitializedProperty(member *ClassElement) bool {
34913495
return member.Kind == KindPropertyDeclaration &&
34923496
member.Initializer() != nil
34933497
}
3498+
3499+
func IsTrivia(token Kind) bool {
3500+
return KindFirstTriviaToken <= token && token <= KindLastTriviaToken
3501+
}
3502+
3503+
func HasDecorators(node *Node) bool {
3504+
return HasSyntacticModifier(node, ModifierFlagsDecorator)
3505+
}

internal/checker/checker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5672,7 +5672,7 @@ func (c *Checker) checkVarDeclaredNamesNotShadowed(node *ast.Node) {
56725672
func (c *Checker) checkDecorators(node *ast.Node) {
56735673
// skip this check for nodes that cannot have decorators. These should have already had an error reported by
56745674
// checkGrammarModifiers.
5675-
if !ast.CanHaveDecorators(node) || !hasDecorators(node) || !nodeCanBeDecorated(c.legacyDecorators, node, node.Parent, node.Parent.Parent) {
5675+
if !ast.CanHaveDecorators(node) || !ast.HasDecorators(node) || !nodeCanBeDecorated(c.legacyDecorators, node, node.Parent, node.Parent.Parent) {
56765676
return
56775677
}
56785678
firstDecorator := core.Find(node.ModifierNodes(), ast.IsDecorator)
@@ -11639,7 +11639,7 @@ func (c *Checker) getThisTypeOfDeclaration(declaration *ast.Node) *Type {
1163911639
func (c *Checker) checkThisInStaticClassFieldInitializerInDecoratedClass(thisExpression *ast.Node, container *ast.Node) {
1164011640
if ast.IsPropertyDeclaration(container) && ast.HasStaticModifier(container) && c.legacyDecorators {
1164111641
initializer := container.Initializer()
11642-
if initializer != nil && initializer.Loc.ContainsInclusive(thisExpression.Pos()) && hasDecorators(container.Parent) {
11642+
if initializer != nil && initializer.Loc.ContainsInclusive(thisExpression.Pos()) && ast.HasDecorators(container.Parent) {
1164311643
c.error(thisExpression, diagnostics.Cannot_use_this_in_a_static_property_initializer_of_a_decorated_class)
1164411644
}
1164511645
}
@@ -26816,7 +26816,7 @@ func (c *Checker) markLinkedReferences(location *ast.Node, hint ReferenceHint, p
2681626816
if !c.compilerOptions.EmitDecoratorMetadata.IsTrue() {
2681726817
return
2681826818
}
26819-
if !ast.CanHaveDecorators(location) || !hasDecorators(location) || location.Modifiers() == nil || !nodeCanBeDecorated(c.legacyDecorators, location, location.Parent, location.Parent.Parent) {
26819+
if !ast.CanHaveDecorators(location) || !ast.HasDecorators(location) || location.Modifiers() == nil || !nodeCanBeDecorated(c.legacyDecorators, location, location.Parent, location.Parent.Parent) {
2682026820
return
2682126821
}
2682226822

internal/checker/grammarchecks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (c *Checker) checkGrammarModifiers(node *ast.Node /*Union[HasModifiers, Has
233233
}
234234
} else if c.legacyDecorators && (node.Kind == ast.KindGetAccessor || node.Kind == ast.KindSetAccessor) {
235235
accessors := c.getAllAccessorDeclarationsForDeclaration(node)
236-
if hasDecorators(accessors.firstAccessor) && node == accessors.secondAccessor {
236+
if ast.HasDecorators(accessors.firstAccessor) && node == accessors.secondAccessor {
237237
return c.grammarErrorOnFirstToken(node, diagnostics.Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name)
238238
}
239239
}

internal/checker/utilities.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ func hasAsyncModifier(node *ast.Node) bool {
8383
return ast.HasSyntacticModifier(node, ast.ModifierFlagsAsync)
8484
}
8585

86-
func hasDecorators(node *ast.Node) bool {
87-
return ast.HasSyntacticModifier(node, ast.ModifierFlagsDecorator)
88-
}
89-
9086
func getSelectedModifierFlags(node *ast.Node, flags ast.ModifierFlags) ast.ModifierFlags {
9187
return node.ModifierFlags() & flags
9288
}

internal/core/text.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,13 @@ func (t TextRange) WithPos(pos int) TextRange {
5454
func (t TextRange) WithEnd(end int) TextRange {
5555
return TextRange{pos: t.pos, end: TextPos(end)}
5656
}
57+
58+
func (t TextRange) ContainedBy(t2 TextRange) bool {
59+
return t2.pos <= t.pos && t2.end >= t.end
60+
}
61+
62+
func (t TextRange) Overlaps(t2 TextRange) bool {
63+
start := max(t.pos, t2.pos)
64+
end := min(t.end, t2.end)
65+
return start < end
66+
}

internal/core/textchange.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package core
2+
3+
type TextChange struct {
4+
TextRange
5+
NewText string
6+
}
7+
8+
func (t TextChange) ApplyTo(text string) string {
9+
return text[:t.Pos()] + t.NewText + text[t.End():]
10+
}

internal/format/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# How does TypeScript formatting work?
2+
3+
To format code you need to have a formatting context and a `SourceFile`. The formatting context contains
4+
all user settings like tab size, newline character, etc.
5+
6+
The end result of formatting is represented by TextChange objects which hold the new string content, and
7+
the text to replace it with.
8+
9+
## Internals
10+
11+
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
12+
the sourcefile which should be formatted.
13+
14+
The formatSpan then uses a scanner (either with or without JSX support) which starts at the highest
15+
node the covers the span of text and recurses down through the node's children.
16+
17+
As it recurses, `processNode` is called on the children setting the indentation is decided and passed
18+
through into each of that node's children.
19+
20+
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`.
21+
22+
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.
23+
24+
### Where is this used?
25+
26+
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.

internal/format/api.go

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package format
2+
3+
import (
4+
"context"
5+
"unicode/utf8"
6+
7+
"github.com/microsoft/typescript-go/internal/ast"
8+
"github.com/microsoft/typescript-go/internal/core"
9+
"github.com/microsoft/typescript-go/internal/scanner"
10+
"github.com/microsoft/typescript-go/internal/stringutil"
11+
)
12+
13+
type FormatRequestKind int
14+
15+
const (
16+
FormatRequestKindFormatDocument FormatRequestKind = iota
17+
FormatRequestKindFormatSelection
18+
FormatRequestKindFormatOnEnter
19+
FormatRequestKindFormatOnSemicolon
20+
FormatRequestKindFormatOnOpeningCurlyBrace
21+
FormatRequestKindFormatOnClosingCurlyBrace
22+
)
23+
24+
type formatContextKey int
25+
26+
const (
27+
formatOptionsKey formatContextKey = iota
28+
formatNewlineKey
29+
)
30+
31+
func WithFormatCodeSettings(ctx context.Context, options *FormatCodeSettings, newLine string) context.Context {
32+
ctx = context.WithValue(ctx, formatOptionsKey, options)
33+
ctx = context.WithValue(ctx, formatNewlineKey, newLine)
34+
// 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.
35+
return ctx
36+
}
37+
38+
func GetFormatCodeSettingsFromContext(ctx context.Context) *FormatCodeSettings {
39+
opt := ctx.Value(formatOptionsKey).(*FormatCodeSettings)
40+
return opt
41+
}
42+
43+
func GetNewLineOrDefaultFromContext(ctx context.Context) string { // TODO: Move into broader LS - more than just the formatter uses the newline editor setting/host new line
44+
opt := GetFormatCodeSettingsFromContext(ctx)
45+
if opt != nil && len(opt.NewLineCharacter) > 0 {
46+
return opt.NewLineCharacter
47+
}
48+
host := ctx.Value(formatNewlineKey).(string)
49+
if len(host) > 0 {
50+
return host
51+
}
52+
return "\n"
53+
}
54+
55+
func FormatSpan(ctx context.Context, span core.TextRange, file *ast.SourceFile, kind FormatRequestKind) []core.TextChange {
56+
// find the smallest node that fully wraps the range and compute the initial indentation for the node
57+
enclosingNode := findEnclosingNode(span, file)
58+
opts := GetFormatCodeSettingsFromContext(ctx)
59+
60+
return newFormattingScanner(
61+
file.Text(),
62+
file.LanguageVariant,
63+
getScanStartPosition(enclosingNode, span, file),
64+
span.End(),
65+
newFormatSpanWorker(
66+
ctx,
67+
span,
68+
enclosingNode,
69+
GetIndentationForNode(enclosingNode, &span, file, opts),
70+
getOwnOrInheritedDelta(enclosingNode, opts, file),
71+
kind,
72+
prepareRangeContainsErrorFunction(file.Diagnostics(), span),
73+
file,
74+
),
75+
)
76+
}
77+
78+
func formatNodeLines(ctx context.Context, sourceFile *ast.SourceFile, node *ast.Node, requestKind FormatRequestKind) []core.TextChange {
79+
if node == nil {
80+
return nil
81+
}
82+
tokenStart := scanner.GetTokenPosOfNode(node, sourceFile, false)
83+
lineStart := getLineStartPositionForPosition(tokenStart, sourceFile)
84+
span := core.NewTextRange(lineStart, node.End())
85+
return FormatSpan(ctx, span, sourceFile, requestKind)
86+
}
87+
88+
func FormatDocument(ctx context.Context, sourceFile *ast.SourceFile) []core.TextChange {
89+
return FormatSpan(ctx, core.NewTextRange(0, sourceFile.End()), sourceFile, FormatRequestKindFormatDocument)
90+
}
91+
92+
func FormatSelection(ctx context.Context, sourceFile *ast.SourceFile, start int, end int) []core.TextChange {
93+
return FormatSpan(ctx, core.NewTextRange(getLineStartPositionForPosition(start, sourceFile), end), sourceFile, FormatRequestKindFormatSelection)
94+
}
95+
96+
func FormatOnOpeningCurly(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
97+
openingCurly := findImmediatelyPrecedingTokenOfKind(position, ast.KindOpenBraceToken, sourceFile)
98+
if openingCurly == nil {
99+
return nil
100+
}
101+
curlyBraceRange := openingCurly.Parent
102+
outermostNode := findOutermostNodeWithinListLevel(curlyBraceRange)
103+
/**
104+
* We limit the span to end at the opening curly to handle the case where
105+
* the brace matched to that just typed will be incorrect after further edits.
106+
* For example, we could type the opening curly for the following method
107+
* body without brace-matching activated:
108+
* ```
109+
* class C {
110+
* foo()
111+
* }
112+
* ```
113+
* and we wouldn't want to move the closing brace.
114+
*/
115+
textRange := core.NewTextRange(getLineStartPositionForPosition(scanner.GetTokenPosOfNode(outermostNode, sourceFile, false), sourceFile), position)
116+
return FormatSpan(ctx, textRange, sourceFile, FormatRequestKindFormatOnOpeningCurlyBrace)
117+
}
118+
119+
func FormatOnClosingCurly(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
120+
precedingToken := findImmediatelyPrecedingTokenOfKind(position, ast.KindCloseBraceToken, sourceFile)
121+
return formatNodeLines(ctx, sourceFile, findOutermostNodeWithinListLevel(precedingToken), FormatRequestKindFormatOnClosingCurlyBrace)
122+
}
123+
124+
func FormatOnSemicolon(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
125+
semicolon := findImmediatelyPrecedingTokenOfKind(position, ast.KindSemicolonToken, sourceFile)
126+
return formatNodeLines(ctx, sourceFile, findOutermostNodeWithinListLevel(semicolon), FormatRequestKindFormatOnSemicolon)
127+
}
128+
129+
func FormatOnEnter(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
130+
line, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, position)
131+
if line == 0 {
132+
return nil
133+
}
134+
// get start position for the previous line
135+
startPos := int(scanner.GetLineStarts(sourceFile)[line-1])
136+
// After the enter key, the cursor is now at a new line. The new line may or may not contain non-whitespace characters.
137+
// If the new line has only whitespaces, we won't want to format this line, because that would remove the indentation as
138+
// trailing whitespaces. So the end of the formatting span should be the later one between:
139+
// 1. the end of the previous line
140+
// 2. the last non-whitespace character in the current line
141+
endOfFormatSpan := scanner.GetEndLinePosition(sourceFile, line)
142+
for endOfFormatSpan > startPos {
143+
ch, s := utf8.DecodeRuneInString(sourceFile.Text()[endOfFormatSpan:])
144+
if s == 0 || stringutil.IsWhiteSpaceSingleLine(ch) { // on multibyte character keep backing up
145+
endOfFormatSpan--
146+
continue
147+
}
148+
break
149+
}
150+
151+
// 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
152+
// touch the current line at all. Also, on some OSes the line break consists of two characters (\r\n), we should test if the
153+
// previous character before the end of format span is line break character as well.
154+
ch, _ := utf8.DecodeRuneInString(sourceFile.Text()[endOfFormatSpan:])
155+
if stringutil.IsLineBreak(ch) {
156+
endOfFormatSpan--
157+
}
158+
159+
span := core.NewTextRange(
160+
startPos,
161+
// end value is exclusive so add 1 to the result
162+
endOfFormatSpan+1,
163+
)
164+
165+
return FormatSpan(ctx, span, sourceFile, FormatRequestKindFormatOnEnter)
166+
}

0 commit comments

Comments
 (0)