Search in sources :

Example 46 with GrImportStatement

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);
    });
}
Also used : CodeInsightWorkspaceSettings(com.intellij.codeInsight.CodeInsightWorkspaceSettings) DaemonListeners(com.intellij.codeInsight.daemon.impl.DaemonListeners) VirtualFile(com.intellij.openapi.vfs.VirtualFile) HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) Document(com.intellij.openapi.editor.Document) DaemonCodeAnalyzer(com.intellij.codeInsight.daemon.DaemonCodeAnalyzer) DaemonCodeAnalyzerEx(com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx) DocumentUtil(com.intellij.util.DocumentUtil) Comparing(com.intellij.openapi.util.Comparing) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Logger(com.intellij.openapi.diagnostic.Logger) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) GroovyImportOptimizer(org.jetbrains.plugins.groovy.editor.GroovyImportOptimizer) UndoManager(com.intellij.openapi.command.undo.UndoManager) Attachment(com.intellij.openapi.diagnostic.Attachment) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) DaemonCodeAnalyzerImpl(com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl) NotNull(org.jetbrains.annotations.NotNull) TransactionGuard(com.intellij.openapi.application.TransactionGuard) TextRange(com.intellij.openapi.util.TextRange) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)

Example 47 with GrImportStatement

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());
    }
}
Also used : GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) PsiElement(com.intellij.psi.PsiElement) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)

Example 48 with GrImportStatement

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

Example 49 with GrImportStatement

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;
}
Also used : GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)49 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)15 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)11 NotNull (org.jetbrains.annotations.NotNull)9 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)9 PsiElement (com.intellij.psi.PsiElement)7 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)6 TextRange (com.intellij.openapi.util.TextRange)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 UsageInfo (com.intellij.usageView.UsageInfo)4 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)4 GrTopStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)4 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)4 ASTNode (com.intellij.lang.ASTNode)3 Document (com.intellij.openapi.editor.Document)3 PsiFile (com.intellij.psi.PsiFile)3 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)3 DaemonCodeAnalyzerImpl (com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)2