diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/createByPattern.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/createByPattern.kt index d901d2edd84..77806818665 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/createByPattern.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/createByPattern.kt @@ -41,7 +41,7 @@ fun KtPsiFactory.createDeclarationByPattern( pattern: String, vararg args: Any, reformat: Boolean = true -): TDeclaration = createByPattern(pattern, *args, reformat = reformat) { createDeclaration(it) } +): TDeclaration = createByPattern(pattern, *args, reformat = reformat) { createDeclaration(it) } fun KtPsiFactory.createDestructuringDeclarationByPattern( pattern: String, @@ -109,8 +109,9 @@ private val SUPPORTED_ARGUMENT_TYPES = listOf( PsiChildRangeArgumentType ) -@TestOnly -var CREATEBYPATTERN_MAY_NOT_REFORMAT = false +@get:TestOnly +@set:TestOnly +var CREATE_BY_PATTERN_MAY_NOT_REFORMAT = false fun createByPattern( pattern: String, @@ -120,7 +121,10 @@ fun createByPattern( ): TElement { val argumentTypes = args.map { arg -> SUPPORTED_ARGUMENT_TYPES.firstOrNull { it.klass.isInstance(arg) } - ?: throw IllegalArgumentException("Unsupported argument type: ${arg::class.java}, should be one of: ${SUPPORTED_ARGUMENT_TYPES.joinToString { it.klass.simpleName }}") + ?: throw IllegalArgumentException( + "Unsupported argument type: ${arg::class.java}, should be one of: " + + SUPPORTED_ARGUMENT_TYPES.joinToString { it.klass.simpleName } + ) } // convert arguments that can be converted into plain text @@ -128,14 +132,15 @@ fun createByPattern( val args = args.zip(argumentTypes).map { val (arg, type) = it if (type is PlainTextArgumentType) - (type.toPlainText as Function1).invoke(arg) // TODO: see KT-7833 + @Suppress("UNCHECKED_CAST") // Suppress compiler checks as types checks were done in runtime + (type.toPlainText as Function1).invoke(arg) else arg } val (processedText, allPlaceholders) = processPattern(pattern, args) - var resultElement = factory(processedText.trim()) + var resultElement: KtElement = factory(processedText.trim()) val project = resultElement.project val start = resultElement.startOffset @@ -156,7 +161,7 @@ fun createByPattern( val elementRange = element.textRange.shiftRight(-start) if (elementRange == range && expectedElementType.isInstance(element)) { val pointer = pointerManager.createSmartPsiElementPointer(element) - pointers.put(pointer, n) + pointers[pointer] = n break } @@ -170,7 +175,7 @@ fun createByPattern( val codeStyleManager = CodeStyleManager.getInstance(project) if (reformat) { - if (CREATEBYPATTERN_MAY_NOT_REFORMAT) { + if (CREATE_BY_PATTERN_MAY_NOT_REFORMAT) { throw java.lang.IllegalArgumentException("Reformatting is not allowed in the current context; please change the invocation to use reformat=false") } val stringPlaceholderRanges = allPlaceholders @@ -182,15 +187,15 @@ fun createByPattern( // reformat whole text except for String arguments (as they can contain user's formatting to be preserved) resultElement = if (stringPlaceholderRanges.none()) { - codeStyleManager.reformat(resultElement, true) as TElement + codeStyleManager.reformat(resultElement, true) as KtElement } 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 + resultElement = codeStyleManager.reformatRange(resultElement, range.endOffset + start, bound + 1, true) as KtElement bound = range.startOffset + start } - codeStyleManager.reformatRange(resultElement, start, bound + 1, true) as TElement + codeStyleManager.reformatRange(resultElement, start, bound + 1, true) as KtElement } // do not reformat the whole expression in PostprocessReformattingAspect @@ -207,14 +212,15 @@ fun createByPattern( if (element == resultElement) { assert(range.first == range.last) - resultElement = range.first as TElement + resultElement = range.first as KtElement } } if (reformat) codeStyleManager.adjustLineIndent(resultElement.containingFile, resultElement.textRange) - return resultElement + @Suppress("UNCHECKED_CAST") + return resultElement as TElement } private data class Placeholder(val range: TextRange, val text: String) @@ -244,7 +250,7 @@ private fun processPattern(pattern: String, args: List): PatternData { } else { check(nextChar?.isDigit() ?: false, "unclosed '$'") - val lastIndex = (i..pattern.length - 1).firstOrNull { !pattern[it].isDigit() } ?: pattern.length + val lastIndex = (i until pattern.length).firstOrNull { !pattern[it].isDigit() } ?: pattern.length val n = pattern.substring(i, lastIndex).toInt() check(n >= 0, "invalid placeholder number: $n") i = lastIndex @@ -265,7 +271,7 @@ private fun processPattern(pattern: String, args: List): PatternData { append(placeholderText) val range = TextRange(length - placeholderText.length, length) - ranges.getOrPut(n, { ArrayList() }).add(Placeholder(range, placeholderText)) + ranges.getOrPut(n) { ArrayList() }.add(Placeholder(range, placeholderText)) continue } } else { diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt index 904f0bdd4f7..72b0f0de1bc 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt @@ -28,7 +28,7 @@ import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection -import org.jetbrains.kotlin.psi.CREATEBYPATTERN_MAY_NOT_REFORMAT +import org.jetbrains.kotlin.psi.CREATE_BY_PATTERN_MAY_NOT_REFORMAT import org.jetbrains.kotlin.psi.KtBlockExpression import org.jetbrains.kotlin.psi.KtElement import org.jetbrains.kotlin.psi.psiUtil.containsInside @@ -86,13 +86,13 @@ abstract class SelfTargetingIntention( final override fun isAvailable(project: Project, editor: Editor, file: PsiFile): Boolean { if (ApplicationManager.getApplication().isUnitTestMode) { - CREATEBYPATTERN_MAY_NOT_REFORMAT = true + CREATE_BY_PATTERN_MAY_NOT_REFORMAT = true } try { return getTarget(editor, file) != null } finally { - CREATEBYPATTERN_MAY_NOT_REFORMAT = false + CREATE_BY_PATTERN_MAY_NOT_REFORMAT = false } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixActionBase.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixActionBase.kt index d2d963f65bb..8539c7c04af 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixActionBase.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixActionBase.kt @@ -22,7 +22,7 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile -import org.jetbrains.kotlin.psi.CREATEBYPATTERN_MAY_NOT_REFORMAT +import org.jetbrains.kotlin.psi.CREATE_BY_PATTERN_MAY_NOT_REFORMAT import org.jetbrains.kotlin.psi.KtCodeFragment import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer @@ -39,7 +39,7 @@ abstract class QuickFixActionBase(element: T) : IntentionAct final override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean { if (ApplicationManager.getApplication().isUnitTestMode) { - CREATEBYPATTERN_MAY_NOT_REFORMAT = true + CREATE_BY_PATTERN_MAY_NOT_REFORMAT = true } try { val element = element ?: return false @@ -50,7 +50,7 @@ abstract class QuickFixActionBase(element: T) : IntentionAct isAvailableImpl(project, editor, file) } finally { - CREATEBYPATTERN_MAY_NOT_REFORMAT = false + CREATE_BY_PATTERN_MAY_NOT_REFORMAT = false } }