-
Notifications
You must be signed in to change notification settings - Fork 380
/
RsIntroduceParameterHandler.kt
45 lines (40 loc) · 1.75 KB
/
RsIntroduceParameterHandler.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
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.ide.refactoring.introduceParameter
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.refactoring.RefactoringActionHandler
import com.intellij.refactoring.RefactoringBundle
import org.rust.ide.refactoring.findCandidateExpressionsToExtract
import org.rust.ide.refactoring.showErrorMessageForExtractParameter
import org.rust.ide.refactoring.showExpressionChooser
import org.rust.lang.core.psi.RsFile
class RsIntroduceParameterHandler : RefactoringActionHandler {
override fun invoke(project: Project, editor: Editor, file: PsiFile, dataContext: DataContext) {
if (file !is RsFile) return
val exprs = findCandidateExpressionsToExtract(editor, file)
.filter { it -> checkTypeIsExtractable(it) }
when (exprs.size) {
0 -> {
val message = RefactoringBundle.message(if (editor.selectionModel.hasSelection())
"selected.block.should.represent.an.expression"
else
"refactoring.introduce.selection.error"
)
showErrorMessageForExtractParameter(project, editor, message)
}
1 -> extractExpression(editor, exprs.single())
else -> showExpressionChooser(editor, exprs) {
extractExpression(editor, it)
}
}
}
override fun invoke(project: Project, elements: Array<out PsiElement>, dataContext: DataContext?) {
//this doesn't get called from the editor.
}
}