Search in sources :

Example 1 with PackageEntry

use of com.intellij.psi.codeStyle.PackageEntry in project intellij-community by JetBrains.

the class GroovyCodeStyleManagerImpl method getPackageEntryIdx.

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;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) PackageEntry(com.intellij.psi.codeStyle.PackageEntry)

Example 2 with PackageEntry

use of com.intellij.psi.codeStyle.PackageEntry in project intellij-community by JetBrains.

the class GroovyCodeStyleManagerImpl method addLineFeedBefore.

protected void addLineFeedBefore(@NotNull PsiElement psiFile, @NotNull GrImportStatement result) {
    final CodeStyleSettings commonSettings = CodeStyleSettingsManager.getInstance(psiFile.getProject()).getCurrentSettings();
    final GroovyCodeStyleSettings settings = commonSettings.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();
    ASTNode node = psiFile.getNode();
    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();
        while (PsiImplUtil.isWhiteSpaceOrNls(prev.getNextSibling())) {
            node.removeChild(prev.getNextSibling().getNode());
        }
        node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", spaceCount + 1), result.getNode());
    } else if (prev instanceof GrPackageDefinition) {
        node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", commonSettings.BLANK_LINES_AFTER_PACKAGE), result.getNode());
    }
}
Also used : CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) PackageEntry(com.intellij.psi.codeStyle.PackageEntry) PackageEntryTable(com.intellij.psi.codeStyle.PackageEntryTable) ASTNode(com.intellij.lang.ASTNode) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PackageEntry

use of com.intellij.psi.codeStyle.PackageEntry in project intellij-community by JetBrains.

the class GroovyCodeStyleManagerImpl method getAnchorToInsertImportAfter.

@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;
}
Also used : PackageEntry(com.intellij.psi.codeStyle.PackageEntry) PackageEntryTable(com.intellij.psi.codeStyle.PackageEntryTable) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with PackageEntry

use of com.intellij.psi.codeStyle.PackageEntry in project intellij-community by JetBrains.

the class ImportLayoutPanel method resizeColumns.

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());
    }
}
Also used : PackageEntry(com.intellij.psi.codeStyle.PackageEntry) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 5 with PackageEntry

use of com.intellij.psi.codeStyle.PackageEntry in project intellij-community by JetBrains.

the class ImportLayoutPanel method moveRowUp.

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);
}
Also used : PackageEntry(com.intellij.psi.codeStyle.PackageEntry) AbstractTableModel(javax.swing.table.AbstractTableModel)

Aggregations

PackageEntry (com.intellij.psi.codeStyle.PackageEntry)14 PackageEntryTable (com.intellij.psi.codeStyle.PackageEntryTable)4 AbstractTableModel (javax.swing.table.AbstractTableModel)4 PsiElement (com.intellij.psi.PsiElement)3 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)3 ASTNode (com.intellij.lang.ASTNode)2 GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 JBTable (com.intellij.ui.table.JBTable)1 CellEditorListener (javax.swing.event.CellEditorListener)1 ChangeEvent (javax.swing.event.ChangeEvent)1 TableCellEditor (javax.swing.table.TableCellEditor)1 TableModel (javax.swing.table.TableModel)1 Nullable (org.jetbrains.annotations.Nullable)1 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)1