diff --git a/kitcom/internal/tsgo/format/README.md b/kitcom/internal/tsgo/format/README.md deleted file mode 100644 index fe19d28..0000000 --- a/kitcom/internal/tsgo/format/README.md +++ /dev/null @@ -1,26 +0,0 @@ -# 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. diff --git a/kitcom/internal/tsgo/format/api.go b/kitcom/internal/tsgo/format/api.go deleted file mode 100644 index 22f35ac..0000000 --- a/kitcom/internal/tsgo/format/api.go +++ /dev/null @@ -1,185 +0,0 @@ -package format - -import ( - "context" - "unicode/utf8" - - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/core" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/scanner" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/stringutil" -) - -type FormatRequestKind int - -const ( - FormatRequestKindFormatDocument FormatRequestKind = iota - FormatRequestKindFormatSelection - FormatRequestKindFormatOnEnter - FormatRequestKindFormatOnSemicolon - FormatRequestKindFormatOnOpeningCurlyBrace - FormatRequestKindFormatOnClosingCurlyBrace -) - -type formatContextKey int - -const ( - formatOptionsKey formatContextKey = iota - formatNewlineKey -) - -func WithFormatCodeSettings(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 GetFormatCodeSettingsFromContext(ctx context.Context) *FormatCodeSettings { - opt := ctx.Value(formatOptionsKey).(*FormatCodeSettings) - return opt -} - -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 := GetFormatCodeSettingsFromContext(ctx) - 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 := GetFormatCodeSettingsFromContext(ctx) - - 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 FormatNodeGivenIndentation(ctx context.Context, node *ast.Node, file *ast.SourceFile, languageVariant core.LanguageVariant, initialIndentation int, delta int) []core.TextChange { - textRange := core.NewTextRange(node.Pos(), node.End()) - return newFormattingScanner( - file.Text(), - languageVariant, - textRange.Pos(), - textRange.End(), - newFormatSpanWorker( - ctx, - textRange, - node, - initialIndentation, - delta, - FormatRequestKindFormatSelection, - func(core.TextRange) bool { return false }, // assume that node does not have any errors - 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.GetECMALineAndCharacterOfPosition(sourceFile, position) - if line == 0 { - return nil - } - // get start position for the previous line - startPos := int(scanner.GetECMALineStarts(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.GetECMAEndLinePosition(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) -} diff --git a/kitcom/internal/tsgo/format/api_test.go b/kitcom/internal/tsgo/format/api_test.go deleted file mode 100644 index 9b5cfbc..0000000 --- a/kitcom/internal/tsgo/format/api_test.go +++ /dev/null @@ -1,114 +0,0 @@ -package format_test - -import ( - "os" - "path/filepath" - "strings" - "testing" - - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/core" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/format" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/parser" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/printer" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/repo" - "gotest.tools/v3/assert" -) - -func applyBulkEdits(text string, edits []core.TextChange) string { - b := strings.Builder{} - b.Grow(len(text)) - lastEnd := 0 - for _, e := range edits { - start := e.TextRange.Pos() - if start != lastEnd { - b.WriteString(text[lastEnd:e.TextRange.Pos()]) - } - b.WriteString(e.NewText) - - lastEnd = e.TextRange.End() - } - b.WriteString(text[lastEnd:]) - - return b.String() -} - -func TestFormat(t *testing.T) { - t.Parallel() - - t.Run("format checker.ts", func(t *testing.T) { - t.Parallel() - ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{ - EditorSettings: format.EditorSettings{ - TabSize: 4, - IndentSize: 4, - BaseIndentSize: 4, - NewLineCharacter: "\n", - ConvertTabsToSpaces: true, - IndentStyle: format.IndentStyleSmart, - TrimTrailingWhitespace: true, - }, - InsertSpaceBeforeTypeAnnotation: core.TSTrue, - }, "\n") - repo.SkipIfNoTypeScriptSubmodule(t) - filePath := filepath.Join(repo.TypeScriptSubmodulePath, "src/compiler/checker.ts") - fileContent, err := os.ReadFile(filePath) - assert.NilError(t, err) - text := string(fileContent) - sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{ - FileName: "/checker.ts", - Path: "/checker.ts", - }, text, core.ScriptKindTS) - edits := format.FormatDocument(ctx, sourceFile) - newText := applyBulkEdits(text, edits) - assert.Assert(t, len(newText) > 0) - assert.Assert(t, text != newText) - }) -} - -func BenchmarkFormat(b *testing.B) { - ctx := format.WithFormatCodeSettings(b.Context(), &format.FormatCodeSettings{ - EditorSettings: format.EditorSettings{ - TabSize: 4, - IndentSize: 4, - BaseIndentSize: 4, - NewLineCharacter: "\n", - ConvertTabsToSpaces: true, - IndentStyle: format.IndentStyleSmart, - TrimTrailingWhitespace: true, - }, - InsertSpaceBeforeTypeAnnotation: core.TSTrue, - }, "\n") - repo.SkipIfNoTypeScriptSubmodule(b) - filePath := filepath.Join(repo.TypeScriptSubmodulePath, "src/compiler/checker.ts") - fileContent, err := os.ReadFile(filePath) - assert.NilError(b, err) - text := string(fileContent) - sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{ - FileName: "/checker.ts", - Path: "/checker.ts", - }, text, core.ScriptKindTS) - - b.Run("format checker.ts", func(b *testing.B) { - for b.Loop() { - edits := format.FormatDocument(ctx, sourceFile) - newText := applyBulkEdits(text, edits) - assert.Assert(b, len(newText) > 0) - } - }) - - b.Run("format checker.ts (no edit application)", func(b *testing.B) { // for comparison (how long does applying many edits take?) - for b.Loop() { - edits := format.FormatDocument(ctx, sourceFile) - assert.Assert(b, len(edits) > 0) - } - }) - - p := printer.NewPrinter(printer.PrinterOptions{}, printer.PrintHandlers{}, printer.NewEmitContext()) - b.Run("pretty print checker.ts", func(b *testing.B) { // for comparison - for b.Loop() { - newText := p.EmitSourceFile(sourceFile) - assert.Assert(b, len(newText) > 0) - } - }) -} diff --git a/kitcom/internal/tsgo/format/comment_test.go b/kitcom/internal/tsgo/format/comment_test.go deleted file mode 100644 index e79a9e7..0000000 --- a/kitcom/internal/tsgo/format/comment_test.go +++ /dev/null @@ -1,71 +0,0 @@ -package format_test - -import ( - "strings" - "testing" - - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/core" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/format" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/parser" - "gotest.tools/v3/assert" -) - -func TestCommentFormatting(t *testing.T) { - t.Parallel() - - t.Run("format comment issue reproduction", func(t *testing.T) { - t.Parallel() - ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{ - EditorSettings: format.EditorSettings{ - TabSize: 4, - IndentSize: 4, - BaseIndentSize: 4, - NewLineCharacter: "\n", - ConvertTabsToSpaces: true, - IndentStyle: format.IndentStyleSmart, - TrimTrailingWhitespace: true, - }, - InsertSpaceBeforeTypeAnnotation: core.TSTrue, - }, "\n") - - // Original code that causes the bug - originalText := `class C { - /** - * - */ - async x() {} -}` - - sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{ - FileName: "/test.ts", - Path: "/test.ts", - }, originalText, core.ScriptKindTS) - - // Apply formatting once - edits := format.FormatDocument(ctx, sourceFile) - firstFormatted := applyBulkEdits(originalText, edits) - - // Check that the asterisk is not corrupted - assert.Check(t, !contains(firstFormatted, "*/\n /"), "should not corrupt */ to /") - assert.Check(t, contains(firstFormatted, "*/"), "should preserve */ token") - assert.Check(t, contains(firstFormatted, "async"), "should preserve async keyword") - - // Apply formatting a second time to test stability - sourceFile2 := parser.ParseSourceFile(ast.SourceFileParseOptions{ - FileName: "/test.ts", - Path: "/test.ts", - }, firstFormatted, core.ScriptKindTS) - - edits2 := format.FormatDocument(ctx, sourceFile2) - secondFormatted := applyBulkEdits(firstFormatted, edits2) - - // Check that second formatting doesn't introduce corruption - assert.Check(t, !contains(secondFormatted, " sync x()"), "should not corrupt async to sync") - assert.Check(t, contains(secondFormatted, "async"), "should preserve async keyword on second pass") - }) -} - -func contains(s, substr string) bool { - return len(substr) > 0 && strings.Contains(s, substr) -} diff --git a/kitcom/internal/tsgo/format/context.go b/kitcom/internal/tsgo/format/context.go deleted file mode 100644 index f3b5777..0000000 --- a/kitcom/internal/tsgo/format/context.go +++ /dev/null @@ -1,215 +0,0 @@ -package format - -import ( - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/core" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/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 - contextNode *ast.Node - currentTokenParent *ast.Node - nextTokenParent *ast.Node - - contextNodeAllOnSameLine core.Tristate - nextNodeAllOnSameLine core.Tristate - tokensAreOnSameLine core.Tristate - contextNodeBlockIsOnOneLine core.Tristate - nextNodeBlockIsOnOneLine core.Tristate - - SourceFile *ast.SourceFile - FormattingRequestKind FormatRequestKind - Options *FormatCodeSettings - - scanner *scanner.Scanner -} - -func NewFormattingContext(file *ast.SourceFile, kind FormatRequestKind, options *FormatCodeSettings) *formattingContext { - res := &formattingContext{ - SourceFile: file, - FormattingRequestKind: kind, - Options: options, - scanner: scanner.NewScanner(), - } - res.scanner.SetText(file.Text()) - res.scanner.SetSkipTrivia(true) - return res -} - -func (this *formattingContext) UpdateContext(cur TextRangeWithKind, curParent *ast.Node, next TextRangeWithKind, nextParent *ast.Node, commonParent *ast.Node) { - if curParent == nil { - panic("nil current range node parent in update context") - } - if nextParent == nil { - panic("nil next range node parent in update context") - } - if commonParent == nil { - panic("nil common parent node in update context") - } - this.currentTokenSpan = cur - this.currentTokenParent = curParent - this.nextTokenSpan = next - this.nextTokenParent = nextParent - this.contextNode = commonParent - - // drop cached results - this.contextNodeAllOnSameLine = core.TSUnknown - this.nextNodeAllOnSameLine = core.TSUnknown - this.tokensAreOnSameLine = core.TSUnknown - this.contextNodeBlockIsOnOneLine = core.TSUnknown - this.nextNodeBlockIsOnOneLine = core.TSUnknown -} - -func (this *formattingContext) rangeIsOnOneLine(node core.TextRange) core.Tristate { - if rangeIsOnOneLine(node, this.SourceFile) { - return core.TSTrue - } - return core.TSFalse -} - -func (this *formattingContext) nodeIsOnOneLine(node *ast.Node) core.Tristate { - return this.rangeIsOnOneLine(withTokenStart(node, this.SourceFile)) -} - -func withTokenStart(loc *ast.Node, file *ast.SourceFile) core.TextRange { - startPos := scanner.GetTokenPosOfNode(loc, file, false) - return core.NewTextRange(startPos, loc.End()) -} - -func (this *formattingContext) blockIsOnOneLine(node *ast.Node) core.Tristate { - // In strada, this relies on token child manifesting - we just use the scanner here, - // so this will have a differing performance profile. Is this OK? Needs profiling to know. - this.scanner.ResetPos(node.Pos()) - end := node.End() - firstOpenBrace := -1 - lastCloseBrace := -1 - for this.scanner.TokenEnd() < end { - // tokenStart instead of tokenfullstart to skip trivia - if firstOpenBrace == -1 && this.scanner.Token() == ast.KindOpenBraceToken { - firstOpenBrace = this.scanner.TokenStart() - } else if this.scanner.Token() == ast.KindCloseBraceToken { - lastCloseBrace = this.scanner.TokenStart() - } - this.scanner.Scan() - } - if firstOpenBrace != -1 && lastCloseBrace != -1 { - return this.rangeIsOnOneLine(core.NewTextRange(firstOpenBrace, lastCloseBrace)) - } - return core.TSFalse -} - -func (this *formattingContext) ContextNodeAllOnSameLine() bool { - if this.contextNodeAllOnSameLine == core.TSUnknown { - this.contextNodeAllOnSameLine = this.nodeIsOnOneLine(this.contextNode) - } - return this.contextNodeAllOnSameLine == core.TSTrue -} - -func (this *formattingContext) NextNodeAllOnSameLine() bool { - if this.nextNodeAllOnSameLine == core.TSUnknown { - this.nextNodeAllOnSameLine = this.nodeIsOnOneLine(this.nextTokenParent) - } - return this.nextNodeAllOnSameLine == core.TSTrue -} - -func (this *formattingContext) TokensAreOnSameLine() bool { - if this.tokensAreOnSameLine == core.TSUnknown { - this.tokensAreOnSameLine = this.rangeIsOnOneLine(core.NewTextRange(this.currentTokenSpan.Loc.Pos(), this.nextTokenSpan.Loc.End())) - } - return this.tokensAreOnSameLine == core.TSTrue -} - -func (this *formattingContext) ContextNodeBlockIsOnOneLine() bool { - if this.contextNodeBlockIsOnOneLine == core.TSUnknown { - this.contextNodeBlockIsOnOneLine = this.blockIsOnOneLine(this.contextNode) - } - return this.contextNodeBlockIsOnOneLine == core.TSTrue -} - -func (this *formattingContext) NextNodeBlockIsOnOneLine() bool { - if this.nextNodeBlockIsOnOneLine == core.TSUnknown { - this.nextNodeBlockIsOnOneLine = this.blockIsOnOneLine(this.nextTokenParent) - } - return this.nextNodeBlockIsOnOneLine == core.TSTrue -} diff --git a/kitcom/internal/tsgo/format/indent.go b/kitcom/internal/tsgo/format/indent.go deleted file mode 100644 index 6c2c5a4..0000000 --- a/kitcom/internal/tsgo/format/indent.go +++ /dev/null @@ -1,559 +0,0 @@ -package format - -import ( - "slices" - "unicode/utf8" - - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/astnav" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/core" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/debug" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/scanner" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/stringutil" -) - -func GetIndentationForNode(n *ast.Node, ignoreActualIndentationRange *core.TextRange, sourceFile *ast.SourceFile, options *FormatCodeSettings) int { - startline, startpos := scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false)) - return getIndentationForNodeWorker(n, startline, startpos, ignoreActualIndentationRange /*indentationDelta*/, 0, sourceFile /*isNextChild*/, false, options) -} - -func getIndentationForNodeWorker( - current *ast.Node, - currentStartLine int, - currentStartCharacter int, - ignoreActualIndentationRange *core.TextRange, - indentationDelta int, - sourceFile *ast.SourceFile, - isNextChild bool, - options *FormatCodeSettings, -) int { - parent := current.Parent - - // Walk up the tree and collect indentation for parent-child node pairs. Indentation is not added if - // * parent and child nodes start on the same line, or - // * parent is an IfStatement and child starts on the same line as an 'else clause'. - for parent != nil { - useActualIndentation := true - if ignoreActualIndentationRange != nil { - start := scanner.GetTokenPosOfNode(current, sourceFile, false) - useActualIndentation = start < ignoreActualIndentationRange.Pos() || start > ignoreActualIndentationRange.End() - } - - containingListOrParentStartLine, containingListOrParentStartCharacter := getContainingListOrParentStart(parent, current, sourceFile) - parentAndChildShareLine := containingListOrParentStartLine == currentStartLine || - childStartsOnTheSameLineWithElseInIfStatement(parent, current, currentStartLine, sourceFile) - - if useActualIndentation { - // check if current node is a list item - if yes, take indentation from it - var firstListChild *ast.Node - containerList := GetContainingList(current, sourceFile) - if containerList != nil { - firstListChild = core.FirstOrNil(containerList.Nodes) - } - // A list indents its children if the children begin on a later line than the list itself: - // - // f1( L0 - List start - // { L1 - First child start: indented, along with all other children - // prop: 0 - // }, - // { - // prop: 1 - // } - // ) - // - // f2({ L0 - List start and first child start: children are not indented. - // prop: 0 Object properties are indented only one level, because the list - // }, { itself contributes nothing. - // prop: 1 L3 - The indentation of the second object literal is best understood by - // }) looking at the relationship between the list and *first* list item. - listLine, _ := getStartLineAndCharacterForNode(firstListChild, sourceFile) - listIndentsChild := firstListChild != nil && listLine > containingListOrParentStartLine - actualIndentation := getActualIndentationForListItem(current, sourceFile, options, listIndentsChild) - if actualIndentation != -1 { - return actualIndentation + indentationDelta - } - - // try to fetch actual indentation for current node from source text - actualIndentation = getActualIndentationForNode(current, parent, currentStartLine, currentStartCharacter, parentAndChildShareLine, sourceFile, options) - if actualIndentation != -1 { - return actualIndentation + indentationDelta - } - } - - // increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line - if ShouldIndentChildNode(options, parent, current, sourceFile, isNextChild) && !parentAndChildShareLine { - indentationDelta += options.IndentSize - } - - // In our AST, a call argument's `parent` is the call-expression, not the argument list. - // We would like to increase indentation based on the relationship between an argument and its argument-list, - // so we spoof the starting position of the (parent) call-expression to match the (non-parent) argument-list. - // But, the spoofed start-value could then cause a problem when comparing the start position of the call-expression - // to *its* parent (in the case of an iife, an expression statement), adding an extra level of indentation. - // - // Instead, when at an argument, we unspoof the starting position of the enclosing call expression - // *after* applying indentation for the argument. - - useTrueStart := isArgumentAndStartLineOverlapsExpressionBeingCalled(parent, current, currentStartLine, sourceFile) - - current = parent - parent = current.Parent - - if useTrueStart { - currentStartLine, currentStartCharacter = scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(current, sourceFile, false)) - } else { - currentStartLine = containingListOrParentStartLine - currentStartCharacter = containingListOrParentStartCharacter - } - } - - return indentationDelta + options.BaseIndentSize -} - -/* -* Function returns -1 if actual indentation for node should not be used (i.e because node is nested expression) - */ -func getActualIndentationForNode(current *ast.Node, parent *ast.Node, cuurentLine int, currentChar int, parentAndChildShareLine bool, sourceFile *ast.SourceFile, options *FormatCodeSettings) int { - // actual indentation is used for statements\declarations if one of cases below is true: - // - parent is SourceFile - by default immediate children of SourceFile are not indented except when user indents them manually - // - parent and child are not on the same line - useActualIndentation := (ast.IsDeclaration(current) || ast.IsStatementButNotDeclaration(current)) && (parent.Kind == ast.KindSourceFile || !parentAndChildShareLine) - - if !useActualIndentation { - return -1 - } - - return findColumnForFirstNonWhitespaceCharacterInLine(cuurentLine, currentChar, sourceFile, options) -} - -func isArgumentAndStartLineOverlapsExpressionBeingCalled(parent *ast.Node, child *ast.Node, childStartLine int, sourceFile *ast.SourceFile) bool { - if !(ast.IsCallExpression(child) && slices.Contains(parent.AsCallExpression().Arguments.Nodes, child)) { - return false - } - expressionOfCallExpressionEnd := parent.Expression().End() - expressionOfCallExpressionEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, expressionOfCallExpressionEnd) - return expressionOfCallExpressionEndLine == childStartLine -} - -func getActualIndentationForListItem(node *ast.Node, sourceFile *ast.SourceFile, options *FormatCodeSettings, listIndentsChild bool) int { - if node.Parent != nil && node.Parent.Kind == ast.KindVariableDeclarationList { - // VariableDeclarationList has no wrapping tokens - return -1 - } - containingList := GetContainingList(node, sourceFile) - if containingList != nil { - index := core.FindIndex(containingList.Nodes, func(e *ast.Node) bool { return e == node }) - if index != -1 { - result := deriveActualIndentationFromList(containingList, index, sourceFile, options) - if result != -1 { - return result - } - } - delta := 0 - if listIndentsChild { - delta = options.IndentSize - } - res := getActualIndentationForListStartLine(containingList, sourceFile, options) - if res == -1 { - return delta - } - return res + delta - } - return -1 -} - -func getActualIndentationForListStartLine(list *ast.NodeList, sourceFile *ast.SourceFile, options *FormatCodeSettings) int { - if list == nil { - return -1 - } - line, char := scanner.GetECMALineAndCharacterOfPosition(sourceFile, list.Loc.Pos()) - return findColumnForFirstNonWhitespaceCharacterInLine(line, char, sourceFile, options) -} - -func deriveActualIndentationFromList(list *ast.NodeList, index int, sourceFile *ast.SourceFile, options *FormatCodeSettings) int { - debug.Assert(list != nil && index >= 0 && index < len(list.Nodes)) - - node := list.Nodes[index] - - // walk toward the start of the list starting from current node and check if the line is the same for all items. - // if end line for item [i - 1] differs from the start line for item [i] - find column of the first non-whitespace character on the line of item [i] - - line, char := getStartLineAndCharacterForNode(node, sourceFile) - - for i := index; i >= 0; i-- { - if list.Nodes[i].Kind == ast.KindCommaToken { - continue - } - // skip list items that ends on the same line with the current list element - prevEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, list.Nodes[i].End()) - if prevEndLine != line { - return findColumnForFirstNonWhitespaceCharacterInLine(line, char, sourceFile, options) - } - - line, char = getStartLineAndCharacterForNode(list.Nodes[i], sourceFile) - } - return -1 -} - -func findColumnForFirstNonWhitespaceCharacterInLine(line int, char int, sourceFile *ast.SourceFile, options *FormatCodeSettings) int { - lineStart := scanner.GetECMAPositionOfLineAndCharacter(sourceFile, line, 0) - return FindFirstNonWhitespaceColumn(lineStart, lineStart+char, sourceFile, options) -} - -func FindFirstNonWhitespaceColumn(startPos int, endPos int, sourceFile *ast.SourceFile, options *FormatCodeSettings) int { - _, col := findFirstNonWhitespaceCharacterAndColumn(startPos, endPos, sourceFile, options) - return col -} - -/** -* Character is the actual index of the character since the beginning of the line. -* Column - position of the character after expanding tabs to spaces. -* "0\t2$" -* value of 'character' for '$' is 3 -* value of 'column' for '$' is 6 (assuming that tab size is 4) - */ -func findFirstNonWhitespaceCharacterAndColumn(startPos int, endPos int, sourceFile *ast.SourceFile, options *FormatCodeSettings) (character int, column int) { - character = 0 - column = 0 - text := sourceFile.Text() - for pos := startPos; pos < endPos; pos++ { - ch, size := utf8.DecodeRuneInString(text[pos:]) - if size == 0 && ch == utf8.RuneError { - continue // multibyte character - TODO: recognize non-tab multicolumn characters? ideographic space? - } - if !stringutil.IsWhiteSpaceSingleLine(ch) { - break - } - - if ch == '\t' { - column += options.TabSize + (column % options.TabSize) - } else { - column++ - } - - character++ - } - return character, column -} - -func childStartsOnTheSameLineWithElseInIfStatement(parent *ast.Node, child *ast.Node, childStartLine int, sourceFile *ast.SourceFile) bool { - if parent.Kind == ast.KindIfStatement && parent.AsIfStatement().ElseStatement == child { - elseKeyword := astnav.FindPrecedingToken(sourceFile, child.Pos()) - debug.AssertIsDefined(elseKeyword) - elseKeywordStartLine, _ := getStartLineAndCharacterForNode(elseKeyword, sourceFile) - return elseKeywordStartLine == childStartLine - } - return false -} - -func getStartLineAndCharacterForNode(n *ast.Node, sourceFile *ast.SourceFile) (line int, character int) { - return scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false)) -} - -func GetContainingList(node *ast.Node, sourceFile *ast.SourceFile) *ast.NodeList { - if node.Parent == nil { - return nil - } - return getListByRange(scanner.GetTokenPosOfNode(node, sourceFile, false), node.End(), node.Parent, sourceFile) -} - -func getListByPosition(pos int, node *ast.Node, sourceFile *ast.SourceFile) *ast.NodeList { - if node == nil { - return nil - } - return getListByRange(pos, pos, node, sourceFile) -} - -func getListByRange(start int, end int, node *ast.Node, sourceFile *ast.SourceFile) *ast.NodeList { - r := core.NewTextRange(start, end) - switch node.Kind { - case ast.KindTypeReference: - return getList(node.AsTypeReferenceNode().TypeArguments, r, node, sourceFile) - case ast.KindObjectLiteralExpression: - return getList(node.AsObjectLiteralExpression().Properties, r, node, sourceFile) - case ast.KindArrayLiteralExpression: - return getList(node.AsArrayLiteralExpression().Elements, r, node, sourceFile) - case ast.KindTypeLiteral: - return getList(node.AsTypeLiteralNode().Members, r, node, sourceFile) - case ast.KindFunctionDeclaration, - ast.KindFunctionExpression, - ast.KindArrowFunction, - ast.KindMethodDeclaration, - ast.KindMethodSignature, - ast.KindCallSignature, - ast.KindConstructor, - ast.KindConstructorType, - ast.KindConstructSignature: - tpl := getList(node.TypeParameterList(), r, node, sourceFile) - if tpl != nil { - return tpl - } - return getList(node.ParameterList(), r, node, sourceFile) - case ast.KindGetAccessor: - return getList(node.ParameterList(), r, node, sourceFile) - case ast.KindClassDeclaration, - ast.KindClassExpression, - ast.KindInterfaceDeclaration, - ast.KindTypeAliasDeclaration, - ast.KindJSDocTemplateTag: - return getList(node.TypeParameterList(), r, node, sourceFile) - case ast.KindNewExpression, ast.KindCallExpression: - l := getList(node.TypeArgumentList(), r, node, sourceFile) - if l != nil { - return l - } - return getList(node.ArgumentList(), r, node, sourceFile) - case ast.KindVariableDeclarationList: - return getList(node.AsVariableDeclarationList().Declarations, r, node, sourceFile) - case ast.KindNamedImports: - return getList(node.AsNamedImports().Elements, r, node, sourceFile) - case ast.KindNamedExports: - return getList(node.AsNamedExports().Elements, r, node, sourceFile) - case ast.KindObjectBindingPattern, ast.KindArrayBindingPattern: - return getList(node.AsBindingPattern().Elements, r, node, sourceFile) - } - return nil // TODO: should this be a panic? It isn't in strada. -} - -func getList(list *ast.NodeList, r core.TextRange, node *ast.Node, sourceFile *ast.SourceFile) *ast.NodeList { - if list == nil { - return nil - } - if r.ContainedBy(getVisualListRange(node, list.Loc, sourceFile)) { - return list - } - return nil -} - -func getVisualListRange(node *ast.Node, list core.TextRange, sourceFile *ast.SourceFile) core.TextRange { - // In strada, this relied on the services .getChildren method, which manifested synthetic token nodes - // _however_, the logic boils down to "find the child with the matching span and adjust its start to the - // previous (possibly token) child's end and its end to the token start of the following element" - basically - // expanding the range to encompass all the neighboring non-token trivia - // Now, we perform that logic with the scanner instead - prior := astnav.FindPrecedingToken(sourceFile, list.Pos()) - var priorEnd int - if prior == nil { - priorEnd = list.Pos() - } else { - priorEnd = prior.End() - } - next := astnav.FindNextToken(prior, node, sourceFile) - var nextStart int - if next == nil { - nextStart = list.End() - } else { - nextStart = next.Pos() - } - return core.NewTextRange(priorEnd, nextStart) -} - -func getContainingListOrParentStart(parent *ast.Node, child *ast.Node, sourceFile *ast.SourceFile) (line int, character int) { - containingList := GetContainingList(child, sourceFile) - var startPos int - if containingList != nil { - startPos = containingList.Loc.Pos() - } else { - startPos = scanner.GetTokenPosOfNode(parent, sourceFile, false) - } - return scanner.GetECMALineAndCharacterOfPosition(sourceFile, startPos) -} - -func isControlFlowEndingStatement(kind ast.Kind, parentKind ast.Kind) bool { - switch kind { - case ast.KindReturnStatement, ast.KindThrowStatement, ast.KindContinueStatement, ast.KindBreakStatement: - return parentKind != ast.KindBlock - default: - return false - } -} - -/** -* True when the parent node should indent the given child by an explicit rule. -* @param isNextChild If true, we are judging indent of a hypothetical child *after* this one, not the current child. - */ -func ShouldIndentChildNode(settings *FormatCodeSettings, parent *ast.Node, child *ast.Node, sourceFile *ast.SourceFile, isNextChildArg ...bool) bool { - isNextChild := false - if len(isNextChildArg) > 0 { - isNextChild = isNextChildArg[0] - } - - return NodeWillIndentChild(settings, parent, child, sourceFile, false) && !(isNextChild && child != nil && isControlFlowEndingStatement(child.Kind, parent.Kind)) -} - -func NodeWillIndentChild(settings *FormatCodeSettings, parent *ast.Node, child *ast.Node, sourceFile *ast.SourceFile, indentByDefault bool) bool { - childKind := ast.KindUnknown - if child != nil { - childKind = child.Kind - } - - switch parent.Kind { - case ast.KindExpressionStatement, - ast.KindClassDeclaration, - ast.KindClassExpression, - ast.KindInterfaceDeclaration, - ast.KindEnumDeclaration, - ast.KindTypeAliasDeclaration, - ast.KindArrayLiteralExpression, - ast.KindBlock, - ast.KindModuleBlock, - ast.KindObjectLiteralExpression, - ast.KindTypeLiteral, - ast.KindMappedType, - ast.KindTupleType, - ast.KindParenthesizedExpression, - ast.KindPropertyAccessExpression, - ast.KindCallExpression, - ast.KindNewExpression, - ast.KindVariableStatement, - ast.KindExportAssignment, - ast.KindReturnStatement, - ast.KindConditionalExpression, - ast.KindArrayBindingPattern, - ast.KindObjectBindingPattern, - ast.KindJsxOpeningElement, - ast.KindJsxOpeningFragment, - ast.KindJsxSelfClosingElement, - ast.KindJsxExpression, - ast.KindMethodSignature, - ast.KindCallSignature, - ast.KindConstructSignature, - ast.KindParameter, - ast.KindFunctionType, - ast.KindConstructorType, - ast.KindParenthesizedType, - ast.KindTaggedTemplateExpression, - ast.KindAwaitExpression, - ast.KindNamedExports, - ast.KindNamedImports, - ast.KindExportSpecifier, - ast.KindImportSpecifier, - ast.KindPropertyDeclaration, - ast.KindCaseClause, - ast.KindDefaultClause: - return true - case ast.KindCaseBlock: - return settings.IndentSwitchCase.IsTrueOrUnknown() - case ast.KindVariableDeclaration, ast.KindPropertyAssignment, ast.KindBinaryExpression: - if settings.IndentMultiLineObjectLiteralBeginningOnBlankLine.IsFalseOrUnknown() && sourceFile != nil && childKind == ast.KindObjectLiteralExpression { - return rangeIsOnOneLine(child.Loc, sourceFile) - } - if parent.Kind == ast.KindBinaryExpression && sourceFile != nil && childKind == ast.KindJsxElement { - parentStartLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.SkipTrivia(sourceFile.Text(), parent.Pos())) - childStartLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.SkipTrivia(sourceFile.Text(), child.Pos())) - return parentStartLine != childStartLine - } - if parent.Kind != ast.KindBinaryExpression { - return true - } - return indentByDefault - case ast.KindDoStatement, - ast.KindWhileStatement, - ast.KindForInStatement, - ast.KindForOfStatement, - ast.KindForStatement, - ast.KindIfStatement, - ast.KindFunctionDeclaration, - ast.KindFunctionExpression, - ast.KindMethodDeclaration, - ast.KindConstructor, - ast.KindGetAccessor, - ast.KindSetAccessor: - return childKind != ast.KindBlock - case ast.KindArrowFunction: - if sourceFile != nil && childKind == ast.KindParenthesizedExpression { - return rangeIsOnOneLine(child.Loc, sourceFile) - } - return childKind != ast.KindBlock - case ast.KindExportDeclaration: - return childKind != ast.KindNamedExports - case ast.KindImportDeclaration: - return childKind != ast.KindImportClause || (child.AsImportClause().NamedBindings != nil && child.AsImportClause().NamedBindings.Kind != ast.KindNamedImports) - case ast.KindJsxElement: - return childKind != ast.KindJsxClosingElement - case ast.KindJsxFragment: - return childKind != ast.KindJsxClosingFragment - case ast.KindIntersectionType, ast.KindUnionType, ast.KindSatisfiesExpression: - if childKind == ast.KindTypeLiteral || childKind == ast.KindTupleType || childKind == ast.KindMappedType { - return false - } - return indentByDefault - case ast.KindTryStatement: - if childKind == ast.KindBlock { - return false - } - return indentByDefault - } - - // No explicit rule for given nodes so the result will follow the default value argument - return indentByDefault -} - -// A multiline conditional typically increases the indentation of its whenTrue and whenFalse children: -// -// condition -// -// ? whenTrue -// : whenFalse; -// -// However, that indentation does not apply if the subexpressions themselves span multiple lines, -// applying their own indentation: -// -// (() => { -// return complexCalculationForCondition(); -// })() ? { -// -// whenTrue: 'multiline object literal' -// } : ( -// -// whenFalse('multiline parenthesized expression') -// -// ); -// -// In these cases, we must discard the indentation increase that would otherwise be applied to the -// whenTrue and whenFalse children to avoid double-indenting their contents. To identify this scenario, -// we check for the whenTrue branch beginning on the line that the condition ends, and the whenFalse -// branch beginning on the line that the whenTrue branch ends. -func childIsUnindentedBranchOfConditionalExpression(parent *ast.Node, child *ast.Node, childStartLine int, sourceFile *ast.SourceFile) bool { - if parent.Kind == ast.KindConditionalExpression && (child == parent.AsConditionalExpression().WhenTrue || child == parent.AsConditionalExpression().WhenFalse) { - conditionEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, parent.AsConditionalExpression().Condition.End()) - if child == parent.AsConditionalExpression().WhenTrue { - return childStartLine == conditionEndLine - } else { - // On the whenFalse side, we have to look at the whenTrue side, because if that one was - // indented, whenFalse must also be indented: - // - // const y = true - // ? 1 : ( L1: whenTrue indented because it's on a new line - // 0 L2: indented two stops, one because whenTrue was indented - // ); and one because of the parentheses spanning multiple lines - trueStartLine, _ := getStartLineAndCharacterForNode(parent.AsConditionalExpression().WhenTrue, sourceFile) - trueEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, parent.AsConditionalExpression().WhenTrue.End()) - return conditionEndLine == trueStartLine && trueEndLine == childStartLine - } - } - return false -} - -func argumentStartsOnSameLineAsPreviousArgument(parent *ast.Node, child *ast.Node, childStartLine int, sourceFile *ast.SourceFile) bool { - if ast.IsCallExpression(parent) || ast.IsNewExpression(parent) { - if len(parent.Arguments()) == 0 { - return false - } - currentIndex := core.FindIndex(parent.Arguments(), func(n *ast.Node) bool { return n == child }) - if currentIndex == -1 { - // If it's not one of the arguments, don't look past this - return false - } - if currentIndex == 0 { - return false // Can't look at previous node if first - } - - previousNode := parent.Arguments()[currentIndex-1] - lineOfPreviousNode, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, previousNode.End()) - if childStartLine == lineOfPreviousNode { - return true - } - } - return false -} diff --git a/kitcom/internal/tsgo/format/rule.go b/kitcom/internal/tsgo/format/rule.go deleted file mode 100644 index 55115ec..0000000 --- a/kitcom/internal/tsgo/format/rule.go +++ /dev/null @@ -1,109 +0,0 @@ -package format - -import "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast" - -type ruleImpl struct { - debugName string - context []contextPredicate - action ruleAction - flags ruleFlags -} - -func (r ruleImpl) Action() ruleAction { - return r.action -} - -func (r ruleImpl) Context() []contextPredicate { - return r.context -} - -func (r ruleImpl) Flags() ruleFlags { - return r.flags -} - -func (r ruleImpl) String() string { - return r.debugName -} - -type tokenRange struct { - tokens []ast.Kind - isSpecific bool -} - -type ruleSpec struct { - leftTokenRange tokenRange - rightTokenRange tokenRange - rule *ruleImpl -} - -/** - * A rule takes a two tokens (left/right) and a particular context - * for which you're meant to look at them. You then declare what should the - * whitespace annotation be between these tokens via the action param. - * - * @param debugName Name to print - * @param left The left side of the comparison - * @param right The right side of the comparison - * @param context A set of filters to narrow down the space in which this formatter rule applies - * @param action a declaration of the expected whitespace - * @param flags whether the rule deletes a line or not, defaults to no-op - */ -func rule(debugName string, left any, right any, context []contextPredicate, action ruleAction, flags ...ruleFlags) ruleSpec { - flag := ruleFlagsNone - if len(flags) > 0 { - flag = flags[0] - } - leftRange := toTokenRange(left) - rightRange := toTokenRange(right) - rule := &ruleImpl{ - debugName: debugName, - context: context, - action: action, - flags: flag, - } - return ruleSpec{ - leftTokenRange: leftRange, - rightTokenRange: rightRange, - rule: rule, - } -} - -func toTokenRange(e any) tokenRange { - switch t := e.(type) { - case ast.Kind: - return tokenRange{isSpecific: true, tokens: []ast.Kind{t}} - case []ast.Kind: - return tokenRange{isSpecific: true, tokens: t} - case tokenRange: - return t - } - panic("Unknown argument type passed to toTokenRange - only ast.Kind, []ast.Kind, and tokenRange supported") -} - -type contextPredicate = func(ctx *formattingContext) bool - -var anyContext = []contextPredicate{} - -type ruleAction int - -const ( - ruleActionNone ruleAction = 0 - ruleActionStopProcessingSpaceActions ruleAction = 1 << 0 - ruleActionStopProcessingTokenActions ruleAction = 1 << 1 - ruleActionInsertSpace ruleAction = 1 << 2 - ruleActionInsertNewLine ruleAction = 1 << 3 - ruleActionDeleteSpace ruleAction = 1 << 4 - ruleActionDeleteToken ruleAction = 1 << 5 - ruleActionInsertTrailingSemicolon ruleAction = 1 << 6 - - ruleActionStopAction ruleAction = ruleActionStopProcessingSpaceActions | ruleActionStopProcessingTokenActions - ruleActionModifySpaceAction ruleAction = ruleActionInsertSpace | ruleActionInsertNewLine | ruleActionDeleteSpace - ruleActionModifyTokenAction ruleAction = ruleActionDeleteToken | ruleActionInsertTrailingSemicolon -) - -type ruleFlags int - -const ( - ruleFlagsNone ruleFlags = iota - ruleFlagsCanDeleteNewLines -) diff --git a/kitcom/internal/tsgo/format/rulecontext.go b/kitcom/internal/tsgo/format/rulecontext.go deleted file mode 100644 index ff8663d..0000000 --- a/kitcom/internal/tsgo/format/rulecontext.go +++ /dev/null @@ -1,644 +0,0 @@ -package format - -import ( - "strings" - - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/astnav" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/core" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/lsutil" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/scanner" -) - -/// -/// Contexts -/// - -type ( - optionSelector = func(options *FormatCodeSettings) core.Tristate - anyOptionSelector[T comparable] = func(options *FormatCodeSettings) T -) - -func semicolonOption(options *FormatCodeSettings) SemicolonPreference { return options.Semicolons } -func insertSpaceAfterCommaDelimiterOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceAfterCommaDelimiter -} - -func insertSpaceAfterSemicolonInForStatementsOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceAfterSemicolonInForStatements -} - -func insertSpaceBeforeAndAfterBinaryOperatorsOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceBeforeAndAfterBinaryOperators -} - -func insertSpaceAfterConstructorOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceAfterConstructor -} - -func insertSpaceAfterKeywordsInControlFlowStatementsOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceAfterKeywordsInControlFlowStatements -} - -func insertSpaceAfterFunctionKeywordForAnonymousFunctionsOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceAfterFunctionKeywordForAnonymousFunctions -} - -func insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis -} - -func insertSpaceAfterOpeningAndBeforeClosingNonemptyBracketsOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets -} - -func insertSpaceAfterOpeningAndBeforeClosingNonemptyBracesOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces -} - -func insertSpaceAfterOpeningAndBeforeClosingEmptyBracesOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceAfterOpeningAndBeforeClosingEmptyBraces -} - -func insertSpaceAfterOpeningAndBeforeClosingTemplateStringBracesOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces -} - -func insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBracesOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces -} - -func insertSpaceAfterTypeAssertionOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceAfterTypeAssertion -} - -func insertSpaceBeforeFunctionParenthesisOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceBeforeFunctionParenthesis -} - -func placeOpenBraceOnNewLineForFunctionsOption(options *FormatCodeSettings) core.Tristate { - return options.PlaceOpenBraceOnNewLineForFunctions -} - -func placeOpenBraceOnNewLineForControlBlocksOption(options *FormatCodeSettings) core.Tristate { - return options.PlaceOpenBraceOnNewLineForControlBlocks -} - -func insertSpaceBeforeTypeAnnotationOption(options *FormatCodeSettings) core.Tristate { - return options.InsertSpaceBeforeTypeAnnotation -} - -func indentMultiLineObjectLiteralBeginningOnBlankLineOption(options *FormatCodeSettings) core.Tristate { - return options.IndentMultiLineObjectLiteralBeginningOnBlankLine -} - -func indentSwitchCaseOption(options *FormatCodeSettings) core.Tristate { - return options.IndentSwitchCase -} - -func optionEquals[T comparable](optionName anyOptionSelector[T], optionValue T) contextPredicate { - return func(context *formattingContext) bool { - if context.Options == nil { - return false - } - return optionName(context.Options) == optionValue - } -} - -func isOptionEnabled(optionName optionSelector) contextPredicate { - return func(context *formattingContext) bool { - if context.Options == nil { - return false - } - return optionName(context.Options).IsTrue() - } -} - -func isOptionDisabled(optionName optionSelector) contextPredicate { - return func(context *formattingContext) bool { - if context.Options == nil { - return true - } - return optionName(context.Options).IsFalse() - } -} - -func isOptionDisabledOrUndefined(optionName optionSelector) contextPredicate { - return func(context *formattingContext) bool { - if context.Options == nil { - return true - } - return optionName(context.Options).IsFalseOrUnknown() - } -} - -func isOptionDisabledOrUndefinedOrTokensOnSameLine(optionName optionSelector) contextPredicate { - return func(context *formattingContext) bool { - if context.Options == nil { - return true - } - return optionName(context.Options).IsFalseOrUnknown() || context.TokensAreOnSameLine() - } -} - -func isOptionEnabledOrUndefined(optionName optionSelector) contextPredicate { - return func(context *formattingContext) bool { - if context.Options == nil { - return true - } - return optionName(context.Options).IsTrueOrUnknown() - } -} - -func isForContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindForStatement -} - -func isNotForContext(context *formattingContext) bool { - return !isForContext(context) -} - -func isBinaryOpContext(context *formattingContext) bool { - switch context.contextNode.Kind { - case ast.KindBinaryExpression: - return context.contextNode.AsBinaryExpression().OperatorToken.Kind != ast.KindCommaToken - case ast.KindConditionalExpression, - ast.KindConditionalType, - ast.KindAsExpression, - ast.KindExportSpecifier, - ast.KindImportSpecifier, - ast.KindTypePredicate, - ast.KindUnionType, - ast.KindIntersectionType, - ast.KindSatisfiesExpression: - return true - - // equals in binding elements func foo([[x, y] = [1, 2]]) - case ast.KindBindingElement: - // equals in type X = ... - fallthrough - case ast.KindTypeAliasDeclaration: - // equal in import a = module('a'); - fallthrough - case ast.KindImportEqualsDeclaration: - // equal in export = 1 - fallthrough - case ast.KindExportAssignment: - // equal in let a = 0 - fallthrough - case ast.KindVariableDeclaration: - // equal in p = 0 - fallthrough - case ast.KindParameter, - ast.KindEnumMember, - ast.KindPropertyDeclaration, - ast.KindPropertySignature: - return context.currentTokenSpan.Kind == ast.KindEqualsToken || context.nextTokenSpan.Kind == ast.KindEqualsToken - // "in" keyword in for (let x in []) { } - case ast.KindForInStatement: - // "in" keyword in [P in keyof T] T[P] - fallthrough - case ast.KindTypeParameter: - return context.currentTokenSpan.Kind == ast.KindInKeyword || context.nextTokenSpan.Kind == ast.KindInKeyword || context.currentTokenSpan.Kind == ast.KindEqualsToken || context.nextTokenSpan.Kind == ast.KindEqualsToken - // Technically, "of" is not a binary operator, but format it the same way as "in" - case ast.KindForOfStatement: - return context.currentTokenSpan.Kind == ast.KindOfKeyword || context.nextTokenSpan.Kind == ast.KindOfKeyword - } - return false -} - -func isNotBinaryOpContext(context *formattingContext) bool { - return !isBinaryOpContext(context) -} - -func isNotTypeAnnotationContext(context *formattingContext) bool { - return !isTypeAnnotationContext(context) -} - -func isTypeAnnotationContext(context *formattingContext) bool { - contextKind := context.contextNode.Kind - return contextKind == ast.KindPropertyDeclaration || - contextKind == ast.KindPropertySignature || - contextKind == ast.KindParameter || - contextKind == ast.KindVariableDeclaration || - ast.IsFunctionLikeKind(contextKind) -} - -func isOptionalPropertyContext(context *formattingContext) bool { - return ast.IsPropertyDeclaration(context.contextNode) && context.contextNode.AsPropertyDeclaration().PostfixToken != nil && context.contextNode.AsPropertyDeclaration().PostfixToken.Kind == ast.KindQuestionToken -} - -func isNonOptionalPropertyContext(context *formattingContext) bool { - return !isOptionalPropertyContext(context) -} - -func isConditionalOperatorContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindConditionalExpression || - context.contextNode.Kind == ast.KindConditionalType -} - -func isSameLineTokenOrBeforeBlockContext(context *formattingContext) bool { - return context.TokensAreOnSameLine() || isBeforeBlockContext(context) -} - -func isBraceWrappedContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindObjectBindingPattern || - context.contextNode.Kind == ast.KindMappedType || - isSingleLineBlockContext(context) -} - -// This check is done before an open brace in a control construct, a function, or a typescript block declaration -func isBeforeMultilineBlockContext(context *formattingContext) bool { - return isBeforeBlockContext(context) && !(context.NextNodeAllOnSameLine() || context.NextNodeBlockIsOnOneLine()) -} - -func isMultilineBlockContext(context *formattingContext) bool { - return isBlockContext(context) && !(context.ContextNodeAllOnSameLine() || context.ContextNodeBlockIsOnOneLine()) -} - -func isSingleLineBlockContext(context *formattingContext) bool { - return isBlockContext(context) && (context.ContextNodeAllOnSameLine() || context.ContextNodeBlockIsOnOneLine()) -} - -func isBlockContext(context *formattingContext) bool { - return nodeIsBlockContext(context.contextNode) -} - -func isBeforeBlockContext(context *formattingContext) bool { - return nodeIsBlockContext(context.nextTokenParent) -} - -// IMPORTANT!!! This method must return true ONLY for nodes with open and close braces as immediate children -func nodeIsBlockContext(node *ast.Node) bool { - if nodeIsTypeScriptDeclWithBlockContext(node) { - // This means we are in a context that looks like a block to the user, but in the grammar is actually not a node (it's a class, module, enum, object type literal, etc). - return true - } - - switch node.Kind { - case ast.KindBlock, - ast.KindCaseBlock, - ast.KindObjectLiteralExpression, - ast.KindModuleBlock: - return true - } - - return false -} - -func isFunctionDeclContext(context *formattingContext) bool { - switch context.contextNode.Kind { - case ast.KindFunctionDeclaration, - ast.KindMethodDeclaration, - ast.KindMethodSignature: - // case ast.KindMemberFunctionDeclaration: - fallthrough - case ast.KindGetAccessor, - ast.KindSetAccessor: - // case ast.KindMethodSignature: - fallthrough - case ast.KindCallSignature, - ast.KindFunctionExpression, - ast.KindConstructor, - ast.KindArrowFunction: - // case ast.KindConstructorDeclaration: - // case ast.KindSimpleArrowFunctionExpression: - // case ast.KindParenthesizedArrowFunctionExpression: - fallthrough - case ast.KindInterfaceDeclaration: // This one is not truly a function, but for formatting purposes, it acts just like one - return true - } - - return false -} - -func isNotFunctionDeclContext(context *formattingContext) bool { - return !isFunctionDeclContext(context) -} - -func isFunctionDeclarationOrFunctionExpressionContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindFunctionDeclaration || context.contextNode.Kind == ast.KindFunctionExpression -} - -func isTypeScriptDeclWithBlockContext(context *formattingContext) bool { - return nodeIsTypeScriptDeclWithBlockContext(context.contextNode) -} - -func nodeIsTypeScriptDeclWithBlockContext(node *ast.Node) bool { - switch node.Kind { - case ast.KindClassDeclaration, - ast.KindClassExpression, - ast.KindInterfaceDeclaration, - ast.KindEnumDeclaration, - ast.KindTypeLiteral, - ast.KindModuleDeclaration, - ast.KindExportDeclaration, - ast.KindNamedExports, - ast.KindImportDeclaration, - ast.KindNamedImports: - return true - } - - return false -} - -func isAfterCodeBlockContext(context *formattingContext) bool { - switch context.currentTokenParent.Kind { - case ast.KindClassDeclaration, - ast.KindModuleDeclaration, - ast.KindEnumDeclaration, - ast.KindCatchClause, - ast.KindModuleBlock, - ast.KindSwitchStatement: - return true - case ast.KindBlock: - blockParent := context.currentTokenParent.Parent - // In a codefix scenario, we can't rely on parents being set. So just always return true. - if blockParent == nil || blockParent.Kind != ast.KindArrowFunction && blockParent.Kind != ast.KindFunctionExpression { - return true - } - } - return false -} - -func isControlDeclContext(context *formattingContext) bool { - switch context.contextNode.Kind { - case ast.KindIfStatement, - ast.KindSwitchStatement, - ast.KindForStatement, - ast.KindForInStatement, - ast.KindForOfStatement, - ast.KindWhileStatement, - ast.KindTryStatement, - ast.KindDoStatement, - ast.KindWithStatement: - // TODO - // case ast.KindElseClause: - fallthrough - case ast.KindCatchClause: - return true - - default: - return false - } -} - -func isObjectContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindObjectLiteralExpression -} - -func isFunctionCallContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindCallExpression -} - -func isNewContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindNewExpression -} - -func isFunctionCallOrNewContext(context *formattingContext) bool { - return isFunctionCallContext(context) || isNewContext(context) -} - -func isPreviousTokenNotComma(context *formattingContext) bool { - return context.currentTokenSpan.Kind != ast.KindCommaToken -} - -func isNextTokenNotCloseBracket(context *formattingContext) bool { - return context.nextTokenSpan.Kind != ast.KindCloseBracketToken -} - -func isNextTokenNotCloseParen(context *formattingContext) bool { - return context.nextTokenSpan.Kind != ast.KindCloseParenToken -} - -func isArrowFunctionContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindArrowFunction -} - -func isImportTypeContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindImportType -} - -func isNonJsxSameLineTokenContext(context *formattingContext) bool { - return context.TokensAreOnSameLine() && context.contextNode.Kind != ast.KindJsxText -} - -func isNonJsxTextContext(context *formattingContext) bool { - return context.contextNode.Kind != ast.KindJsxText -} - -func isNonJsxElementOrFragmentContext(context *formattingContext) bool { - return context.contextNode.Kind != ast.KindJsxElement && context.contextNode.Kind != ast.KindJsxFragment -} - -func isJsxExpressionContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindJsxExpression || context.contextNode.Kind == ast.KindJsxSpreadAttribute -} - -func isNextTokenParentJsxAttribute(context *formattingContext) bool { - return context.nextTokenParent.Kind == ast.KindJsxAttribute || (context.nextTokenParent.Kind == ast.KindJsxNamespacedName && context.nextTokenParent.Parent.Kind == ast.KindJsxAttribute) -} - -func isJsxAttributeContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindJsxAttribute -} - -func isNextTokenParentNotJsxNamespacedName(context *formattingContext) bool { - return context.nextTokenParent.Kind != ast.KindJsxNamespacedName -} - -func isNextTokenParentJsxNamespacedName(context *formattingContext) bool { - return context.nextTokenParent.Kind == ast.KindJsxNamespacedName -} - -func isJsxSelfClosingElementContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindJsxSelfClosingElement -} - -func isNotBeforeBlockInFunctionDeclarationContext(context *formattingContext) bool { - return !isFunctionDeclContext(context) && !isBeforeBlockContext(context) -} - -func isEndOfDecoratorContextOnSameLine(context *formattingContext) bool { - return context.TokensAreOnSameLine() && - ast.HasDecorators(context.contextNode) && - nodeIsInDecoratorContext(context.currentTokenParent) && - !nodeIsInDecoratorContext(context.nextTokenParent) -} - -func nodeIsInDecoratorContext(node *ast.Node) bool { - for node != nil && ast.IsExpression(node) { - node = node.Parent - } - return node != nil && node.Kind == ast.KindDecorator -} - -func isStartOfVariableDeclarationList(context *formattingContext) bool { - return context.currentTokenParent.Kind == ast.KindVariableDeclarationList && - scanner.GetTokenPosOfNode(context.currentTokenParent, context.SourceFile, false) == context.currentTokenSpan.Loc.Pos() -} - -func isNotFormatOnEnter(context *formattingContext) bool { - return context.FormattingRequestKind != FormatRequestKindFormatOnEnter -} - -func isModuleDeclContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindModuleDeclaration -} - -func isObjectTypeContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindTypeLiteral // && context.contextNode.parent.Kind != ast.KindInterfaceDeclaration; -} - -func isConstructorSignatureContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindConstructSignature -} - -func isTypeArgumentOrParameterOrAssertion(token TextRangeWithKind, parent *ast.Node) bool { - if token.Kind != ast.KindLessThanToken && token.Kind != ast.KindGreaterThanToken { - return false - } - switch parent.Kind { - case ast.KindTypeReference, - ast.KindTypeAssertionExpression, - ast.KindTypeAliasDeclaration, - ast.KindClassDeclaration, - ast.KindClassExpression, - ast.KindInterfaceDeclaration, - ast.KindFunctionDeclaration, - ast.KindFunctionExpression, - ast.KindArrowFunction, - ast.KindMethodDeclaration, - ast.KindMethodSignature, - ast.KindCallSignature, - ast.KindConstructSignature, - ast.KindCallExpression, - ast.KindNewExpression, - ast.KindExpressionWithTypeArguments: - return true - default: - return false - } -} - -func isTypeArgumentOrParameterOrAssertionContext(context *formattingContext) bool { - return isTypeArgumentOrParameterOrAssertion(context.currentTokenSpan, context.currentTokenParent) || - isTypeArgumentOrParameterOrAssertion(context.nextTokenSpan, context.nextTokenParent) -} - -func isTypeAssertionContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindTypeAssertionExpression -} - -func isNonTypeAssertionContext(context *formattingContext) bool { - return !isTypeAssertionContext(context) -} - -func isVoidOpContext(context *formattingContext) bool { - return context.currentTokenSpan.Kind == ast.KindVoidKeyword && context.currentTokenParent.Kind == ast.KindVoidExpression -} - -func isYieldOrYieldStarWithOperand(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindYieldExpression && context.contextNode.AsYieldExpression().Expression != nil -} - -func isNonNullAssertionContext(context *formattingContext) bool { - return context.contextNode.Kind == ast.KindNonNullExpression -} - -func isNotStatementConditionContext(context *formattingContext) bool { - return !isStatementConditionContext(context) -} - -func isStatementConditionContext(context *formattingContext) bool { - switch context.contextNode.Kind { - case ast.KindIfStatement, - ast.KindForStatement, - ast.KindForInStatement, - ast.KindForOfStatement, - ast.KindDoStatement, - ast.KindWhileStatement: - return true - - default: - return false - } -} - -func isSemicolonDeletionContext(context *formattingContext) bool { - nextTokenKind := context.nextTokenSpan.Kind - nextTokenStart := context.nextTokenSpan.Loc.Pos() - if ast.IsTrivia(nextTokenKind) { - var nextRealToken *ast.Node - if context.nextTokenParent == context.currentTokenParent { - // !!! TODO: very different from strada, but strada's logic here is wonky - find the first ancestor without a parent? that's just the source file. - nextRealToken = astnav.FindNextToken(context.nextTokenParent, context.SourceFile.AsNode(), context.SourceFile) - } else { - nextRealToken = lsutil.GetFirstToken(context.nextTokenParent, context.SourceFile) - } - - if nextRealToken == nil { - return true - } - nextTokenKind = nextRealToken.Kind - nextTokenStart = scanner.GetTokenPosOfNode(nextRealToken, context.SourceFile, false) - } - - startLine, _ := scanner.GetECMALineAndCharacterOfPosition(context.SourceFile, context.currentTokenSpan.Loc.Pos()) - endLine, _ := scanner.GetECMALineAndCharacterOfPosition(context.SourceFile, nextTokenStart) - if startLine == endLine { - return nextTokenKind == ast.KindCloseBraceToken || nextTokenKind == ast.KindEndOfFile - } - - if nextTokenKind == ast.KindSemicolonToken && - context.currentTokenSpan.Kind == ast.KindSemicolonToken { - return true - } - - if nextTokenKind == ast.KindSemicolonClassElement || - nextTokenKind == ast.KindSemicolonToken { - return false - } - - if context.contextNode.Kind == ast.KindInterfaceDeclaration || - context.contextNode.Kind == ast.KindTypeAliasDeclaration { - // Can't remove semicolon after `foo`; it would parse as a method declaration: - // - // interface I { - // foo; - // () void - // } - return context.currentTokenParent.Kind != ast.KindPropertySignature || - context.currentTokenParent.Type() != nil || - nextTokenKind != ast.KindOpenParenToken - } - - if ast.IsPropertyDeclaration(context.currentTokenParent) { - return context.currentTokenParent.Initializer() == nil - } - - return context.currentTokenParent.Kind != ast.KindForStatement && - context.currentTokenParent.Kind != ast.KindEmptyStatement && - context.currentTokenParent.Kind != ast.KindSemicolonClassElement && - nextTokenKind != ast.KindOpenBracketToken && - nextTokenKind != ast.KindOpenParenToken && - nextTokenKind != ast.KindPlusToken && - nextTokenKind != ast.KindMinusToken && - nextTokenKind != ast.KindSlashToken && - nextTokenKind != ast.KindRegularExpressionLiteral && - nextTokenKind != ast.KindCommaToken && - nextTokenKind != ast.KindTemplateExpression && - nextTokenKind != ast.KindTemplateHead && - nextTokenKind != ast.KindNoSubstitutionTemplateLiteral && - nextTokenKind != ast.KindDotToken -} - -func isSemicolonInsertionContext(context *formattingContext) bool { - return lsutil.PositionIsASICandidate(context.currentTokenSpan.Loc.End(), context.currentTokenParent, context.SourceFile) -} - -func isNotPropertyAccessOnIntegerLiteral(context *formattingContext) bool { - return !ast.IsPropertyAccessExpression(context.contextNode) || - !ast.IsNumericLiteral(context.contextNode.Expression()) || - strings.Contains(context.contextNode.Expression().Text(), ".") -} diff --git a/kitcom/internal/tsgo/format/rules.go b/kitcom/internal/tsgo/format/rules.go deleted file mode 100644 index 4ccfb87..0000000 --- a/kitcom/internal/tsgo/format/rules.go +++ /dev/null @@ -1,446 +0,0 @@ -package format - -import ( - "slices" - - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast" -) - -func getAllRules() []ruleSpec { - allTokens := make([]ast.Kind, 0, ast.KindLastToken-ast.KindFirstToken+1) - for token := ast.KindFirstToken; token <= ast.KindLastToken; token++ { - allTokens = append(allTokens, token) - } - - anyTokenExcept := func(tokens ...ast.Kind) tokenRange { - newTokens := make([]ast.Kind, 0, ast.KindLastToken-ast.KindFirstToken+1) - for token := ast.KindFirstToken; token <= ast.KindLastToken; token++ { - if slices.Contains(tokens, token) { - continue - } - newTokens = append(newTokens, token) - } - return tokenRange{ - isSpecific: false, - tokens: newTokens, - } - } - - anyToken := tokenRange{ - isSpecific: false, - tokens: allTokens, - } - - anyTokenIncludingMultilineComments := tokenRangeFromEx(allTokens, ast.KindMultiLineCommentTrivia) - anyTokenIncludingEOF := tokenRangeFromEx(allTokens, ast.KindEndOfFile) - keywords := tokenRangeFromRange(ast.KindFirstKeyword, ast.KindLastKeyword) - binaryOperators := tokenRangeFromRange(ast.KindFirstBinaryOperator, ast.KindLastBinaryOperator) - binaryKeywordOperators := []ast.Kind{ - ast.KindInKeyword, - ast.KindInstanceOfKeyword, - ast.KindOfKeyword, - ast.KindAsKeyword, - ast.KindIsKeyword, - ast.KindSatisfiesKeyword, - } - unaryPrefixOperators := []ast.Kind{ast.KindPlusPlusToken, ast.KindMinusToken, ast.KindTildeToken, ast.KindExclamationToken} - unaryPrefixExpressions := []ast.Kind{ - ast.KindNumericLiteral, - ast.KindBigIntLiteral, - ast.KindIdentifier, - ast.KindOpenParenToken, - ast.KindOpenBracketToken, - ast.KindOpenBraceToken, - ast.KindThisKeyword, - ast.KindNewKeyword, - } - unaryPreincrementExpressions := []ast.Kind{ast.KindIdentifier, ast.KindOpenParenToken, ast.KindThisKeyword, ast.KindNewKeyword} - unaryPostincrementExpressions := []ast.Kind{ast.KindIdentifier, ast.KindCloseParenToken, ast.KindCloseBracketToken, ast.KindNewKeyword} - unaryPredecrementExpressions := []ast.Kind{ast.KindIdentifier, ast.KindOpenParenToken, ast.KindThisKeyword, ast.KindNewKeyword} - unaryPostdecrementExpressions := []ast.Kind{ast.KindIdentifier, ast.KindCloseParenToken, ast.KindCloseBracketToken, ast.KindNewKeyword} - comments := []ast.Kind{ast.KindSingleLineCommentTrivia, ast.KindMultiLineCommentTrivia} - typeKeywords := []ast.Kind{ - ast.KindAnyKeyword, - ast.KindAssertsKeyword, - ast.KindBigIntKeyword, - ast.KindBooleanKeyword, - ast.KindFalseKeyword, - ast.KindInferKeyword, - ast.KindKeyOfKeyword, - ast.KindNeverKeyword, - ast.KindNullKeyword, - ast.KindNumberKeyword, - ast.KindObjectKeyword, - ast.KindReadonlyKeyword, - ast.KindStringKeyword, - ast.KindSymbolKeyword, - ast.KindTypeOfKeyword, - ast.KindTrueKeyword, - ast.KindVoidKeyword, - ast.KindUndefinedKeyword, - ast.KindUniqueKeyword, - ast.KindUnknownKeyword, - } - typeNames := append([]ast.Kind{ast.KindIdentifier}, typeKeywords...) - - // Place a space before open brace in a function declaration - // TypeScript: Function can have return types, which can be made of tons of different token kinds - functionOpenBraceLeftTokenRange := anyTokenIncludingMultilineComments - - // Place a space before open brace in a TypeScript declaration that has braces as children (class, module, enum, etc) - typeScriptOpenBraceLeftTokenRange := tokenRangeFrom(ast.KindIdentifier, ast.KindGreaterThanToken, ast.KindMultiLineCommentTrivia, ast.KindClassKeyword, ast.KindExportKeyword, ast.KindImportKeyword) - - // Place a space before open brace in a control flow construct - controlOpenBraceLeftTokenRange := tokenRangeFrom(ast.KindCloseParenToken, ast.KindMultiLineCommentTrivia, ast.KindDoKeyword, ast.KindTryKeyword, ast.KindFinallyKeyword, ast.KindElseKeyword, ast.KindCatchKeyword) - - // These rules are higher in priority than user-configurable - highPriorityCommonRules := []ruleSpec{ - // Leave comments alone - rule("IgnoreBeforeComment", anyToken, comments, anyContext, ruleActionStopProcessingSpaceActions), - rule("IgnoreAfterLineComment", ast.KindSingleLineCommentTrivia, anyToken, anyContext, ruleActionStopProcessingSpaceActions), - - rule("NotSpaceBeforeColon", anyToken, ast.KindColonToken, []contextPredicate{isNonJsxSameLineTokenContext, isNotBinaryOpContext, isNotTypeAnnotationContext}, ruleActionDeleteSpace), - rule("SpaceAfterColon", ast.KindColonToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isNotBinaryOpContext, isNextTokenParentNotJsxNamespacedName}, ruleActionInsertSpace), - rule("NoSpaceBeforeQuestionMark", anyToken, ast.KindQuestionToken, []contextPredicate{isNonJsxSameLineTokenContext, isNotBinaryOpContext, isNotTypeAnnotationContext}, ruleActionDeleteSpace), - // insert space after '?' only when it is used in conditional operator - rule("SpaceAfterQuestionMarkInConditionalOperator", ast.KindQuestionToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isConditionalOperatorContext}, ruleActionInsertSpace), - - // in other cases there should be no space between '?' and next token - rule("NoSpaceAfterQuestionMark", ast.KindQuestionToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isNonOptionalPropertyContext}, ruleActionDeleteSpace), - - rule("NoSpaceBeforeDot", anyToken, []ast.Kind{ast.KindDotToken, ast.KindQuestionDotToken}, []contextPredicate{isNonJsxSameLineTokenContext, isNotPropertyAccessOnIntegerLiteral}, ruleActionDeleteSpace), - rule("NoSpaceAfterDot", []ast.Kind{ast.KindDotToken, ast.KindQuestionDotToken}, anyToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - - rule("NoSpaceBetweenImportParenInImportType", ast.KindImportKeyword, ast.KindOpenParenToken, []contextPredicate{isNonJsxSameLineTokenContext, isImportTypeContext}, ruleActionDeleteSpace), - - // Special handling of unary operators. - // Prefix operators generally shouldn't have a space between - // them and their target unary expression. - rule("NoSpaceAfterUnaryPrefixOperator", unaryPrefixOperators, unaryPrefixExpressions, []contextPredicate{isNonJsxSameLineTokenContext, isNotBinaryOpContext}, ruleActionDeleteSpace), - rule("NoSpaceAfterUnaryPreincrementOperator", ast.KindPlusPlusToken, unaryPreincrementExpressions, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - rule("NoSpaceAfterUnaryPredecrementOperator", ast.KindMinusMinusToken, unaryPredecrementExpressions, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - rule("NoSpaceBeforeUnaryPostincrementOperator", unaryPostincrementExpressions, ast.KindPlusPlusToken, []contextPredicate{isNonJsxSameLineTokenContext, isNotStatementConditionContext}, ruleActionDeleteSpace), - rule("NoSpaceBeforeUnaryPostdecrementOperator", unaryPostdecrementExpressions, ast.KindMinusMinusToken, []contextPredicate{isNonJsxSameLineTokenContext, isNotStatementConditionContext}, ruleActionDeleteSpace), - - // More unary operator special-casing. - // DevDiv 181814: Be careful when removing leading whitespace - // around unary operators. Examples: - // 1 - -2 --X--> 1--2 - // a + ++b --X--> a+++b - rule("SpaceAfterPostincrementWhenFollowedByAdd", ast.KindPlusPlusToken, ast.KindPlusToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace), - rule("SpaceAfterAddWhenFollowedByUnaryPlus", ast.KindPlusToken, ast.KindPlusToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace), - rule("SpaceAfterAddWhenFollowedByPreincrement", ast.KindPlusToken, ast.KindPlusPlusToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace), - rule("SpaceAfterPostdecrementWhenFollowedBySubtract", ast.KindMinusMinusToken, ast.KindMinusToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace), - rule("SpaceAfterSubtractWhenFollowedByUnaryMinus", ast.KindMinusToken, ast.KindMinusToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace), - rule("SpaceAfterSubtractWhenFollowedByPredecrement", ast.KindMinusToken, ast.KindMinusMinusToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace), - - rule("NoSpaceAfterCloseBrace", ast.KindCloseBraceToken, []ast.Kind{ast.KindCommaToken, ast.KindSemicolonToken}, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - // For functions and control block place } on a new line []ast.Kind{multi-line rule} - rule("NewLineBeforeCloseBraceInBlockContext", anyTokenIncludingMultilineComments, ast.KindCloseBraceToken, []contextPredicate{isMultilineBlockContext}, ruleActionInsertNewLine), - - // Space/new line after }. - rule("SpaceAfterCloseBrace", ast.KindCloseBraceToken, anyTokenExcept(ast.KindCloseParenToken), []contextPredicate{isNonJsxSameLineTokenContext, isAfterCodeBlockContext}, ruleActionInsertSpace), - // Special case for (}, else) and (}, while) since else & while tokens are not part of the tree which makes SpaceAfterCloseBrace rule not applied - // Also should not apply to }) - rule("SpaceBetweenCloseBraceAndElse", ast.KindCloseBraceToken, ast.KindElseKeyword, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("SpaceBetweenCloseBraceAndWhile", ast.KindCloseBraceToken, ast.KindWhileKeyword, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("NoSpaceBetweenEmptyBraceBrackets", ast.KindOpenBraceToken, ast.KindCloseBraceToken, []contextPredicate{isNonJsxSameLineTokenContext, isObjectContext}, ruleActionDeleteSpace), - - // Add a space after control dec context if the next character is an open bracket ex: 'if (false)[]ast.Kind{a, b} = []ast.Kind{1, 2};' -> 'if (false) []ast.Kind{a, b} = []ast.Kind{1, 2};' - rule("SpaceAfterConditionalClosingParen", ast.KindCloseParenToken, ast.KindOpenBracketToken, []contextPredicate{isControlDeclContext}, ruleActionInsertSpace), - - rule("NoSpaceBetweenFunctionKeywordAndStar", ast.KindFunctionKeyword, ast.KindAsteriskToken, []contextPredicate{isFunctionDeclarationOrFunctionExpressionContext}, ruleActionDeleteSpace), - rule("SpaceAfterStarInGeneratorDeclaration", ast.KindAsteriskToken, ast.KindIdentifier, []contextPredicate{isFunctionDeclarationOrFunctionExpressionContext}, ruleActionInsertSpace), - - rule("SpaceAfterFunctionInFuncDecl", ast.KindFunctionKeyword, anyToken, []contextPredicate{isFunctionDeclContext}, ruleActionInsertSpace), - // Insert new line after { and before } in multi-line contexts. - rule("NewLineAfterOpenBraceInBlockContext", ast.KindOpenBraceToken, anyToken, []contextPredicate{isMultilineBlockContext}, ruleActionInsertNewLine), - - // For get/set members, we check for (identifier,identifier) since get/set don't have tokens and they are represented as just an identifier token. - // Though, we do extra check on the context to make sure we are dealing with get/set node. Example: - // get x() {} - // set x(val) {} - rule("SpaceAfterGetSetInMember", []ast.Kind{ast.KindGetKeyword, ast.KindSetKeyword}, ast.KindIdentifier, []contextPredicate{isFunctionDeclContext}, ruleActionInsertSpace), - - rule("NoSpaceBetweenYieldKeywordAndStar", ast.KindYieldKeyword, ast.KindAsteriskToken, []contextPredicate{isNonJsxSameLineTokenContext, isYieldOrYieldStarWithOperand}, ruleActionDeleteSpace), - rule("SpaceBetweenYieldOrYieldStarAndOperand", []ast.Kind{ast.KindYieldKeyword, ast.KindAsteriskToken}, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isYieldOrYieldStarWithOperand}, ruleActionInsertSpace), - - rule("NoSpaceBetweenReturnAndSemicolon", ast.KindReturnKeyword, ast.KindSemicolonToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - rule("SpaceAfterCertainKeywords", []ast.Kind{ast.KindVarKeyword, ast.KindThrowKeyword, ast.KindNewKeyword, ast.KindDeleteKeyword, ast.KindReturnKeyword, ast.KindTypeOfKeyword, ast.KindAwaitKeyword}, anyToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("SpaceAfterLetConstInVariableDeclaration", []ast.Kind{ast.KindLetKeyword, ast.KindConstKeyword}, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isStartOfVariableDeclarationList}, ruleActionInsertSpace), - rule("NoSpaceBeforeOpenParenInFuncCall", anyToken, ast.KindOpenParenToken, []contextPredicate{isNonJsxSameLineTokenContext, isFunctionCallOrNewContext, isPreviousTokenNotComma}, ruleActionDeleteSpace), - - // Special case for binary operators (that are keywords). For these we have to add a space and shouldn't follow any user options. - rule("SpaceBeforeBinaryKeywordOperator", anyToken, binaryKeywordOperators, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace), - rule("SpaceAfterBinaryKeywordOperator", binaryKeywordOperators, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace), - - rule("SpaceAfterVoidOperator", ast.KindVoidKeyword, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isVoidOpContext}, ruleActionInsertSpace), - - // Async-await - rule("SpaceBetweenAsyncAndOpenParen", ast.KindAsyncKeyword, ast.KindOpenParenToken, []contextPredicate{isArrowFunctionContext, isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("SpaceBetweenAsyncAndFunctionKeyword", ast.KindAsyncKeyword, []ast.Kind{ast.KindFunctionKeyword, ast.KindIdentifier}, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - - // Template string - rule("NoSpaceBetweenTagAndTemplateString", []ast.Kind{ast.KindIdentifier, ast.KindCloseParenToken}, []ast.Kind{ast.KindNoSubstitutionTemplateLiteral, ast.KindTemplateHead}, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - - // JSX opening elements - rule("SpaceBeforeJsxAttribute", anyToken, ast.KindIdentifier, []contextPredicate{isNextTokenParentJsxAttribute, isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("SpaceBeforeSlashInJsxOpeningElement", anyToken, ast.KindSlashToken, []contextPredicate{isJsxSelfClosingElementContext, isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("NoSpaceBeforeGreaterThanTokenInJsxOpeningElement", ast.KindSlashToken, ast.KindGreaterThanToken, []contextPredicate{isJsxSelfClosingElementContext, isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - rule("NoSpaceBeforeEqualInJsxAttribute", anyToken, ast.KindEqualsToken, []contextPredicate{isJsxAttributeContext, isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - rule("NoSpaceAfterEqualInJsxAttribute", ast.KindEqualsToken, anyToken, []contextPredicate{isJsxAttributeContext, isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - rule("NoSpaceBeforeJsxNamespaceColon", ast.KindIdentifier, ast.KindColonToken, []contextPredicate{isNextTokenParentJsxNamespacedName}, ruleActionDeleteSpace), - rule("NoSpaceAfterJsxNamespaceColon", ast.KindColonToken, ast.KindIdentifier, []contextPredicate{isNextTokenParentJsxNamespacedName}, ruleActionDeleteSpace), - - // TypeScript-specific rules - // Use of module as a function call. e.g.: import m2 = module("m2"); - rule("NoSpaceAfterModuleImport", []ast.Kind{ast.KindModuleKeyword, ast.KindRequireKeyword}, ast.KindOpenParenToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - // Add a space around certain TypeScript keywords - rule( - "SpaceAfterCertainTypeScriptKeywords", - []ast.Kind{ - ast.KindAbstractKeyword, - ast.KindAccessorKeyword, - ast.KindClassKeyword, - ast.KindDeclareKeyword, - ast.KindDefaultKeyword, - ast.KindEnumKeyword, - ast.KindExportKeyword, - ast.KindExtendsKeyword, - ast.KindGetKeyword, - ast.KindImplementsKeyword, - ast.KindImportKeyword, - ast.KindInterfaceKeyword, - ast.KindModuleKeyword, - ast.KindNamespaceKeyword, - ast.KindPrivateKeyword, - ast.KindPublicKeyword, - ast.KindProtectedKeyword, - ast.KindReadonlyKeyword, - ast.KindSetKeyword, - ast.KindStaticKeyword, - ast.KindTypeKeyword, - ast.KindFromKeyword, - ast.KindKeyOfKeyword, - ast.KindInferKeyword, - }, - anyToken, - []contextPredicate{isNonJsxSameLineTokenContext}, - ruleActionInsertSpace, - ), - rule( - "SpaceBeforeCertainTypeScriptKeywords", - anyToken, - []ast.Kind{ast.KindExtendsKeyword, ast.KindImplementsKeyword, ast.KindFromKeyword}, - []contextPredicate{isNonJsxSameLineTokenContext}, - ruleActionInsertSpace, - ), - // Treat string literals in module names as identifiers, and add a space between the literal and the opening Brace braces, e.g.: module "m2" { - rule("SpaceAfterModuleName", ast.KindStringLiteral, ast.KindOpenBraceToken, []contextPredicate{isModuleDeclContext}, ruleActionInsertSpace), - - // Lambda expressions - rule("SpaceBeforeArrow", anyToken, ast.KindEqualsGreaterThanToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("SpaceAfterArrow", ast.KindEqualsGreaterThanToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - - // Optional parameters and let args - rule("NoSpaceAfterEllipsis", ast.KindDotDotDotToken, ast.KindIdentifier, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - rule("NoSpaceAfterOptionalParameters", ast.KindQuestionToken, []ast.Kind{ast.KindCloseParenToken, ast.KindCommaToken}, []contextPredicate{isNonJsxSameLineTokenContext, isNotBinaryOpContext}, ruleActionDeleteSpace), - - // Remove spaces in empty interface literals. e.g.: x: {} - rule("NoSpaceBetweenEmptyInterfaceBraceBrackets", ast.KindOpenBraceToken, ast.KindCloseBraceToken, []contextPredicate{isNonJsxSameLineTokenContext, isObjectTypeContext}, ruleActionDeleteSpace), - - // generics and type assertions - rule("NoSpaceBeforeOpenAngularBracket", typeNames, ast.KindLessThanToken, []contextPredicate{isNonJsxSameLineTokenContext, isTypeArgumentOrParameterOrAssertionContext}, ruleActionDeleteSpace), - rule("NoSpaceBetweenCloseParenAndAngularBracket", ast.KindCloseParenToken, ast.KindLessThanToken, []contextPredicate{isNonJsxSameLineTokenContext, isTypeArgumentOrParameterOrAssertionContext}, ruleActionDeleteSpace), - rule("NoSpaceAfterOpenAngularBracket", ast.KindLessThanToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isTypeArgumentOrParameterOrAssertionContext}, ruleActionDeleteSpace), - rule("NoSpaceBeforeCloseAngularBracket", anyToken, ast.KindGreaterThanToken, []contextPredicate{isNonJsxSameLineTokenContext, isTypeArgumentOrParameterOrAssertionContext}, ruleActionDeleteSpace), - rule("NoSpaceAfterCloseAngularBracket", ast.KindGreaterThanToken, []ast.Kind{ast.KindOpenParenToken, ast.KindOpenBracketToken, ast.KindGreaterThanToken, ast.KindCommaToken}, []contextPredicate{ - isNonJsxSameLineTokenContext, - isTypeArgumentOrParameterOrAssertionContext, - isNotFunctionDeclContext, /*To prevent an interference with the SpaceBeforeOpenParenInFuncDecl rule*/ - isNonTypeAssertionContext, - }, ruleActionDeleteSpace), - - // decorators - rule("SpaceBeforeAt", []ast.Kind{ast.KindCloseParenToken, ast.KindIdentifier}, ast.KindAtToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("NoSpaceAfterAt", ast.KindAtToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - // Insert space after @ in decorator - rule( - "SpaceAfterDecorator", - anyToken, - []ast.Kind{ - ast.KindAbstractKeyword, - ast.KindIdentifier, - ast.KindExportKeyword, - ast.KindDefaultKeyword, - ast.KindClassKeyword, - ast.KindStaticKeyword, - ast.KindPublicKeyword, - ast.KindPrivateKeyword, - ast.KindProtectedKeyword, - ast.KindGetKeyword, - ast.KindSetKeyword, - ast.KindOpenBracketToken, - ast.KindAsteriskToken, - }, - []contextPredicate{isEndOfDecoratorContextOnSameLine}, - ruleActionInsertSpace, - ), - - rule("NoSpaceBeforeNonNullAssertionOperator", anyToken, ast.KindExclamationToken, []contextPredicate{isNonJsxSameLineTokenContext, isNonNullAssertionContext}, ruleActionDeleteSpace), - rule("NoSpaceAfterNewKeywordOnConstructorSignature", ast.KindNewKeyword, ast.KindOpenParenToken, []contextPredicate{isNonJsxSameLineTokenContext, isConstructorSignatureContext}, ruleActionDeleteSpace), - rule("SpaceLessThanAndNonJSXTypeAnnotation", ast.KindLessThanToken, ast.KindLessThanToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - } - - // These rules are applied after high priority - userConfigurableRules := []ruleSpec{ - // Treat constructor as an identifier in a function declaration, and remove spaces between constructor and following left parentheses - rule("SpaceAfterConstructor", ast.KindConstructorKeyword, ast.KindOpenParenToken, []contextPredicate{isOptionEnabled(insertSpaceAfterConstructorOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("NoSpaceAfterConstructor", ast.KindConstructorKeyword, ast.KindOpenParenToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterConstructorOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - - rule("SpaceAfterComma", ast.KindCommaToken, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterCommaDelimiterOption), isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext, isNextTokenNotCloseBracket, isNextTokenNotCloseParen}, ruleActionInsertSpace), - rule("NoSpaceAfterComma", ast.KindCommaToken, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterCommaDelimiterOption), isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext}, ruleActionDeleteSpace), - - // Insert space after function keyword for anonymous functions - rule("SpaceAfterAnonymousFunctionKeyword", []ast.Kind{ast.KindFunctionKeyword, ast.KindAsteriskToken}, ast.KindOpenParenToken, []contextPredicate{isOptionEnabled(insertSpaceAfterFunctionKeywordForAnonymousFunctionsOption), isFunctionDeclContext}, ruleActionInsertSpace), - rule("NoSpaceAfterAnonymousFunctionKeyword", []ast.Kind{ast.KindFunctionKeyword, ast.KindAsteriskToken}, ast.KindOpenParenToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterFunctionKeywordForAnonymousFunctionsOption), isFunctionDeclContext}, ruleActionDeleteSpace), - - // Insert space after keywords in control flow statements - rule("SpaceAfterKeywordInControl", keywords, ast.KindOpenParenToken, []contextPredicate{isOptionEnabled(insertSpaceAfterKeywordsInControlFlowStatementsOption), isControlDeclContext}, ruleActionInsertSpace), - rule("NoSpaceAfterKeywordInControl", keywords, ast.KindOpenParenToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterKeywordsInControlFlowStatementsOption), isControlDeclContext}, ruleActionDeleteSpace), - - // Insert space after opening and before closing nonempty parenthesis - rule("SpaceAfterOpenParen", ast.KindOpenParenToken, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("SpaceBeforeCloseParen", anyToken, ast.KindCloseParenToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("SpaceBetweenOpenParens", ast.KindOpenParenToken, ast.KindOpenParenToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("NoSpaceBetweenParens", ast.KindOpenParenToken, ast.KindCloseParenToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - rule("NoSpaceAfterOpenParen", ast.KindOpenParenToken, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - rule("NoSpaceBeforeCloseParen", anyToken, ast.KindCloseParenToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - - // Insert space after opening and before closing nonempty brackets - rule("SpaceAfterOpenBracket", ast.KindOpenBracketToken, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracketsOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("SpaceBeforeCloseBracket", anyToken, ast.KindCloseBracketToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracketsOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("NoSpaceBetweenBrackets", ast.KindOpenBracketToken, ast.KindCloseBracketToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - rule("NoSpaceAfterOpenBracket", ast.KindOpenBracketToken, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracketsOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - rule("NoSpaceBeforeCloseBracket", anyToken, ast.KindCloseBracketToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracketsOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - - // Insert a space after { and before } in single-line contexts, but remove space from empty object literals {}. - rule("SpaceAfterOpenBrace", ast.KindOpenBraceToken, anyToken, []contextPredicate{isOptionEnabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracesOption), isBraceWrappedContext}, ruleActionInsertSpace), - rule("SpaceBeforeCloseBrace", anyToken, ast.KindCloseBraceToken, []contextPredicate{isOptionEnabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracesOption), isBraceWrappedContext}, ruleActionInsertSpace), - rule("NoSpaceBetweenEmptyBraceBrackets", ast.KindOpenBraceToken, ast.KindCloseBraceToken, []contextPredicate{isNonJsxSameLineTokenContext, isObjectContext}, ruleActionDeleteSpace), - rule("NoSpaceAfterOpenBrace", ast.KindOpenBraceToken, anyToken, []contextPredicate{isOptionDisabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracesOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - rule("NoSpaceBeforeCloseBrace", anyToken, ast.KindCloseBraceToken, []contextPredicate{isOptionDisabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracesOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - - // Insert a space after opening and before closing empty brace brackets - rule("SpaceBetweenEmptyBraceBrackets", ast.KindOpenBraceToken, ast.KindCloseBraceToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingEmptyBracesOption)}, ruleActionInsertSpace), - rule("NoSpaceBetweenEmptyBraceBrackets", ast.KindOpenBraceToken, ast.KindCloseBraceToken, []contextPredicate{isOptionDisabled(insertSpaceAfterOpeningAndBeforeClosingEmptyBracesOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - - // Insert space after opening and before closing template string braces - rule("SpaceAfterTemplateHeadAndMiddle", []ast.Kind{ast.KindTemplateHead, ast.KindTemplateMiddle}, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingTemplateStringBracesOption), isNonJsxTextContext}, ruleActionInsertSpace, ruleFlagsCanDeleteNewLines), - rule("SpaceBeforeTemplateMiddleAndTail", anyToken, []ast.Kind{ast.KindTemplateMiddle, ast.KindTemplateTail}, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingTemplateStringBracesOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - rule("NoSpaceAfterTemplateHeadAndMiddle", []ast.Kind{ast.KindTemplateHead, ast.KindTemplateMiddle}, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingTemplateStringBracesOption), isNonJsxTextContext}, ruleActionDeleteSpace, ruleFlagsCanDeleteNewLines), - rule("NoSpaceBeforeTemplateMiddleAndTail", anyToken, []ast.Kind{ast.KindTemplateMiddle, ast.KindTemplateTail}, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingTemplateStringBracesOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - - // No space after { and before } in JSX expression - rule("SpaceAfterOpenBraceInJsxExpression", ast.KindOpenBraceToken, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBracesOption), isNonJsxSameLineTokenContext, isJsxExpressionContext}, ruleActionInsertSpace), - rule("SpaceBeforeCloseBraceInJsxExpression", anyToken, ast.KindCloseBraceToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBracesOption), isNonJsxSameLineTokenContext, isJsxExpressionContext}, ruleActionInsertSpace), - rule("NoSpaceAfterOpenBraceInJsxExpression", ast.KindOpenBraceToken, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBracesOption), isNonJsxSameLineTokenContext, isJsxExpressionContext}, ruleActionDeleteSpace), - rule("NoSpaceBeforeCloseBraceInJsxExpression", anyToken, ast.KindCloseBraceToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBracesOption), isNonJsxSameLineTokenContext, isJsxExpressionContext}, ruleActionDeleteSpace), - - // Insert space after semicolon in for statement - rule("SpaceAfterSemicolonInFor", ast.KindSemicolonToken, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterSemicolonInForStatementsOption), isNonJsxSameLineTokenContext, isForContext}, ruleActionInsertSpace), - rule("NoSpaceAfterSemicolonInFor", ast.KindSemicolonToken, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterSemicolonInForStatementsOption), isNonJsxSameLineTokenContext, isForContext}, ruleActionDeleteSpace), - - // Insert space before and after binary operators - rule("SpaceBeforeBinaryOperator", anyToken, binaryOperators, []contextPredicate{isOptionEnabled(insertSpaceBeforeAndAfterBinaryOperatorsOption), isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace), - rule("SpaceAfterBinaryOperator", binaryOperators, anyToken, []contextPredicate{isOptionEnabled(insertSpaceBeforeAndAfterBinaryOperatorsOption), isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace), - rule("NoSpaceBeforeBinaryOperator", anyToken, binaryOperators, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceBeforeAndAfterBinaryOperatorsOption), isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionDeleteSpace), - rule("NoSpaceAfterBinaryOperator", binaryOperators, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceBeforeAndAfterBinaryOperatorsOption), isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionDeleteSpace), - - rule("SpaceBeforeOpenParenInFuncDecl", anyToken, ast.KindOpenParenToken, []contextPredicate{isOptionEnabled(insertSpaceBeforeFunctionParenthesisOption), isNonJsxSameLineTokenContext, isFunctionDeclContext}, ruleActionInsertSpace), - rule("NoSpaceBeforeOpenParenInFuncDecl", anyToken, ast.KindOpenParenToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceBeforeFunctionParenthesisOption), isNonJsxSameLineTokenContext, isFunctionDeclContext}, ruleActionDeleteSpace), - - // Open Brace braces after control block - rule("NewLineBeforeOpenBraceInControl", controlOpenBraceLeftTokenRange, ast.KindOpenBraceToken, []contextPredicate{isOptionEnabled(placeOpenBraceOnNewLineForControlBlocksOption), isControlDeclContext, isBeforeMultilineBlockContext}, ruleActionInsertNewLine, ruleFlagsCanDeleteNewLines), - - // Open Brace braces after function - // TypeScript: Function can have return types, which can be made of tons of different token kinds - rule("NewLineBeforeOpenBraceInFunction", functionOpenBraceLeftTokenRange, ast.KindOpenBraceToken, []contextPredicate{isOptionEnabled(placeOpenBraceOnNewLineForFunctionsOption), isFunctionDeclContext, isBeforeMultilineBlockContext}, ruleActionInsertNewLine, ruleFlagsCanDeleteNewLines), - // Open Brace braces after TypeScript module/class/interface - rule("NewLineBeforeOpenBraceInTypeScriptDeclWithBlock", typeScriptOpenBraceLeftTokenRange, ast.KindOpenBraceToken, []contextPredicate{isOptionEnabled(placeOpenBraceOnNewLineForFunctionsOption), isTypeScriptDeclWithBlockContext, isBeforeMultilineBlockContext}, ruleActionInsertNewLine, ruleFlagsCanDeleteNewLines), - - rule("SpaceAfterTypeAssertion", ast.KindGreaterThanToken, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterTypeAssertionOption), isNonJsxSameLineTokenContext, isTypeAssertionContext}, ruleActionInsertSpace), - rule("NoSpaceAfterTypeAssertion", ast.KindGreaterThanToken, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterTypeAssertionOption), isNonJsxSameLineTokenContext, isTypeAssertionContext}, ruleActionDeleteSpace), - - rule("SpaceBeforeTypeAnnotation", anyToken, []ast.Kind{ast.KindQuestionToken, ast.KindColonToken}, []contextPredicate{isOptionEnabled(insertSpaceBeforeTypeAnnotationOption), isNonJsxSameLineTokenContext, isTypeAnnotationContext}, ruleActionInsertSpace), - rule("NoSpaceBeforeTypeAnnotation", anyToken, []ast.Kind{ast.KindQuestionToken, ast.KindColonToken}, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceBeforeTypeAnnotationOption), isNonJsxSameLineTokenContext, isTypeAnnotationContext}, ruleActionDeleteSpace), - - rule("NoOptionalSemicolon", ast.KindSemicolonToken, anyTokenIncludingEOF, []contextPredicate{optionEquals(semicolonOption, SemicolonPreferenceRemove), isSemicolonDeletionContext}, ruleActionDeleteToken), - rule("OptionalSemicolon", anyToken, anyTokenIncludingEOF, []contextPredicate{optionEquals(semicolonOption, SemicolonPreferenceInsert), isSemicolonInsertionContext}, ruleActionInsertTrailingSemicolon), - } - - // These rules are lower in priority than user-configurable. Rules earlier in this list have priority over rules later in the list. - lowPriorityCommonRules := []ruleSpec{ - // Space after keyword but not before ; or : or ? - rule("NoSpaceBeforeSemicolon", anyToken, ast.KindSemicolonToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - - rule("SpaceBeforeOpenBraceInControl", controlOpenBraceLeftTokenRange, ast.KindOpenBraceToken, []contextPredicate{isOptionDisabledOrUndefinedOrTokensOnSameLine(placeOpenBraceOnNewLineForControlBlocksOption), isControlDeclContext, isNotFormatOnEnter, isSameLineTokenOrBeforeBlockContext}, ruleActionInsertSpace, ruleFlagsCanDeleteNewLines), - rule("SpaceBeforeOpenBraceInFunction", functionOpenBraceLeftTokenRange, ast.KindOpenBraceToken, []contextPredicate{isOptionDisabledOrUndefinedOrTokensOnSameLine(placeOpenBraceOnNewLineForFunctionsOption), isFunctionDeclContext, isBeforeBlockContext, isNotFormatOnEnter, isSameLineTokenOrBeforeBlockContext}, ruleActionInsertSpace, ruleFlagsCanDeleteNewLines), - rule("SpaceBeforeOpenBraceInTypeScriptDeclWithBlock", typeScriptOpenBraceLeftTokenRange, ast.KindOpenBraceToken, []contextPredicate{isOptionDisabledOrUndefinedOrTokensOnSameLine(placeOpenBraceOnNewLineForFunctionsOption), isTypeScriptDeclWithBlockContext, isNotFormatOnEnter, isSameLineTokenOrBeforeBlockContext}, ruleActionInsertSpace, ruleFlagsCanDeleteNewLines), - - rule("NoSpaceBeforeComma", anyToken, ast.KindCommaToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - - // No space before and after indexer `x[]ast.Kind{}` - rule("NoSpaceBeforeOpenBracket", anyTokenExcept(ast.KindAsyncKeyword, ast.KindCaseKeyword), ast.KindOpenBracketToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - rule("NoSpaceAfterCloseBracket", ast.KindCloseBracketToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isNotBeforeBlockInFunctionDeclarationContext}, ruleActionDeleteSpace), - rule("SpaceAfterSemicolon", ast.KindSemicolonToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - - // Remove extra space between for and await - rule("SpaceBetweenForAndAwaitKeyword", ast.KindForKeyword, ast.KindAwaitKeyword, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - - // Remove extra spaces between ... and type name in tuple spread - rule("SpaceBetweenDotDotDotAndTypeName", ast.KindDotDotDotToken, typeNames, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace), - - // Add a space between statements. All keywords except (do,else,case) has open/close parens after them. - // So, we have a rule to add a space for []ast.Kind{),Any}, []ast.Kind{do,Any}, []ast.Kind{else,Any}, and []ast.Kind{case,Any} - rule( - "SpaceBetweenStatements", - []ast.Kind{ast.KindCloseParenToken, ast.KindDoKeyword, ast.KindElseKeyword, ast.KindCaseKeyword}, - anyToken, - []contextPredicate{isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext, isNotForContext}, - ruleActionInsertSpace, - ), - // This low-pri rule takes care of "try {", "catch {" and "finally {" in case the rule SpaceBeforeOpenBraceInControl didn't execute on FormatOnEnter. - rule("SpaceAfterTryCatchFinally", []ast.Kind{ast.KindTryKeyword, ast.KindCatchKeyword, ast.KindFinallyKeyword}, ast.KindOpenBraceToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace), - } - - result := make([]ruleSpec, 0, len(highPriorityCommonRules)+len(userConfigurableRules)+len(lowPriorityCommonRules)) - result = append(result, highPriorityCommonRules...) - result = append(result, userConfigurableRules...) - result = append(result, lowPriorityCommonRules...) - return result -} - -func tokenRangeFrom(tokens ...ast.Kind) tokenRange { - return tokenRange{ - isSpecific: true, - tokens: tokens, - } -} - -func tokenRangeFromEx(prefix []ast.Kind, tokens ...ast.Kind) tokenRange { - tokens = append(prefix, tokens...) - return tokenRange{ - isSpecific: true, - tokens: tokens, - } -} - -func tokenRangeFromRange(start ast.Kind, end ast.Kind) tokenRange { - tokens := make([]ast.Kind, 0, end-start+1) - for token := start; token <= end; token++ { - tokens = append(tokens, token) - } - - return tokenRangeFrom(tokens...) -} diff --git a/kitcom/internal/tsgo/format/rulesmap.go b/kitcom/internal/tsgo/format/rulesmap.go deleted file mode 100644 index 38e8748..0000000 --- a/kitcom/internal/tsgo/format/rulesmap.go +++ /dev/null @@ -1,156 +0,0 @@ -package format - -import ( - "slices" - "sync" - - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/debug" -) - -func getRules(context *formattingContext, rules []*ruleImpl) []*ruleImpl { - bucket := getRulesMap()[getRuleBucketIndex(context.currentTokenSpan.Kind, context.nextTokenSpan.Kind)] - if len(bucket) > 0 { - ruleActionMask := ruleActionNone - outer: - for _, rule := range bucket { - acceptRuleActions := ^getRuleActionExclusion(ruleActionMask) - if rule.Action()&acceptRuleActions != 0 { - preds := rule.Context() - for _, p := range preds { - if !p(context) { - continue outer - } - } - rules = append(rules, rule) - ruleActionMask |= rule.Action() - } - } - return rules - } - return rules -} - -func getRuleBucketIndex(row ast.Kind, column ast.Kind) int { - debug.Assert(row <= ast.KindLastKeyword && column <= ast.KindLastKeyword, "Must compute formatting context from tokens") - return (int(row) * mapRowLength) + int(column) -} - -const ( - maskBitSize = 5 - mask = 0b11111 // MaskBitSize bits - mapRowLength = int(ast.KindLastToken) + 1 -) - -/** - * For a given rule action, gets a mask of other rule actions that - * cannot be applied at the same position. - */ -func getRuleActionExclusion(ruleAction ruleAction) ruleAction { - mask := ruleActionNone - if ruleAction&ruleActionStopProcessingSpaceActions != 0 { - mask |= ruleActionModifySpaceAction - } - if ruleAction&ruleActionStopProcessingTokenActions != 0 { - mask |= ruleActionModifyTokenAction - } - if ruleAction&ruleActionModifySpaceAction != 0 { - mask |= ruleActionModifySpaceAction - } - if ruleAction&ruleActionModifyTokenAction != 0 { - mask |= ruleActionModifyTokenAction - } - return mask -} - -var getRulesMap = sync.OnceValue(buildRulesMap) - -func buildRulesMap() [][]*ruleImpl { - rules := getAllRules() - // Map from bucket index to array of rules - m := make([][]*ruleImpl, mapRowLength*mapRowLength) - // This array is used only during construction of the rulesbucket in the map - rulesBucketConstructionStateList := make([]int, len(m)) - for _, rule := range rules { - specificRule := rule.leftTokenRange.isSpecific && rule.rightTokenRange.isSpecific - - for _, left := range rule.leftTokenRange.tokens { - for _, right := range rule.rightTokenRange.tokens { - index := getRuleBucketIndex(left, right) - m[index] = addRule(m[index], rule.rule, specificRule, rulesBucketConstructionStateList, index) - } - } - } - return m -} - -type RulesPosition int - -const ( - RulesPositionStopRulesSpecific RulesPosition = 0 - RulesPositionStopRulesAny RulesPosition = maskBitSize * 1 - RulesPositionContextRulesSpecific RulesPosition = maskBitSize * 2 - RulesPositionContextRulesAny RulesPosition = maskBitSize * 3 - RulesPositionNoContextRulesSpecific RulesPosition = maskBitSize * 4 - RulesPositionNoContextRulesAny RulesPosition = maskBitSize * 5 -) - -// The Rules list contains all the inserted rules into a rulebucket in the following order: -// -// 1- Ignore rules with specific token combination -// 2- Ignore rules with any token combination -// 3- Context rules with specific token combination -// 4- Context rules with any token combination -// 5- Non-context rules with specific token combination -// 6- Non-context rules with any token combination -// -// The member rulesInsertionIndexBitmap is used to describe the number of rules -// in each sub-bucket (above) hence can be used to know the index of where to insert -// the next rule. It's a bitmap which contains 6 different sections each is given 5 bits. -// -// Example: -// In order to insert a rule to the end of sub-bucket (3), we get the index by adding -// the values in the bitmap segments 3rd, 2nd, and 1st. -func addRule(rules []*ruleImpl, rule *ruleImpl, specificTokens bool, constructionState []int, rulesBucketIndex int) []*ruleImpl { - var position RulesPosition - if rule.Action()&ruleActionStopAction != 0 { - if specificTokens { - position = RulesPositionStopRulesSpecific - } else { - position = RulesPositionStopRulesAny - } - } else if len(rule.Context()) != 0 { - if specificTokens { - position = RulesPositionContextRulesSpecific - } else { - position = RulesPositionContextRulesAny - } - } else { - if specificTokens { - position = RulesPositionNoContextRulesSpecific - } else { - position = RulesPositionNoContextRulesAny - } - } - - state := constructionState[rulesBucketIndex] - - rules = slices.Insert(rules, getRuleInsertionIndex(state, position), rule) - constructionState[rulesBucketIndex] = increaseInsertionIndex(state, position) - return rules -} - -func getRuleInsertionIndex(indexBitmap int, maskPosition RulesPosition) int { - index := 0 - for pos := 0; pos <= int(maskPosition); pos += maskBitSize { - index += indexBitmap & mask - indexBitmap >>= maskBitSize - } - return index -} - -func increaseInsertionIndex(indexBitmap int, maskPosition RulesPosition) int { - value := ((indexBitmap >> maskPosition) & mask) + 1 - debug.Assert((value&mask) == value, "Adding more rules into the sub-bucket than allowed. Maximum allowed is 32 rules.") - return (indexBitmap & ^(mask << maskPosition)) | (value << maskPosition) -} diff --git a/kitcom/internal/tsgo/format/scanner.go b/kitcom/internal/tsgo/format/scanner.go deleted file mode 100644 index aeb07d9..0000000 --- a/kitcom/internal/tsgo/format/scanner.go +++ /dev/null @@ -1,355 +0,0 @@ -package format - -import ( - "slices" - - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/core" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/debug" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/scanner" -) - -type TextRangeWithKind struct { - Loc core.TextRange - Kind ast.Kind -} - -func NewTextRangeWithKind(pos int, end int, kind ast.Kind) TextRangeWithKind { - return TextRangeWithKind{ - Loc: core.NewTextRange(pos, end), - Kind: kind, - } -} - -type tokenInfo struct { - leadingTrivia []TextRangeWithKind - token TextRangeWithKind - trailingTrivia []TextRangeWithKind -} - -type formattingScanner struct { - s *scanner.Scanner - startPos int - endPos int - savedPos int - hasLastTokenInfo bool - lastTokenInfo tokenInfo - lastScanAction scanAction - leadingTrivia []TextRangeWithKind - trailingTrivia []TextRangeWithKind - wasNewLine bool -} - -func newFormattingScanner(text string, languageVariant core.LanguageVariant, startPos int, endPos int, worker *formatSpanWorker) []core.TextChange { - scan := scanner.NewScanner() - scan.Reset() - scan.SetSkipTrivia(false) - scan.SetLanguageVariant(languageVariant) - scan.SetText(text) - - fmtScn := &formattingScanner{ - s: scan, - startPos: startPos, - endPos: endPos, - wasNewLine: true, - } - - res := worker.execute(fmtScn) - - fmtScn.hasLastTokenInfo = false - scan.Reset() - - return res -} - -func (s *formattingScanner) advance() { - s.hasLastTokenInfo = false - isStarted := s.s.TokenFullStart() != s.startPos - - if isStarted { - s.wasNewLine = len(s.trailingTrivia) > 0 && core.LastOrNil(s.trailingTrivia).Kind == ast.KindNewLineTrivia - } else { - s.s.Scan() - } - - s.leadingTrivia = nil - s.trailingTrivia = nil - - pos := s.s.TokenFullStart() - - // Read leading trivia and token - for pos < s.endPos { - t := s.s.Token() - if !ast.IsTrivia(t) { - break - } - - // consume leading trivia - s.s.Scan() - item := NewTextRangeWithKind(pos, s.s.TokenFullStart(), t) - - pos = s.s.TokenFullStart() - - s.leadingTrivia = append(s.leadingTrivia, item) - } - - s.savedPos = s.s.TokenFullStart() -} - -func shouldRescanGreaterThanToken(node *ast.Node) bool { - switch node.Kind { - case ast.KindGreaterThanEqualsToken, - ast.KindGreaterThanGreaterThanEqualsToken, - ast.KindGreaterThanGreaterThanGreaterThanEqualsToken, - ast.KindGreaterThanGreaterThanGreaterThanToken, - ast.KindGreaterThanGreaterThanToken: - return true - } - return false -} - -func shouldRescanJsxIdentifier(node *ast.Node) bool { - if node.Parent != nil { - switch node.Parent.Kind { - case ast.KindJsxAttribute, - ast.KindJsxOpeningElement, - ast.KindJsxClosingElement, - ast.KindJsxSelfClosingElement: - // May parse an identifier like `module-layout`; that will be scanned as a keyword at first, but we should parse the whole thing to get an identifier. - return ast.IsKeywordKind(node.Kind) || node.Kind == ast.KindIdentifier - } - } - return false -} - -func (s *formattingScanner) shouldRescanJsxText(node *ast.Node) bool { - if ast.IsJsxText(node) { - return true - } - if !ast.IsJsxElement(node) || s.hasLastTokenInfo == false { - return false - } - - return s.lastTokenInfo.token.Kind == ast.KindJsxText -} - -func shouldRescanSlashToken(container *ast.Node) bool { - return container.Kind == ast.KindRegularExpressionLiteral -} - -func shouldRescanTemplateToken(container *ast.Node) bool { - return container.Kind == ast.KindTemplateMiddle || - container.Kind == ast.KindTemplateTail -} - -func shouldRescanJsxAttributeValue(node *ast.Node) bool { - return node.Parent != nil && ast.IsJsxAttribute(node.Parent) && node.Parent.Initializer() == node -} - -func startsWithSlashToken(t ast.Kind) bool { - return t == ast.KindSlashToken || t == ast.KindSlashEqualsToken -} - -type scanAction int - -const ( - actionScan scanAction = iota - actionRescanGreaterThanToken - actionRescanSlashToken - actionRescanTemplateToken - actionRescanJsxIdentifier - actionRescanJsxText - actionRescanJsxAttributeValue -) - -func fixTokenKind(tokenInfo tokenInfo, container *ast.Node) tokenInfo { - if ast.IsTokenKind(container.Kind) && tokenInfo.token.Kind != container.Kind { - tokenInfo.token.Kind = container.Kind - } - return tokenInfo -} - -func (s *formattingScanner) readTokenInfo(n *ast.Node) tokenInfo { - debug.Assert(s.isOnToken()) - - // normally scanner returns the smallest available token - // check the kind of context node to determine if scanner should have more greedy behavior and consume more text. - - var expectedScanAction scanAction - if shouldRescanGreaterThanToken(n) { - expectedScanAction = actionRescanGreaterThanToken - } else if shouldRescanSlashToken(n) { - expectedScanAction = actionRescanSlashToken - } else if shouldRescanTemplateToken(n) { - expectedScanAction = actionRescanTemplateToken - } else if shouldRescanJsxIdentifier(n) { - expectedScanAction = actionRescanJsxIdentifier - } else if s.shouldRescanJsxText(n) { - expectedScanAction = actionRescanJsxText - } else if shouldRescanJsxAttributeValue(n) { - expectedScanAction = actionRescanJsxAttributeValue - } else { - expectedScanAction = actionScan - } - - if s.hasLastTokenInfo && expectedScanAction == s.lastScanAction { - // readTokenInfo was called before with the same expected scan action. - // No need to re-scan text, return existing 'lastTokenInfo' - // it is ok to call fixTokenKind here since it does not affect - // what portion of text is consumed. In contrast rescanning can change it, - // i.e. for '>=' when originally scanner eats just one character - // and rescanning forces it to consume more. - s.lastTokenInfo = fixTokenKind(s.lastTokenInfo, n) - return s.lastTokenInfo - } - - if s.s.TokenFullStart() != s.savedPos { - // readTokenInfo was called before but scan action differs - rescan text - s.s.ResetTokenState(s.savedPos) - s.s.Scan() - } - - currentToken := s.getNextToken(n, expectedScanAction) - - token := NewTextRangeWithKind( - s.s.TokenFullStart(), - s.s.TokenEnd(), - currentToken, - ) - - // consume trailing trivia - s.trailingTrivia = nil - for s.s.TokenFullStart() < s.endPos { - currentToken = s.s.Scan() - if !ast.IsTrivia(currentToken) { - break - } - trivia := NewTextRangeWithKind( - s.s.TokenFullStart(), - s.s.TokenEnd(), - currentToken, - ) - - s.trailingTrivia = append(s.trailingTrivia, trivia) - - if currentToken == ast.KindNewLineTrivia { - // move past new line - s.s.Scan() - break - } - } - - s.hasLastTokenInfo = true - s.lastTokenInfo = tokenInfo{ - leadingTrivia: slices.Clone(s.leadingTrivia), - token: token, - trailingTrivia: slices.Clone(s.trailingTrivia), - } - s.lastTokenInfo = fixTokenKind(s.lastTokenInfo, n) - - return s.lastTokenInfo -} - -func (s *formattingScanner) getNextToken(n *ast.Node, expectedScanAction scanAction) ast.Kind { - token := s.s.Token() - s.lastScanAction = actionScan - switch expectedScanAction { - case actionRescanGreaterThanToken: - if token == ast.KindGreaterThanToken { - s.lastScanAction = actionRescanGreaterThanToken - newToken := s.s.ReScanGreaterThanToken() - debug.Assert(n.Kind == newToken) - return newToken - } - case actionRescanSlashToken: - if startsWithSlashToken(token) { - s.lastScanAction = actionRescanSlashToken - newToken := s.s.ReScanSlashToken() - debug.Assert(n.Kind == newToken) - return newToken - } - case actionRescanTemplateToken: - if token == ast.KindCloseBraceToken { - s.lastScanAction = actionRescanTemplateToken - return s.s.ReScanTemplateToken( /*isTaggedTemplate*/ false) - } - case actionRescanJsxIdentifier: - s.lastScanAction = actionRescanJsxIdentifier - return s.s.ScanJsxIdentifier() - case actionRescanJsxText: - s.lastScanAction = actionRescanJsxText - return s.s.ReScanJsxToken( /*allowMultilineJsxText*/ false) - case actionRescanJsxAttributeValue: - s.lastScanAction = actionRescanJsxAttributeValue - return s.s.ReScanJsxAttributeValue() - case actionScan: - break - default: - debug.AssertNever(expectedScanAction, "unhandled scan action kind") - } - return token -} - -func (s *formattingScanner) readEOFTokenRange() TextRangeWithKind { - debug.Assert(s.isOnEOF()) - return NewTextRangeWithKind( - s.s.TokenFullStart(), - s.s.TokenEnd(), - ast.KindEndOfFile, - ) -} - -func (s *formattingScanner) isOnToken() bool { - current := s.s.Token() - if s.hasLastTokenInfo { - current = s.lastTokenInfo.token.Kind - } - return current != ast.KindEndOfFile && !ast.IsTrivia(current) -} - -func (s *formattingScanner) isOnEOF() bool { - current := s.s.Token() - if s.hasLastTokenInfo { - current = s.lastTokenInfo.token.Kind - } - return current == ast.KindEndOfFile -} - -func (s *formattingScanner) skipToEndOf(r *core.TextRange) { - s.s.ResetTokenState(r.End()) - s.savedPos = s.s.TokenFullStart() - s.lastScanAction = actionScan - s.hasLastTokenInfo = false - s.wasNewLine = false - s.leadingTrivia = nil - s.trailingTrivia = nil -} - -func (s *formattingScanner) skipToStartOf(r *core.TextRange) { - s.s.ResetTokenState(r.Pos()) - s.savedPos = s.s.TokenFullStart() - s.lastScanAction = actionScan - s.hasLastTokenInfo = false - s.wasNewLine = false - s.leadingTrivia = nil - s.trailingTrivia = nil -} - -func (s *formattingScanner) getCurrentLeadingTrivia() []TextRangeWithKind { - return s.leadingTrivia -} - -func (s *formattingScanner) lastTrailingTriviaWasNewLine() bool { - return s.wasNewLine -} - -func (s *formattingScanner) getTokenFullStart() int { - if s.hasLastTokenInfo { - return s.lastTokenInfo.token.Loc.Pos() - } - return s.s.TokenFullStart() -} - -func (s *formattingScanner) getStartPos() int { // TODO: redundant? - return s.getTokenFullStart() -} diff --git a/kitcom/internal/tsgo/format/span.go b/kitcom/internal/tsgo/format/span.go deleted file mode 100644 index faa9207..0000000 --- a/kitcom/internal/tsgo/format/span.go +++ /dev/null @@ -1,1215 +0,0 @@ -package format - -import ( - "context" - "math" - "slices" - "strings" - "unicode/utf8" - - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/astnav" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/core" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/debug" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/scanner" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/stringutil" -) - -/** find node that fully contains given text range */ -func findEnclosingNode(r core.TextRange, sourceFile *ast.SourceFile) *ast.Node { - var find func(*ast.Node) *ast.Node - find = func(n *ast.Node) *ast.Node { - var candidate *ast.Node - n.ForEachChild(func(c *ast.Node) bool { - if r.ContainedBy(withTokenStart(c, sourceFile)) { - candidate = c - return true - } - return false - }) - if candidate != nil { - result := find(candidate) - if result != nil { - return result - } - } - - return n - } - return find(sourceFile.AsNode()) -} - -/** - * Start of the original range might fall inside the comment - scanner will not yield appropriate results - * This function will look for token that is located before the start of target range - * and return its end as start position for the scanner. - */ -func getScanStartPosition(enclosingNode *ast.Node, originalRange core.TextRange, sourceFile *ast.SourceFile) int { - adjusted := withTokenStart(enclosingNode, sourceFile) - start := adjusted.Pos() - if start == originalRange.Pos() && enclosingNode.End() == originalRange.End() { - return start - } - - precedingToken := astnav.FindPrecedingToken(sourceFile, originalRange.Pos()) - if precedingToken == nil { - // no preceding token found - start from the beginning of enclosing node - return enclosingNode.Pos() - } - - // preceding token ends after the start of original range (i.e when originalRange.pos falls in the middle of literal) - // start from the beginning of enclosingNode to handle the entire 'originalRange' - if precedingToken.End() >= originalRange.Pos() { - return enclosingNode.Pos() - } - - return precedingToken.End() -} - -/* - * For cases like - * if (a || - * b ||$ - * c) {...} - * If we hit Enter at $ we want line ' b ||' to be indented. - * Formatting will be applied to the last two lines. - * Node that fully encloses these lines is binary expression 'a ||...'. - * Initial indentation for this node will be 0. - * Binary expressions don't introduce new indentation scopes, however it is possible - * that some parent node on the same line does - like if statement in this case. - * Note that we are considering parents only from the same line with initial node - - * if parent is on the different line - its delta was already contributed - * to the initial indentation. - */ -func getOwnOrInheritedDelta(n *ast.Node, options *FormatCodeSettings, sourceFile *ast.SourceFile) int { - previousLine := -1 - var child *ast.Node - for n != nil { - line, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, withTokenStart(n, sourceFile).Pos()) - if previousLine != -1 && line != previousLine { - break - } - - if ShouldIndentChildNode(options, n, child, sourceFile) { - return options.IndentSize // !!! nil check??? - } - - previousLine = line - child = n - n = n.Parent - } - return 0 -} - -func rangeHasNoErrors(_ core.TextRange) bool { - return false -} - -func prepareRangeContainsErrorFunction(errors []*ast.Diagnostic, originalRange core.TextRange) func(r core.TextRange) bool { - if len(errors) == 0 { - return rangeHasNoErrors - } - - // pick only errors that fall in range - sorted := core.Filter(errors, func(d *ast.Diagnostic) bool { - return originalRange.Overlaps(d.Loc()) - }) - if len(sorted) == 0 { - return rangeHasNoErrors - } - slices.SortStableFunc(sorted, func(a *ast.Diagnostic, b *ast.Diagnostic) int { return a.Pos() - b.Pos() }) - - index := 0 - return func(r core.TextRange) bool { - // in current implementation sequence of arguments [r1, r2...] is monotonically increasing. - // 'index' tracks the index of the most recent error that was checked. - for true { - if index >= len(sorted) { - // all errors in the range were already checked -> no error in specified range - return false - } - - err := sorted[index] - - if r.End() <= err.Pos() { - // specified range ends before the error referred by 'index' - no error in range - return false - } - - if r.Overlaps(err.Loc()) { - // specified range overlaps with error range - return true - } - - index++ - } - return false // unreachable - } -} - -type formatSpanWorker struct { - originalRange core.TextRange - enclosingNode *ast.Node - initialIndentation int - delta int - requestKind FormatRequestKind - rangeContainsError func(r core.TextRange) bool - sourceFile *ast.SourceFile - - ctx context.Context - - formattingScanner *formattingScanner - formattingContext *formattingContext - - edits []core.TextChange - previousRange TextRangeWithKind - previousRangeTriviaEnd int - previousParent *ast.Node - previousRangeStartLine int - - childContextNode *ast.Node - lastIndentedLine int - indentationOnLastIndentedLine int - - visitor *ast.NodeVisitor - visitingNode *ast.Node - visitingIndenter *dynamicIndenter - visitingNodeStartLine int - visitingUndecoratedNodeStartLine int - - currentRules []*ruleImpl -} - -func newFormatSpanWorker( - ctx context.Context, - originalRange core.TextRange, - enclosingNode *ast.Node, - initialIndentation int, - delta int, - requestKind FormatRequestKind, - rangeContainsError func(r core.TextRange) bool, - sourceFile *ast.SourceFile, -) *formatSpanWorker { - return &formatSpanWorker{ - ctx: ctx, - originalRange: originalRange, - enclosingNode: enclosingNode, - initialIndentation: initialIndentation, - delta: delta, - requestKind: requestKind, - rangeContainsError: rangeContainsError, - sourceFile: sourceFile, - currentRules: make([]*ruleImpl, 0, 32), // increaseInsertionIndex should assert there are no more than 32 rules in a given bucket - } -} - -func getNonDecoratorTokenPosOfNode(node *ast.Node, file *ast.SourceFile) int { - var lastDecorator *ast.Node - if ast.HasDecorators(node) { - lastDecorator = core.FindLast(node.Modifiers().Nodes, ast.IsDecorator) - } - if file == nil { - file = ast.GetSourceFileOfNode(node) - } - if lastDecorator == nil { - return withTokenStart(node, file).Pos() - } - return scanner.SkipTrivia(file.Text(), lastDecorator.End()) -} - -func (w *formatSpanWorker) execute(s *formattingScanner) []core.TextChange { - w.formattingScanner = s - w.indentationOnLastIndentedLine = -1 - opt := GetFormatCodeSettingsFromContext(w.ctx) - w.formattingContext = NewFormattingContext(w.sourceFile, w.requestKind, opt) - // formatting context is used by rules provider - w.visitor = ast.NewNodeVisitor(func(child *ast.Node) *ast.Node { - if child == nil { - return child - } - w.processChildNode(w.visitingNode, w.visitingIndenter, w.visitingNodeStartLine, w.visitingUndecoratedNodeStartLine, child, -1, w.visitingNode, w.visitingIndenter, w.visitingNodeStartLine, w.visitingUndecoratedNodeStartLine, false, false) - return child - }, &ast.NodeFactory{}, ast.NodeVisitorHooks{ - VisitNodes: func(nodes *ast.NodeList, v *ast.NodeVisitor) *ast.NodeList { - if nodes == nil { - return nodes - } - w.processChildNodes(w.visitingNode, w.visitingIndenter, w.visitingNodeStartLine, w.visitingUndecoratedNodeStartLine, nodes, w.visitingNode, w.visitingNodeStartLine, w.visitingIndenter) - return nodes - }, - }) - - w.formattingScanner.advance() - - if w.formattingScanner.isOnToken() { - startLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, withTokenStart(w.enclosingNode, w.sourceFile).Pos()) - undecoratedStartLine := startLine - if ast.HasDecorators(w.enclosingNode) { - undecoratedStartLine, _ = scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, getNonDecoratorTokenPosOfNode(w.enclosingNode, w.sourceFile)) - } - - w.processNode(w.enclosingNode, w.enclosingNode, startLine, undecoratedStartLine, w.initialIndentation, w.delta) - } - - // Leading trivia items get attached to and processed with the token that proceeds them. If the - // range ends in the middle of some leading trivia, the token that proceeds them won't be in the - // range and thus won't get processed. So we process those remaining trivia items here. - remainingTrivia := w.formattingScanner.getCurrentLeadingTrivia() - if len(remainingTrivia) > 0 { - indentation := w.initialIndentation - if NodeWillIndentChild(w.formattingContext.Options, w.enclosingNode, nil, w.sourceFile, false) { - indentation += opt.IndentSize // !!! TODO: nil check??? - } - - w.indentTriviaItems(remainingTrivia, indentation, true, func(item TextRangeWithKind) { - startLine, startChar := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, item.Loc.Pos()) - w.processRange(item, startLine, startChar, w.enclosingNode, w.enclosingNode, nil) - w.insertIndentation(item.Loc.Pos(), indentation, false) - }) - - if opt.TrimTrailingWhitespace != false { - w.trimTrailingWhitespacesForRemainingRange(remainingTrivia) - } - } - - if w.previousRange != NewTextRangeWithKind(0, 0, 0) && w.formattingScanner.getTokenFullStart() >= w.originalRange.End() { - // Formatting edits happen by looking at pairs of contiguous tokens (see `processPair`), - // typically inserting or deleting whitespace between them. The recursive `processNode` - // logic above bails out as soon as it encounters a token that is beyond the end of the - // range we're supposed to format (or if we reach the end of the file). But this potentially - // leaves out an edit that would occur *inside* the requested range but cannot be discovered - // without looking at one token *beyond* the end of the range: consider the line `x = { }` - // with a selection from the beginning of the line to the space inside the curly braces, - // inclusive. We would expect a format-selection would delete the space (if rules apply), - // but in order to do that, we need to process the pair ["{", "}"], but we stopped processing - // just before getting there. This block handles this trailing edit. - var tokenInfo TextRangeWithKind - if w.formattingScanner.isOnEOF() { - tokenInfo = w.formattingScanner.readEOFTokenRange() - } else if w.formattingScanner.isOnToken() { - tokenInfo = w.formattingScanner.readTokenInfo(w.enclosingNode).token - } - - if tokenInfo.Loc.Pos() == w.previousRangeTriviaEnd { - // We need to check that tokenInfo and previousRange are contiguous: the `originalRange` - // may have ended in the middle of a token, which means we will have stopped formatting - // on that token, leaving `previousRange` pointing to the token before it, but already - // having moved the formatting scanner (where we just got `tokenInfo`) to the next token. - // If this happens, our supposed pair [previousRange, tokenInfo] actually straddles the - // token that intersects the end of the range we're supposed to format, so the pair will - // produce bogus edits if we try to `processPair`. Recall that the point of this logic is - // to perform a trailing edit at the end of the selection range: but there can be no valid - // edit in the middle of a token where the range ended, so if we have a non-contiguous - // pair here, we're already done and we can ignore it. - parent := astnav.FindPrecedingToken(w.sourceFile, tokenInfo.Loc.End()) - if parent == nil { - parent = w.previousParent - } - line, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, tokenInfo.Loc.Pos()) - w.processPair( - tokenInfo, - line, - parent, - w.previousRange, - w.previousRangeStartLine, - w.previousParent, - parent, - nil, - ) - } - } - - return w.edits -} - -func (w *formatSpanWorker) processChildNode( - node *ast.Node, - indenter *dynamicIndenter, - nodeStartLine int, - undecoratedNodeStartLine int, - child *ast.Node, - inheritedIndentation int, - parent *ast.Node, - parentDynamicIndentation *dynamicIndenter, - parentStartLine int, - undecoratedParentStartLine int, - isListItem bool, - isFirstListItem bool, -) int { - debug.Assert(!ast.NodeIsSynthesized(child)) - - if ast.NodeIsMissing(child) || isGrammarError(parent, child) { - return inheritedIndentation - } - - childStartPos := scanner.GetTokenPosOfNode(child, w.sourceFile, false) - childStartLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, childStartPos) - - undecoratedChildStartLine := childStartLine - if ast.HasDecorators(child) { - undecoratedChildStartLine, _ = scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, getNonDecoratorTokenPosOfNode(child, w.sourceFile)) - } - - // if child is a list item - try to get its indentation, only if parent is within the original range. - childIndentationAmount := -1 - - if isListItem && parent.Loc.ContainedBy(w.originalRange) { - childIndentationAmount = w.tryComputeIndentationForListItem(childStartPos, child.End(), parentStartLine, w.originalRange, inheritedIndentation) - if childIndentationAmount != -1 { - inheritedIndentation = childIndentationAmount - } - } - - // child node is outside the target range - do not dive inside - if !w.originalRange.Overlaps(child.Loc) { - if child.End() < w.originalRange.Pos() { - w.formattingScanner.skipToEndOf(&child.Loc) - } - return inheritedIndentation - } - - if child.Loc.Len() == 0 { - return inheritedIndentation - } - - for w.formattingScanner.isOnToken() && w.formattingScanner.getTokenFullStart() < w.originalRange.End() { - // proceed any parent tokens that are located prior to child.getStart() - tokenInfo := w.formattingScanner.readTokenInfo(node) - if tokenInfo.token.Loc.End() > w.originalRange.End() { - return inheritedIndentation - } - if tokenInfo.token.Loc.End() > childStartPos { - if tokenInfo.token.Loc.Pos() > childStartPos { - w.formattingScanner.skipToStartOf(&child.Loc) - } - // stop when formatting scanner advances past the beginning of the child - break - } - - w.consumeTokenAndAdvanceScanner(tokenInfo, node, parentDynamicIndentation, node, false) - } - - if !w.formattingScanner.isOnToken() || w.formattingScanner.getTokenFullStart() >= w.originalRange.End() { - return inheritedIndentation - } - - if ast.IsTokenKind(child.Kind) { - // if child node is a token, it does not impact indentation, proceed it using parent indentation scope rules - tokenInfo := w.formattingScanner.readTokenInfo(child) - // JSX text shouldn't affect indenting - if child.Kind != ast.KindJsxText { - debug.Assert(tokenInfo.token.Loc.End() == child.Loc.End(), "Token end is child end") - w.consumeTokenAndAdvanceScanner(tokenInfo, node, parentDynamicIndentation, child, false) - return inheritedIndentation - } - } - - effectiveParentStartLine := undecoratedParentStartLine - if child.Kind == ast.KindDecorator { - effectiveParentStartLine = childStartLine - } - childIndentation, delta := w.computeIndentation(child, childStartLine, childIndentationAmount, node, parentDynamicIndentation, effectiveParentStartLine) - - w.processNode(child, w.childContextNode, childStartLine, undecoratedChildStartLine, childIndentation, delta) - - w.childContextNode = node - - if isFirstListItem && parent.Kind == ast.KindArrayLiteralExpression && inheritedIndentation == -1 { - inheritedIndentation = childIndentation - } - - return inheritedIndentation -} - -func (w *formatSpanWorker) processChildNodes( - node *ast.Node, - indenter *dynamicIndenter, - nodeStartLine int, - undecoratedNodeStartLine int, - nodes *ast.NodeList, - parent *ast.Node, - parentStartLine int, - parentDynamicIndentation *dynamicIndenter, -) { - debug.AssertIsDefined(nodes) - debug.Assert(!ast.PositionIsSynthesized(nodes.Pos())) - debug.Assert(!ast.PositionIsSynthesized(nodes.End())) - - listStartToken := getOpenTokenForList(parent, nodes) - - listDynamicIndentation := parentDynamicIndentation - startLine := parentStartLine - - // node range is outside the target range - do not dive inside - if !w.originalRange.Overlaps(nodes.Loc) { - if nodes.End() < w.originalRange.Pos() { - w.formattingScanner.skipToEndOf(&nodes.Loc) - } - return - } - - if listStartToken != -1 { - // introduce a new indentation scope for lists (including list start and end tokens) - for w.formattingScanner.isOnToken() && w.formattingScanner.getTokenFullStart() < w.originalRange.End() { - tokenInfo := w.formattingScanner.readTokenInfo(parent) - if tokenInfo.token.Loc.End() > nodes.Pos() { - // stop when formatting scanner moves past the beginning of node list - break - } else if tokenInfo.token.Kind == listStartToken { - // consume list start token - startLine, _ = scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, tokenInfo.token.Loc.Pos()) - - w.consumeTokenAndAdvanceScanner(tokenInfo, parent, parentDynamicIndentation, parent, false) - - indentationOnListStartToken := 0 - if w.indentationOnLastIndentedLine != -1 { - // scanner just processed list start token so consider last indentation as list indentation - // function foo(): { // last indentation was 0, list item will be indented based on this value - // foo: number; - // }: {}; - indentationOnListStartToken = w.indentationOnLastIndentedLine - } else { - startLinePosition := GetLineStartPositionForPosition(tokenInfo.token.Loc.Pos(), w.sourceFile) - indentationOnListStartToken = FindFirstNonWhitespaceColumn(startLinePosition, tokenInfo.token.Loc.Pos(), w.sourceFile, w.formattingContext.Options) - } - - listDynamicIndentation = w.getDynamicIndentation(parent, parentStartLine, indentationOnListStartToken, w.formattingContext.Options.IndentSize) - } else { - // consume any tokens that precede the list as child elements of 'node' using its indentation scope - w.consumeTokenAndAdvanceScanner(tokenInfo, parent, parentDynamicIndentation, parent, false) - } - } - } - - inheritedIndentation := -1 - for i := range len(nodes.Nodes) { - child := nodes.Nodes[i] - inheritedIndentation = w.processChildNode(node, indenter, nodeStartLine, undecoratedNodeStartLine, child, inheritedIndentation, node, listDynamicIndentation, startLine, startLine, true, i == 0) - } - - listEndToken := getCloseTokenForOpenToken(listStartToken) - if listEndToken != ast.KindUnknown && w.formattingScanner.isOnToken() && w.formattingScanner.getTokenFullStart() < w.originalRange.End() { - tokenInfo := w.formattingScanner.readTokenInfo(parent) - if tokenInfo.token.Kind == ast.KindCommaToken { - // consume the comma - w.consumeTokenAndAdvanceScanner(tokenInfo, parent, listDynamicIndentation, parent, false) - if w.formattingScanner.isOnToken() { - tokenInfo = w.formattingScanner.readTokenInfo(parent) - } else { - return - } - } - - // consume the list end token only if it is still belong to the parent - // there might be the case when current token matches end token but does not considered as one - // function (x: function) <-- - // without this check close paren will be interpreted as list end token for function expression which is wrong - if tokenInfo.token.Kind == listEndToken && tokenInfo.token.Loc.ContainedBy(parent.Loc) { - // consume list end token - w.consumeTokenAndAdvanceScanner(tokenInfo, parent, listDynamicIndentation, parent /*isListEndToken*/, true) - } - } -} - -func (w *formatSpanWorker) executeProcessNodeVisitor(node *ast.Node, indenter *dynamicIndenter, nodeStartLine int, undecoratedNodeStartLine int) { - oldNode := w.visitingNode - oldIndenter := w.visitingIndenter - oldStart := w.visitingNodeStartLine - oldUndecoratedStart := w.visitingUndecoratedNodeStartLine - w.visitingNode = node - w.visitingIndenter = indenter - w.visitingNodeStartLine = nodeStartLine - w.visitingUndecoratedNodeStartLine = undecoratedNodeStartLine - node.VisitEachChild(w.visitor) - w.visitingNode = oldNode - w.visitingIndenter = oldIndenter - w.visitingNodeStartLine = oldStart - w.visitingUndecoratedNodeStartLine = oldUndecoratedStart -} - -func (w *formatSpanWorker) computeIndentation(node *ast.Node, startLine int, inheritedIndentation int, parent *ast.Node, parentDynamicIndentation *dynamicIndenter, effectiveParentStartLine int) (indentation int, delta int) { - delta = 0 - if ShouldIndentChildNode(w.formattingContext.Options, node, nil, nil) { - delta = w.formattingContext.Options.IndentSize - } - - if effectiveParentStartLine == startLine { - // if node is located on the same line with the parent - // - inherit indentation from the parent - // - push children if either parent of node itself has non-zero delta - indentation = w.indentationOnLastIndentedLine - if startLine != w.lastIndentedLine { - indentation = parentDynamicIndentation.getIndentation() - } - delta = min(w.formattingContext.Options.IndentSize, parentDynamicIndentation.getDelta(node)+delta) - return indentation, delta - } else if inheritedIndentation == -1 { - if node.Kind == ast.KindOpenParenToken && startLine == w.lastIndentedLine { - // the is used for chaining methods formatting - // - we need to get the indentation on last line and the delta of parent - return w.indentationOnLastIndentedLine, parentDynamicIndentation.getDelta(node) - } else if childStartsOnTheSameLineWithElseInIfStatement(parent, node, startLine, w.sourceFile) || - childIsUnindentedBranchOfConditionalExpression(parent, node, startLine, w.sourceFile) || - argumentStartsOnSameLineAsPreviousArgument(parent, node, startLine, w.sourceFile) { - return parentDynamicIndentation.getIndentation(), delta - } else { - i := parentDynamicIndentation.getIndentation() - if i == -1 { - return parentDynamicIndentation.getIndentation(), delta - } - return i + parentDynamicIndentation.getDelta(node), delta - } - } - - return inheritedIndentation, delta -} - -/** Tries to compute the indentation for a list element. -* If list element is not in range then -* function will pick its actual indentation -* so it can be pushed downstream as inherited indentation. -* If list element is in the range - its indentation will be equal -* to inherited indentation from its predecessors. - */ -func (w *formatSpanWorker) tryComputeIndentationForListItem(startPos int, endPos int, parentStartLine int, r core.TextRange, inheritedIndentation int) int { - r2 := core.NewTextRange(startPos, endPos) - if r.Overlaps(r2) || r2.ContainedBy(r) { /* Not to miss zero-range nodes e.g. JsxText */ - if inheritedIndentation != -1 { - return inheritedIndentation - } - } else { - startLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, startPos) - startLinePosition := GetLineStartPositionForPosition(startPos, w.sourceFile) - column := FindFirstNonWhitespaceColumn(startLinePosition, startPos, w.sourceFile, w.formattingContext.Options) - if startLine != parentStartLine || startPos == column { - // Use the base indent size if it is greater than - // the indentation of the inherited predecessor. - baseIndentSize := w.formattingContext.Options.BaseIndentSize - if baseIndentSize > column { - return baseIndentSize - } - return column - } - } - return -1 -} - -func (w *formatSpanWorker) processNode(node *ast.Node, contextNode *ast.Node, nodeStartLine int, undecoratedNodeStartLine int, indentation int, delta int) { - if !w.originalRange.Overlaps(withTokenStart(node, w.sourceFile)) { - return - } - - nodeDynamicIndentation := w.getDynamicIndentation(node, nodeStartLine, indentation, delta) - - // a useful observations when tracking context node - // / - // [a] - // / | \ - // [b] [c] [d] - // node 'a' is a context node for nodes 'b', 'c', 'd' - // except for the leftmost leaf token in [b] - in this case context node ('e') is located somewhere above 'a' - // this rule can be applied recursively to child nodes of 'a'. - // - // context node is set to parent node value after processing every child node - // context node is set to parent of the token after processing every token - - w.childContextNode = contextNode - - // if there are any tokens that logically belong to node and interleave child nodes - // such tokens will be consumed in processChildNode for the child that follows them - w.executeProcessNodeVisitor(node, nodeDynamicIndentation, nodeStartLine, undecoratedNodeStartLine) - - // proceed any tokens in the node that are located after child nodes - for w.formattingScanner.isOnToken() && w.formattingScanner.getTokenFullStart() < w.originalRange.End() { - tokenInfo := w.formattingScanner.readTokenInfo(node) - if tokenInfo.token.Loc.End() > min(node.End(), w.originalRange.End()) { - break - } - w.consumeTokenAndAdvanceScanner(tokenInfo, node, nodeDynamicIndentation, node, false) - } -} - -func (w *formatSpanWorker) processPair(currentItem TextRangeWithKind, currentStartLine int, currentParent *ast.Node, previousItem TextRangeWithKind, previousStartLine int, previousParent *ast.Node, contextNode *ast.Node, dynamicIndentation *dynamicIndenter) LineAction { - w.formattingContext.UpdateContext(previousItem, previousParent, currentItem, currentParent, contextNode) - - w.currentRules = w.currentRules[:0] - w.currentRules = getRules(w.formattingContext, w.currentRules) - - trimTrailingWhitespaces := w.formattingContext.Options.TrimTrailingWhitespace != false - lineAction := LineActionNone - - if len(w.currentRules) > 0 { - // Apply rules in reverse order so that higher priority rules (which are first in the array) - // win in a conflict with lower priority rules. - for i := len(w.currentRules) - 1; i >= 0; i-- { - rule := w.currentRules[i] - lineAction = w.applyRuleEdits(rule, previousItem, previousStartLine, currentItem, currentStartLine) - if dynamicIndentation != nil { - switch lineAction { - case LineActionLineRemoved: - // Handle the case where the next line is moved to be the end of this line. - // In this case we don't indent the next line in the next pass. - if scanner.GetTokenPosOfNode(currentParent, w.sourceFile, false) == currentItem.Loc.Pos() { - dynamicIndentation.recomputeIndentation( /*lineAddedByFormatting*/ false, contextNode) - } - case LineActionLineAdded: - // Handle the case where token2 is moved to the new line. - // In this case we indent token2 in the next pass but we set - // sameLineIndent flag to notify the indenter that the indentation is within the line. - if scanner.GetTokenPosOfNode(currentParent, w.sourceFile, false) == currentItem.Loc.Pos() { - dynamicIndentation.recomputeIndentation( /*lineAddedByFormatting*/ true, contextNode) - } - default: - debug.Assert(lineAction == LineActionNone) - } - } - - // We need to trim trailing whitespace between the tokens if they were on different lines, and no rule was applied to put them on the same line - trimTrailingWhitespaces = trimTrailingWhitespaces && (rule.Action()&ruleActionDeleteSpace == 0) && rule.Flags() != ruleFlagsCanDeleteNewLines - } - } else { - trimTrailingWhitespaces = trimTrailingWhitespaces && currentItem.Kind != ast.KindEndOfFile - } - - if currentStartLine != previousStartLine && trimTrailingWhitespaces { - // We need to trim trailing whitespace between the tokens if they were on different lines, and no rule was applied to put them on the same line - w.trimTrailingWhitespacesForLines(previousStartLine, currentStartLine, previousItem) - } - - return lineAction -} - -func (w *formatSpanWorker) applyRuleEdits(rule *ruleImpl, previousRange TextRangeWithKind, previousStartLine int, currentRange TextRangeWithKind, currentStartLine int) LineAction { - onLaterLine := currentStartLine != previousStartLine - switch rule.Action() { - case ruleActionStopProcessingSpaceActions: - // no action required - return LineActionNone - case ruleActionDeleteSpace: - if previousRange.Loc.End() != currentRange.Loc.Pos() { - // delete characters starting from t1.end up to t2.pos exclusive - w.recordDelete(previousRange.Loc.End(), currentRange.Loc.Pos()-previousRange.Loc.End()) - if onLaterLine { - return LineActionLineRemoved - } - return LineActionNone - } - case ruleActionDeleteToken: - w.recordDelete(previousRange.Loc.Pos(), previousRange.Loc.Len()) - case ruleActionInsertNewLine: - // exit early if we on different lines and rule cannot change number of newlines - // if line1 and line2 are on subsequent lines then no edits are required - ok to exit - // if line1 and line2 are separated with more than one newline - ok to exit since we cannot delete extra new lines - if rule.Flags() != ruleFlagsCanDeleteNewLines && previousStartLine != currentStartLine { - return LineActionNone - } - - // edit should not be applied if we have one line feed between elements - lineDelta := currentStartLine - previousStartLine - if lineDelta != 1 { - w.recordReplace(previousRange.Loc.End(), currentRange.Loc.Pos()-previousRange.Loc.End(), GetNewLineOrDefaultFromContext(w.ctx)) - if onLaterLine { - return LineActionNone - } - return LineActionLineAdded - } - case ruleActionInsertSpace: - // exit early if we on different lines and rule cannot change number of newlines - if rule.Flags() != ruleFlagsCanDeleteNewLines && previousStartLine != currentStartLine { - return LineActionNone - } - - posDelta := currentRange.Loc.Pos() - previousRange.Loc.End() - if posDelta != 1 || !strings.HasPrefix(w.sourceFile.Text()[previousRange.Loc.End():], " ") { - w.recordReplace(previousRange.Loc.End(), posDelta, " ") - if onLaterLine { - return LineActionLineRemoved - } - return LineActionNone - } - case ruleActionInsertTrailingSemicolon: - w.recordInsert(previousRange.Loc.End(), ";") - } - return LineActionNone -} - -type LineAction int - -const ( - LineActionNone LineAction = iota - LineActionLineAdded - LineActionLineRemoved -) - -func (w *formatSpanWorker) processRange(r TextRangeWithKind, rangeStartLine int, rangeStartCharacter int, parent *ast.Node, contextNode *ast.Node, dynamicIndentation *dynamicIndenter) LineAction { - rangeHasError := w.rangeContainsError(r.Loc) - lineAction := LineActionNone - if !rangeHasError { - if w.previousRange == NewTextRangeWithKind(0, 0, 0) { - // trim whitespaces starting from the beginning of the span up to the current line - originalStartLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, w.originalRange.Pos()) - w.trimTrailingWhitespacesForLines(originalStartLine, rangeStartLine, NewTextRangeWithKind(0, 0, 0)) - } else { - lineAction = w.processPair(r, rangeStartLine, parent, w.previousRange, w.previousRangeStartLine, w.previousParent, contextNode, dynamicIndentation) - } - } - - w.previousRange = r - w.previousRangeTriviaEnd = r.Loc.End() - w.previousParent = parent - w.previousRangeStartLine = rangeStartLine - - return lineAction -} - -func (w *formatSpanWorker) processTrivia(trivia []TextRangeWithKind, parent *ast.Node, contextNode *ast.Node, dynamicIndentation *dynamicIndenter) { - for _, triviaItem := range trivia { - if isComment(triviaItem.Kind) && triviaItem.Loc.ContainedBy(w.originalRange) { - triviaItemStartLine, triviaItemStartCharacter := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, triviaItem.Loc.Pos()) - w.processRange(triviaItem, triviaItemStartLine, triviaItemStartCharacter, parent, contextNode, dynamicIndentation) - } - } -} - -/** -* Trimming will be done for lines after the previous range. -* Exclude comments as they had been previously processed. - */ -func (w *formatSpanWorker) trimTrailingWhitespacesForRemainingRange(trivias []TextRangeWithKind) { - startPos := w.originalRange.Pos() - if w.previousRange != NewTextRangeWithKind(0, 0, 0) { - startPos = w.previousRange.Loc.End() - } - - for _, trivia := range trivias { - if isComment(trivia.Kind) { - if startPos < trivia.Loc.Pos() { - w.trimTrailingWitespacesForPositions(startPos, trivia.Loc.Pos()-1, w.previousRange) - } - - startPos = trivia.Loc.End() + 1 - } - } - - if startPos < w.originalRange.End() { - w.trimTrailingWitespacesForPositions(startPos, w.originalRange.End(), w.previousRange) - } -} - -func (w *formatSpanWorker) trimTrailingWitespacesForPositions(startPos int, endPos int, previousRange TextRangeWithKind) { - startLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, startPos) - endLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, endPos) - - w.trimTrailingWhitespacesForLines(startLine, endLine+1, previousRange) -} - -func (w *formatSpanWorker) trimTrailingWhitespacesForLines(line1 int, line2 int, r TextRangeWithKind) { - lineStarts := scanner.GetECMALineStarts(w.sourceFile) - for line := line1; line < line2; line++ { - lineStartPosition := int(lineStarts[line]) - lineEndPosition := scanner.GetECMAEndLinePosition(w.sourceFile, line) - - // do not trim whitespaces in comments or template expression - if r != NewTextRangeWithKind(0, 0, 0) && (isComment(r.Kind) || isStringOrRegularExpressionOrTemplateLiteral(r.Kind)) && r.Loc.Pos() <= lineEndPosition && r.Loc.End() > lineEndPosition { - continue - } - - whitespaceStart := w.getTrailingWhitespaceStartPosition(lineStartPosition, lineEndPosition) - if whitespaceStart != -1 { - r, _ := utf8.DecodeRuneInString(w.sourceFile.Text()[whitespaceStart-1:]) - debug.Assert(whitespaceStart == lineStartPosition || !stringutil.IsWhiteSpaceSingleLine(r)) - w.recordDelete(whitespaceStart, lineEndPosition+1-whitespaceStart) - } - } -} - -/** -* @param start The position of the first character in range -* @param end The position of the last character in range - */ -func (w *formatSpanWorker) getTrailingWhitespaceStartPosition(start int, end int) int { - pos := end - text := w.sourceFile.Text() - for pos >= start { - ch, size := utf8.DecodeRuneInString(text[pos:]) - if size == 0 { - pos-- // multibyte character, rewind more - continue - } - if !stringutil.IsWhiteSpaceSingleLine(ch) { - break - } - pos-- - } - if pos != end { - return pos + 1 - } - return -1 -} - -func isStringOrRegularExpressionOrTemplateLiteral(kind ast.Kind) bool { - return kind == ast.KindStringLiteral || kind == ast.KindRegularExpressionLiteral || ast.IsTemplateLiteralKind(kind) -} - -func isComment(kind ast.Kind) bool { - return kind == ast.KindSingleLineCommentTrivia || kind == ast.KindMultiLineCommentTrivia -} - -func (w *formatSpanWorker) insertIndentation(pos int, indentation int, lineAdded bool) { - indentationString := getIndentationString(indentation, w.formattingContext.Options) - if lineAdded { - // new line is added before the token by the formatting rules - // insert indentation string at the very beginning of the token - w.recordReplace(pos, 0, indentationString) - } else { - tokenStartLine, tokenStartCharacter := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, pos) - startLinePosition := int(scanner.GetECMALineStarts(w.sourceFile)[tokenStartLine]) - if indentation != w.characterToColumn(startLinePosition, tokenStartCharacter) || w.indentationIsDifferent(indentationString, startLinePosition) { - w.recordReplace(startLinePosition, tokenStartCharacter, indentationString) - } - } -} - -func (w *formatSpanWorker) characterToColumn(startLinePosition int, characterInLine int) int { - column := 0 - for i := range characterInLine { - if w.sourceFile.Text()[startLinePosition+i] == '\t' { - column += w.formattingContext.Options.TabSize - (column % w.formattingContext.Options.TabSize) - } else { - column++ - } - } - return column -} - -func (w *formatSpanWorker) indentationIsDifferent(indentationString string, startLinePosition int) bool { - return indentationString != w.sourceFile.Text()[startLinePosition:startLinePosition+len(indentationString)] -} - -func (w *formatSpanWorker) indentTriviaItems(trivia []TextRangeWithKind, commentIndentation int, indentNextTokenOrTrivia bool, indentSingleLine func(item TextRangeWithKind)) bool { - for _, triviaItem := range trivia { - triviaInRange := triviaItem.Loc.ContainedBy(w.originalRange) - switch triviaItem.Kind { - case ast.KindMultiLineCommentTrivia: - if triviaInRange { - w.indentMultilineComment(triviaItem.Loc, commentIndentation, !indentNextTokenOrTrivia, true) - } - indentNextTokenOrTrivia = false - case ast.KindSingleLineCommentTrivia: - if indentNextTokenOrTrivia && triviaInRange { - indentSingleLine(triviaItem) - } - indentNextTokenOrTrivia = false - case ast.KindNewLineTrivia: - indentNextTokenOrTrivia = true - } - } - return indentNextTokenOrTrivia -} - -func (w *formatSpanWorker) indentMultilineComment(commentRange core.TextRange, indentation int, firstLineIsIndented bool, indentFinalLine bool) { - // split comment in lines - startLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, commentRange.Pos()) - endLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, commentRange.End()) - - if startLine == endLine { - if !firstLineIsIndented { - // treat as single line comment - w.insertIndentation(commentRange.Pos(), indentation, false) - } - return - } - - parts := make([]core.TextRange, 0, strings.Count(w.sourceFile.Text()[commentRange.Pos():commentRange.End()], "\n")) - startPos := commentRange.Pos() - for line := startLine; line < endLine; line++ { - endOfLine := scanner.GetECMAEndLinePosition(w.sourceFile, line) - parts = append(parts, core.NewTextRange(startPos, endOfLine)) - startPos = int(scanner.GetECMALineStarts(w.sourceFile)[line+1]) - } - - if indentFinalLine { - parts = append(parts, core.NewTextRange(startPos, commentRange.End())) - } - - if len(parts) == 0 { - return - } - - startLinePos := int(scanner.GetECMALineStarts(w.sourceFile)[startLine]) - - nonWhitespaceInFirstPartCharacter, nonWhitespaceInFirstPartColumn := findFirstNonWhitespaceCharacterAndColumn(startLinePos, parts[0].Pos(), w.sourceFile, w.formattingContext.Options) - - startIndex := 0 - - if firstLineIsIndented { - startIndex = 1 - startLine++ - } - - // shift all parts on the delta size - delta := indentation - nonWhitespaceInFirstPartColumn - for i := startIndex; i < len(parts); i++ { - startLinePos := int(scanner.GetECMALineStarts(w.sourceFile)[startLine]) - nonWhitespaceCharacter := nonWhitespaceInFirstPartCharacter - nonWhitespaceColumn := nonWhitespaceInFirstPartColumn - if i != 0 { - nonWhitespaceCharacter, nonWhitespaceColumn = findFirstNonWhitespaceCharacterAndColumn(parts[i].Pos(), parts[i].End(), w.sourceFile, w.formattingContext.Options) - } - newIndentation := nonWhitespaceColumn + delta - if newIndentation > 0 { - indentationString := getIndentationString(newIndentation, w.formattingContext.Options) - w.recordReplace(startLinePos, nonWhitespaceCharacter, indentationString) - } else { - w.recordDelete(startLinePos, nonWhitespaceCharacter) - } - - startLine++ - } -} - -func getIndentationString(indentation int, options *FormatCodeSettings) string { - // go's `strings.Repeat` already has static, global caching for repeated tabs and spaces, so there's no need to cache here like in strada - if !options.ConvertTabsToSpaces { - tabs := int(math.Floor(float64(indentation) / float64(options.TabSize))) - spaces := indentation - (tabs * options.TabSize) - res := strings.Repeat("\t", tabs) - if spaces > 0 { - res = strings.Repeat(" ", spaces) + res - } - - return res - } else { - return strings.Repeat(" ", indentation) - } -} - -func createTextChangeFromStartLength(start int, length int, newText string) core.TextChange { - return core.TextChange{ - NewText: newText, - TextRange: core.NewTextRange(start, start+length), - } -} - -func (w *formatSpanWorker) recordDelete(start int, length int) { - if length != 0 { - w.edits = append(w.edits, createTextChangeFromStartLength(start, length, "")) - } -} - -func (w *formatSpanWorker) recordReplace(start int, length int, newText string) { - if length != 0 || newText != "" { - w.edits = append(w.edits, createTextChangeFromStartLength(start, length, newText)) - } -} - -func (w *formatSpanWorker) recordInsert(start int, text string) { - if text != "" { - w.edits = append(w.edits, createTextChangeFromStartLength(start, 0, text)) - } -} - -func (w *formatSpanWorker) consumeTokenAndAdvanceScanner(currentTokenInfo tokenInfo, parent *ast.Node, dynamicIndenation *dynamicIndenter, container *ast.Node, isListEndToken bool) { - // assert(currentTokenInfo.token.Loc.ContainedBy(parent.Loc)) // !!! - lastTriviaWasNewLine := w.formattingScanner.lastTrailingTriviaWasNewLine() - indentToken := false - - if len(currentTokenInfo.leadingTrivia) > 0 { - w.processTrivia(currentTokenInfo.leadingTrivia, parent, w.childContextNode, dynamicIndenation) - } - - lineAction := LineActionNone - isTokenInRange := currentTokenInfo.token.Loc.ContainedBy(w.originalRange) - - tokenStartLine, tokenStartChar := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, currentTokenInfo.token.Loc.Pos()) - - if isTokenInRange { - rangeHasError := w.rangeContainsError(currentTokenInfo.token.Loc) - // save previousRange since processRange will overwrite this value with current one - savePreviousRange := w.previousRange - lineAction = w.processRange(currentTokenInfo.token, tokenStartLine, tokenStartChar, parent, w.childContextNode, dynamicIndenation) - // do not indent comments\token if token range overlaps with some error - if !rangeHasError { - if lineAction == LineActionNone { - // indent token only if end line of previous range does not match start line of the token - if savePreviousRange != NewTextRangeWithKind(0, 0, 0) { - prevEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, savePreviousRange.Loc.End()) - indentToken = lastTriviaWasNewLine && tokenStartLine != prevEndLine - } - } else { - indentToken = lineAction == LineActionLineAdded - } - } - } - - if len(currentTokenInfo.trailingTrivia) > 0 { - w.previousRangeTriviaEnd = core.LastOrNil(currentTokenInfo.trailingTrivia).Loc.End() - w.processTrivia(currentTokenInfo.trailingTrivia, parent, w.childContextNode, dynamicIndenation) - } - - if indentToken { - tokenIndentation := -1 - if isTokenInRange && !w.rangeContainsError(currentTokenInfo.token.Loc) { - tokenIndentation = dynamicIndenation.getIndentationForToken(tokenStartLine, currentTokenInfo.token.Kind, container, !!isListEndToken) - } - indentNextTokenOrTrivia := true - if len(currentTokenInfo.leadingTrivia) > 0 { - commentIndentation := dynamicIndenation.getIndentationForComment(currentTokenInfo.token.Kind, tokenIndentation, container) - indentNextTokenOrTrivia = w.indentTriviaItems(currentTokenInfo.leadingTrivia, commentIndentation, indentNextTokenOrTrivia, func(item TextRangeWithKind) { - w.insertIndentation(item.Loc.Pos(), commentIndentation, false) - }) - } - - // indent token only if is it is in target range and does not overlap with any error ranges - if tokenIndentation != -1 && indentNextTokenOrTrivia { - w.insertIndentation(currentTokenInfo.token.Loc.Pos(), tokenIndentation, lineAction == LineActionLineAdded) - - w.lastIndentedLine = tokenStartLine - w.indentationOnLastIndentedLine = tokenIndentation - } - } - - w.formattingScanner.advance() - - w.childContextNode = parent -} - -type dynamicIndenter struct { - node *ast.Node - nodeStartLine int - indentation int - delta int - - options *FormatCodeSettings - sourceFile *ast.SourceFile -} - -func (i *dynamicIndenter) getIndentationForComment(kind ast.Kind, tokenIndentation int, container *ast.Node) int { - switch kind { - // preceding comment to the token that closes the indentation scope inherits the indentation from the scope - // .. { - // // comment - // } - case ast.KindCloseBraceToken, ast.KindCloseBracketToken, ast.KindCloseParenToken: - return i.indentation + i.getDelta(container) - } - return i.indentation -} - -// if list end token is LessThanToken '>' then its delta should be explicitly suppressed -// so that LessThanToken as a binary operator can still be indented. -// foo.then -// -// < -// number, -// string, -// >(); -// -// vs -// var a = xValue -// -// > yValue; -func (i *dynamicIndenter) getIndentationForToken(line int, kind ast.Kind, container *ast.Node, suppressDelta bool) int { - if !suppressDelta && i.shouldAddDelta(line, kind, container) { - return i.indentation + i.getDelta(container) - } - return i.indentation -} - -func (i *dynamicIndenter) getIndentation() int { - return i.indentation -} - -func (i *dynamicIndenter) getDelta(child *ast.Node) int { - // Delta value should be zero when the node explicitly prevents indentation of the child node - if NodeWillIndentChild(i.options, i.node, child, i.sourceFile, true) { - return i.delta - } - return 0 -} - -func (i *dynamicIndenter) recomputeIndentation(lineAdded bool, parent *ast.Node) { - if ShouldIndentChildNode(i.options, parent, i.node, i.sourceFile) { - if lineAdded { - i.indentation += i.options.IndentSize // !!! no nil check??? - } else { - i.indentation -= i.options.IndentSize // !!! no nil check??? - } - if ShouldIndentChildNode(i.options, i.node, nil, nil) { - i.delta = i.options.IndentSize - } else { - i.delta = 0 - } - } -} - -func (i *dynamicIndenter) shouldAddDelta(line int, kind ast.Kind, container *ast.Node) bool { - switch kind { - // open and close brace, 'else' and 'while' (in do statement) tokens has indentation of the parent - case ast.KindOpenBraceToken, ast.KindCloseBraceToken, ast.KindCloseParenToken, ast.KindElseKeyword, ast.KindWhileKeyword, ast.KindAtToken: - return false - case ast.KindSlashToken, ast.KindGreaterThanToken: - switch container.Kind { - case ast.KindJsxOpeningElement, ast.KindJsxClosingElement, ast.KindJsxSelfClosingElement: - return false - } - break - case ast.KindOpenBracketToken, ast.KindCloseBracketToken: - if container.Kind != ast.KindMappedType { - return false - } - break - } - // if token line equals to the line of containing node (this is a first token in the node) - use node indentation - return i.nodeStartLine != line && - // if this token is the first token following the list of decorators, we do not need to indent - !(ast.HasDecorators(i.node) && kind == getFirstNonDecoratorTokenOfNode(i.node)) -} - -func getFirstNonDecoratorTokenOfNode(node *ast.Node) ast.Kind { - if ast.CanHaveModifiers(node) { - modifier := core.Find(node.Modifiers().Nodes[core.FindIndex(node.Modifiers().Nodes, ast.IsDecorator):], ast.IsModifier) - if modifier != nil { - return modifier.Kind - } - } - - switch node.Kind { - case ast.KindClassDeclaration: - return ast.KindClassKeyword - case ast.KindInterfaceDeclaration: - return ast.KindInterfaceKeyword - case ast.KindFunctionDeclaration: - return ast.KindFunctionKeyword - case ast.KindEnumDeclaration: - return ast.KindEnumDeclaration - case ast.KindGetAccessor: - return ast.KindGetKeyword - case ast.KindSetAccessor: - return ast.KindSetKeyword - case ast.KindMethodDeclaration: - if node.AsMethodDeclaration().AsteriskToken != nil { - return ast.KindAsteriskToken - } - fallthrough - - case ast.KindPropertyDeclaration, ast.KindParameter: - name := ast.GetNameOfDeclaration(node) - if name != nil { - return name.Kind - } - } - - return ast.KindUnknown -} - -func (w *formatSpanWorker) getDynamicIndentation(node *ast.Node, nodeStartLine int, indentation int, delta int) *dynamicIndenter { - return &dynamicIndenter{ - node: node, - nodeStartLine: nodeStartLine, - indentation: indentation, - delta: delta, - options: w.formattingContext.Options, - sourceFile: w.sourceFile, - } -} diff --git a/kitcom/internal/tsgo/format/util.go b/kitcom/internal/tsgo/format/util.go deleted file mode 100644 index 0fa5033..0000000 --- a/kitcom/internal/tsgo/format/util.go +++ /dev/null @@ -1,190 +0,0 @@ -package format - -import ( - "slices" - - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/astnav" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/core" - "efprojects.com/kitten-ipc/kitcom/internal/tsgo/scanner" -) - -func rangeIsOnOneLine(node core.TextRange, file *ast.SourceFile) bool { - startLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, node.Pos()) - endLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, node.End()) - return startLine == endLine -} - -func getOpenTokenForList(node *ast.Node, list *ast.NodeList) ast.Kind { - switch node.Kind { - case ast.KindConstructor, - ast.KindFunctionDeclaration, - ast.KindFunctionExpression, - ast.KindMethodDeclaration, - ast.KindMethodSignature, - ast.KindArrowFunction, - ast.KindCallSignature, - ast.KindConstructSignature, - ast.KindFunctionType, - ast.KindConstructorType, - ast.KindGetAccessor, - ast.KindSetAccessor: - if node.TypeParameterList() == list { - return ast.KindLessThanToken - } else if node.ParameterList() == list { - return ast.KindOpenParenToken - } - case ast.KindCallExpression, ast.KindNewExpression: - if node.TypeArgumentList() == list { - return ast.KindLessThanToken - } else if node.ArgumentList() == list { - return ast.KindOpenParenToken - } - case ast.KindClassDeclaration, - ast.KindClassExpression, - ast.KindInterfaceDeclaration, - ast.KindTypeAliasDeclaration: - if node.TypeParameterList() == list { - return ast.KindLessThanToken - } - case ast.KindTypeReference, - ast.KindTaggedTemplateExpression, - ast.KindTypeQuery, - ast.KindExpressionWithTypeArguments, - ast.KindImportType: - if node.TypeArgumentList() == list { - return ast.KindLessThanToken - } - case ast.KindTypeLiteral: - return ast.KindOpenBraceToken - } - - return ast.KindUnknown -} - -func getCloseTokenForOpenToken(kind ast.Kind) ast.Kind { - // TODO: matches strada - seems like it could handle more pairs of braces, though? [] notably missing - switch kind { - case ast.KindOpenParenToken: - return ast.KindCloseParenToken - case ast.KindLessThanToken: - return ast.KindGreaterThanToken - case ast.KindOpenBraceToken: - return ast.KindCloseBraceToken - } - return ast.KindUnknown -} - -func GetLineStartPositionForPosition(position int, sourceFile *ast.SourceFile) int { - lineStarts := scanner.GetECMALineStarts(sourceFile) - line, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, position) - return int(lineStarts[line]) -} - -/** - * Tests whether `child` is a grammar error on `parent`. - * In strada, this also checked node arrays, but it is never actually called with one in practice. - */ -func isGrammarError(parent *ast.Node, child *ast.Node) bool { - if ast.IsTypeParameterDeclaration(parent) { - return child == parent.AsTypeParameter().Expression - } - if ast.IsPropertySignatureDeclaration(parent) { - return child == parent.AsPropertySignatureDeclaration().Initializer - } - if ast.IsPropertyDeclaration(parent) { - return ast.IsAutoAccessorPropertyDeclaration(parent) && child == parent.AsPropertyDeclaration().PostfixToken && child.Kind == ast.KindQuestionToken - } - if ast.IsPropertyAssignment(parent) { - pa := parent.AsPropertyAssignment() - mods := pa.Modifiers() - return child == pa.PostfixToken || (mods != nil && isGrammarErrorElement(&mods.NodeList, child, ast.IsModifierLike)) - } - if ast.IsShorthandPropertyAssignment(parent) { - sp := parent.AsShorthandPropertyAssignment() - mods := sp.Modifiers() - return child == sp.EqualsToken || child == sp.PostfixToken || (mods != nil && isGrammarErrorElement(&mods.NodeList, child, ast.IsModifierLike)) - } - if ast.IsMethodDeclaration(parent) { - return child == parent.AsMethodDeclaration().PostfixToken && child.Kind == ast.KindExclamationToken - } - if ast.IsConstructorDeclaration(parent) { - return child == parent.AsConstructorDeclaration().Type || isGrammarErrorElement(parent.AsConstructorDeclaration().TypeParameters, child, ast.IsTypeParameterDeclaration) - } - if ast.IsGetAccessorDeclaration(parent) { - return isGrammarErrorElement(parent.AsGetAccessorDeclaration().TypeParameters, child, ast.IsTypeParameterDeclaration) - } - if ast.IsSetAccessorDeclaration(parent) { - return child == parent.AsSetAccessorDeclaration().Type || isGrammarErrorElement(parent.AsSetAccessorDeclaration().TypeParameters, child, ast.IsTypeParameterDeclaration) - } - if ast.IsNamespaceExportDeclaration(parent) { - mods := parent.AsNamespaceExportDeclaration().Modifiers() - return mods != nil && isGrammarErrorElement(&mods.NodeList, child, ast.IsModifierLike) - } - return false -} - -func isGrammarErrorElement(list *ast.NodeList, child *ast.Node, isPossibleElement func(node *ast.Node) bool) bool { - if list == nil || len(list.Nodes) == 0 { - return false - } - if !isPossibleElement(child) { - return false - } - return slices.Contains(list.Nodes, child) -} - -/** - * Validating `expectedTokenKind` ensures the token was typed in the context we expect (eg: not a comment). - * @param expectedTokenKind The kind of the last token constituting the desired parent node. - */ -func findImmediatelyPrecedingTokenOfKind(end int, expectedTokenKind ast.Kind, sourceFile *ast.SourceFile) *ast.Node { - precedingToken := astnav.FindPrecedingToken(sourceFile, end) - if precedingToken == nil || precedingToken.Kind != expectedTokenKind || precedingToken.End() != end { - return nil - } - return precedingToken -} - -/** - * Finds the highest node enclosing `node` at the same list level as `node` - * and whose end does not exceed `node.end`. - * - * Consider typing the following - * ``` - * let x = 1; - * while (true) { - * } - * ``` - * Upon typing the closing curly, we want to format the entire `while`-statement, but not the preceding - * variable declaration. - */ -func findOutermostNodeWithinListLevel(node *ast.Node) *ast.Node { - current := node - for current != nil && - current.Parent != nil && - current.Parent.End() == node.End() && - !isListElement(current.Parent, current) { - current = current.Parent - } - - return current -} - -// Returns true if node is a element in some list in parent -// i.e. parent is class declaration with the list of members and node is one of members. -func isListElement(parent *ast.Node, node *ast.Node) bool { - switch parent.Kind { - case ast.KindClassDeclaration, ast.KindInterfaceDeclaration: - return node.Loc.ContainedBy(parent.MemberList().Loc) - case ast.KindModuleDeclaration: - body := parent.Body() - return body != nil && body.Kind == ast.KindModuleBlock && node.Loc.ContainedBy(body.StatementList().Loc) - case ast.KindSourceFile, ast.KindBlock, ast.KindModuleBlock: - return node.Loc.ContainedBy(parent.StatementList().Loc) - case ast.KindCatchClause: - return node.Loc.ContainedBy(parent.AsCatchClause().Block.StatementList().Loc) - } - - return false -}