@@ -33,6 +33,7 @@ import com.tang.intellij.lua.comment.psi.LuaDocTagVararg
3333import com.tang.intellij.lua.comment.psi.api.LuaComment
3434import com.tang.intellij.lua.lang.LuaIcons
3535import com.tang.intellij.lua.lang.type.LuaString
36+ import com.tang.intellij.lua.psi.impl.*
3637import com.tang.intellij.lua.search.SearchContext
3738import com.tang.intellij.lua.stubs.LuaClassMemberStub
3839import com.tang.intellij.lua.stubs.LuaFuncBodyOwnerStub
@@ -188,26 +189,137 @@ fun guessParentType(callExpr: LuaCallExpr, context: SearchContext): ITy {
188189 return callExpr.expr.guessType(context)
189190}
190191
192+ fun getStringValue (valueExpr : PsiElement ): String {
193+ if (valueExpr is LuaLiteralExprImpl )
194+ {
195+ return valueExpr.stringValue
196+ }
197+ else
198+ {
199+ if (valueExpr is LuaNameExpr )
200+ {
201+ val declaration = resolve(valueExpr as LuaNameExpr , SearchContext .get(valueExpr.project))
202+ if (declaration is LuaNameExprImpl )
203+ {
204+ return declaration.text
205+ }
206+ else if (declaration is LuaNameDefImpl )
207+ {
208+ val exp = declaration?.parent?.parent?.lastChild?.lastChild
209+ if (exp != null )
210+ {
211+ return getStringValue(exp)
212+ }
213+ }
214+ }
215+ else if (valueExpr is LuaIndexExpr )
216+ {
217+ val declaration = resolve(valueExpr as LuaIndexExpr , SearchContext .get(valueExpr.project))
218+ if (declaration is LuaTableFieldImpl )
219+ {
220+ val strExp = declaration?.lastChild
221+ if (strExp != null )
222+ {
223+ return getStringValue(strExp)
224+ }
225+ }
226+ else if (declaration is LuaIndexExprImpl )
227+ {
228+ val exp = declaration?.parent?.parent?.lastChild?.lastChild
229+ if (exp != null )
230+ {
231+ return getStringValue(exp)
232+ }
233+ }
234+ }
235+ }
236+
237+ return " " ;
238+ }
239+
240+ fun getParamStringValue (valueExpr : PsiElement ): String {
241+ if (valueExpr is LuaNameExpr )
242+ {
243+ return valueExpr.text;
244+ }
245+ else if (valueExpr is LuaIndexExpr )
246+ {
247+ return valueExpr.lastChild.text;
248+ }
249+ return " " ;
250+ }
251+
252+ fun getParamAllStringValue (valueExpr : PsiElement ): String {
253+ if (valueExpr is LuaNameExpr )
254+ {
255+ return valueExpr.text;
256+ }
257+ else if (valueExpr is LuaIndexExpr )
258+ {
259+ return valueExpr.text;
260+ }
261+ return " " ;
262+ }
263+
264+ /* *
265+ * 获取第n个字符串参数
266+ * @param callExpr callExpr
267+ * *
268+ * @return String PsiElement
269+ */
270+ fun getStringArgByIndex (callExpr : LuaCallExpr , index : Int ): PsiElement ? {
271+ val args = callExpr.args
272+ var path: PsiElement ? = null
273+
274+ when (args) {
275+ is LuaSingleArg -> {
276+ val expr = args.expr
277+ if (expr is LuaLiteralExpr && index == 0 ) path = expr
278+ }
279+ is LuaListArgs -> args.exprList.let { list ->
280+ if (list.isNotEmpty() && list.size > index) {
281+ if (list[index] is LuaLiteralExpr ) {
282+ val valueExpr = list[index] as LuaLiteralExpr
283+ if (valueExpr.kind == LuaLiteralKind .String )
284+ path = valueExpr
285+ }
286+ else {
287+ val context = SearchContext .get(callExpr.project)
288+ if (list[index].guessType((context)).displayName == " string" )
289+ {
290+ path = list[index]
291+ }
292+ }
293+ }
294+ }
295+ }
296+ return path
297+ }
298+
191299/* *
192- * 获取第一个字符串参数
300+ * 获取第n个参数的名字
193301 * @param callExpr callExpr
194302 * *
195303 * @return String PsiElement
196304 */
197- fun getFirstStringArg (callExpr : LuaCallExpr ): PsiElement ? {
305+ fun getParamNameByIndex (callExpr : LuaCallExpr , index : Int ): PsiElement ? {
198306 val args = callExpr.args
199307 var path: PsiElement ? = null
200308
201309 when (args) {
202310 is LuaSingleArg -> {
203311 val expr = args.expr
204- if (expr is LuaLiteralExpr ) path = expr
312+ if (expr is LuaNameExpr && index == 0 ) path = expr
313+ if (expr is LuaIndexExpr && index == 0 ) path = expr
205314 }
206315 is LuaListArgs -> args.exprList.let { list ->
207- if (list.isNotEmpty() && list[0 ] is LuaLiteralExpr ) {
208- val valueExpr = list[0 ] as LuaLiteralExpr
209- if (valueExpr.kind == LuaLiteralKind .String )
210- path = valueExpr
316+ if (list.isNotEmpty() && list.size > index) {
317+ if (list[index] is LuaNameExpr ) {
318+ path = list[index]
319+ }
320+ else if (list[index] is LuaIndexExpr ) {
321+ path = list[index]
322+ }
211323 }
212324 }
213325 }
@@ -257,6 +369,54 @@ fun guessTypeAt(list: LuaExprList, context: SearchContext): ITy {
257369 return Ty .UNKNOWN
258370}
259371
372+ fun getStringValue (typeName : LuaLiteralExpr ): String {
373+ return typeName.stringValue;
374+ }
375+
376+ fun getNameExprStringValue (valueExpr : PsiElement ): String {
377+ val tree = LuaDeclarationTree .get(valueExpr.containingFile)
378+ val declaration = tree.find(valueExpr as LuaExpr )?.firstDeclaration?.psi
379+ val exp = declaration?.parent?.parent
380+ if (exp != null )
381+ {
382+ val strExp = exp.lastChild.lastChild
383+ if (strExp is LuaLiteralExprImpl )
384+ {
385+ val str = strExp.text
386+ return str.substring(1 , str.length - 1 )
387+ }
388+ else
389+ {
390+ return getNameExprStringValue(strExp)
391+ }
392+
393+ }
394+ return " " ;
395+ }
396+
397+ fun newType (typeName : String , ty : ITy , sourceStr : String , targetStr : String ): ITy {
398+ if (ty is TyArray ) {
399+ val t = typeName.substringBefore(' [' ).trim()
400+ val ty = TyLazyClass (t)
401+ return TyArray (ty);
402+ }
403+ else if (ty is TySerializedGeneric ){
404+ val list = mutableListOf<ITy >();
405+ ty.params.forEach {
406+ var name = it.displayName
407+ if (name.contains(sourceStr))
408+ {
409+ name = name.replace(sourceStr, targetStr)
410+ }
411+ list.add(newType(name, it, sourceStr, targetStr))
412+ }
413+ return TySerializedGeneric (list.toTypedArray(), ty.base)
414+ }
415+ else {
416+ return TyLazyClass (typeName);
417+ }
418+ }
419+
260420fun guessParentType (indexExpr : LuaIndexExpr , context : SearchContext ): ITy {
261421 val expr = PsiTreeUtil .getStubChildOfType(indexExpr, LuaExpr ::class .java)
262422 return expr?.guessType(context) ? : Ty .UNKNOWN
0 commit comments