Search in sources :

Example 1 with PackageEntryTable

use of com.intellij.psi.codeStyle.PackageEntryTable 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 2 with PackageEntryTable

use of com.intellij.psi.codeStyle.PackageEntryTable 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 3 with PackageEntryTable

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

the class CodeStyleImportsPanelBase method getCopyWithoutEmptyPackages.

@NotNull
private static PackageEntryTable getCopyWithoutEmptyPackages(PackageEntryTable table) {
    try {
        PackageEntryTable copy = (PackageEntryTable) table.clone();
        copy.removeEmptyPackages();
        return copy;
    } catch (CloneNotSupportedException ignored) {
        throw new IllegalStateException("Clone should be supported");
    }
}
Also used : PackageEntryTable(com.intellij.psi.codeStyle.PackageEntryTable) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PackageEntryTable

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

the class CodeStyleImportsPanelBase method isModifiedLayoutSettings.

public boolean isModifiedLayoutSettings(ImportsLayoutSettings settings) {
    boolean isModified = isModified(myImportLayoutPanel.getCbLayoutStaticImportsSeparately(), settings.isLayoutStaticImportsSeparately());
    isModified |= isModified(myCbUseFQClassNames, settings.isUseFqClassNames());
    isModified |= isModified(myCbUseSingleClassImports, settings.isUseSingleClassImports());
    isModified |= isModified(myCbInsertInnerClassImports, settings.isInsertInnerClassImports());
    isModified |= isModified(myClassCountField, settings.getClassCountToUseImportOnDemand());
    isModified |= isModified(myNamesCountField, settings.getNamesCountToUseImportOnDemand());
    PackageEntryTable list = myImportLayoutPanel.getImportLayoutList();
    isModified |= isModified(getCopyWithoutEmptyPackages(list), settings.getImportLayoutTable());
    isModified |= isModified(getCopyWithoutEmptyPackages(myPackageList), settings.getPackagesToUseImportOnDemand());
    return isModified;
}
Also used : PackageEntryTable(com.intellij.psi.codeStyle.PackageEntryTable)

Example 5 with PackageEntryTable

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

the class CodeStyleImportsPanelBase method applyLayoutSettings.

public void applyLayoutSettings(ImportsLayoutSettings settings) {
    stopTableEditing();
    settings.setLayoutStaticImportsSeparately(myImportLayoutPanel.areStaticImportsEnabled());
    settings.setUseFqClassNames(myCbUseFQClassNames.isSelected());
    settings.setUseSingleClassImports(myCbUseSingleClassImports.isSelected());
    settings.setInsertInnerClassImports(myCbInsertInnerClassImports.isSelected());
    try {
        int value = Integer.parseInt(myClassCountField.getText());
        settings.setClassCountToUseImportOnDemand(value);
    } catch (NumberFormatException e) {
    //just a bad number
    }
    try {
        int value = Integer.parseInt(myNamesCountField.getText());
        settings.setNamesCountToUseImportOnDemand(value);
    } catch (NumberFormatException e) {
    //just a bad number
    }
    PackageEntryTable list = myImportLayoutPanel.getImportLayoutList();
    settings.getImportLayoutTable().copyFrom(getCopyWithoutEmptyPackages(list));
    settings.getPackagesToUseImportOnDemand().copyFrom(getCopyWithoutEmptyPackages(myPackageList));
}
Also used : PackageEntryTable(com.intellij.psi.codeStyle.PackageEntryTable)

Aggregations

PackageEntryTable (com.intellij.psi.codeStyle.PackageEntryTable)9 PackageEntry (com.intellij.psi.codeStyle.PackageEntry)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 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 AndroidXmlPredefinedCodeStyle (org.jetbrains.android.formatter.AndroidXmlPredefinedCodeStyle)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1