z_mirror/luapls
git clone https://git.raiguard.me/z_mirror/luapls
9e06851
main
1package lsp 2 3import ( 4"encoding/json" 5 6"github.com/raiguard/luapls/lua/ast" 7"github.com/raiguard/luapls/lua/token" 8) 9 10func toJSON (v any )string { 11res ,err := json .Marshal (v ) 12if err != nil { 13return "<ERROR>" 14 } 15return string (res ) 16} 17 18// getLocals returns a list of all local variables contained in node for the given pos. 19func getLocals (node ast.Node ,pos token.Pos ,includeSelf bool )map [string ]* ast.Identifier { 20locals := map [string ]* ast.Identifier {} 21 22ast .WalkSemantic (node ,func (node ast.Node )bool { 23isAfter := node .Pos ()> pos && pos < node .End () 24if isAfter { 25return false 26 } 27isBefore := node .Pos ()<= pos && pos > node .End () 28isInside := node .Pos ()<= pos && pos < node .End () 29switch node := node .(type ) { 30case * ast.ForInStatement : 31if isInside { 32for _ ,ident := range node .Names .Pairs { 33locals [ident .Node .Token .Literal ]= ident .Node 34 } 35 } 36case * ast.ForStatement : 37if isInside { 38if node .Name != nil { 39locals [node .Name .Token .Literal ]= node .Name 40 } 41 } 42case * ast.FunctionExpression : 43if isInside { 44for _ ,ident := range node .Params .Pairs { 45locals [ident .Node .Token .Literal ]= ident .Node 46 } 47 } 48case * ast.FunctionStatement : 49if isInside { 50for _ ,ident := range node .Params .Pairs { 51locals [ident .Node .Token .Literal ]= ident .Node 52 } 53 } 54if isBefore || includeSelf { 55if ident ,ok := node .Name .(* ast.Identifier );ok { 56locals [ident .Token .Literal ]= ident 57 } 58 } 59case * ast.LocalStatement : 60if isBefore || includeSelf { 61for _ ,ident := range node .Names .Pairs { 62locals [ident .Node .Token .Literal ]= ident .Node 63 } 64 } 65default : 66return isInside 67 } 68 69return true 70 }) 71 72return locals 73}