Search in sources :

Example 1 with TemplateBuilder

use of com.intellij.codeInsight.template.TemplateBuilder in project intellij-community by JetBrains.

the class StatementEffectIntroduceVariableQuickFix method applyFix.

public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    PsiElement expression = descriptor.getPsiElement();
    PyPsiUtils.assertValid(expression);
    if (expression != null && expression.isValid()) {
        final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        final PyAssignmentStatement assignment = elementGenerator.createFromText(LanguageLevel.forElement(expression), PyAssignmentStatement.class, "var = " + expression.getText());
        expression = expression.replace(assignment);
        if (expression == null)
            return;
        expression = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(expression);
        if (expression == null)
            return;
        final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(expression);
        final PyExpression leftHandSideExpression = ((PyAssignmentStatement) expression).getLeftHandSideExpression();
        assert leftHandSideExpression != null;
        builder.replaceElement(leftHandSideExpression, "var");
        builder.run();
    }
}
Also used : TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) PsiElement(com.intellij.psi.PsiElement)

Example 2 with TemplateBuilder

use of com.intellij.codeInsight.template.TemplateBuilder in project intellij-community by JetBrains.

the class CreateClassQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement anchor = myAnchor.getElement();
    if (anchor == null || !anchor.isValid()) {
        return;
    }
    if (!(anchor instanceof PyFile)) {
        anchor = PyPsiUtils.getParentRightBefore(anchor, anchor.getContainingFile());
        assert anchor != null;
    }
    PyClass pyClass = PyElementGenerator.getInstance(project).createFromText(LanguageLevel.getDefault(), PyClass.class, "class " + myClassName + "(object):\n    pass");
    if (anchor instanceof PyFile) {
        pyClass = (PyClass) anchor.add(pyClass);
    } else {
        pyClass = (PyClass) anchor.getParent().addBefore(pyClass, anchor);
    }
    pyClass = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(pyClass);
    final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(pyClass);
    builder.replaceElement(pyClass.getSuperClassExpressions()[0], "object");
    builder.replaceElement(pyClass.getStatementList(), PyNames.PASS);
    final FileEditor editor = FileEditorManager.getInstance(project).getSelectedEditor(anchor.getContainingFile().getVirtualFile());
    if (!(editor instanceof TextEditor)) {
        return;
    }
    builder.run(((TextEditor) editor).getEditor(), false);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) PyFile(com.jetbrains.python.psi.PyFile) PsiElement(com.intellij.psi.PsiElement)

Example 3 with TemplateBuilder

use of com.intellij.codeInsight.template.TemplateBuilder in project intellij-community by JetBrains.

the class AddFieldQuickFix method showTemplateBuilder.

private void showTemplateBuilder(PsiElement initStatement, @NotNull final PsiFile file) {
    initStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(initStatement);
    if (initStatement instanceof PyAssignmentStatement) {
        final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(initStatement);
        final PyExpression assignedValue = ((PyAssignmentStatement) initStatement).getAssignedValue();
        final PyExpression leftExpression = ((PyAssignmentStatement) initStatement).getLeftHandSideExpression();
        if (assignedValue != null && leftExpression != null) {
            if (replaceInitializer)
                builder.replaceElement(assignedValue, myInitializer);
            else
                builder.replaceElement(leftExpression.getLastChild(), myIdentifier);
            final VirtualFile virtualFile = file.getVirtualFile();
            if (virtualFile == null)
                return;
            final Editor editor = FileEditorManager.getInstance(file.getProject()).openTextEditor(new OpenFileDescriptor(file.getProject(), virtualFile), true);
            if (editor == null)
                return;
            builder.run(editor, false);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor)

Example 4 with TemplateBuilder

use of com.intellij.codeInsight.template.TemplateBuilder in project intellij-community by JetBrains.

the class DataPointHolderConversionIntention method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException {
    final PsiElement holder = element.getParent();
    PsiModifierListOwner createdElement = holder instanceof PsiField ? convertToMethod((PsiField) holder) : convertToField((PsiMethod) holder);
    final PsiModifierListOwner oldElement = (PsiModifierListOwner) holder;
    final PsiAnnotation psiAnnotation = AnnotationUtil.findAnnotation(oldElement, DATA_POINT_FQN, DATA_POINTS_FQN);
    assert psiAnnotation != null;
    final String annotation = psiAnnotation.getQualifiedName();
    assert annotation != null;
    final PsiModifierList modifierList = createdElement.getModifierList();
    assert modifierList != null;
    modifierList.addAnnotation(annotation);
    modifierList.setModifierProperty(PsiModifier.STATIC, true);
    modifierList.setModifierProperty(PsiModifier.PUBLIC, true);
    createdElement = (PsiModifierListOwner) oldElement.replace(createdElement);
    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
    final TemplateBuilder templateBuilder = TemplateBuilderFactory.getInstance().createTemplateBuilder(createdElement);
    final PsiNameIdentifierOwner asNameIdOwner = (PsiNameIdentifierOwner) createdElement;
    templateBuilder.replaceElement(asNameIdOwner.getNameIdentifier(), asNameIdOwner.getName());
    templateBuilder.run(editor, false);
}
Also used : TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder)

Example 5 with TemplateBuilder

use of com.intellij.codeInsight.template.TemplateBuilder in project Intellij-Plugin by getgauge.

the class StepCompletionProvider method addCompletions.

@Override
public void addCompletions(@NotNull final CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {
    resultSet.stopHere();
    final String prefix = getPrefix(parameters);
    resultSet = resultSet.withPrefixMatcher(new GaugePrefixMatcher(prefix));
    Module moduleForPsiElement = GaugeUtil.moduleForPsiElement(parameters.getPosition());
    if (moduleForPsiElement == null) {
        return;
    }
    for (Type item : getStepsInModule(moduleForPsiElement)) {
        LookupElementBuilder element = LookupElementBuilder.create(item.getText()).withTypeText(item.getType(), true);
        element = element.withInsertHandler((InsertionContext context1, LookupElement item1) -> {
            if (context1.getCompletionChar() == '\t') {
                context1.getDocument().insertString(context1.getEditor().getCaretModel().getOffset(), "\n");
                PsiDocumentManager.getInstance(context1.getProject()).commitDocument(context1.getDocument());
            }
            PsiElement stepElement = context1.getFile().findElementAt(context1.getStartOffset()).getParent();
            final TemplateBuilder templateBuilder = TemplateBuilderFactory.getInstance().createTemplateBuilder(stepElement);
            replaceStepParamElements(prefix, context1, stepElement, templateBuilder);
            templateBuilder.run(context1.getEditor(), false);
        });
        resultSet.addElement(element);
    }
}
Also used : TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) Module(com.intellij.openapi.module.Module) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)13 Editor (com.intellij.openapi.editor.Editor)6 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)6 PsiElement (com.intellij.psi.PsiElement)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 ParamHelper (com.jetbrains.python.psi.impl.ParamHelper)3 FileEditor (com.intellij.openapi.fileEditor.FileEditor)2 TextEditor (com.intellij.openapi.fileEditor.TextEditor)2 TextRange (com.intellij.openapi.util.TextRange)2 PyClass (com.jetbrains.python.psi.PyClass)2 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 PopupStep (com.intellij.openapi.ui.popup.PopupStep)1 BaseListPopupStep (com.intellij.openapi.ui.popup.util.BaseListPopupStep)1 PsiFile (com.intellij.psi.PsiFile)1 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)1