Skip to content

Commit

Permalink
refactor(provider): improve rename functionality and command handling #…
Browse files Browse the repository at this point in the history
…181

Refined the rename method in the refactoring tool by adding null safety checks and simplifying the lookup process for named elements. Additionally, updated the refactor command handling to ensure the language is properly identified and to support rename commands for methods.
  • Loading branch information
phodal committed May 28, 2024
1 parent 705d2a0 commit 2ca9d30
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,30 @@ import com.intellij.psi.PsiManager
class RefactorInsCommand(val myProject: Project, private val argument: String, private val textSegment: String) :
InsCommand {
override suspend fun execute(): String? {
val java = Language.findLanguageByID("JAVA") ?: return "Java language not found"
val refactoringTool = cc.unitmesh.devti.provider.RefactoringTool.forLanguage(java)
var currentEditFile: PsiFile? = null
val editor = FileEditorManager.getInstance(myProject).selectedTextEditor
if (editor != null) {
val currentFile = FileDocumentManager.getInstance().getFile(editor.document) ?: return "File not found"
val currentPsiFile = PsiManager.getInstance(myProject).findFile(currentFile)

// will not handle the case where the current file is not a DevInFile
currentEditFile = if (currentPsiFile is DevInFile) {
null
} else {
currentPsiFile
}
}

val language = currentEditFile?.language ?: Language.findLanguageByID("JAVA") ?: return "Language not found"
val refactoringTool = cc.unitmesh.devti.provider.RefactoringTool.forLanguage(language)
?: return "Refactoring tool not found for Java"

val command = BuiltinRefactorCommand.fromString(argument) ?: return "Unknown refactor command: $argument"

when (command) {
BuiltinRefactorCommand.RENAME -> {
val (from, to) = textSegment.split(" to ")
var psiFile: PsiFile? = null
val editor = FileEditorManager.getInstance(myProject).selectedTextEditor
if (editor != null) {
val currentFile = FileDocumentManager.getInstance().getFile(editor.document) ?: return "File not found"
val currentPsiFile = PsiManager.getInstance(myProject).findFile(currentFile)

// will not handle the case where the current file is not a DevInFile
psiFile = if (currentPsiFile is DevInFile) {
null
} else {
currentPsiFile
}
}

refactoringTool.rename(from.trim(), to.trim(), psiFile)
refactoringTool.rename(from.trim(), to.trim(), currentEditFile)
}

BuiltinRefactorCommand.SAFEDELETE -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ import com.intellij.codeInsight.daemon.impl.quickfix.RenameElementFix
import com.intellij.codeInsight.daemon.impl.quickfix.SafeDeleteFix
import com.intellij.codeInspection.MoveToPackageFix
import com.intellij.openapi.project.ProjectManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiNamedElement
import com.intellij.psi.*
import com.intellij.psi.search.FileTypeIndex
import com.intellij.psi.search.ProjectScope
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtNamedFunction

Expand Down Expand Up @@ -41,7 +37,12 @@ class KotlinRefactoringTool : RefactoringTool {

override fun rename(sourceName: String, targetName: String, psiFile: PsiFile?): Boolean {
if (project == null) return false
val elementInfo = getElementInfo(sourceName, psiFile) ?: return false
val retrieveElementInfo = getElementInfo(sourceName, psiFile)
val elementInfo = if (retrieveElementInfo != null) {
retrieveElementInfo
} else {
return false
}

val element: PsiNamedElement =
if (psiFile != null) {
Expand All @@ -51,17 +52,14 @@ class KotlinRefactoringTool : RefactoringTool {
val pkgName = elementInfo.pkgName

if (elementInfo.isMethod) {
// lookup by class and function
val findClassAndMethod: KtNamedFunction? = psiFile.classes
.filterIsInstance<KtClass>()
.firstOrNull { it: KtClass ->
val findClassAndMethod: PsiMethod? = psiFile.classes
.firstOrNull {
it.name == className
}?.declarations?.filterIsInstance<KtNamedFunction>()
?.firstOrNull { it.name == methodName }
}
?.findMethodsByName(methodName, false)?.firstOrNull()

// lookup by function only
findClassAndMethod
?: (psiFile.declarations
findClassAndMethod ?: (psiFile.declarations
.filterIsInstance<KtNamedFunction>()
.firstOrNull {
it.name == methodName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ open class RefactorThisAction : ChatBaseAction() {
"""```
|- You should summary in the end with `DevIn` language in markdown fence-code block, I will handle it.
|- the DevIn language current only support rename method.
|- If you had rename method or class, return follow format:
|- If you had rename method name or class name, return follow format:
|```DevIn
|/refactor:rename <sourceName> to <targetName>
|/refactor:rename <sourceMethodName> to <targetMethodName>
""".trimMargin()

return staticCodeResults + devinRefactorPrompt
Expand Down

0 comments on commit 2ca9d30

Please sign in to comment.