PY-16412 SpecifyTypeInPy3AnnotationIntention uses PyUtil#updateDocument

This commit is contained in:
Mikhail Golubev
2018-02-05 21:41:59 +03:00
committed by Andrey Vlasovskikh
parent d8762e94d8
commit 46cff81cee
@@ -17,14 +17,12 @@ package com.jetbrains.python.codeInsight.intentions;
import com.intellij.codeInsight.CodeInsightUtilCore;
import com.intellij.codeInsight.template.*;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
@@ -153,36 +151,31 @@ public class SpecifyTypeInPy3AnnotationsIntention extends TypeIntention {
final String annotationText = "-> " + returnType;
final PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
Document documentWithCallable = manager.getDocument(function.getContainingFile());
if (documentWithCallable != null) {
try {
manager.doPostponedOperationsAndUnblockDocument(documentWithCallable);
final PyAnnotation oldAnnotation = function.getAnnotation();
if (oldAnnotation != null) {
final TextRange oldRange = oldAnnotation.getTextRange();
documentWithCallable.replaceString(oldRange.getStartOffset(), oldRange.getEndOffset(), annotationText);
PyFunction annotatedFunction = PyUtil.updateDocumentUnblockedAndCommitted(function, document -> {
final PyAnnotation oldAnnotation = function.getAnnotation();
if (oldAnnotation != null) {
final TextRange oldRange = oldAnnotation.getTextRange();
document.replaceString(oldRange.getStartOffset(), oldRange.getEndOffset(), annotationText);
}
else {
final PsiElement prevElem = PyPsiUtils.getPrevNonCommentSibling(function.getStatementList(), true);
assert prevElem != null;
final TextRange range = prevElem.getTextRange();
if (prevElem.getNode().getElementType() == PyTokenTypes.COLON) {
document.insertString(range.getStartOffset(), " " + annotationText);
}
else {
final PsiElement prevElem = PyPsiUtils.getPrevNonCommentSibling(function.getStatementList(), true);
assert prevElem != null;
final TextRange range = prevElem.getTextRange();
if (prevElem.getNode().getElementType() == PyTokenTypes.COLON) {
documentWithCallable.insertString(range.getStartOffset(), " " + annotationText);
}
else {
documentWithCallable.insertString(range.getEndOffset(), " " + annotationText + ":");
}
document.insertString(range.getEndOffset(), " " + annotationText + ":");
}
}
finally {
manager.commitDocument(documentWithCallable);
}
return CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(function);
});
if (annotatedFunction == null) {
return null;
}
function = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(function);
final PyAnnotation annotation = function.getAnnotation();
final PyAnnotation annotation = annotatedFunction.getAnnotation();
assert annotation != null;
final PyExpression annotationValue = annotation.getValue();
assert annotationValue != null : "Generated function must have annotation";
@@ -193,11 +186,7 @@ public class SpecifyTypeInPy3AnnotationsIntention extends TypeIntention {
final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(annotationValue);
builder.replaceRange(TextRange.create(0, returnType.length()), returnType);
final Template template = ((TemplateBuilderImpl)builder).buildInlineTemplate();
final OpenFileDescriptor descriptor = new OpenFileDescriptor(
project,
function.getContainingFile().getVirtualFile(),
offset
);
final OpenFileDescriptor descriptor = new OpenFileDescriptor(project, annotatedFunction.getContainingFile().getVirtualFile(), offset);
final Editor targetEditor = FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
if (targetEditor != null) {
targetEditor.getCaretModel().moveToOffset(offset);