Skip to content

Commit 31328ea

Browse files
committed
safe SearchContext.index
1 parent c67230d commit 31328ea

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

src/main/java/com/tang/intellij/lua/psi/LuaPsiImplUtil.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,15 @@ fun guessTypeAt(list: LuaExprList, context: SearchContext): ITy {
264264
//local a, b = getValues12() -- a = 1, b = 2
265265
//local a, b, c = getValues12(), 3, 4 --a = 1, b = 3, c = 4
266266
//local a, b, c = getValues12(), getValue34() --a = 1, b = 3, c = 4
267+
var index = -1
267268
if (exprList.size > 1) {
268269
val nameSize = context.index + 1
269-
if (nameSize > exprList.size) {
270+
index = if (nameSize > exprList.size) {
270271
val valueSize = exprList.size
271-
context.index = nameSize - valueSize
272-
} else context.index = 0
272+
nameSize - valueSize
273+
} else 0
273274
}
274-
return expr.guessType(context)
275+
return context.withIndex(index) { expr.guessType(context) }
275276
}
276277
return Ty.UNKNOWN
277278
}
@@ -353,8 +354,9 @@ fun guessValueType(indexExpr: LuaIndexExpr, context: SearchContext): ITy {
353354
var ret: ITy = Ty.UNKNOWN
354355
val assignStat = indexExpr.assignStat
355356
if (assignStat != null) {
356-
context.index = assignStat.getIndexFor(indexExpr)
357-
ret = assignStat.valueExprList?.guessTypeAt(context) ?: Ty.UNKNOWN
357+
ret = context.withIndex(assignStat.getIndexFor(indexExpr)) {
358+
assignStat.valueExprList?.guessTypeAt(context) ?: Ty.UNKNOWN
359+
}
358360
}
359361
return ret
360362
}
@@ -576,11 +578,12 @@ fun guessReturnType(returnStat: LuaReturnStat?, index: Int, context: SearchConte
576578
if (returnStat != null) {
577579
val returnExpr = returnStat.exprList
578580
if (returnExpr != null) {
579-
context.index = index
580-
return if (context.guessTuple())
581-
returnExpr.guessType(context)
582-
else
583-
returnExpr.guessTypeAt(context)
581+
return context.withIndex(index) {
582+
if (context.guessTuple())
583+
returnExpr.guessType(context)
584+
else
585+
returnExpr.guessTypeAt(context)
586+
}
584587
}
585588
}
586589
return Ty.UNKNOWN

src/main/java/com/tang/intellij/lua/search/SearchContext.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ class SearchContext(val project: Project, private val currentFile: PsiFile? = nu
3434
/**
3535
* 用于有多返回值的索引设定
3636
*/
37-
var index: Int = -1
37+
val index: Int get() = myIndex
3838

39-
fun resetIndex() {
40-
index = -1
39+
private var myIndex: Int = -1
40+
41+
fun <T> withIndex(index: Int, action: () -> T): T {
42+
val savedIndex = this.index
43+
myIndex = index
44+
val ret = action()
45+
myIndex = savedIndex
46+
return ret
4147
}
4248

4349
fun guessTuple() = index < 0

src/main/java/com/tang/intellij/lua/ty/Declarations.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ private fun LuaNameDef.infer(context: SearchContext): ITy {
136136
val nameList = localDef.nameList
137137
val exprList = localDef.exprList
138138
if (nameList != null && exprList != null) {
139-
context.index = localDef.getIndexFor(this)
140-
type = exprList.guessTypeAt(context)
139+
type = context.withIndex(localDef.getIndexFor(this)) {
140+
exprList.guessTypeAt(context)
141+
}
141142
}
142143
}
143144

src/main/java/com/tang/intellij/lua/ty/Expressions.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,9 @@ private fun getType(context: SearchContext, def: PsiElement): ITy {
254254
if (stat != null) {
255255
val exprList = stat.valueExprList
256256
if (exprList != null) {
257-
context.index = stat.getIndexFor(def)
258-
type = exprList.guessTypeAt(context)
257+
type = context.withIndex(stat.getIndexFor(def)) {
258+
exprList.guessTypeAt(context)
259+
}
259260
}
260261
}
261262
}
@@ -323,10 +324,7 @@ private fun LuaIndexExpr.infer(context: SearchContext): ITy {
323324
//from other class member
324325
val propName = indexExpr.name
325326
if (propName != null) {
326-
// 查找 parent 定义时不应该影响当前index
327-
val saveIdx = context.index
328327
val prefixType = indexExpr.guessParentType(context)
329-
context.index = saveIdx
330328

331329
prefixType.eachTopClass(Processor {
332330
result = result.union(guessFieldType(propName, it, context))

0 commit comments

Comments
 (0)