From 57e4bbd4d7afe612a9ed1cedb7db33268de3cc3d Mon Sep 17 00:00:00 2001
From: Hau Nguyen <hauvipapro@gmail.com>
Date: Sun, 27 Nov 2022 01:14:51 +0700
Subject: [PATCH] feat: sort imports using default Go (wip)

---
 internal/imports/formatter.go | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/internal/imports/formatter.go b/internal/imports/formatter.go
index 8d6f9f3..3f7bded 100644
--- a/internal/imports/formatter.go
+++ b/internal/imports/formatter.go
@@ -239,6 +239,20 @@ func (ft *Formatter) formatImports(
 		return nil, err
 	}
 
+	// Post process to sort imports using default Go
+	// TODO: find a way to use ast.SortImports without parse ast again
+	formattedASTFile, err := parser.ParseFile(fset, path, formattedBuffer.Bytes(), parserMode)
+	if err != nil {
+		return nil, fmt.Errorf("parser: failed to parse file [%s]: %w", path, err)
+	}
+
+	ast.SortImports(fset, formattedASTFile)
+
+	formattedBuffer.Reset()
+	if err := printer.Fprint(formattedBuffer, fset, astFile); err != nil {
+		return nil, err
+	}
+
 	return formattedBuffer.Bytes(), nil
 }