Optimization of Basic Code Completion
Now we don't perform code formatting on temporary psi used in ShadowedDeclarationFilter #KT-16856
This commit is contained in:
@@ -30,17 +30,17 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
|||||||
import org.jetbrains.kotlin.renderer.render
|
import org.jetbrains.kotlin.renderer.render
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
fun KtPsiFactory.createExpressionByPattern(pattern: String, vararg args: Any): KtExpression
|
fun KtPsiFactory.createExpressionByPattern(pattern: String, vararg args: Any, reformat: Boolean = true): KtExpression
|
||||||
= createByPattern(pattern, *args) { createExpression(it) }
|
= createByPattern(pattern, *args, reformat = reformat) { createExpression(it) }
|
||||||
|
|
||||||
fun KtPsiFactory.createValueArgumentListByPattern(pattern: String, vararg args: Any): KtValueArgumentList
|
fun KtPsiFactory.createValueArgumentListByPattern(pattern: String, vararg args: Any, reformat: Boolean = true): KtValueArgumentList
|
||||||
= createByPattern(pattern, *args) { createCallArguments(it) }
|
= createByPattern(pattern, *args, reformat = reformat) { createCallArguments(it) }
|
||||||
|
|
||||||
fun <TDeclaration : KtDeclaration> KtPsiFactory.createDeclarationByPattern(pattern: String, vararg args: Any): TDeclaration
|
fun <TDeclaration : KtDeclaration> KtPsiFactory.createDeclarationByPattern(pattern: String, vararg args: Any, reformat: Boolean = true): TDeclaration
|
||||||
= createByPattern(pattern, *args) { createDeclaration<TDeclaration>(it) }
|
= createByPattern(pattern, *args, reformat = reformat) { createDeclaration<TDeclaration>(it) }
|
||||||
|
|
||||||
fun KtPsiFactory.createDestructuringDeclarationByPattern(pattern: String, vararg args: Any): KtDestructuringDeclaration
|
fun KtPsiFactory.createDestructuringDeclarationByPattern(pattern: String, vararg args: Any, reformat: Boolean = true): KtDestructuringDeclaration
|
||||||
= createByPattern(pattern, *args) { createDestructuringDeclaration(it) }
|
= createByPattern(pattern, *args, reformat = reformat) { createDestructuringDeclaration(it) }
|
||||||
|
|
||||||
private abstract class ArgumentType<T : Any>(val klass: Class<T>)
|
private abstract class ArgumentType<T : Any>(val klass: Class<T>)
|
||||||
|
|
||||||
@@ -94,7 +94,7 @@ private val SUPPORTED_ARGUMENT_TYPES = listOf(
|
|||||||
PsiChildRangeArgumentType
|
PsiChildRangeArgumentType
|
||||||
)
|
)
|
||||||
|
|
||||||
fun <TElement : KtElement> createByPattern(pattern: String, vararg args: Any, factory: (String) -> TElement): TElement {
|
fun <TElement : KtElement> createByPattern(pattern: String, vararg args: Any, reformat: Boolean = true, factory: (String) -> TElement): TElement {
|
||||||
val argumentTypes = args.map { arg ->
|
val argumentTypes = args.map { arg ->
|
||||||
SUPPORTED_ARGUMENT_TYPES.firstOrNull { it.klass.isInstance(arg) }
|
SUPPORTED_ARGUMENT_TYPES.firstOrNull { it.klass.isInstance(arg) }
|
||||||
?: throw IllegalArgumentException("Unsupported argument type: ${arg::class.java}, should be one of: ${SUPPORTED_ARGUMENT_TYPES.map { it.klass.simpleName }.joinToString()}")
|
?: throw IllegalArgumentException("Unsupported argument type: ${arg::class.java}, should be one of: ${SUPPORTED_ARGUMENT_TYPES.map { it.klass.simpleName }.joinToString()}")
|
||||||
@@ -146,30 +146,31 @@ fun <TElement : KtElement> createByPattern(pattern: String, vararg args: Any, fa
|
|||||||
|
|
||||||
val codeStyleManager = CodeStyleManager.getInstance(project)
|
val codeStyleManager = CodeStyleManager.getInstance(project)
|
||||||
|
|
||||||
val stringPlaceholderRanges = allPlaceholders
|
if (reformat) {
|
||||||
.filter { args[it.key] is String }
|
val stringPlaceholderRanges = allPlaceholders
|
||||||
.flatMap { it.value }
|
.filter { args[it.key] is String }
|
||||||
.map { it.range }
|
.flatMap { it.value }
|
||||||
.filterNot { it.isEmpty }
|
.map { it.range }
|
||||||
.sortedByDescending { it.startOffset }
|
.filterNot { it.isEmpty }
|
||||||
|
.sortedByDescending { it.startOffset }
|
||||||
|
|
||||||
// reformat whole text except for String arguments (as they can contain user's formatting to be preserved)
|
// reformat whole text except for String arguments (as they can contain user's formatting to be preserved)
|
||||||
if (stringPlaceholderRanges.none()) {
|
if (stringPlaceholderRanges.none()) {
|
||||||
resultElement = codeStyleManager.reformat(resultElement, true) as TElement
|
resultElement = codeStyleManager.reformat(resultElement, true) as TElement
|
||||||
}
|
|
||||||
else {
|
|
||||||
var bound = resultElement.endOffset - 1
|
|
||||||
for (range in stringPlaceholderRanges) {
|
|
||||||
// we extend reformatting range by 1 to the right because otherwise some of spaces are not reformatted
|
|
||||||
resultElement = codeStyleManager.reformatRange(resultElement, range.endOffset + start, bound + 1, true) as TElement
|
|
||||||
bound = range.startOffset + start
|
|
||||||
}
|
}
|
||||||
resultElement = codeStyleManager.reformatRange(resultElement, start, bound + 1, true) as TElement
|
else {
|
||||||
|
var bound = resultElement.endOffset - 1
|
||||||
|
for (range in stringPlaceholderRanges) {
|
||||||
|
// we extend reformatting range by 1 to the right because otherwise some of spaces are not reformatted
|
||||||
|
resultElement = codeStyleManager.reformatRange(resultElement, range.endOffset + start, bound + 1, true) as TElement
|
||||||
|
bound = range.startOffset + start
|
||||||
|
}
|
||||||
|
resultElement = codeStyleManager.reformatRange(resultElement, start, bound + 1, true) as TElement
|
||||||
|
}
|
||||||
|
|
||||||
|
// do not reformat the whole expression in PostprocessReformattingAspect
|
||||||
|
CodeEditUtil.setNodeGeneratedRecursively(resultElement.node, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// do not reformat the whole expression in PostprocessReformattingAspect
|
|
||||||
CodeEditUtil.setNodeGeneratedRecursively(resultElement.node, false)
|
|
||||||
|
|
||||||
for ((pointer, n) in pointers) {
|
for ((pointer, n) in pointers) {
|
||||||
var element = pointer.element!!
|
var element = pointer.element!!
|
||||||
if (element is KtFunctionLiteral) {
|
if (element is KtFunctionLiteral) {
|
||||||
@@ -185,7 +186,8 @@ fun <TElement : KtElement> createByPattern(pattern: String, vararg args: Any, fa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
codeStyleManager.adjustLineIndent(resultElement.containingFile, resultElement.textRange)
|
if (reformat)
|
||||||
|
codeStyleManager.adjustLineIndent(resultElement.containingFile, resultElement.textRange)
|
||||||
|
|
||||||
return resultElement
|
return resultElement
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ class ShadowedDeclarationsFilter(
|
|||||||
val newCall = object : Call {
|
val newCall = object : Call {
|
||||||
//TODO: compiler crash (KT-8011)
|
//TODO: compiler crash (KT-8011)
|
||||||
//val arguments = parameters.indices.map { DummyArgument(it) }
|
//val arguments = parameters.indices.map { DummyArgument(it) }
|
||||||
val callee = psiFactory.createExpressionByPattern("$0", name)
|
val callee = psiFactory.createExpressionByPattern("$0", name, reformat = false)
|
||||||
|
|
||||||
override fun getCalleeExpression() = callee
|
override fun getCalleeExpression() = callee
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user