Skip to content

Commit 23f2121

Browse files
committed
Content assist to pickup current library
1 parent dd3ea9b commit 23f2121

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/base.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
const vscode = require(`vscode`);
3+
4+
module.exports = () => {
5+
const baseExtension = (vscode.extensions ? vscode.extensions.getExtension(`halcyontechltd.code-for-ibmi`) : undefined);
6+
return (baseExtension && baseExtension.isActive && baseExtension.exports ? baseExtension.exports.instance : null);
7+
}

src/language/provider.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const vscode = require(`vscode`);
22

33
const { Parser } = require(`node-sql-parser`);
44

5+
const getInstance = require(`../base`);
56
const Store = require(`./store`);
67
const Configuration = require(`../configuration`);
78

@@ -75,6 +76,10 @@ exports.initialise = async (context) => {
7576
///** @type vscode.CompletionItem[] */
7677
const items = [];
7778

79+
const instance = getInstance();
80+
const config = instance ? instance.getConfig() : null;
81+
const currentLibrary = config ? config.currentLibrary : null;
82+
7883
const astList = workingAst[document.uri.path];
7984
astList.forEach((ast) => {
8085
if (ast.from && ast.from.length > 0) {
@@ -86,21 +91,41 @@ exports.initialise = async (context) => {
8691
}
8792
});
8893

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+
89114
return items;
90115
}
91116
}, ` `)
92117
),
93118

94119
vscode.languages.registerCompletionItemProvider({language: `sql` }, {
95120
provideCompletionItems: async (document, position) => {
96-
///** @type vscode.CompletionItem[] */
121+
//** @type vscode.CompletionItem[] */
97122
const items = [];
98123

99124
if (!Store.hasConnection()) return [];
100125

101126
const currentPosition = new vscode.Position(position.line, position.character - 1);
102127
const range = document.getWordRangeAtPosition(currentPosition);
103-
128+
104129
const prefix = range ? document.getText(range) : null;
105130

106131
let fallbackLookup = false;
@@ -117,10 +142,15 @@ exports.initialise = async (context) => {
117142
ast.from.find(f => f.as === prefix) ||
118143
ast.from.find(f => f.table === prefix);
119144

145+
const instance = getInstance();
146+
const config = instance ? instance.getConfig() : null;
147+
const currentLibrary = config ? config.currentLibrary : null;
148+
120149
if (definedAs) {
121150

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);
124154

125155
columns.forEach(column => {
126156
const item = new vscode.CompletionItem(column.COLUMN_NAME.toLowerCase(), vscode.CompletionItemKind.Field);
@@ -145,7 +175,7 @@ exports.initialise = async (context) => {
145175
if (column.COLUMN_TEXT)
146176
docs.push(column.COLUMN_TEXT);
147177

148-
docs.push(`(\`${definedAs.db}.${definedAs.table}\`)`);
178+
docs.push(`(\`${usingSchema}.${definedAs.table}\`)`);
149179

150180
item.documentation = new vscode.MarkdownString(docs.join(`\n\n`));
151181

0 commit comments

Comments
 (0)