editor is nullable in SelfTargetingIntention.applyTo()

This commit is contained in:
Dmitry Jemerov
2016-01-11 19:45:56 +01:00
parent 9bfa90dc0e
commit 7390e0ac66
101 changed files with 179 additions and 155 deletions
@@ -25,8 +25,8 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiFile
@@ -118,13 +118,12 @@ abstract class IntentionBasedInspection<TElement : KtElement>(
assert(startElement == endElement)
if (!isAvailable(project, file, startElement, endElement)) return
startElement.getOrCreateEditor()?.let { editor ->
editor.caretModel.moveToOffset(startElement.textOffset)
intention.applyTo(startElement as TElement, editor)
}
val editor = startElement.findExistingEditor()
editor?.caretModel?.moveToOffset(startElement.textOffset)
intention.applyTo(startElement as TElement, editor)
}
private fun PsiElement.getOrCreateEditor(): Editor? {
private fun PsiElement.findExistingEditor(): Editor? {
val file = containingFile?.virtualFile ?: return null
val document = FileDocumentManager.getInstance().getDocument(file) ?: return null
@@ -263,7 +263,7 @@ class OperatorToFunctionIntention : SelfTargetingIntention<KtExpression>(KtExpre
}
}
override fun applyTo(element: KtExpression, editor: Editor) {
override fun applyTo(element: KtExpression, editor: Editor?) {
convert(element)
}
}
@@ -51,7 +51,7 @@ abstract class SelfTargetingIntention<TElement : KtElement>(
abstract fun isApplicableTo(element: TElement, caretOffset: Int): Boolean
abstract fun applyTo(element: TElement, editor: Editor)
abstract fun applyTo(element: TElement, editor: Editor?)
private fun getTarget(editor: Editor, file: PsiFile): TElement? {
val offset = editor.caretModel.offset
@@ -98,6 +98,12 @@ private fun moveCaretIntoGeneratedElementDocumentUnblocked(editor: Editor, eleme
return false
}
fun Editor.unblockDocument() {
project?.let {
PsiDocumentManager.getInstance(it).doPostponedOperationsAndUnblockDocument(document)
}
}
fun Editor.moveCaret(offset: Int, scrollType: ScrollType = ScrollType.RELATIVE) {
caretModel.moveToOffset(offset)
scrollingModel.scrollToCaret(scrollType)