use of com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment in project idea-php-typo3-plugin by cedricziel.
the class CreateInjectorQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemDescriptor) {
PhpDocComment docCommentElement = (PhpDocComment) element.getElement().getParent();
PsiElement owner = docCommentElement.getOwner();
if (!(owner instanceof Field)) {
return;
}
Field field = (Field) owner;
final String fieldName = field.getName();
final PhpClass containingClass = PsiTreeUtil.getParentOfType(element.getElement(), PhpClass.class);
if (containingClass == null) {
return;
}
PhpType type = field.getType();
if (type == PhpType.SCALAR) {
// Scalar type - can't inject those, heh.
return;
}
final String methodName = "inject" + StringUtils.capitalize(fieldName);
Method injectorFunction = PhpPsiElementFactory.createMethod(project, "public function " + methodName + " (" + type + " $" + fieldName + ") {" + " $this->" + fieldName + " = $" + fieldName + ";" + "}");
final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, element.getContainingFile().getVirtualFile()), true);
if (editor == null) {
return;
}
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
final int insertPos = CodeUtil.getMethodInsertPosition(containingClass, methodName);
if (insertPos == -1) {
return;
}
new WriteCommandAction(project) {
@Override
protected void run(@NotNull Result result) throws Throwable {
StringBuffer textBuf = new StringBuffer();
textBuf.append("\n");
textBuf.append(injectorFunction.getText());
editor.getDocument().insertString(insertPos, textBuf);
final int endPos = insertPos + textBuf.length();
CodeStyleManager.getInstance(project).reformatText(containingClass.getContainingFile(), insertPos, endPos);
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
Method insertedMethod = containingClass.findMethodByName(methodName);
if (insertedMethod != null) {
editor.getCaretModel().moveToOffset(insertedMethod.getTextRange().getStartOffset());
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
element.getElement().delete();
}
}
@Override
public String getGroupID() {
return "Create Injection Method";
}
}.execute();
}
Aggregations