use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class CodeInsightUtilCore method forcePsiPostprocessAndRestoreElement.
public static <T extends PsiElement> T forcePsiPostprocessAndRestoreElement(@NotNull T element, boolean useFileLanguage) {
final PsiFile psiFile = element.getContainingFile();
final Document document = psiFile.getViewProvider().getDocument();
//if (document == null) return element;
final Language language = useFileLanguage ? psiFile.getLanguage() : PsiUtilCore.getDialect(element);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(psiFile.getProject());
final RangeMarker rangeMarker = document.createRangeMarker(element.getTextRange());
documentManager.doPostponedOperationsAndUnblockDocument(document);
documentManager.commitDocument(document);
T elementInRange = findElementInRange(psiFile, rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), (Class<? extends T>) element.getClass(), language, element);
rangeMarker.dispose();
return elementInRange;
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class CreateClassFromNewFix method setupClassFromNewExpression.
protected void setupClassFromNewExpression(final PsiClass psiClass, final PsiNewExpression newExpression) {
assert ApplicationManager.getApplication().isWriteAccessAllowed();
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(newExpression.getProject()).getElementFactory();
PsiClass aClass = psiClass;
if (aClass == null)
return;
final PsiJavaCodeReferenceElement classReference = newExpression.getClassReference();
if (classReference != null) {
classReference.bindToElement(aClass);
}
setupInheritance(newExpression, aClass);
PsiExpressionList argList = newExpression.getArgumentList();
final Project project = aClass.getProject();
if (argList != null && argList.getExpressions().length > 0) {
PsiMethod constructor = elementFactory.createConstructor();
constructor = (PsiMethod) aClass.add(constructor);
TemplateBuilderImpl templateBuilder = new TemplateBuilderImpl(aClass);
CreateFromUsageUtils.setupMethodParameters(constructor, templateBuilder, argList, getTargetSubstitutor(newExpression));
setupSuperCall(aClass, constructor, templateBuilder);
getReferenceElement(newExpression).bindToElement(aClass);
aClass = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(aClass);
final Template template = templateBuilder.buildTemplate();
template.setToReformat(true);
final Editor editor = positionCursor(project, aClass.getContainingFile(), aClass);
if (editor == null)
return;
final RangeMarker textRange = editor.getDocument().createRangeMarker(aClass.getTextRange());
final Runnable runnable = () -> {
new WriteCommandAction(project, getText(), getText()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
try {
editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset());
} finally {
textRange.dispose();
}
}
}.execute();
startTemplate(editor, template, project, null, getText());
};
if (ApplicationManager.getApplication().isUnitTestMode()) {
runnable.run();
} else {
ApplicationManager.getApplication().invokeLater(runnable);
}
} else {
positionCursor(project, aClass.getContainingFile(), ObjectUtils.notNull(aClass.getNameIdentifier(), aClass));
}
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class CreateConstructorFromThisOrSuperFix method invokeImpl.
@Override
protected void invokeImpl(PsiClass targetClass) {
final PsiFile callSite = myMethodCall.getContainingFile();
final Project project = myMethodCall.getProject();
PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace();
try {
PsiMethod constructor = elementFactory.createConstructor();
constructor = (PsiMethod) targetClass.add(constructor);
final TemplateBuilderImpl templateBuilder = new TemplateBuilderImpl(constructor);
CreateFromUsageUtils.setupMethodParameters(constructor, templateBuilder, myMethodCall.getArgumentList(), getTargetSubstitutor(myMethodCall));
final PsiFile psiFile = myMethodCall.getContainingFile();
templateBuilder.setEndVariableAfter(constructor.getBody().getLBrace());
final RangeMarker rangeMarker = psiFile.getViewProvider().getDocument().createRangeMarker(myMethodCall.getTextRange());
constructor = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(constructor);
targetClass = constructor.getContainingClass();
myMethodCall = CodeInsightUtil.findElementInRange(psiFile, rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), myMethodCall.getClass());
rangeMarker.dispose();
Template template = templateBuilder.buildTemplate();
final Editor editor = positionCursor(project, targetClass.getContainingFile(), targetClass);
if (editor == null)
return;
final TextRange textRange = constructor.getTextRange();
final PsiFile file = targetClass.getContainingFile();
editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset());
editor.getCaretModel().moveToOffset(textRange.getStartOffset());
startTemplate(editor, template, project, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
ApplicationManager.getApplication().runWriteAction(() -> {
try {
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
final int offset = editor.getCaretModel().getOffset();
PsiMethod constructor1 = PsiTreeUtil.findElementOfClassAtOffset(file, offset, PsiMethod.class, false);
CreateFromUsageUtils.setupMethodBody(constructor1);
CreateFromUsageUtils.setupEditor(constructor1, editor);
UndoUtil.markPsiFileForUndo(callSite);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
});
}
});
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class DefineParamsDefaultValueAction method startTemplate.
public static void startTemplate(@NotNull Project project, Editor editor, PsiExpression[] argsToBeDelegated, PsiMethod delegateMethod) {
TemplateBuilderImpl builder = new TemplateBuilderImpl(delegateMethod);
RangeMarker rangeMarker = editor.getDocument().createRangeMarker(delegateMethod.getTextRange());
for (final PsiExpression exprToBeDefault : argsToBeDelegated) {
builder.replaceElement(exprToBeDefault, new TextExpression(""));
}
Template template = builder.buildTemplate();
editor.getCaretModel().moveToOffset(rangeMarker.getStartOffset());
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
editor.getDocument().deleteString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset());
rangeMarker.dispose();
CreateFromUsageBaseFix.startTemplate(editor, template, project);
}
use of com.intellij.openapi.editor.RangeMarker in project intellij-community by JetBrains.
the class InlineMethodProcessor method performRefactoring.
protected void performRefactoring(@NotNull UsageInfo[] usages) {
RangeMarker position = null;
if (myEditor != null) {
final int offset = myEditor.getCaretModel().getOffset();
position = myEditor.getDocument().createRangeMarker(offset, offset + 1);
}
LocalHistoryAction a = LocalHistory.getInstance().startAction(getCommandName());
try {
doRefactoring(usages);
} finally {
a.finish();
}
if (position != null) {
if (position.isValid()) {
myEditor.getCaretModel().moveToOffset(position.getStartOffset());
}
position.dispose();
}
}
Aggregations