|
| 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