Search in sources :

Example 6 with GrDocComment

use of org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment in project intellij-community by JetBrains.

the class GroovyGenerationInfo method adjustDocCommentIfExists.

private static void adjustDocCommentIfExists(PsiMember member) {
    final PsiElement child = member.getFirstChild();
    if (child instanceof PsiDocComment) {
        final Project project = member.getProject();
        final GrDocComment groovyDoc = GroovyPsiElementFactory.getInstance(project).createDocCommentFromText(child.getText());
        child.delete();
        CodeStyleManager.getInstance(project).reformat(member);
        member.getParent().addBefore(groovyDoc, member);
    }
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) Project(com.intellij.openapi.project.Project) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)

Example 7 with GrDocComment

use of org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment in project intellij-community by JetBrains.

the class MoveGroovyClassHandler method doMoveClass.

@Override
public PsiClass doMoveClass(@NotNull PsiClass aClass, @NotNull PsiDirectory moveDestination) throws IncorrectOperationException {
    if (!aClass.getLanguage().equals(GroovyLanguage.INSTANCE))
        return null;
    PsiFile file = aClass.getContainingFile();
    if (!(file instanceof GroovyFile))
        return null;
    final PsiPackage newPackage = JavaDirectoryService.getInstance().getPackage(moveDestination);
    LOG.assertTrue(newPackage != null);
    PsiClass newClass = null;
    final String newPackageName = newPackage.getQualifiedName();
    if (aClass instanceof GroovyScriptClass) {
        final PsiClass[] classes = ((GroovyFile) file).getClasses();
        if (classes.length == 1) {
            if (!moveDestination.equals(file.getContainingDirectory())) {
                Project project = file.getProject();
                MoveFilesOrDirectoriesUtil.doMoveFile(file, moveDestination);
                DumbService.getInstance(project).completeJustSubmittedTasks();
                file = moveDestination.findFile(file.getName());
                assert file != null;
                ((PsiClassOwner) file).setPackageName(newPackageName);
            }
            return ((GroovyFile) file).getScriptClass();
        }
        //script class is moved the first from the file due to MoveClassOrPackageProcessor:88 (element sort)
        correctSelfReferences(aClass, newPackage);
        final GroovyFile newFile = generateNewScript((GroovyFile) file, newPackage);
        for (PsiElement child : file.getChildren()) {
            if (!(child instanceof GrTopStatement || child instanceof PsiComment))
                continue;
            if (child instanceof PsiClass || child instanceof GrImportStatement || child instanceof GrPackageDefinition)
                continue;
            if (child instanceof GrDocComment) {
                final GrDocCommentOwner owner = GrDocCommentUtil.findDocOwner((GrDocComment) child);
                if (owner instanceof PsiClass)
                    continue;
            }
            child.delete();
        }
        if (!moveDestination.equals(file.getContainingDirectory())) {
            moveDestination.add(newFile);
        //aClass.getManager().moveFile(newFile, moveDestination);
        }
        newClass = newFile.getClasses()[0];
        correctOldClassReferences(newClass, aClass);
    } else {
        if (!moveDestination.equals(file.getContainingDirectory()) && moveDestination.findFile(file.getName()) != null) {
            // moving second of two classes which were in the same file to a different directory (IDEADEV-3089)
            correctSelfReferences(aClass, newPackage);
            PsiFile newFile = moveDestination.findFile(file.getName());
            final FileASTNode fileNode = newFile.getNode();
            fileNode.addChild(Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n\n", 0, 2, null, aClass.getManager()));
            final PsiDocComment docComment = aClass.getDocComment();
            if (docComment != null) {
                newFile.add(docComment);
                fileNode.addChild(Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n", 0, 1, null, aClass.getManager()));
            }
            newClass = (GrTypeDefinition) newFile.add(aClass);
            correctOldClassReferences(newClass, aClass);
            aClass.delete();
        } else if (((GroovyFile) file).getClasses().length > 1) {
            correctSelfReferences(aClass, newPackage);
            Project project = aClass.getProject();
            PsiFileFactory fileFactory = PsiFileFactory.getInstance(project);
            GroovyFile newFile = (GroovyFile) moveDestination.add(fileFactory.createFileFromText(aClass.getName() + "." + GroovyFileType.DEFAULT_EXTENSION, GroovyLanguage.INSTANCE, "class XXX {}"));
            final PsiClass created = newFile.getClasses()[0];
            PsiDocComment docComment = aClass.getDocComment();
            if (docComment != null) {
                newFile.addBefore(docComment, created);
                docComment.delete();
            }
            newClass = (PsiClass) created.replace(aClass);
            setPackageDefinition((GroovyFile) file, newFile, newPackageName);
            correctOldClassReferences(newClass, aClass);
            aClass.delete();
        }
    }
    return newClass;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) Project(com.intellij.openapi.project.Project) FileASTNode(com.intellij.lang.FileASTNode) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)

Example 8 with GrDocComment

use of org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment in project intellij-community by JetBrains.

the class GrVariableInplaceRenameHandler method isAvailable.

@Override
protected boolean isAvailable(PsiElement element, Editor editor, PsiFile file) {
    if (!editor.getSettings().isVariableInplaceRenameEnabled())
        return false;
    if (!(element instanceof GrVariable))
        return false;
    if (element instanceof GrField)
        return false;
    final SearchScope scope = element.getUseScope();
    if (!(scope instanceof LocalSearchScope))
        return false;
    final PsiElement[] scopeElements = ((LocalSearchScope) scope).getScope();
    return scopeElements.length == 1 || scopeElements.length == 2 && (scopeElements[0] instanceof GrDocComment ^ scopeElements[1] instanceof GrDocComment);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiElement(com.intellij.psi.PsiElement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)

Example 9 with GrDocComment

use of org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment in project intellij-community by JetBrains.

the class MoveGroovyClassHandler method generateNewScript.

private static GroovyFile generateNewScript(GroovyFile file, PsiPackage newPackage) {
    for (GrImportStatement importStatement : file.getImportStatements()) {
        importStatement.delete();
    }
    final GroovyFile newFile = GroovyPsiElementFactory.getInstance(file.getProject()).createGroovyFile("", true, null);
    newFile.addRange(file.getFirstChild(), file.getLastChild());
    final PsiClass[] newFileClasses = newFile.getClasses();
    for (PsiClass psiClass : newFileClasses) {
        if (psiClass instanceof GroovyScriptClass)
            continue;
        final GrDocComment docComment = GrDocCommentUtil.findDocComment((GrDocCommentOwner) psiClass);
        if (docComment != null)
            docComment.delete();
        psiClass.delete();
    }
    final GrPackageDefinition packageDefinition = newFile.getPackageDefinition();
    if (packageDefinition != null)
        packageDefinition.delete();
    PsiElement cur = newFile.getFirstChild();
    while (cur != null && PsiImplUtil.isWhiteSpaceOrNls(cur)) {
        cur = cur.getNextSibling();
    }
    if (cur != null && cur != newFile.getFirstChild()) {
        cur = cur.getPrevSibling();
        newFile.deleteChildRange(newFile.getFirstChild(), cur);
    }
    cur = newFile.getLastChild();
    while (cur != null && PsiImplUtil.isWhiteSpaceOrNls(cur)) {
        cur = cur.getPrevSibling();
    }
    if (cur != null && cur != newFile.getLastChild()) {
        cur = cur.getNextSibling();
        newFile.deleteChildRange(cur, newFile.getLastChild());
    }
    newFile.setName(file.getName());
    setPackageDefinition(file, newFile, newPackage.getQualifiedName());
    GroovyChangeContextUtil.decodeContextInfo(newFile, null, null);
    return newFile;
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)

Example 10 with GrDocComment

use of org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment in project intellij-community by JetBrains.

the class GroovySuppressableInspectionTool method getElementToolSuppressedIn.

@Nullable
public static PsiElement getElementToolSuppressedIn(final PsiElement place, @NotNull String toolId) {
    if (place == null)
        return null;
    AccessToken accessToken = ApplicationManager.getApplication().acquireReadActionLock();
    try {
        final PsiElement statement = PsiUtil.findEnclosingStatement(place);
        if (statement != null) {
            PsiElement prev = statement.getPrevSibling();
            while (prev != null && StringUtil.isEmpty(prev.getText().trim())) {
                prev = prev.getPrevSibling();
            }
            if (prev instanceof PsiComment) {
                String text = prev.getText();
                Matcher matcher = SuppressionUtil.SUPPRESS_IN_LINE_COMMENT_PATTERN.matcher(text);
                if (matcher.matches() && SuppressionUtil.isInspectionToolIdMentioned(matcher.group(1), toolId)) {
                    return prev;
                }
            }
        }
        GrMember member = null;
        GrDocComment docComment = PsiTreeUtil.getParentOfType(place, GrDocComment.class);
        if (docComment != null) {
            GrDocCommentOwner owner = docComment.getOwner();
            if (owner instanceof GrMember) {
                member = (GrMember) owner;
            }
        }
        if (member == null) {
            member = PsiTreeUtil.getNonStrictParentOfType(place, GrMember.class);
        }
        while (member != null) {
            GrModifierList modifierList = member.getModifierList();
            for (String ids : getInspectionIdsSuppressedInAnnotation(modifierList)) {
                if (SuppressionUtil.isInspectionToolIdMentioned(ids, toolId)) {
                    return modifierList;
                }
            }
            member = PsiTreeUtil.getParentOfType(member, GrMember.class);
        }
        return null;
    } finally {
        accessToken.finish();
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) PsiComment(com.intellij.psi.PsiComment) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) Matcher(java.util.regex.Matcher) AccessToken(com.intellij.openapi.application.AccessToken) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) PsiElement(com.intellij.psi.PsiElement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)22 PsiElement (com.intellij.psi.PsiElement)9 GrDocCommentOwner (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner)8 NotNull (org.jetbrains.annotations.NotNull)5 Project (com.intellij.openapi.project.Project)4 Nullable (org.jetbrains.annotations.Nullable)4 GrDocTag (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag)4 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)4 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)3 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)3 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)3 ASTNode (com.intellij.lang.ASTNode)2 Document (com.intellij.openapi.editor.Document)2 PsiComment (com.intellij.psi.PsiComment)2 PsiFile (com.intellij.psi.PsiFile)2 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)2 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)2