add import grouping into the sorting

This commit is contained in:
Stefan Koch 2017-11-27 12:36:49 +01:00
parent 7158cff8c7
commit 44f550bcb5
2 changed files with 43 additions and 14 deletions

View File

@ -15,12 +15,6 @@ struct Import
string attribString; 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. /// AST information that is needed by the formatter.
struct ASTInformation struct ASTInformation
{ {
@ -89,11 +83,24 @@ struct ASTInformation
return bestOrdinal; 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 /// returns an array of indecies into the token array
/// which are the indecies of the imports to be written /// which are the indecies of the imports to be written
/// in sorted order /// in sorted order
/// newlines for grouping are enoded as a null entry
string[] importsFor(const size_t scopeOrdinal) const string[] importsFor(const size_t scopeOrdinal) const
{ {
import std.algorithm; import std.algorithm;
@ -106,12 +113,31 @@ struct ASTInformation
if (imports.length) 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[idx++] = imp.attribString ~ imp.importStrings.join(".");
} }
result = result[0 .. idx];
} }
else else
{ {
@ -166,6 +192,8 @@ struct ASTInformation
/// contains all imports inside scope /// contains all imports inside scope
Import[][] importScopes; Import[][] importScopes;
///contain the current fqn of the module
string[] moduleNameStrings;
} }
/// Collects information from the AST that is useful for the formatter /// Collects information from the AST that is useful for the formatter

View File

@ -817,12 +817,13 @@ private:
foreach(importString;astInformation.importsFor(scopeOrdinal)) foreach(importString;astInformation.importsFor(scopeOrdinal))
{ {
newline(); newline();
write("import "); if (importString !is null)
write(importString); {
write(";"); write("import ");
write(importString);
write(";");
}
} }
newline();
} }
} }
} }