@@ -2,6 +2,7 @@ const vscode = require(`vscode`);
2
2
3
3
const { Parser } = require ( `node-sql-parser` ) ;
4
4
5
+ const getInstance = require ( `../base` ) ;
5
6
const Store = require ( `./store` ) ;
6
7
const Configuration = require ( `../configuration` ) ;
7
8
@@ -75,6 +76,10 @@ exports.initialise = async (context) => {
75
76
///** @type vscode.CompletionItem[] */
76
77
const items = [ ] ;
77
78
79
+ const instance = getInstance ( ) ;
80
+ const config = instance ? instance . getConfig ( ) : null ;
81
+ const currentLibrary = config ? config . currentLibrary : null ;
82
+
78
83
const astList = workingAst [ document . uri . path ] ;
79
84
astList . forEach ( ( ast ) => {
80
85
if ( ast . from && ast . from . length > 0 ) {
@@ -86,21 +91,41 @@ exports.initialise = async (context) => {
86
91
}
87
92
} ) ;
88
93
94
+ if ( currentLibrary ) {
95
+ const objects = await Store . getObjects ( currentLibrary ) ;
96
+
97
+ objects . forEach ( object => {
98
+ let type ;
99
+
100
+ switch ( object . TABLE_TYPE ) {
101
+ case `T` : type = `Table` ; break ;
102
+ case `V` : type = `View` ; break ;
103
+ case `P` : type = `Table` ; break ;
104
+ }
105
+
106
+ const item = new vscode . CompletionItem ( object . TABLE_NAME . toLowerCase ( ) , vscode . CompletionItemKind . Struct ) ;
107
+ item . insertText = new vscode . SnippetString ( object . TABLE_NAME . toLowerCase ( ) ) ;
108
+ item . detail = type ;
109
+ item . documentation = object . TABLE_TEXT ;
110
+ items . push ( item ) ;
111
+ } ) ;
112
+ }
113
+
89
114
return items ;
90
115
}
91
116
} , ` ` )
92
117
) ,
93
118
94
119
vscode . languages . registerCompletionItemProvider ( { language : `sql` } , {
95
120
provideCompletionItems : async ( document , position ) => {
96
- /// ** @type vscode.CompletionItem[] */
121
+ //** @type vscode.CompletionItem[] */
97
122
const items = [ ] ;
98
123
99
124
if ( ! Store . hasConnection ( ) ) return [ ] ;
100
125
101
126
const currentPosition = new vscode . Position ( position . line , position . character - 1 ) ;
102
127
const range = document . getWordRangeAtPosition ( currentPosition ) ;
103
-
128
+
104
129
const prefix = range ? document . getText ( range ) : null ;
105
130
106
131
let fallbackLookup = false ;
@@ -117,10 +142,15 @@ exports.initialise = async (context) => {
117
142
ast . from . find ( f => f . as === prefix ) ||
118
143
ast . from . find ( f => f . table === prefix ) ;
119
144
145
+ const instance = getInstance ( ) ;
146
+ const config = instance ? instance . getConfig ( ) : null ;
147
+ const currentLibrary = config ? config . currentLibrary : null ;
148
+
120
149
if ( definedAs ) {
121
150
122
- if ( definedAs . db ) {
123
- const columns = await Store . getColumns ( definedAs . db , definedAs . table ) ;
151
+ if ( definedAs . db || definedAs . as ) {
152
+ const usingSchema = definedAs . db || currentLibrary ;
153
+ const columns = await Store . getColumns ( usingSchema , definedAs . table ) ;
124
154
125
155
columns . forEach ( column => {
126
156
const item = new vscode . CompletionItem ( column . COLUMN_NAME . toLowerCase ( ) , vscode . CompletionItemKind . Field ) ;
@@ -145,7 +175,7 @@ exports.initialise = async (context) => {
145
175
if ( column . COLUMN_TEXT )
146
176
docs . push ( column . COLUMN_TEXT ) ;
147
177
148
- docs . push ( `(\`${ definedAs . db } .${ definedAs . table } \`)` ) ;
178
+ docs . push ( `(\`${ usingSchema } .${ definedAs . table } \`)` ) ;
149
179
150
180
item . documentation = new vscode . MarkdownString ( docs . join ( `\n\n` ) ) ;
151
181
0 commit comments