private void removeEntryFromImportLayouts() { int selected = myImportLayoutTable.getSelectedRow(); if (selected < 0) { return; } PackageEntry entry = myImportLayoutList.getEntryAt(selected); if (entry == PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY || entry == PackageEntry.ALL_OTHER_IMPORTS_ENTRY) { return; } TableUtil.stopEditing(myImportLayoutTable); myImportLayoutList.removeEntryAt(selected); AbstractTableModel model = (AbstractTableModel)myImportLayoutTable.getModel(); model.fireTableRowsDeleted(selected, selected); if (selected >= myImportLayoutList.getEntryCount()) { selected--; } if (selected >= 0) { myImportLayoutTable.setRowSelectionInterval(selected, selected); } }
public static List<Pair<String, Boolean>> sortItemsAccordingToSettings(List<Pair<String, Boolean>> names, final CodeStyleSettings settings) { int[] entryForName = ArrayUtil.newIntArray(names.size()); PackageEntry[] entries = settings.IMPORT_LAYOUT_TABLE.getEntries(); for(int i = 0; i < names.size(); i++){ Pair<String, Boolean> pair = names.get(i); String packageName = pair.getFirst(); Boolean isStatic = pair.getSecond(); entryForName[i] = findEntryIndex(packageName, isStatic, entries); } List<Pair<String, Boolean>> resultList = new ArrayList<Pair<String, Boolean>>(names.size()); for(int i = 0; i < entries.length; i++){ for(int j = 0; j < names.size(); j++){ if (entryForName[j] == i){ resultList.add(names.get(j)); names.set(j, null); } } } for (Pair<String, Boolean> name : names) { if (name != null) resultList.add(name); } return resultList; }
public int getEmptyLinesBetween(@NotNull PsiImportStatementBase statement1, @NotNull PsiImportStatementBase statement2){ int index1 = findEntryIndex(statement1); int index2 = findEntryIndex(statement2); if (index1 == index2) return 0; if (index1 > index2) { int t = index1; index1 = index2; index2 = t; } PackageEntry[] entries = mySettings.IMPORT_LAYOUT_TABLE.getEntries(); int maxSpace = 0; for(int i = index1 + 1; i < index2; i++){ if (entries[i] == PackageEntry.BLANK_LINE_ENTRY){ int space = 0; do{ space++; } while(entries[++i] == PackageEntry.BLANK_LINE_ENTRY); maxSpace = Math.max(maxSpace, space); } } return maxSpace; }
private static int findEntryIndex(@NotNull String packageName, boolean isStatic, @NotNull PackageEntry[] entries) { PackageEntry bestEntry = null; int bestEntryIndex = -1; int allOtherStaticIndex = -1; int allOtherIndex = -1; for(int i = 0; i < entries.length; i++){ PackageEntry entry = entries[i]; if (entry == PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY) { allOtherStaticIndex = i; } if (entry == PackageEntry.ALL_OTHER_IMPORTS_ENTRY) { allOtherIndex = i; } if (entry.isBetterMatchForPackageThan(bestEntry, packageName, isStatic)) { bestEntry = entry; bestEntryIndex = i; } } if (bestEntryIndex == -1 && isStatic && allOtherStaticIndex == -1 && allOtherIndex != -1) { // if no layout for static imports specified, put them among all others bestEntryIndex = allOtherIndex; } return bestEntryIndex; }
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 void addPackageToImportLayouts() { int selected = myImportLayoutTable.getSelectedRow() + 1; if (selected < 0) { selected = myImportLayoutList.getEntryCount(); } PackageEntry entry = new PackageEntry(false, "", true); myImportLayoutList.insertEntryAt(entry, selected); refreshTableModel(selected, myImportLayoutTable); }
private void addBlankLine() { int selected = myImportLayoutTable.getSelectedRow() + 1; if (selected < 0) { selected = myImportLayoutList.getEntryCount(); } myImportLayoutList.insertEntryAt(PackageEntry.BLANK_LINE_ENTRY, selected); AbstractTableModel model = (AbstractTableModel)myImportLayoutTable.getModel(); model.fireTableRowsInserted(selected, selected); myImportLayoutTable.setRowSelectionInterval(selected, selected); }
private void moveRowUp() { int selected = myImportLayoutTable.getSelectedRow(); if (selected < 1) { return; } TableUtil.stopEditing(myImportLayoutTable); PackageEntry entry = myImportLayoutList.getEntryAt(selected); PackageEntry previousEntry = myImportLayoutList.getEntryAt(selected - 1); myImportLayoutList.setEntryAt(previousEntry, selected); myImportLayoutList.setEntryAt(entry, selected - 1); AbstractTableModel model = (AbstractTableModel)myImportLayoutTable.getModel(); model.fireTableRowsUpdated(selected - 1, selected); myImportLayoutTable.setRowSelectionInterval(selected - 1, selected - 1); }
private void moveRowDown() { int selected = myImportLayoutTable.getSelectedRow(); if (selected >= myImportLayoutList.getEntryCount() - 1) { return; } TableUtil.stopEditing(myImportLayoutTable); PackageEntry entry = myImportLayoutList.getEntryAt(selected); PackageEntry nextEntry = myImportLayoutList.getEntryAt(selected + 1); myImportLayoutList.setEntryAt(nextEntry, selected); myImportLayoutList.setEntryAt(entry, selected + 1); AbstractTableModel model = (AbstractTableModel)myImportLayoutTable.getModel(); model.fireTableRowsUpdated(selected, selected + 1); myImportLayoutTable.setRowSelectionInterval(selected + 1, selected + 1); }
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 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 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 static int getPackageEntryIdx(@NotNull PackageEntry[] entries, @NotNull GrImportStatement statement) { final GrCodeReferenceElement reference = statement.getImportReference(); if (reference == null) return -1; final String packageName = StringUtil.getPackageName(reference.getCanonicalText()); final boolean isStatic = statement.isStatic(); int best = -1; int allOtherStatic = -1; int allOther = -1; PackageEntry bestEntry = null; for (int i = 0, length = entries.length; i < length; i++) { PackageEntry entry = entries[i]; if (entry.isBetterMatchForPackageThan(bestEntry, packageName, isStatic)) { best = i; bestEntry = entry; } else if (entry == PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY) { allOtherStatic = i; } else if (entry == PackageEntry.ALL_OTHER_IMPORTS_ENTRY) { allOther = i; } } if (best >= 0) return best; if (isStatic && allOtherStatic != -1) return allOtherStatic; return allOther; }
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()); } }
protected static int getPackageEntryIdx(PackageEntry[] entries, GrImportStatement statement) { final GrCodeReferenceElement reference = statement.getImportReference(); if (reference == null) return -1; final String packageName = StringUtil.getPackageName(reference.getCanonicalText()); final boolean isStatic = statement.isStatic(); int best = -1; int allOtherStatic = -1; int allOther = -1; PackageEntry bestEntry = null; for (int i = 0, length = entries.length; i < length; i++) { PackageEntry entry = entries[i]; if (entry.isBetterMatchForPackageThan(bestEntry, packageName, isStatic)) { best = i; bestEntry = entry; } else if (entry == PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY) { allOtherStatic = i; } else if (entry == PackageEntry.ALL_OTHER_IMPORTS_ENTRY) { allOther = i; } } if (best >= 0) return best; if (isStatic && allOtherStatic != -1) return allOtherStatic; return allOther; }
public static List<Pair<String, Boolean>> sortItemsAccordingToSettings(List<Pair<String, Boolean>> names, final CodeStyleSettings settings) { int[] entryForName = ArrayUtil.newIntArray(names.size()); PackageEntry[] entries = settings.IMPORT_LAYOUT_TABLE.getEntries(); for(int i = 0; i < names.size(); i++) { Pair<String, Boolean> pair = names.get(i); String packageName = pair.getFirst(); Boolean isStatic = pair.getSecond(); entryForName[i] = findEntryIndex(packageName, isStatic, entries); } List<Pair<String, Boolean>> resultList = new ArrayList<Pair<String, Boolean>>(names.size()); for(int i = 0; i < entries.length; i++) { for(int j = 0; j < names.size(); j++) { if(entryForName[j] == i) { resultList.add(names.get(j)); names.set(j, null); } } } for(Pair<String, Boolean> name : names) { if(name != null) { resultList.add(name); } } return resultList; }