J2k post-processings based on diagnostics are included into the common scheme
This commit is contained in:
@@ -18,44 +18,21 @@ package org.jetbrains.kotlin.j2k
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.psi.JetFile
|
||||
|
||||
public class AfterConversionPass(val project: Project, val postProcessor: PostProcessor) {
|
||||
public fun run(kotlinFile: JetFile, range: TextRange?) {
|
||||
val bindingContext = postProcessor.analyzeFile(kotlinFile, range)
|
||||
|
||||
fun fixForProblem(diagnostic: Diagnostic): (() -> Unit)? {
|
||||
val psiElement = diagnostic.getPsiElement()
|
||||
if (range != null && psiElement.getTextRange() !in range) return null
|
||||
return postProcessor.fixForProblem(diagnostic)
|
||||
}
|
||||
|
||||
val fixes = bindingContext.getDiagnostics()
|
||||
.map {
|
||||
val fix = fixForProblem(it)
|
||||
if (fix != null) Pair(it.getPsiElement(), fix) else null
|
||||
}
|
||||
.filterNotNull()
|
||||
|
||||
val rangeMarker = if (range != null) {
|
||||
val document = kotlinFile.getViewProvider().getDocument()!!
|
||||
val marker = document.createRangeMarker(range.getStartOffset(), range.getEndOffset())
|
||||
marker.setGreedyToLeft(true)
|
||||
marker.setGreedyToRight(true)
|
||||
val document = kotlinFile.viewProvider.document!!
|
||||
val marker = document.createRangeMarker(range.startOffset, range.endOffset)
|
||||
marker.isGreedyToLeft = true
|
||||
marker.isGreedyToRight = true
|
||||
marker
|
||||
}
|
||||
else {
|
||||
null
|
||||
}
|
||||
|
||||
for ((psiElement, fix) in fixes) {
|
||||
if (psiElement.isValid()) {
|
||||
fix()
|
||||
}
|
||||
}
|
||||
|
||||
postProcessor.doAdditionalProcessing(kotlinFile, rangeMarker)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,59 +17,28 @@
|
||||
package org.jetbrains.kotlin.j2k
|
||||
|
||||
import com.intellij.lang.java.JavaLanguage
|
||||
import com.intellij.openapi.application.ModalityState
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import com.intellij.openapi.editor.RangeMarker
|
||||
import com.intellij.openapi.progress.*
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.impl.source.DummyHolder
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.JetLanguage
|
||||
import org.jetbrains.kotlin.j2k.ast.Element
|
||||
import org.jetbrains.kotlin.j2k.usageProcessing.UsageProcessing
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.JetFile
|
||||
import org.jetbrains.kotlin.psi.JetPsiFactory
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import java.util.ArrayList
|
||||
import java.util.Comparator
|
||||
import java.util.LinkedHashMap
|
||||
|
||||
public interface PostProcessor {
|
||||
public fun analyzeFile(file: JetFile, range: TextRange?): BindingContext
|
||||
|
||||
public fun insertImport(file: JetFile, fqName: FqName)
|
||||
|
||||
public open fun fixForProblem(problem: Diagnostic): (() -> Unit)? {
|
||||
val psiElement = problem.getPsiElement()
|
||||
return when (problem.getFactory()) {
|
||||
Errors.UNNECESSARY_NOT_NULL_ASSERTION -> { ->
|
||||
val exclExclOp = psiElement as JetSimpleNameExpression
|
||||
val exclExclExpr = exclExclOp.getParent() as JetUnaryExpression
|
||||
exclExclExpr.replace(exclExclExpr.getBaseExpression()!!)
|
||||
}
|
||||
|
||||
Errors.VAL_REASSIGNMENT -> { ->
|
||||
if (psiElement is JetSimpleNameExpression) {
|
||||
val property = simpleNameReference(psiElement).resolve() as? JetProperty
|
||||
if (property != null && !property.isVar()) {
|
||||
val factory = JetPsiFactory(psiElement.getProject())
|
||||
property.getValOrVarKeyword().replace(factory.createVarKeyword())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
public fun doAdditionalProcessing(file: JetFile, rangeMarker: RangeMarker?)
|
||||
|
||||
public fun simpleNameReference(nameExpression: JetSimpleNameExpression): PsiReference
|
||||
}
|
||||
|
||||
public enum class ParseContext {
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ public object Test {
|
||||
return false
|
||||
}
|
||||
val result = true
|
||||
if (parent.isDirectory()) {
|
||||
if (parent.isDirectory) {
|
||||
return true
|
||||
} else
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user