Search in sources :

Example 26 with GrMethodCall

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall in project android by JetBrains.

the class AndroidDslContributor method getContributingMethod.

@Nullable
private static PsiMethod getContributingMethod(PsiElement place, PsiClass contributorClass, String methodName) {
    GrMethodCall call = PsiTreeUtil.getParentOfType(place, GrMethodCall.class);
    if (call == null) {
        return null;
    }
    GrArgumentList args = call.getArgumentList();
    int argsCount = GradleResolverUtil.getGrMethodArumentsCount(args);
    PsiMethod[] methodsByName = findMethodByName(contributorClass, methodName);
    // first check to see if we can narrow down by # of arguments
    for (PsiMethod method : methodsByName) {
        if (method.getParameterList().getParametersCount() == argsCount) {
            return method;
        }
    }
    // if we couldn't narrow down by # of arguments, just use the first one
    return methodsByName.length > 0 ? methodsByName[0] : null;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) Nullable(org.jetbrains.annotations.Nullable)

Example 27 with GrMethodCall

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall in project android by JetBrains.

the class AndroidDslContributor method getParentContributor.

/**
   * Returns the contributor of the enclosing block.
   * This is performed by first obtaining the closeable block that contains this element, and figuring out the method whose
   * closure argument is the closeable block. We do this instead of directly looking for a parent element of type method call
   * since this scheme allows us to handle both the following two cases:
   *   sourceSets {
   *      ^main {}
   *      ^debug.setRoot()
   *   }
   * In the above example, parent(parent('main')) == parent('debug') == 'sourceSets'.
   */
@Nullable
private static PsiElement getParentContributor(PsiElement place) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    GrClosableBlock closeableBlock = PsiTreeUtil.getParentOfType(place, GrClosableBlock.class);
    if (closeableBlock == null || !(closeableBlock.getParent() instanceof GrMethodCall)) {
        return null;
    }
    PsiElement parentContributor = closeableBlock.getParent().getUserData(CONTRIBUTOR_KEY);
    if (parentContributor == null) {
        return null;
    }
    return parentContributor;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) Nullable(org.jetbrains.annotations.Nullable)

Example 28 with GrMethodCall

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

the class GrStepDefinition method getVariableNames.

@Override
public List<String> getVariableNames() {
    PsiElement element = getElement();
    if (element instanceof GrMethodCall) {
        GrClosableBlock[] closures = ((GrMethodCall) element).getClosureArguments();
        assert closures.length == 1;
        GrParameter[] parameters = closures[0].getParameterList().getParameters();
        ArrayList<String> result = new ArrayList<>();
        for (GrParameter parameter : parameters) {
            result.add(parameter.getName());
        }
        return result;
    }
    return Collections.emptyList();
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) ArrayList(java.util.ArrayList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement)

Example 29 with GrMethodCall

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall 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 30 with GrMethodCall

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

the class GrCucumberExtension method getGlue.

@Nullable
private String getGlue(PsiElement stepDefinition) {
    if (stepDefinition != null && stepDefinition instanceof GrMethodCall) {
        GroovyFile groovyFile = (GroovyFile) stepDefinition.getContainingFile();
        VirtualFile vfile = groovyFile.getVirtualFile();
        if (vfile != null) {
            VirtualFile parentDir = vfile.getParent();
            return PathUtil.getLocalPath(parentDir);
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)51 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)23 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)22 Nullable (org.jetbrains.annotations.Nullable)18 PsiElement (com.intellij.psi.PsiElement)17 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)9 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 NotNull (org.jetbrains.annotations.NotNull)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiFile (com.intellij.psi.PsiFile)3 PsiMethod (com.intellij.psi.PsiMethod)3 PsiType (com.intellij.psi.PsiType)3 ArrayList (java.util.ArrayList)3 GroovyMapContentProvider (org.jetbrains.plugins.groovy.extensions.GroovyMapContentProvider)3 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)3 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)3