public static void modify(CodeStyleSettings settings) { // Use Android XML formatter by default AndroidXmlCodeStyleSettings.getInstance(settings).USE_CUSTOM_SETTINGS = true; // XML: // Copy Android code style AndroidXmlPredefinedCodeStyle xmlStyle = new AndroidXmlPredefinedCodeStyle(); xmlStyle.apply(settings); // Java: // Set Import order settings.IMPORT_LAYOUT_TABLE.copyFrom(getAndroidImportOrder()); // Don't use wildcard imports settings.PACKAGES_TO_USE_IMPORT_ON_DEMAND.copyFrom(new PackageEntryTable()); settings.CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND = 99; // Class count to use import with "*" settings.NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND = 99; // Name count to use static import with "*" // set margin to 100 columns settings.setDefaultRightMargin(100); }
protected void addLineFeedAfter(@NotNull PsiElement psiFile, GrImportStatement result) { final GroovyCodeStyleSettings settings = CodeStyleSettingsManager.getInstance(psiFile.getProject()).getCurrentSettings().getCustomSettings(GroovyCodeStyleSettings.class); final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE; final PackageEntry[] entries = layoutTable.getEntries(); PsiElement next = result.getNextSibling(); if (PsiImplUtil.isWhiteSpaceOrNls(next)) next = next.getNextSibling(); if (PsiImplUtil.hasElementType(next, GroovyTokenTypes.mSEMI)) next = next.getNextSibling(); while (PsiImplUtil.isWhiteSpaceOrNls(next)) { next = next.getNextSibling(); } if (next instanceof GrImportStatement) { final int idx_after = getPackageEntryIdx(entries, (GrImportStatement)next); final int idx = getPackageEntryIdx(entries, result); final int spaceCount = getMaxSpaceCount(entries, idx, idx_after); ASTNode node = psiFile.getNode(); while (PsiImplUtil.isWhiteSpaceOrNls(next.getPrevSibling())) { node.removeChild(next.getPrevSibling().getNode()); } node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", spaceCount + 1), next.getNode()); } }
protected void addLineFeedAfter(GrImportStatement result) { final GroovyCodeStyleSettings settings = CodeStyleSettingsManager.getInstance(getProject()).getCurrentSettings().getCustomSettings(GroovyCodeStyleSettings.class); final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE; final PackageEntry[] entries = layoutTable.getEntries(); PsiElement next = result.getNextSibling(); if (isWhiteSpace(next)) next = next.getNextSibling(); if (hasElementType(next, GroovyTokenTypes.mSEMI)) next = next.getNextSibling(); while (isWhiteSpace(next)) { next = next.getNextSibling(); } if (next instanceof GrImportStatement) { final int idx_after = getPackageEntryIdx(entries, (GrImportStatement)next); final int idx = getPackageEntryIdx(entries, result); final int spaceCount = getMaxSpaceCount(entries, idx, idx_after); final FileASTNode node = getNode(); while (isWhiteSpace(next.getPrevSibling())) { node.removeChild(next.getPrevSibling().getNode()); } node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", spaceCount + 1), next.getNode()); } }
private static boolean isToUseImportOnDemand(@NotNull String packageName, int classCount, boolean isStaticImportNeeded, final CodeStyleSettings settings) { if(!settings.USE_SINGLE_CLASS_IMPORTS) { return true; } int limitCount = isStaticImportNeeded ? settings.NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND : settings.CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND; if(classCount >= limitCount) { return true; } if(packageName.isEmpty()) { return false; } PackageEntryTable table = settings.PACKAGES_TO_USE_IMPORT_ON_DEMAND; return table != null && table.contains(packageName); }
public static void resizeColumns(final PackageEntryTable packageTable, JBTable result, boolean areStaticImportsEnabled) { ColoredTableCellRenderer packageRenderer = new ColoredTableCellRenderer() { @Override protected void customizeCellRenderer(JTable table, Object value, boolean selected, boolean hasFocus, int row, int column) { PackageEntry entry = packageTable.getEntryAt(row); if (entry == PackageEntry.BLANK_LINE_ENTRY) { append("<blank line>", SimpleTextAttributes.GRAYED_ATTRIBUTES); } else { TextAttributes attributes = JavaHighlightingColors.KEYWORD.getDefaultAttributes(); append("import", SimpleTextAttributes.fromTextAttributes(attributes)); if (entry.isStatic()) { append(" ", SimpleTextAttributes.REGULAR_ATTRIBUTES); append("static", SimpleTextAttributes.fromTextAttributes(attributes)); } append(" ", SimpleTextAttributes.REGULAR_ATTRIBUTES); if (entry == PackageEntry.ALL_OTHER_IMPORTS_ENTRY || entry == PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY) { append("all other imports", SimpleTextAttributes.REGULAR_ATTRIBUTES); } else { append(entry.getPackageName() + ".*", SimpleTextAttributes.REGULAR_ATTRIBUTES); } } } }; if (areStaticImportsEnabled) { fixColumnWidthToHeader(result, 0); fixColumnWidthToHeader(result, 2); result.getColumnModel().getColumn(1).setCellRenderer(packageRenderer); result.getColumnModel().getColumn(0).setCellRenderer(new BooleanTableCellRenderer()); result.getColumnModel().getColumn(2).setCellRenderer(new BooleanTableCellRenderer()); } else { fixColumnWidthToHeader(result, 1); result.getColumnModel().getColumn(0).setCellRenderer(packageRenderer); result.getColumnModel().getColumn(1).setCellRenderer(new BooleanTableCellRenderer()); } }
private static void addPackageToPackages(JBTable table, PackageEntryTable list) { int selected = table.getSelectedRow() + 1; if (selected < 0) { selected = list.getEntryCount(); } PackageEntry entry = new PackageEntry(false, "", true); list.insertEntryAt(entry, selected); ImportLayoutPanel.refreshTableModel(selected, table); }
private static void removeEntryFromPackages(JBTable table, PackageEntryTable list) { int selected = table.getSelectedRow(); if (selected < 0) return; TableUtil.stopEditing(table); list.removeEntryAt(selected); AbstractTableModel model = (AbstractTableModel)table.getModel(); model.fireTableRowsDeleted(selected, selected); if (selected >= list.getEntryCount()) { selected--; } if (selected >= 0) { table.setRowSelectionInterval(selected, selected); } }
private static boolean isModified(PackageEntryTable list, PackageEntryTable table) { if (list.getEntryCount() != table.getEntryCount()) { return true; } for (int i = 0; i < list.getEntryCount(); i++) { PackageEntry entry1 = list.getEntryAt(i); PackageEntry entry2 = table.getEntryAt(i); if (!entry1.equals(entry2)) { return true; } } return false; }
private static boolean isToUseImportOnDemand(@NotNull String packageName, int classCount, boolean isStaticImportNeeded, final CodeStyleSettings settings){ if (!settings.USE_SINGLE_CLASS_IMPORTS) return true; int limitCount = isStaticImportNeeded ? settings.NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND : settings.CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND; if (classCount >= limitCount) return true; if (packageName.isEmpty()) return false; PackageEntryTable table = settings.PACKAGES_TO_USE_IMPORT_ON_DEMAND; return table != null && table.contains(packageName); }
private static PackageEntryTable getAndroidImportOrder() { PackageEntryTable table = new PackageEntryTable(); // From http://s.android.com/source/code-style.html, the order should be: // 1. Android imports // 2. Imports from 3rd parties (com, junit, net, org) // 3. java and javax table.addEntry(new PackageEntry(false, "android", true)); table.addEntry(PackageEntry.BLANK_LINE_ENTRY); table.addEntry(new PackageEntry(false, "com", true)); table.addEntry(PackageEntry.BLANK_LINE_ENTRY); table.addEntry(new PackageEntry(false, "junit", true)); table.addEntry(PackageEntry.BLANK_LINE_ENTRY); table.addEntry(new PackageEntry(false, "net", true)); table.addEntry(PackageEntry.BLANK_LINE_ENTRY); table.addEntry(new PackageEntry(false, "org", true)); table.addEntry(PackageEntry.BLANK_LINE_ENTRY); table.addEntry(new PackageEntry(false, "java", true)); table.addEntry(PackageEntry.BLANK_LINE_ENTRY); table.addEntry(new PackageEntry(false, "javax", true)); table.addEntry(PackageEntry.BLANK_LINE_ENTRY); // handle any other conditions table.addEntry(PackageEntry.ALL_OTHER_IMPORTS_ENTRY); table.addEntry(PackageEntry.BLANK_LINE_ENTRY); // location of static imports is not defined, we put it last table.addEntry(PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY); table.addEntry(PackageEntry.BLANK_LINE_ENTRY); return table; }
protected void addLineFeedBefore(@NotNull PsiElement psiFile, @NotNull GrImportStatement result) { final GroovyCodeStyleSettings settings = CodeStyleSettingsManager.getInstance(psiFile.getProject()).getCurrentSettings().getCustomSettings(GroovyCodeStyleSettings.class); final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE; final PackageEntry[] entries = layoutTable.getEntries(); PsiElement prev = result.getPrevSibling(); while (PsiImplUtil.isWhiteSpaceOrNls(prev)) { prev = prev.getPrevSibling(); } if (PsiImplUtil.hasElementType(prev, GroovyTokenTypes.mSEMI)) prev = prev.getPrevSibling(); if (PsiImplUtil.isWhiteSpaceOrNls(prev)) prev = prev.getPrevSibling(); if (prev instanceof GrImportStatement) { final int idx_before = getPackageEntryIdx(entries, (GrImportStatement)prev); final int idx = getPackageEntryIdx(entries, result); final int spaceCount = getMaxSpaceCount(entries, idx_before, idx); //skip space and semicolon after import if (PsiImplUtil.isWhiteSpaceOrNls(prev.getNextSibling()) && PsiImplUtil .hasElementType(prev.getNextSibling().getNextSibling(), GroovyTokenTypes.mSEMI)) prev = prev.getNextSibling().getNextSibling(); ASTNode node = psiFile.getNode(); while (PsiImplUtil.isWhiteSpaceOrNls(prev.getNextSibling())) { node.removeChild(prev.getNextSibling().getNode()); } node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", spaceCount + 1), result.getNode()); } }
public static void resizeColumns(final PackageEntryTable packageTable, JBTable result, boolean areStaticImportsEnabled) { ColoredTableCellRenderer packageRenderer = new ColoredTableCellRenderer() { @Override protected void customizeCellRenderer(JTable table, Object value, boolean selected, boolean hasFocus, int row, int column) { PackageEntry entry = packageTable.getEntryAt(row); if (entry == PackageEntry.BLANK_LINE_ENTRY) { append(" <blank line>", SimpleTextAttributes.LINK_ATTRIBUTES); } else { TextAttributes attributes = JavaHighlightingColors.KEYWORD.getDefaultAttributes(); append("import", SimpleTextAttributes.fromTextAttributes(attributes)); if (entry.isStatic()) { append(" ", SimpleTextAttributes.REGULAR_ATTRIBUTES); append("static", SimpleTextAttributes.fromTextAttributes(attributes)); } append(" ", SimpleTextAttributes.REGULAR_ATTRIBUTES); if (entry == PackageEntry.ALL_OTHER_IMPORTS_ENTRY || entry == PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY) { append("all other imports", SimpleTextAttributes.REGULAR_ATTRIBUTES); } else { append(entry.getPackageName() + ".*", SimpleTextAttributes.REGULAR_ATTRIBUTES); } } } }; if (areStaticImportsEnabled) { fixColumnWidthToHeader(result, 0); fixColumnWidthToHeader(result, 2); result.getColumnModel().getColumn(1).setCellRenderer(packageRenderer); result.getColumnModel().getColumn(0).setCellRenderer(new BooleanTableCellRenderer()); result.getColumnModel().getColumn(2).setCellRenderer(new BooleanTableCellRenderer()); } else { fixColumnWidthToHeader(result, 1); result.getColumnModel().getColumn(0).setCellRenderer(packageRenderer); result.getColumnModel().getColumn(1).setCellRenderer(new BooleanTableCellRenderer()); } }
protected void addLineFeedBefore(GrImportStatement result) { final GroovyCodeStyleSettings settings = CodeStyleSettingsManager.getInstance(getProject()).getCurrentSettings().getCustomSettings(GroovyCodeStyleSettings.class); final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE; final PackageEntry[] entries = layoutTable.getEntries(); PsiElement prev = result.getPrevSibling(); while (isWhiteSpace(prev)) { prev = prev.getPrevSibling(); } if (hasElementType(prev, GroovyTokenTypes.mSEMI)) prev = prev.getPrevSibling(); if (isWhiteSpace(prev)) prev = prev.getPrevSibling(); if (prev instanceof GrImportStatement) { final int idx_before = getPackageEntryIdx(entries, (GrImportStatement)prev); final int idx = getPackageEntryIdx(entries, result); final int spaceCount = getMaxSpaceCount(entries, idx_before, idx); //skip space and semicolon after import if (isWhiteSpace(prev.getNextSibling()) && hasElementType(prev.getNextSibling().getNextSibling(), GroovyTokenTypes.mSEMI)) prev = prev.getNextSibling().getNextSibling(); final FileASTNode node = getNode(); while (isWhiteSpace(prev.getNextSibling())) { node.removeChild(prev.getNextSibling().getNode()); } node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", spaceCount + 1), result.getNode()); } }
public PackageEntryTable getImportLayoutList() { return myImportLayoutList; }
private void refreshTable(final JBTable table, final PackageEntryTable packageTable) { AbstractTableModel model = (AbstractTableModel)table.getModel(); table.createDefaultColumnsFromModel(); model.fireTableDataChanged(); ImportLayoutPanel.resizeColumns(packageTable, table, myImportLayoutPanel.areStaticImportsEnabled()); }
@Nullable private PsiElement getAnchorToInsertImportAfter(@NotNull GroovyFile psiFile, @NotNull GrImportStatement statement) { final GroovyCodeStyleSettings settings = CodeStyleSettingsManager.getInstance(psiFile.getProject()).getCurrentSettings().getCustomSettings( GroovyCodeStyleSettings.class); final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE; final PackageEntry[] entries = layoutTable.getEntries(); GrImportStatement[] importStatements = psiFile.getImportStatements(); if (importStatements.length == 0) { final GrPackageDefinition definition = psiFile.getPackageDefinition(); if (definition != null) { return definition; } return getShellComment(psiFile); } final Comparator<GrImportStatement> comparator = GroovyImportOptimizer.getComparator(settings); final int idx = getPackageEntryIdx(entries, statement); PsiElement anchor = null; for (GrImportStatement importStatement : importStatements) { final int i = getPackageEntryIdx(entries, importStatement); if (i < idx) { anchor = importStatement; } else if (i > idx) { break; } else if (comparator.compare(statement, importStatement) > 0) { anchor = importStatement; } else { break; } } if (anchor == null) anchor = psiFile.getPackageDefinition(); if (anchor == null) anchor = getShellComment(psiFile); if (anchor == null && importStatements.length > 0) anchor = importStatements[0].getPrevSibling(); return anchor; }
@Nullable private PsiElement getAnchorToInsertImportAfter(GrImportStatement statement) { final GroovyCodeStyleSettings settings = CodeStyleSettingsManager.getInstance(getProject()).getCurrentSettings().getCustomSettings( GroovyCodeStyleSettings.class); final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE; final PackageEntry[] entries = layoutTable.getEntries(); GrImportStatement[] importStatements = getImportStatements(); if (importStatements.length == 0) { final GrPackageDefinition definition = getPackageDefinition(); if (definition != null) { return definition; } return getShellComment(); } final Comparator<GrImportStatement> comparator = GroovyImportOptimizer.getComparator(settings); final int idx = getPackageEntryIdx(entries, statement); PsiElement anchor = null; for (GrImportStatement importStatement : importStatements) { final int i = getPackageEntryIdx(entries, importStatement); if (i < idx) { anchor = importStatement; } else if (i > idx) { break; } else if (comparator.compare(statement, importStatement) > 0) { anchor = importStatement; } else { break; } } if (anchor == null) anchor = getPackageDefinition(); if (anchor == null) anchor = getShellComment(); if (anchor == null && importStatements.length > 0) anchor = importStatements[0].getPrevSibling(); return anchor; }
@WrapInCommand public void testStaticImportsGrouping() throws Throwable { @NonNls String text = "import static java.lang.Math.max;\n" + "import java.util.Map;\n" + "\n" + "import static java.lang.Math.min;\n" + "\n" + "import java.awt.Component;\n" + "\n" + "\n" + "\n" + "import static javax.swing.SwingConstants.CENTER;\n" + "class I {{ max(0, 0); Map.class.hashCode(); min(0,0); Component.class.hashCode(); int i = CENTER; }}"; final PsiJavaFile file = (PsiJavaFile) configureByText(JavaFileType.INSTANCE, text); assertEmpty(highlightErrors()); CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() { @Override public void run() { ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { try { CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()).clone(); settings.LAYOUT_STATIC_IMPORTS_SEPARATELY = true; PackageEntryTable table = new PackageEntryTable(); table.addEntry(PackageEntry.ALL_OTHER_IMPORTS_ENTRY); table.addEntry(PackageEntry.BLANK_LINE_ENTRY); table.addEntry(new PackageEntry(false, "javax", true)); table.addEntry(new PackageEntry(false, "java", true)); table.addEntry(PackageEntry.BLANK_LINE_ENTRY); table.addEntry(new PackageEntry(true, "java", true)); table.addEntry(PackageEntry.BLANK_LINE_ENTRY); table.addEntry(PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY); settings.IMPORT_LAYOUT_TABLE.copyFrom(table); CodeStyleSettingsManager.getInstance(getProject()).setTemporarySettings(settings); try { JavaCodeStyleManager.getInstance(getProject()).optimizeImports(file); } finally { CodeStyleSettingsManager.getInstance(getProject()).dropTemporarySettings(); } assertOrder(file, "java.awt.*", CommonClassNames.JAVA_UTIL_MAP, "static java.lang.Math.max", "static java.lang.Math.min", "static javax.swing.SwingConstants.CENTER"); } catch(Throwable e) { LOGGER.error(e); } } }); } }, "", ""); }