@@ -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