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