Merge pull request #5 from oceanbase/feat/subQueryAutoComplete · oceanbase/odc-parser-js@efda70a · GitHub
Skip to content

Commit efda70a

Browse files
authored
Merge pull request #5 from oceanbase/feat/subQueryAutoComplete
feat:support subquery autocomplete
2 parents 91edec3 + 0b137eb commit efda70a

4 files changed

Lines changed: 113 additions & 7 deletions

File tree

‎packages/monaco-plugin-ob/package.json‎

Lines changed: 1 addition & 1 deletion

‎packages/monaco-plugin-ob/src/mysql/worker/parser.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,41 @@ export default {
249249
schemaName = fromTable.schemaName;
250250
break;
251251
}
252+
}
253+
// 如果内层没找到,向外层 Query 查找
254+
if (!tableName) {
255+
const outerQueries: Query[] = [];
256+
queryMap.forEach((query) => {
257+
if (query === tableContext) return;
258+
const [start, stop] = query.location?.range || [];
259+
if (start != null && stop != null && offset - 1 >= start && offset - 1 <= stop) {
260+
outerQueries.push(query);
261+
}
262+
});
263+
// 按范围从小到大排列(最近的外层优先)
264+
outerQueries.sort((a, b) => {
265+
const aSize = (a?.location?.range?.[1] ?? 0) - (a?.location?.range?.[0] ?? 0);
266+
const bSize = (b?.location?.range?.[1] ?? 0) - (b?.location?.range?.[0] ?? 0);
267+
return aSize - bSize;
268+
});
269+
for (const outerQuery of outerQueries) {
270+
for (let fromTable of outerQuery.fromTables) {
271+
let name;
272+
if (fromTable.alias) {
273+
name = fromTable.alias;
274+
} else if (fromTable.tableName) {
275+
name = [fromTable.schemaName, fromTable.tableName]
276+
.filter(Boolean).join('.');
277+
}
278+
if (name === triggerWord) {
279+
isQuery = !!fromTable.query;
280+
tableName = fromTable.tableName;
281+
schemaName = fromTable.schemaName;
282+
break;
283+
}
284+
}
285+
if (tableName) break;
286+
}
252287
}
253288
if (tableName && !isQuery) {
254289
completions = [

‎packages/monaco-plugin-ob/src/obmysql/worker/parser.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,41 @@ export default {
357357
break;
358358
}
359359
}
360+
// 如果内层没找到,向外层 Query 查找
361+
if (!tableName) {
362+
const outerQueries: Query[] = [];
363+
queryMap.forEach((query) => {
364+
if (query === tableContext) return;
365+
const [start, stop] = query.location?.range || [];
366+
if (start != null && stop != null && offset - 1 >= start && offset - 1 <= stop) {
367+
outerQueries.push(query);
368+
}
369+
});
370+
// 按范围从小到大排列(最近的外层优先)
371+
outerQueries.sort((a, b) => {
372+
const aSize = (a?.location?.range?.[1] ?? 0) - (a?.location?.range?.[0] ?? 0);
373+
const bSize = (b?.location?.range?.[1] ?? 0) - (b?.location?.range?.[0] ?? 0);
374+
return aSize - bSize;
375+
});
376+
for (const outerQuery of outerQueries) {
377+
for (let fromTable of outerQuery.fromTables) {
378+
let name;
379+
if (fromTable.alias) {
380+
name = fromTable.alias;
381+
} else if (fromTable.tableName) {
382+
name = [fromTable.schemaName, fromTable.tableName]
383+
.filter(Boolean).join('.');
384+
}
385+
if (name === triggerWord) {
386+
isQuery = !!fromTable.query;
387+
tableName = fromTable.tableName;
388+
schemaName = fromTable.schemaName;
389+
break;
390+
}
391+
}
392+
if (tableName) break;
393+
}
394+
}
360395
if (tableName && !isQuery) {
361396
completions = [
362397
{

‎packages/monaco-plugin-ob/src/oboracle/worker/parser.ts‎

Lines changed: 42 additions & 6 deletions

0 commit comments

Comments
 (0)