From 345e394b0181591c503ad81e4ac36b6b95a49d6f Mon Sep 17 00:00:00 2001 From: Stefan Koch Date: Mon, 27 Nov 2017 15:57:17 +0100 Subject: [PATCH] [WIP] commit - moar import sorting --- src/dfmt/ast_info.d | 27 ++++++++++++++++----- src/dfmt/formatter.d | 56 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 65 insertions(+), 18 deletions(-) diff --git a/src/dfmt/ast_info.d b/src/dfmt/ast_info.d index bebda22..ceb2197 100644 --- a/src/dfmt/ast_info.d +++ b/src/dfmt/ast_info.d @@ -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); } diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index 59d7ae1..4c4e67a 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -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;