[WIP] commit - moar import sorting
This commit is contained in:
parent
44f550bcb5
commit
345e394b01
|
@ -97,17 +97,24 @@ struct ASTInformation
|
|||
return result;
|
||||
}
|
||||
|
||||
static struct ImportLine
|
||||
{
|
||||
string importString;
|
||||
string attribString;
|
||||
string renamedAs;
|
||||
}
|
||||
|
||||
/// 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
|
||||
ImportLine[] importLinesFor(const size_t scopeOrdinal) const
|
||||
{
|
||||
import std.algorithm;
|
||||
import std.range;
|
||||
|
||||
uint idx = 0;
|
||||
string[] result;
|
||||
ImportLine[] result;
|
||||
|
||||
auto imports = importScopes[scopeOrdinal];
|
||||
|
||||
|
@ -129,12 +136,13 @@ struct ASTInformation
|
|||
|| 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];
|
||||
|
@ -156,6 +164,9 @@ struct ASTInformation
|
|||
/// Lines containing attribute declarations
|
||||
size_t[] attributeDeclarationLines;
|
||||
|
||||
/// lines in which imports end
|
||||
size_t[] importEndLines;
|
||||
|
||||
/// Case statement colon locations
|
||||
size_t[] caseEndLocations;
|
||||
|
||||
|
@ -226,9 +237,13 @@ final class FormatVisitor : ASTVisitor
|
|||
astInformation.arrayStartLocations ~= arrayInitializer.startLocation;
|
||||
arrayInitializer.accept(this);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -518,14 +518,17 @@ private:
|
|||
}
|
||||
else
|
||||
{
|
||||
if (!isImport)
|
||||
writeImportLinesFor(0);
|
||||
|
||||
while(currentIs(tok!"import"))
|
||||
{
|
||||
// skip to the ending ; of the import statement
|
||||
while(!currentIs(tok!";")) { index++; }
|
||||
while(!currentIs(tok!";"))
|
||||
index++;
|
||||
// skip past the ;
|
||||
index++;
|
||||
}
|
||||
newline();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -624,6 +627,38 @@ private:
|
|||
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()
|
||||
{
|
||||
import dfmt.editorconfig : OptionalBoolean;
|
||||
|
@ -814,16 +849,7 @@ private:
|
|||
|
||||
if (writeImports && astInformation.importScopes[scopeOrdinal].length)
|
||||
{
|
||||
foreach(importString;astInformation.importsFor(scopeOrdinal))
|
||||
{
|
||||
newline();
|
||||
if (importString !is null)
|
||||
{
|
||||
write("import ");
|
||||
write(importString);
|
||||
write(";");
|
||||
}
|
||||
}
|
||||
writeImportLinesFor(scopeOrdinal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1472,6 +1498,12 @@ private:
|
|||
|
||||
void writeToken()
|
||||
{
|
||||
if (config.dfmt_sort_imports && astInformation.skipTokenLocations.canFindIndex(current.index))
|
||||
{
|
||||
index++;
|
||||
return ;
|
||||
}
|
||||
|
||||
import std.range : retro;
|
||||
import std.algorithm.searching : countUntil;
|
||||
|
||||
|
|
Loading…
Reference in New Issue