Skip to content

Commit d1d2d5a

Browse files
committed
fix #29:解析require函数调用的返回值;用于支持setmetatable
1 parent 79b7b84 commit d1d2d5a

File tree

7 files changed

+42
-15
lines changed

7 files changed

+42
-15
lines changed

luacoderassist-2.0.1-beta.2.rar

-10.6 MB
Binary file not shown.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "LuaCoderAssist",
44
"description": "lua coder assistant in javascript for vscode",
55
"icon": "images/icon.png",
6-
"version": "2.0.1",
6+
"version": "2.0.2",
77
"publisher": "liwangqian",
88
"engines": {
99
"vscode": "^1.25.0"
@@ -252,4 +252,4 @@
252252
"type": "git",
253253
"url": "https://github.com/liwangqian/LuaCoderAssist"
254254
}
255-
}
255+
}

server/lib/engine/completion.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,21 @@ function completionProvider(context) {
9898
}
9999

100100
const filter = item => context.functionOnly && !Is.luaFunction(item.type);
101-
const children = Object.create(null);
102-
def.type.walk(fields => {
103-
for (const name in fields) {
104-
const symbol = fields[name];
105-
if (!children[name]) {
106-
children[name] = symbol;
101+
let children
102+
if (Is.luaModule(def.type)) {
103+
children = def.type.fields;
104+
} else {
105+
children = Object.create(null);
106+
def.type.walk(fields => {
107+
for (const name in fields) {
108+
const symbol = fields[name];
109+
if (!children[name]) {
110+
children[name] = symbol;
111+
}
107112
}
108-
}
113+
});
114+
}
109115

110-
});
111116
return object2Array(def.type.return || children, filter);
112117
}
113118

server/lib/engine/core.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ function analysis(code, uri) {
168168
parent.type = symbol.type; // local xzy; xzy = 1
169169
}
170170
} else {
171+
(currentFunc || theModule).addChild(symbol);
171172
if (moduleType.moduleMode) {
173+
currentScope.push(symbol);
172174
moduleType.set(name, symbol);
173175
} else {
174176
_G.set(name, symbol);

server/lib/engine/definition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function definitionProvider(context) {
6060

6161
for (let i = 1; i < (length - 1); i++) {
6262
const name = names[i];
63-
def = def.type.search(name).value;
63+
def = def.type.search(name, context.range).value;
6464
if (!def || !Is.luaTable(typeOf(def))) {
6565
return [];
6666
}

server/lib/engine/typeof.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function typeOf(symbol) {
5454
}
5555

5656
symbol.type = type;
57-
return symbol.type;
57+
return type;
5858
}
5959

6060
function deduceType(type) {
@@ -111,13 +111,29 @@ function parseCallExpression(node, type) {
111111
return null;
112112
}
113113

114+
const fname = node.base.name;
115+
if (fname === 'require') {
116+
let moduleName = node.arguments[0].value;
117+
let shortPath = moduleName.replace('.', '/');
118+
let mdls = LoadedPackages[moduleName]
119+
// TODO:增加配置项,用来配置搜索路径,然后模拟lua的搜索方法搜索最优匹配模块
120+
for (const uri in mdls) {
121+
if (uri.includes(shortPath)) { // 查找最优匹配,如果存在多个最优匹配,则返回第一个
122+
const ret = mdls[uri].type.return;
123+
return ret && ret.type;
124+
}
125+
}
126+
127+
return null;
128+
}
129+
114130
let R = ftype.returns[type.index || 0];
115131
if (!Is.lazyValue(R.type)) {
116132
return R.type;
117133
}
118134

119135
// 推导调用参数类型,用来支持推导返回值类型
120-
const func_argt = type.node.arguments.map((arg, index) => {
136+
const func_argt = node.arguments.map((arg, index) => {
121137
return { name: ftype.args[index].name, type: parseAstNode(arg, type) };
122138
});
123139

@@ -161,7 +177,7 @@ function parseMemberExpression(node, type) {
161177
return null;
162178
}
163179
const name = names[i];
164-
def = t.search(name).value;
180+
def = t.search(name, node.base.range).value;
165181
}
166182

167183
return typeOf(def);

stdlibs/lua_5_1.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function _new.iterator()
4141
end
4242

4343
function _as.table()
44-
return _table
44+
return {}
4545
end
4646

4747
function _as.number()
@@ -564,6 +564,10 @@ function rawset(table, index, value)
564564
return _as.table(table)
565565
end
566566

567+
function require(fileName)
568+
return _as.table()
569+
end
570+
567571
function select(index, ...)
568572
return _as.any('value') or _as.number('for index is #')
569573
end

0 commit comments

Comments
 (0)