Search in sources :

Example 31 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-plugins by JetBrains.

the class GrStepDefinitionCreator method createStepDefinition.

@Override
public boolean createStepDefinition(@NotNull GherkinStep step, @NotNull final PsiFile file) {
    if (!(file instanceof GroovyFile))
        return false;
    final Project project = file.getProject();
    final VirtualFile vFile = ObjectUtils.assertNotNull(file.getVirtualFile());
    final OpenFileDescriptor descriptor = new OpenFileDescriptor(project, vFile);
    FileEditorManager.getInstance(project).getAllEditors(vFile);
    FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
    final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    if (editor != null) {
        final TemplateManager templateManager = TemplateManager.getInstance(file.getProject());
        final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
        final Template template = templateManager.getActiveTemplate(editor);
        if (templateState != null && template != null) {
            templateState.gotoEnd();
        }
    }
    // snippet text
    final GrMethodCall element = buildStepDefinitionByStep(step);
    GrMethodCall methodCall = (GrMethodCall) ((GroovyFile) file).addStatementBefore(element, null);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(methodCall);
    methodCall = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(methodCall);
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    if (ApplicationManager.getApplication().isUnitTestMode())
        return true;
    final TemplateBuilderImpl builder = (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(methodCall);
    // regexp str
    GrLiteral pattern = GrCucumberUtil.getStepDefinitionPattern(methodCall);
    assert pattern != null;
    String patternText = pattern.getText();
    builder.replaceElement(pattern, new TextRange(1, patternText.length() - 1), patternText.substring(1, patternText.length() - 1));
    // block vars
    GrClosableBlock closure = methodCall.getClosureArguments()[0];
    final GrParameter[] blockVars = closure.getAllParameters();
    for (GrParameter var : blockVars) {
        PsiElement identifier = var.getNameIdentifierGroovy();
        builder.replaceElement(identifier, identifier.getText());
    }
    TemplateManager manager = TemplateManager.getInstance(project);
    final Editor editorToRunTemplate;
    if (editor == null) {
        editorToRunTemplate = IntentionUtils.positionCursor(project, file, methodCall);
    } else {
        editorToRunTemplate = editor;
    }
    Template template = builder.buildTemplate();
    TextRange range = methodCall.getTextRange();
    editorToRunTemplate.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    editorToRunTemplate.getCaretModel().moveToOffset(range.getStartOffset());
    manager.startTemplate(editorToRunTemplate, template, new TemplateEditingAdapter() {

        @Override
        public void templateFinished(Template template, boolean brokenOff) {
            if (brokenOff)
                return;
            ApplicationManager.getApplication().runWriteAction(() -> {
                PsiDocumentManager.getInstance(project).commitDocument(editorToRunTemplate.getDocument());
                final int offset = editorToRunTemplate.getCaretModel().getOffset();
                GrMethodCall methodCall1 = PsiTreeUtil.findElementOfClassAtOffset(file, offset - 1, GrMethodCall.class, false);
                if (methodCall1 != null) {
                    GrClosableBlock[] closures = methodCall1.getClosureArguments();
                    if (closures.length == 1) {
                        GrClosableBlock closure1 = closures[0];
                        selectBody(closure1, editor);
                    }
                }
            });
        }
    });
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) TextRange(com.intellij.openapi.util.TextRange) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) Project(com.intellij.openapi.project.Project) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement)

Example 32 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GroovyAnnotator method visitCatchClause.

@Override
public void visitCatchClause(@NotNull GrCatchClause clause) {
    final GrParameter parameter = clause.getParameter();
    if (parameter == null)
        return;
    final GrTypeElement typeElement = parameter.getTypeElementGroovy();
    if (typeElement != null) {
        final PsiType type = typeElement.getType();
        //don't highlight unresolved types
        if (type instanceof PsiClassType && ((PsiClassType) type).resolve() == null)
            return;
        final PsiClassType throwable = TypesUtil.createType(CommonClassNames.JAVA_LANG_THROWABLE, clause);
        if (!throwable.isAssignableFrom(type)) {
            myHolder.createErrorAnnotation(typeElement, GroovyBundle.message("catch.statement.parameter.type.should.be.a.subclass.of.throwable"));
        }
    }
}
Also used : GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 33 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GroovyAnnotator method checkOptionalParametersInAbstractMethod.

private static void checkOptionalParametersInAbstractMethod(AnnotationHolder holder, GrMethod method) {
    if (!method.hasModifierProperty(PsiModifier.ABSTRACT))
        return;
    if (!(method.getContainingClass() instanceof GrInterfaceDefinition))
        return;
    for (GrParameter parameter : method.getParameters()) {
        GrExpression initializerGroovy = parameter.getInitializerGroovy();
        if (initializerGroovy != null) {
            PsiElement assignOperator = parameter.getNameIdentifierGroovy();
            TextRange textRange = new TextRange(assignOperator.getTextRange().getEndOffset(), initializerGroovy.getTextRange().getEndOffset());
            holder.createErrorAnnotation(textRange, GroovyBundle.message("default.initializers.are.not.allowed.in.abstract.method"));
        }
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 34 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GrChangeVariableType method doFix.

@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
    final PsiElement element = descriptor.getPsiElement();
    final PsiElement parent = element.getParent();
    try {
        final PsiType type = JavaPsiFacade.getElementFactory(project).createTypeFromText(myType, element);
        if (parent instanceof GrVariable) {
            ((GrVariable) parent).setType(type);
        } else if (element instanceof GrReferenceExpression && parent instanceof GrAssignmentExpression && ((GrAssignmentExpression) parent).getLValue() == element) {
            final PsiElement resolved = ((GrReferenceExpression) element).resolve();
            if (resolved instanceof GrVariable && !(resolved instanceof GrParameter)) {
                ((GrVariable) resolved).setType(type);
            }
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 35 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GroovyConstructorUsagesSearcher method processImplicitConstructorCall.

private static void processImplicitConstructorCall(@NotNull final PsiMember usage, final Processor<PsiReference> processor, final PsiMethod constructor) {
    if (constructor instanceof GrMethod) {
        GrParameter[] grParameters = (GrParameter[]) constructor.getParameterList().getParameters();
        if (grParameters.length > 0 && !grParameters[0].isOptional())
            return;
    } else if (constructor.getParameterList().getParameters().length > 0)
        return;
    PsiManager manager = constructor.getManager();
    if (manager.areElementsEquivalent(usage, constructor) || manager.areElementsEquivalent(constructor.getContainingClass(), usage.getContainingClass()))
        return;
    processor.process(new LightMemberReference(manager, usage, PsiSubstitutor.EMPTY) {

        @Override
        public PsiElement getElement() {
            return usage;
        }

        @Override
        public TextRange getRangeInElement() {
            if (usage instanceof PsiClass) {
                PsiIdentifier identifier = ((PsiClass) usage).getNameIdentifier();
                if (identifier != null)
                    return TextRange.from(identifier.getStartOffsetInParent(), identifier.getTextLength());
            } else if (usage instanceof PsiMethod) {
                PsiIdentifier identifier = ((PsiMethod) usage).getNameIdentifier();
                if (identifier != null)
                    return TextRange.from(identifier.getStartOffsetInParent(), identifier.getTextLength());
            }
            return super.getRangeInElement();
        }
    });
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) LightMemberReference(com.intellij.psi.impl.light.LightMemberReference) TextRange(com.intellij.openapi.util.TextRange) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)99 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)22 NotNull (org.jetbrains.annotations.NotNull)20 PsiElement (com.intellij.psi.PsiElement)19 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)19 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)16 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 ArrayList (java.util.ArrayList)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 TextRange (com.intellij.openapi.util.TextRange)9 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 Project (com.intellij.openapi.project.Project)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)6 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)6