Search in sources :

Example 16 with PhpDocComment

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();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) Method(com.jetbrains.php.lang.psi.elements.Method) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) Result(com.intellij.openapi.application.Result) Field(com.jetbrains.php.lang.psi.elements.Field) PhpDocComment(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PhpDocComment (com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment)16 PsiElement (com.intellij.psi.PsiElement)14 NotNull (org.jetbrains.annotations.NotNull)11 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)9 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)5 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)5 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)4 IElementType (com.intellij.psi.tree.IElementType)4 PhpDocTag (com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag)4 com.jetbrains.php.lang.psi.elements (com.jetbrains.php.lang.psi.elements)4 com.kalessil.phpStorm.phpInspectionsEA.utils (com.kalessil.phpStorm.phpInspectionsEA.utils)4 Project (com.intellij.openapi.project.Project)3 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)3 PhpDocType (com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocType)3 PhpTokenTypes (com.jetbrains.php.lang.lexer.PhpTokenTypes)3 BasePhpInspection (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection)3 OptionsComponent (com.kalessil.phpStorm.phpInspectionsEA.options.OptionsComponent)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 javax.swing (javax.swing)3