[WIP] commit - moar import sorting

This commit is contained in:
Stefan Koch 2017-11-27 15:57:17 +01:00
parent 44f550bcb5
commit 345e394b01
2 changed files with 65 additions and 18 deletions

View File

@ -97,17 +97,24 @@ struct ASTInformation
return result; return result;
} }
static struct ImportLine
{
string importString;
string attribString;
string renamedAs;
}
/// 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 /// newlines for grouping are enoded as a null entry
string[] importsFor(const size_t scopeOrdinal) const ImportLine[] importLinesFor(const size_t scopeOrdinal) const
{ {
import std.algorithm; import std.algorithm;
import std.range; import std.range;
uint idx = 0; uint idx = 0;
string[] result; ImportLine[] result;
auto imports = importScopes[scopeOrdinal]; auto imports = importScopes[scopeOrdinal];
@ -129,12 +136,13 @@ struct ASTInformation
|| imp.importStrings[0 .. $-1] != prev.importStrings[0 .. $-1] || imp.importStrings[0 .. $-1] != prev.importStrings[0 .. $-1]
) )
{ {
result[idx++] = null; result[idx++].importString = null;
} }
} }
result[idx++] = imp.attribString ~ imp.importStrings.join("."); result[idx].importString = imp.importStrings.join(".");
result[idx].renamedAs = imp.renamedAs;
result[idx++].attribString = imp.attribString;
} }
result = result[0 .. idx]; result = result[0 .. idx];
@ -156,6 +164,9 @@ struct ASTInformation
/// Lines containing attribute declarations /// Lines containing attribute declarations
size_t[] attributeDeclarationLines; size_t[] attributeDeclarationLines;
/// lines in which imports end
size_t[] importEndLines;
/// Case statement colon locations /// Case statement colon locations
size_t[] caseEndLocations; size_t[] caseEndLocations;
@ -229,6 +240,10 @@ final class FormatVisitor : ASTVisitor
void addImport(size_t scopeId, string[] importString, string renamedAs, string importAttribString) void addImport(size_t scopeId, string[] importString, string renamedAs, string importAttribString)
{ {
import std.stdio;
writeln("addImport(", scopeId, ", ", importString, ", ", renamedAs, ", ", importAttribString, ")");
astInformation.importScopes[scopeId] ~= Import(importString, renamedAs, importAttribString); astInformation.importScopes[scopeId] ~= Import(importString, renamedAs, importAttribString);
} }

View File

@ -518,14 +518,17 @@ private:
} }
else else
{ {
if (!isImport)
writeImportLinesFor(0);
while(currentIs(tok!"import")) while(currentIs(tok!"import"))
{ {
// skip to the ending ; of the import statement // skip to the ending ; of the import statement
while(!currentIs(tok!";")) { index++; } while(!currentIs(tok!";"))
index++;
// skip past the ; // skip past the ;
index++; index++;
} }
newline();
} }
} }
@ -624,6 +627,38 @@ private:
write(" "); write(" ");
} }
void writeImportLinesFor(size_t scopeOrdinal)
{
foreach(importLine;astInformation.importLinesFor(scopeOrdinal))
{
if (importLine.importString !is null)
{
newline();
write(importLine.attribString);
write("import ");
if (importLine.renamedAs)
{
write(importLine.renamedAs);
write(" = ");
}
/+ TODO deal with selective imports
if (importLine.selctiveImports)
{
}
+/
write(importLine.importString);
write(";");
}
else
{
simpleNewline();
}
}
simpleNewline();
simpleNewline();
}
void formatColon() void formatColon()
{ {
import dfmt.editorconfig : OptionalBoolean; import dfmt.editorconfig : OptionalBoolean;
@ -814,16 +849,7 @@ private:
if (writeImports && astInformation.importScopes[scopeOrdinal].length) if (writeImports && astInformation.importScopes[scopeOrdinal].length)
{ {
foreach(importString;astInformation.importsFor(scopeOrdinal)) writeImportLinesFor(scopeOrdinal);
{
newline();
if (importString !is null)
{
write("import ");
write(importString);
write(";");
}
}
} }
} }
} }
@ -1472,6 +1498,12 @@ private:
void writeToken() void writeToken()
{ {
if (config.dfmt_sort_imports && astInformation.skipTokenLocations.canFindIndex(current.index))
{
index++;
return ;
}
import std.range : retro; import std.range : retro;
import std.algorithm.searching : countUntil; import std.algorithm.searching : countUntil;