commit 22cb3fb18f9cdf3a77a98a09ffae06661c0f22ab
parent 38dada38189e29ded7fa8b301a26b026aa68aed9
Author: Friedel Schoen <[email protected]>
Date:   Sat, 22 Oct 2022 23:54:40 +0200
adding --merge
Diffstat:
4 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
@@ -58,6 +58,7 @@ $ importsort-d [-h] [-v] [-r] [-i] [-o <out>] [-k] [-a] [-r] <input...>
 - [ ] watch-mode (struggling with save-timings - can clear files)
   - you can add importsort-d into your onSave-hooks (e. g. [Run on Save](https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave) on VSCode)
 - [ ] support multiple imports in one line (demilited by `;`)
+- [ ] merge imports
 - [ ] stripping unused imports (maybe)
 
 ## Changelog
diff --git a/assets/help.txt b/assets/help.txt
@@ -10,7 +10,8 @@ Options:
   -k, --keep .......... keeps the line as-is instead of formatting
   -a, --attribute ..... public and static imports first
   -b, --binding ....... sorts by binding rather then the original
+  -m, --merge ......... merge imports which uses same file
   
   -r, --recursive ..... recursively search in directories
   -i, --inline ........ writes to the input
-  -o, --output <path> . writes to `path` instead of stdout
+  -o, --output <path> . writes to `path` instead of stdout
+\ No newline at end of file
diff --git a/src/main.d b/src/main.d
@@ -84,6 +84,8 @@ void main(string[] args) {
 			config.byAttribute = true;
 		} else if (arg == "--binding" || arg == "-b") {
 			config.byBinding = true;
+		} else if (arg == "--merge" || arg == "-m") {
+			config.merge = true;
 		} else if (arg == "--inline" || arg == "-i") {
 			inline = true;
 		} else if (arg == "--recursive" || arg == "-r") {
diff --git a/src/sort.d b/src/sort.d
@@ -1,6 +1,6 @@
 module importsort.sort;
 
-import std.algorithm : findSplit, sort;
+import std.algorithm : findSplit, remove, sort;
 import std.array : split;
 import std.file : DirEntry, rename;
 import std.functional : unaryFun;
@@ -15,10 +15,10 @@ enum PATTERN = ctRegex!`^(\s*)(?:(public|static)\s+)?import\s+(?:(\w+)\s*=\s*)?(
 
 struct SortConfig {
 	bool keepLine = false;
-
 	bool byAttribute = false;
 	bool byBinding = false;
 	bool verbose = false;
+	bool merge = false;
 }
 
 struct Identifier {
@@ -60,9 +60,26 @@ void writeImports(File outfile, SortConfig config, Import[] matches) {
 	if (!matches)
 		return;
 
+	if (config.merge) {
+		for (int i = 0; i < matches.length; i++) {
+			for (int j = i + 1; j < matches.length; j++) {
+				if (matches[i].name.original == matches[j].name.original
+					&& matches[i].name.binding == matches[j].name.binding) {
+
+					matches[i].line = null;
+					matches[i].idents ~= matches[j].idents;
+					matches = matches.remove(j);
+					j--;
+				}
+			}
+		}
+	}
+
 	matches.sort!((a, b) => a.sortBy < b.sortBy);
+	bool first;
+
 	foreach (m; matches) {
-		if (config.keepLine) {
+		if (config.keepLine && m.line.length > 0) {
 			outfile.write(m.line);
 		} else {
 			outfile.write(m.begin);
@@ -75,8 +92,10 @@ void writeImports(File outfile, SortConfig config, Import[] matches) {
 			} else {
 				outfile.write("import " ~ m.name.original);
 			}
-			foreach (i, ident; m.idents) {
-				auto begin = i == 0 ? " : " : ", ";
+			first = true;
+			foreach (ident; m.idents) {
+				auto begin = first ? " : " : ", ";
+				first = false;
 				if (ident.hasBinding) { // hasBinding
 					outfile.writef("%s%s = %s", begin, ident.binding, ident.original);
 				} else {