Skip to content

Commit

Permalink
当方法里 local 与 self 同名时,修正 self 指向正确的位置,通过 scope 寻找合适的 variable 位置
Browse files Browse the repository at this point in the history
  • Loading branch information
lalawue committed Jul 3, 2022
1 parent fbbb47c commit 371ad82
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
13 changes: 1 addition & 12 deletions luahelper-lsp/langserver/check/check_lsp_define.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,18 +304,7 @@ func (a *AllProject) getVarCommonFuncParam(strFile string, varStruct *common.Def
}

// 5)冒号 函数,self语法进行转换
if common.ChangeFuncSelfToReferVar(minFunc, varStruct) {
minFunc = minFunc.GetParent()
// 由于 moocscript 的 class/struct AST 是拼凑的,而不是标准的 table:function() 格式,
// 这里特别处理一下
loc := minFunc.Loc
clsScope := a.findClassStructScopeInfo(minScope)
if clsScope != nil && clsScope.Parent != nil {
loc = clsScope.Parent.LocVarMap[varStruct.StrVec[0]].GetLastOneVar().Loc
}
varStruct.PosLine = loc.StartLine
varStruct.PosCh = loc.StartColumn
}
common.ChangeFuncSelfToReferVar(minFunc, varStruct)

// 6) 判断是否找的在table的定义处, 如果是不缺前面的定义
if len(varStruct.StrVec) == 1 && !varStruct.BracketsFlag {
Expand Down
18 changes: 11 additions & 7 deletions luahelper-lsp/langserver/check/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,21 +1030,21 @@ func GetExpLoc(node ast.Exp) (loc lexer.Location) {
// function a:test1()
// self.b = 3 -- 传人的为self.b
// end
func ChangeFuncSelfToReferVar(fi *FuncInfo, varStruct *DefineVarStruct) bool {
func ChangeFuncSelfToReferVar(fi *FuncInfo, varStruct *DefineVarStruct) {
firstColonFunc := fi.FindFirstColonFunc()
if firstColonFunc == nil {
return false
return
}
if !firstColonFunc.IsColon {
return false
return
}

if firstColonFunc.RelateVar == nil {
return false
return
}

if len(varStruct.StrVec) < 1 {
return false
return
}

if IsSelf(fi.FileName, varStruct.StrVec[0]) {
Expand All @@ -1057,9 +1057,13 @@ func ChangeFuncSelfToReferVar(fi *FuncInfo, varStruct *DefineVarStruct) bool {
strArray = append(strArray, varStruct.StrVec[1:]...)
varStruct.StrVec = strArray
}
return true
// 通过 scope 寻找 table name 定义的位置,肯定在 ':' 函数之前
relateVar, _ := firstColonFunc.MainScope.FindLocVar(varStruct.StrVec[0], firstColonFunc.Loc)
if relateVar != nil {
varStruct.PosLine = relateVar.Loc.StartLine
varStruct.PosCh = relateVar.Loc.StartColumn
}
}
return false
}

// ChangeSelfToVarComplete 冒号 函数,self语法进行转换
Expand Down

0 comments on commit 371ad82

Please sign in to comment.