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;
}
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;
}
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();
}
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;
}
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;
}
Aggregations