From 072582d9cbbf91bc07fe4a5fdaac7e7b3197411d Mon Sep 17 00:00:00 2001 From: Maksim Smirnov Date: Sat, 23 Aug 2025 09:49:33 +0200 Subject: [PATCH] fix changed not saved to the file --- editorconfig/fix_command.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/editorconfig/fix_command.go b/editorconfig/fix_command.go index 1fa65bd..ec15966 100644 --- a/editorconfig/fix_command.go +++ b/editorconfig/fix_command.go @@ -50,12 +50,18 @@ func FixCommand(c *cli.Context) error { // Run line checkers and fixers. lines := SplitIntoLines(fileContent) lineNo := 1 - for _, line := range lines { + for i, line := range lines { for ruleName, ruleValue := range rules { if lineChecker, ok := lineCheckers[ruleName]; ok { result := lineChecker(ruleValue, line) if !result.isOk { - fmt.Println(f + ": line " + strconv.Itoa(lineNo) + ": " + ruleName + ": " + result.messageIfNotOk) + if result.fixer != nil { + lines[i] = result.fixer(ruleValue, line) + hasChanged = true + fmt.Println(f + ": line " + strconv.Itoa(lineNo) + ": " + ruleName + ": fixed") + } else { + fmt.Println(f + ": line " + strconv.Itoa(lineNo) + ": " + ruleName + ": " + result.messageIfNotOk) + } // Don't show more than 1 error per line. break } @@ -63,14 +69,19 @@ func FixCommand(c *cli.Context) error { } lineNo++ } + + // Rejoin the lines if any were changed + if hasChanged { + fileContent = strings.Join(lines, "\n") + } if hasChanged { - fileHandler, err := os.Open(f) + err := os.WriteFile(f, []byte(fileContent), 0644) if err != nil { - fmt.Println("Could not write to " + f) + fmt.Println("Could not write to " + f + ": " + err.Error()) + } else { + fmt.Println("Wrote to " + f) } - fileHandler.WriteString(fileContent) - fmt.Println("Wrote to " + f) } }