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());
}
}
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;
}
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");
}
}
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;
}
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));
}
Aggregations