Support incremental analysis of comment and kdoc

#KT-35189 Fixed
This commit is contained in:
Vladimir Dolzhenko
2019-11-27 13:59:27 +01:00
parent 664d69b752
commit f709377735
6 changed files with 47 additions and 2 deletions
@@ -17,6 +17,7 @@ import com.intellij.pom.event.PomModelEvent
import com.intellij.pom.event.PomModelListener
import com.intellij.pom.tree.TreeAspect
import com.intellij.pom.tree.events.TreeChangeEvent
import com.intellij.pom.tree.events.impl.ChangeInfoImpl
import com.intellij.psi.*
import com.intellij.psi.impl.PsiManagerImpl
import com.intellij.psi.impl.PsiModificationTrackerImpl
@@ -26,6 +27,7 @@ import com.intellij.psi.impl.PsiTreeChangeEventImpl.PsiEventType.PROPERTY_CHANGE
import com.intellij.psi.impl.PsiTreeChangePreprocessor
import com.intellij.psi.util.PsiModificationTracker
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getTopmostParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
@@ -92,6 +94,7 @@ class KotlinCodeBlockModificationListener(
if (changedElements.isNotEmpty() &&
// ignore formatting (whitespaces etc)
(isFormattingChange(changeSet) ||
isCommentChange(changeSet) ||
changedElements.all { !it.psi.isPhysical })
) return
@@ -195,7 +198,19 @@ class KotlinCodeBlockModificationListener(
fun isFormattingChange(changeSet: TreeChangeEvent): Boolean =
changeSet.changedElements.all {
changeSet.getChangesByElement(it).affectedChildren.all { c -> (c is PsiWhiteSpace || c is PsiComment) }
changeSet.getChangesByElement(it).affectedChildren.all { c -> c is PsiWhiteSpace }
}
fun isCommentChange(changeSet: TreeChangeEvent): Boolean =
changeSet.changedElements.all { changedElement ->
val changesByElement = changeSet.getChangesByElement(changedElement)
changesByElement.affectedChildren.all { affectedChild ->
val changeByChild = changesByElement.getChangeByChild(affectedChild)
return@all if (changeByChild is ChangeInfoImpl) {
changeByChild.oldChild is PsiComment && changeByChild.newChild is PsiComment ||
changeByChild.oldChild is KDoc && changeByChild.newChild is KDoc
} else false
}
}
fun getInsideCodeBlockModificationScope(element: PsiElement): BlockModificationScopeElement? {
+7
View File
@@ -0,0 +1,7 @@
// OUT_OF_CODE_BLOCK: FALSE
// TYPE: '//'
// ERROR: A 'return' expression required in a function with a block body ('{...}')
fun comment(): String {
<caret>return ""
}
+7
View File
@@ -0,0 +1,7 @@
// OUT_OF_CODE_BLOCK: FALSE
// TYPE: 'some text'
// <caret>
fun comment(): String {
return ""
}
+9
View File
@@ -0,0 +1,9 @@
// OUT_OF_CODE_BLOCK: FALSE
// TYPE: 'some text'
/**
* <caret>
*/
fun kDoc(): String {
return ""
}
@@ -0,0 +1,4 @@
// OUT_OF_CODE_BLOCK: FALSE
class Test {
val a: String = "aasdf<caret>"
}
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.idea.codeInsight
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiManager
import com.intellij.psi.impl.PsiModificationTrackerImpl
@@ -18,6 +19,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.trackers.outOfBlockModificationCount
import org.jetbrains.kotlin.idea.test.DirectiveBasedActionUtils
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.ResolveSession
@@ -75,6 +77,7 @@ abstract class AbstractOutOfBlockModificationTest : KotlinLightCodeInsightFixtur
.incOutOfCodeBlockModificationCounter()
}
val updateElement = myFixture.file.findElementAt(myFixture.caretOffset - 1)
val kDoc: KDoc? = PsiTreeUtil.getParentOfType(updateElement, KDoc::class.java, false)
val ktExpression: KtExpression? = PsiTreeUtil.getParentOfType(updateElement, KtExpression::class.java, false)
val ktDeclaration: KtDeclaration? = PsiTreeUtil.getParentOfType(updateElement, KtDeclaration::class.java, false)
val ktElement = ktExpression ?: ktDeclaration ?: return
@@ -91,7 +94,7 @@ abstract class AbstractOutOfBlockModificationTest : KotlinLightCodeInsightFixtur
"Expected out-of-block should result expression analyzed and vise versa", expectedOutOfBlock,
expressionProcessed
)
} else {
} else if (updateElement !is PsiComment && kDoc == null) { // comments could be ignored from analyze
val declarationProcessed =
context.get(
BindingContext.DECLARATION_TO_DESCRIPTOR,