Skip to content

Commit

Permalink
fix: 解决 .var (LuaLocalPostfixTemplate) 报错问题
Browse files Browse the repository at this point in the history
  • Loading branch information
tangzx committed Dec 27, 2023
1 parent 9c69042 commit 7d58076
Showing 1 changed file with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import com.intellij.openapi.util.Pass
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.SmartPsiElementPointer
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.refactoring.RefactoringActionHandler
import com.intellij.refactoring.introduce.inplace.InplaceVariableIntroducer
import com.intellij.refactoring.introduce.inplace.OccurrencesChooser
import com.intellij.refactoring.suggested.createSmartPointer
import com.tang.intellij.lua.psi.*
import com.tang.intellij.lua.refactoring.LuaRefactoringUtil

Expand Down Expand Up @@ -106,29 +108,34 @@ class LuaIntroduceVarHandler : RefactoringActionHandler {

var commonParent = PsiTreeUtil.findCommonParent(operation.occurrences)
if (commonParent != null) {
var element = operation.element
val pointers = mutableListOf<SmartPsiElementPointer<PsiElement>>()
var positionIndex = 0
val element = operation.element
var localDef = LuaElementFactory.createWith(operation.project, "local var = " + element.text)
val inline = isInline(commonParent, operation)
if (inline) {
if (element is LuaCallExpr && element.parent is LuaExprStat)
element = element.parent
localDef = element.replace(localDef)
operation.position = localDef
val targetToReplace = if (element is LuaCallExpr && element.parent is LuaExprStat) element.parent else element
localDef = targetToReplace.replace(localDef)
pointers.add(localDef.createSmartPointer())
} else {
val anchor = findAnchor(operation.occurrences)
commonParent = anchor?.parent
localDef = commonParent!!.addBefore(localDef, anchor)
commonParent.addAfter(LuaElementFactory.newLine(operation.project), localDef)
operation.occurrences.forEach { occ->
var identifier = LuaElementFactory.createName(operation.project, operation.name)
identifier = occ.replace(identifier)
operation.newOccurrences.add(identifier)
if (occ == operation.element)
operation.position = identifier
operation.occurrences.forEachIndexed { index, occ ->
val identifier = occ.replace(LuaElementFactory.createName(operation.project, operation.name))
pointers.add(identifier.createSmartPointer())
if (occ == operation.element) {
positionIndex = index
}
}
}

localDef = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(localDef) ?: return

operation.newOccurrences.addAll(pointers.map { it.element!! })
operation.position = operation.newOccurrences[positionIndex]

val nameDef = PsiTreeUtil.findChildOfType(localDef, LuaNameDef::class.java)
if (nameDef != null)
operation.editor.caretModel.moveToOffset(nameDef.textOffset)
Expand Down

0 comments on commit 7d58076

Please sign in to comment.