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