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