From 44f550bcb52e3b2358b13e577a334cca0652e818 Mon Sep 17 00:00:00 2001 From: Stefan Koch Date: Mon, 27 Nov 2017 12:36:49 +0100 Subject: [PATCH] add import grouping into the sorting --- src/dfmt/ast_info.d | 46 +++++++++++++++++++++++++++++++++++--------- src/dfmt/formatter.d | 11 ++++++----- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/dfmt/ast_info.d b/src/dfmt/ast_info.d index 30c1be6..bebda22 100644 --- a/src/dfmt/ast_info.d +++ b/src/dfmt/ast_info.d @@ -15,12 +15,6 @@ struct Import string attribString; } -extern (C) static bool importStringLess(const Import a, const Import b) -{ - return a.importStrings < b.importStrings; -} - - /// AST information that is needed by the formatter. struct ASTInformation { @@ -89,11 +83,24 @@ struct ASTInformation return bestOrdinal; } + bool importStringLess(const Import a, const Import b) const + { + bool result; + result = a.importStrings < b.importStrings; + /* + if (moduleNameStrings.length && isCloserTo(a.importStrings, b.importStrings, moduleNameStrings) + { + + } + */ + + return result; + } /// returns an array of indecies into the token array /// which are the indecies of the imports to be written /// in sorted order - + /// newlines for grouping are enoded as a null entry string[] importsFor(const size_t scopeOrdinal) const { import std.algorithm; @@ -106,12 +113,31 @@ struct ASTInformation if (imports.length) { - result.length = imports.length; + const max_sorted_imports_length = imports.length * 2; + // account for newlines + result.length = max_sorted_imports_length; + auto sortedImports = + (cast(Import[])imports).sort!((a, b) => importStringLess(a, b)) + .release; - foreach(imp;(cast(Import[])imports).sort!(importStringLess)) + foreach(i, imp;sortedImports) { + if (i > 0 && imp.importStrings.length > 1) + { + const prev = sortedImports[i-1]; + if (prev.importStrings.length < 2 + || imp.importStrings[0 .. $-1] != prev.importStrings[0 .. $-1] + ) + { + result[idx++] = null; + } + } + result[idx++] = imp.attribString ~ imp.importStrings.join("."); + } + + result = result[0 .. idx]; } else { @@ -166,6 +192,8 @@ struct ASTInformation /// contains all imports inside scope Import[][] importScopes; + ///contain the current fqn of the module + string[] moduleNameStrings; } /// Collects information from the AST that is useful for the formatter diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index fbdd119..59d7ae1 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -817,12 +817,13 @@ private: foreach(importString;astInformation.importsFor(scopeOrdinal)) { newline(); - write("import "); - write(importString); - write(";"); + if (importString !is null) + { + write("import "); + write(importString); + write(";"); + } } - newline(); - } } }