use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyOptimizeImportsFix method timeToOptimizeImports.
private boolean timeToOptimizeImports(GroovyFile myFile, Editor editor) {
if (!CodeInsightWorkspaceSettings.getInstance(myFile.getProject()).optimizeImportsOnTheFly)
return false;
if (onTheFly && editor != null) {
// if we stand inside import statements, do not optimize
final VirtualFile vfile = myFile.getVirtualFile();
if (vfile != null && ProjectRootManager.getInstance(myFile.getProject()).getFileIndex().isInSource(vfile)) {
final GrImportStatement[] imports = myFile.getImportStatements();
if (imports.length > 0) {
final int offset = editor.getCaretModel().getOffset();
if (imports[0].getTextRange().getStartOffset() <= offset && offset <= imports[imports.length - 1].getTextRange().getEndOffset()) {
return false;
}
}
}
}
DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(myFile.getProject());
if (!codeAnalyzer.isHighlightingAvailable(myFile))
return false;
if (!codeAnalyzer.isErrorAnalyzingFinished(myFile))
return false;
Document myDocument = PsiDocumentManager.getInstance(myFile.getProject()).getDocument(myFile);
boolean errors = containsErrorsPreventingOptimize(myFile, myDocument);
return !errors && DaemonListeners.canChangeFileSilently(myFile);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyOptimizeImportsFix method containsErrorsPreventingOptimize.
private static boolean containsErrorsPreventingOptimize(GroovyFile myFile, Document myDocument) {
// ignore unresolved imports errors
final TextRange ignoreRange;
final GrImportStatement[] imports = myFile.getImportStatements();
if (imports.length != 0) {
final int start = imports[0].getTextRange().getStartOffset();
final int end = imports[imports.length - 1].getTextRange().getEndOffset();
ignoreRange = new TextRange(start, end);
} else {
ignoreRange = TextRange.EMPTY_RANGE;
}
return !DaemonCodeAnalyzerEx.processHighlights(myDocument, myFile.getProject(), HighlightSeverity.ERROR, 0, myDocument.getTextLength(), error -> {
int infoStart = error.getActualStartOffset();
int infoEnd = error.getActualEndOffset();
return ignoreRange.containsRange(infoStart, infoEnd) && error.type.equals(HighlightInfoType.WRONG_REF);
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyCodeStyleManagerImpl method removeImport.
@Override
public void removeImport(@NotNull GroovyFileBase psiFile, @NotNull GrImportStatement importStatement) throws IncorrectOperationException {
PsiElement psiElement = psiFile;
PsiElement before = importStatement;
while (PsiImplUtil.isWhiteSpaceOrNls(before.getPrevSibling())) {
before = before.getPrevSibling();
}
if (PsiImplUtil.hasElementType(before.getPrevSibling(), GroovyTokenTypes.mSEMI))
before = before.getPrevSibling();
if (PsiImplUtil.isWhiteSpaceOrNls(before.getPrevSibling()))
before = before.getPrevSibling();
PsiElement after = importStatement;
if (PsiImplUtil.isWhiteSpaceOrNls(after.getNextSibling()))
after = after.getNextSibling();
if (PsiImplUtil.hasElementType(after.getNextSibling(), GroovyTokenTypes.mSEMI))
after = after.getNextSibling();
while (PsiImplUtil.isWhiteSpaceOrNls(after.getNextSibling())) after = after.getNextSibling();
if (before == null)
before = importStatement;
PsiElement anchor_before = before.getPrevSibling();
PsiElement anchor_after = after.getNextSibling();
if (before == after) {
importStatement.delete();
} else {
psiFile.deleteChildRange(before, after);
}
if (anchor_before instanceof GrImportStatement && anchor_after instanceof GrImportStatement) {
addLineFeedAfter(psiFile, (GrImportStatement) anchor_before);
} else if (anchor_before != null && anchor_after != null) {
String text = anchor_after instanceof GrTopStatement && anchor_before instanceof GrTopStatement ? "\n\n" : "\n";
psiElement.getNode().addLeaf(GroovyTokenTypes.mNLS, text, anchor_after.getNode());
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyCodeStyleManagerImpl method addLineFeedAfter.
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());
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement in project intellij-community by JetBrains.
the class GroovyCodeStyleManagerImpl method addImport.
@NotNull
@Override
public GrImportStatement addImport(@NotNull GroovyFile psiFile, @NotNull GrImportStatement statement) throws IncorrectOperationException {
PsiElement anchor = getAnchorToInsertImportAfter(psiFile, statement);
final PsiElement result = psiFile.addAfter(statement, anchor);
final GrImportStatement gImport = (GrImportStatement) result;
addLineFeedBefore(psiFile, gImport);
addLineFeedAfter(psiFile, gImport);
return gImport;
}
Aggregations