Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Organize imports according to Google style #273

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions packages/prettier-plugin-java/src/printers/packages-and-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const {
rejectAndJoinSeps,
displaySemicolon,
putIntoCurlyBraces,
getBlankLinesSeparator
getBlankLinesSeparator,
sortImports
} = require("./printer-utils");

class PackagesAndModulesPrettierVisitor {
Expand All @@ -22,17 +23,20 @@ class PackagesAndModulesPrettierVisitor {

ordinaryCompilationUnit(ctx) {
const packageDecl = this.visit(ctx.packageDeclaration);
// TODO: Should imports be sorted? Can imports in Java be safely sorted?
// TODO2: should the imports be grouped in some manner?
const importsDecl = this.mapVisit(ctx.importDeclaration);

const sortedImportsDecl = sortImports(ctx.importDeclaration);
const nonStaticImports = this.mapVisit(sortedImportsDecl.nonStaticImports);
const staticImports = this.mapVisit(sortedImportsDecl.staticImports);

const typesDecl = this.mapVisit(ctx.typeDeclaration);

// TODO: utility to add item+line (or multiple lines) but only if an item exists
return rejectAndConcat([
rejectAndJoin(concat([line, line]), [
rejectAndJoin(concat([hardline, hardline]), [
packageDecl,
rejectAndJoin(line, importsDecl),
rejectAndJoin(concat([line, line]), typesDecl)
rejectAndJoin(hardline, staticImports),
rejectAndJoin(hardline, nonStaticImports),
rejectAndJoin(concat([hardline, hardline]), typesDecl)
]),
line
]);
Expand Down
55 changes: 54 additions & 1 deletion packages/prettier-plugin-java/src/printers/printer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,58 @@ function isStatementEmptyStatement(statement) {
);
}

function sortImports(imports) {
const staticImports = [];
const nonStaticImports = [];

if (imports !== undefined) {
for (let i = 0; i < imports.length; i++) {
if (imports[i].children.Static !== undefined) {
staticImports.push(imports[i]);
} else if (imports[i].children.emptyStatement === undefined) {
nonStaticImports.push(imports[i]);
}
}

// TODO: Could be optimized as we could expect that the array is already almost sorted
const comparator = (first, second) =>
compareFqn(
first.children.packageOrTypeName[0],
second.children.packageOrTypeName[0]
);
staticImports.sort(comparator);
nonStaticImports.sort(comparator);
}

return {
staticImports,
nonStaticImports
};
}

function compareFqn(packageOrTypeNameFirst, packageOrTypeNameSecond) {
const identifiersFirst = packageOrTypeNameFirst.children.Identifier;
const identifiersSecond = packageOrTypeNameSecond.children.Identifier;

const minParts = Math.min(identifiersFirst.length, identifiersSecond.length);
for (let i = 0; i < minParts; i++) {
const identifierComparison = identifiersFirst[i].image.localeCompare(
identifiersSecond[i].image
);
if (identifierComparison !== 0) {
return identifierComparison;
}
}

if (identifiersFirst.length < identifiersSecond.length) {
return -1;
} else if (identifiersFirst.length > identifiersSecond.length) {
return 1;
}

return 0;
}

module.exports = {
buildFqn,
reject,
Expand All @@ -556,5 +608,6 @@ module.exports = {
retrieveNodesToken,
buildOriginalText,
getCSTNodeStartEndToken,
isStatementEmptyStatement
isStatementEmptyStatement,
sortImports
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package my.own.pkg;

import something.Different;
import java.utils.*;;;
import abc.def.Something;
import abc.def.Another;;;
import abc.def;
import static abc.def;
import static something.Different;
import static java.utils.*;;;
import static abc.def.Something;
import static abc.def.Another;;;
import one.last;;;

public class PackageAndImports {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package my.own.pkg;

import static abc.def;
import static abc.def.Another;
import static abc.def.Something;
import static java.utils.*;
import static something.Different;

import abc.def;
import abc.def.Another;
import abc.def.Something;
import java.utils.*;
import one.last;
import something.Different;

public class PackageAndImports {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package my.own.pkg;

public class PackageAndImports {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package my.own.pkg;

public class PackageAndImports {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
import java.utils.*;;;
import abc.def.Something;
import abc.def.Another;;;
import abc.def;
import one.last;;;

public class PackageAndImports {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package my.own.pkg;

import something.Different;
import java.utils.*;
import abc.def.Something;
import abc.def;
import abc.def.Another;
import abc.def.Something;
import java.utils.*;
import one.last;
import something.Different;

public class PackageAndImports {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package my.own.pkg;

import static abc.def;
import static something.Different;
import static java.utils.*;;;
import static abc.def.Something;
import static abc.def.Another;;;

public class PackageAndImports {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package my.own.pkg;

import static abc.def;
import static abc.def.Another;
import static abc.def.Something;
import static java.utils.*;
import static something.Different;

public class PackageAndImports {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const { testSample } = require("../../test-utils");
const path = require("path");

describe("prettier-java", () => {
require("../../test-utils").testSample(__dirname);
testSample(path.resolve(__dirname, "./classWithMixedImports"));
testSample(path.resolve(__dirname, "./classWithNoImports"));
testSample(path.resolve(__dirname, "./classWithOnlyStaticImports"));
testSample(path.resolve(__dirname, "./classWithOnlyNonStaticImports"));
});