diff options
| author | Fuwn <[email protected]> | 2026-02-05 07:15:07 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-05 07:15:07 +0000 |
| commit | acd264eebfb053e0c3d36e070dd036d3cd137aeb (patch) | |
| tree | ce9bedaa2bd4b11ac00b8da73729db0ed1d2cd4d | |
| parent | feat(engine): Add language-agnostic formatting engine with Go adapter (diff) | |
| download | iku-acd264eebfb053e0c3d36e070dd036d3cd137aeb.tar.xz iku-acd264eebfb053e0c3d36e070dd036d3cd137aeb.zip | |
refactor(formatter): Replace rewrite logic with engine
| -rw-r--r-- | formatter.go | 20 | ||||
| -rw-r--r-- | rewrite.go | 140 |
2 files changed, 5 insertions, 155 deletions
diff --git a/formatter.go b/formatter.go index 7b05b70..c6b5902 100644 --- a/formatter.go +++ b/formatter.go @@ -1,10 +1,6 @@ package main -import ( - "go/format" - "go/parser" - "go/token" -) +import "github.com/Fuwn/iku/engine" type CommentMode int @@ -26,20 +22,14 @@ type lineInformation struct { } func (f *Formatter) Format(source []byte) ([]byte, error) { - formattedSource, err := format.Source(source) - - if err != nil { - return nil, err - } - - tokenFileSet := token.NewFileSet() - parsedFile, err := parser.ParseFile(tokenFileSet, "", formattedSource, parser.ParseComments) + adapter := &GoAdapter{} + _, events, err := adapter.Analyze(source) if err != nil { return nil, err } - lineInformationMap := f.buildLineInfo(tokenFileSet, parsedFile) + formattingEngine := &engine.Engine{CommentMode: MapCommentMode(f.CommentMode)} - return f.rewrite(formattedSource, lineInformationMap), nil + return []byte(formattingEngine.FormatToString(events)), nil } diff --git a/rewrite.go b/rewrite.go deleted file mode 100644 index 4f74650..0000000 --- a/rewrite.go +++ /dev/null @@ -1,140 +0,0 @@ -package main - -import "strings" - -func (f *Formatter) rewrite(formattedSource []byte, lineInformationMap map[int]*lineInformation) []byte { - sourceLines := strings.Split(string(formattedSource), "\n") - resultLines := make([]string, 0, len(sourceLines)) - previousWasOpenBrace := false - previousStatementType := "" - previousWasComment := false - previousWasTopLevel := false - previousWasScoped := false - insideRawString := false - - for lineIndex, currentLine := range sourceLines { - backtickCount := countRawStringDelimiters(currentLine) - wasInsideRawString := insideRawString - - if backtickCount%2 == 1 { - insideRawString = !insideRawString - } - - if wasInsideRawString { - resultLines = append(resultLines, currentLine) - - continue - } - - lineNumber := lineIndex + 1 - trimmedLine := strings.TrimSpace(currentLine) - - if trimmedLine == "" { - continue - } - - isClosingBraceLine := isClosingBrace(currentLine) - isOpeningBraceLine := isOpeningBrace(currentLine) - isCaseLabelLine := isCaseLabel(currentLine) - isCommentOnlyLine := isCommentOnly(currentLine) - isPackageDeclaration := isPackageLine(trimmedLine) - currentInformation := lineInformationMap[lineNumber] - currentStatementType := "" - - if currentInformation != nil { - currentStatementType = currentInformation.statementType - } - - if isPackageDeclaration { - currentStatementType = "package" - } - - needsBlankLine := false - currentIsTopLevel := currentInformation != nil && currentInformation.isTopLevel - currentIsScoped := currentInformation != nil && currentInformation.isScoped - - if len(resultLines) > 0 && !previousWasOpenBrace && !isClosingBraceLine && !isCaseLabelLine { - if currentIsTopLevel && previousWasTopLevel && currentStatementType != previousStatementType { - if f.CommentMode == CommentsFollow && previousWasComment { - } else { - needsBlankLine = true - } - } else if currentInformation != nil && (currentIsScoped || previousWasScoped) { - if f.CommentMode == CommentsFollow && previousWasComment { - } else { - needsBlankLine = true - } - } else if currentStatementType != "" && previousStatementType != "" && currentStatementType != previousStatementType { - if f.CommentMode == CommentsFollow && previousWasComment { - } else { - needsBlankLine = true - } - } - - if f.CommentMode == CommentsFollow && isCommentOnlyLine && !previousWasComment { - nextLineNumber := f.findNextNonCommentLine(sourceLines, lineIndex+1) - - if nextLineNumber > 0 { - nextInformation := lineInformationMap[nextLineNumber] - - if nextInformation != nil { - nextIsTopLevel := nextInformation.isTopLevel - nextIsScoped := nextInformation.isScoped - - if nextIsTopLevel && previousWasTopLevel && nextInformation.statementType != previousStatementType { - needsBlankLine = true - } else if nextIsScoped || previousWasScoped { - needsBlankLine = true - } else if nextInformation.statementType != "" && previousStatementType != "" && nextInformation.statementType != previousStatementType { - needsBlankLine = true - } - } - } - } - } - - if needsBlankLine { - resultLines = append(resultLines, "") - } - - resultLines = append(resultLines, currentLine) - previousWasOpenBrace = isOpeningBraceLine || isCaseLabelLine - previousWasComment = isCommentOnlyLine - - if currentInformation != nil { - previousStatementType = currentInformation.statementType - previousWasTopLevel = currentInformation.isTopLevel - previousWasScoped = currentInformation.isScoped - } else if currentStatementType != "" { - previousStatementType = currentStatementType - previousWasTopLevel = false - previousWasScoped = false - } - } - - outputString := strings.Join(resultLines, "\n") - - if !strings.HasSuffix(outputString, "\n") { - outputString += "\n" - } - - return []byte(outputString) -} - -func (f *Formatter) findNextNonCommentLine(sourceLines []string, startLineIndex int) int { - for lineIndex := startLineIndex; lineIndex < len(sourceLines); lineIndex++ { - trimmedLine := strings.TrimSpace(sourceLines[lineIndex]) - - if trimmedLine == "" { - continue - } - - if isCommentOnly(sourceLines[lineIndex]) { - continue - } - - return lineIndex + 1 - } - - return 0 -} |