-
Notifications
You must be signed in to change notification settings - Fork 380
/
extraxtExpressionUi.kt
82 lines (73 loc) · 2.47 KB
/
extraxtExpressionUi.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.ide.refactoring
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Pass
import com.intellij.openapiext.isUnitTestMode
import com.intellij.refactoring.IntroduceTargetChooser
import com.intellij.refactoring.RefactoringBundle
import com.intellij.refactoring.introduce.inplace.OccurrencesChooser
import com.intellij.refactoring.util.CommonRefactoringUtil
import org.jetbrains.annotations.TestOnly
import org.rust.lang.core.psi.RsExpr
import org.rust.lang.core.psi.RsFunction
fun showExpressionChooser(
editor: Editor,
exprs: List<RsExpr>,
callback: (RsExpr) -> Unit
) {
if (isUnitTestMode) {
callback(MOCK!!.chooseTarget(exprs))
} else {
IntroduceTargetChooser.showChooser(editor, exprs, callback.asPass) { it.text }
}
}
fun showOccurrencesChooser(
editor: Editor,
expr: RsExpr,
occurrences: List<RsExpr>,
callback: (List<RsExpr>) -> Unit
) {
if (isUnitTestMode && occurrences.size > 1) {
callback(MOCK!!.chooseOccurrences(expr, occurrences))
} else {
OccurrencesChooser.simpleChooser<RsExpr>(editor)
.showChooser(
expr,
occurrences,
{ choice: OccurrencesChooser.ReplaceChoice ->
val toReplace = if (choice == OccurrencesChooser.ReplaceChoice.ALL) occurrences else listOf(expr)
callback(toReplace)
}.asPass
)
}
}
fun showErrorMessageForExtractParameter(project: Project, editor: Editor, message: String) {
val title = RefactoringBundle.message("introduce.parameter.title")
val helpId = "refactoring.extractParameter"
CommonRefactoringUtil.showErrorHint(project, editor, message, title, helpId)
}
private val <T> ((T) -> Unit).asPass: Pass<T>
get() = object : Pass<T>() {
override fun pass(t: T) = this@asPass(t)
}
interface ExtractExpressionUi {
fun chooseTarget(exprs: List<RsExpr>): RsExpr
fun chooseOccurrences(expr: RsExpr, occurrences: List<RsExpr>): List<RsExpr>
fun chooseMethod(methods: List<RsFunction>): RsFunction {
return methods.first()
}
}
var MOCK: ExtractExpressionUi? = null
@TestOnly
fun withMockTargetExpressionChooser(mock: ExtractExpressionUi, f: () -> Unit) {
MOCK = mock
try {
f()
} finally {
MOCK = null
}
}