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
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@oceanbase-odc/monaco-plugin-ob",
3-
"version": "1.7.0",
3+
"version": "1.7.1",
44
"description": "",
55
"main": "dist/index.js",
66
"scripts": {

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
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ export default {
157157
statement = sqlDocuments?.statements?.[statementsCount - 1];
158158
}
159159
return {
160-
tables: [],
161-
tableVariables: []
162-
};
163-
160+
tables: [],
161+
tableVariables: []
162+
};
163+
164164
},
165165
getAutoCompletion(text, delimiter, offset): AutoCompletionItems {
166166
const convertMap = {
@@ -271,7 +271,7 @@ export default {
271271
}
272272
return completions;
273273
} else if (result.alterTableStmt) {
274-
console.log('alterTableStmt',result.alterTableStmt);
274+
console.log('alterTableStmt', result.alterTableStmt);
275275
addKeywords();
276276
completions.push({
277277
type: 'allSchemas'
@@ -291,7 +291,7 @@ export default {
291291
return completions;
292292
} else if (result.dropStmt) {
293293
addKeywords();
294-
switch(result.dropStmt.type) {
294+
switch (result.dropStmt.type) {
295295
case "table":
296296
case "view": {
297297
completions.push({
@@ -381,6 +381,42 @@ export default {
381381
break;
382382
}
383383
}
384+
// 如果内层没找到,向外层 Query 查找
385+
if (!tableName) {
386+
const outerQueries: Query[] = [];
387+
queryMap.forEach((query) => {
388+
if (query === tableContext) return;
389+
const [start, stop] = query.location?.range || [];
390+
if (start != null && stop != null && offset - 1 >= start && offset - 1 <= stop) {
391+
outerQueries.push(query);
392+
}
393+
});
394+
// 按范围从小到大排列(最近的外层优先)
395+
outerQueries.sort((a, b) => {
396+
const aSize = (a?.location?.range?.[1] ?? 0) - (a?.location?.range?.[0] ?? 0);
397+
const bSize = (b?.location?.range?.[1] ?? 0) - (b?.location?.range?.[0] ?? 0);
398+
return aSize - bSize;
399+
});
400+
for (const outerQuery of outerQueries) {
401+
for (let fromTable of outerQuery.fromTables) {
402+
let name;
403+
if (fromTable.alias) {
404+
name = fromTable.alias;
405+
} else if (fromTable.tableName) {
406+
name = [fromTable.schemaName, fromTable.tableName]
407+
.filter(Boolean).join('.');
408+
}
409+
if (name === triggerWord) {
410+
isQuery = !!fromTable.query;
411+
tableName = fromTable.tableName;
412+
schemaName = fromTable.schemaName;
413+
break;
414+
}
415+
}
416+
if (tableName) break;
417+
}
418+
}
419+
384420
if (tableName && !isQuery) {
385421
completions = [
386422
{

0 commit comments

Comments
 (0)