Skip to content

Commit 931b47a

Browse files
committed
Return raw query completions as CompletionItem[]
1 parent 7038cae commit 931b47a

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

packages/language-server/src/connection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { NSDatabase, IConnectionDriver, IConnection, MConnectionExplorer, ContextValue, InternalID, IQueryOptions, IRawCompletionItem } from '@sqltools/types';
1+
import { NSDatabase, IConnectionDriver, IConnection, MConnectionExplorer, ContextValue, InternalID, IQueryOptions } from '@sqltools/types';
22
import decorateLSException from '@sqltools/util/decorators/ls-decorate-exception';
33
import { getConnectionId } from '@sqltools/util/connection';
44
import ConfigRO from '@sqltools/util/config-manager';
55
import generateId from '@sqltools/util/internal-id';
66
import LSContext from './context';
7-
import { IConnection as LSIconnection } from 'vscode-languageserver';
7+
import { IConnection as LSIconnection, CompletionItem } from 'vscode-languageserver';
88
import DriverNotInstalledError from './exception/driver-not-installed';
99
import { createLogger } from '@sqltools/log/src';
1010

@@ -178,7 +178,7 @@ export default class Connection {
178178
return this.conn.getStaticCompletions();
179179
}
180180

181-
public getCompletionsForRawQuery(text: string, currentOffset: number): Promise<IRawCompletionItem[] | null> {
181+
public getCompletionsForRawQuery(text: string, currentOffset: number): Promise<CompletionItem[] | null> {
182182
if (typeof this.conn.getCompletionsForRawQuery !== 'function') return Promise.resolve(null);
183183
return this.conn.getCompletionsForRawQuery(text, currentOffset);
184184
}

packages/plugins/intellisense/language-server.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default class IntellisensePlugin<T extends ILanguageServer> implements IL
5252
}
5353
}
5454

55-
private getDatabasesCompletions = async ({ currentWord, conn, suggestDatabases }: { conn: Connection; currentWord: string; suggestDatabases: any }) => {
55+
private getDatabasesCompletions = async ({ currentWord, conn, suggestDatabases }: { conn: Connection; currentWord: string; suggestDatabases: any }): Promise<CompletionItem[]> => {
5656
const prefix = (suggestDatabases.prependQuestionMark ? "? " : "") + (suggestDatabases.prependFrom ? "FROM " : "");
5757
const suffix = suggestDatabases.appendDot ? "." : "";
5858

@@ -70,7 +70,7 @@ export default class IntellisensePlugin<T extends ILanguageServer> implements IL
7070
return [];
7171
}
7272

73-
private getTableCompletions = async ({ currentWord, conn, suggestTables }: { conn: Connection; currentWord: string; suggestTables: any }) => {
73+
private getTableCompletions = async ({ currentWord, conn, suggestTables }: { conn: Connection; currentWord: string; suggestTables: any }): Promise<CompletionItem[]> => {
7474
const prefix = (suggestTables.prependQuestionMark ? "? " : "") + (suggestTables.prependFrom ? "FROM " : "");
7575
const database = suggestTables.identifierChain && suggestTables.identifierChain[0].name;
7676
const suffix = suggestTables.appendDot ? "." : "";
@@ -89,7 +89,7 @@ export default class IntellisensePlugin<T extends ILanguageServer> implements IL
8989
return [];
9090
}
9191

92-
private getColumnCompletions = async ({ currentWord, conn, suggestColumns }: { conn: Connection; currentWord: string; suggestColumns: any }) => {
92+
private getColumnCompletions = async ({ currentWord, conn, suggestColumns }: { conn: Connection; currentWord: string; suggestColumns: any }): Promise<CompletionItem[]> => {
9393
const tables = suggestColumns.tables
9494
.map(table => table.identifierChain.map(id => id.name || id.cte))
9595
.map((t: [string]) => (<NSDatabase.ITable>{ label: t.pop(), database: t.pop() }));
@@ -107,7 +107,7 @@ export default class IntellisensePlugin<T extends ILanguageServer> implements IL
107107
return [];
108108
}
109109

110-
private getCompletionsFromHueAst = async ({ currentWord, conn, text, currentOffset }: { currentWord: string; conn: Connection | null; text: string; currentOffset: number }) => {
110+
private getCompletionsFromHueAst = async ({ currentWord, conn, text, currentOffset }: { currentWord: string; conn: Connection | null; text: string; currentOffset: number }): Promise<CompletionItem[]> => {
111111
let completionsMap = {
112112
query: [],
113113
tables: [],
@@ -166,7 +166,7 @@ export default class IntellisensePlugin<T extends ILanguageServer> implements IL
166166
.concat(completionsMap.tables)
167167
.concat(completionsMap.dbs)
168168
.concat(completionsMap.query);
169-
169+
170170
return completions;
171171
}
172172

@@ -182,13 +182,12 @@ export default class IntellisensePlugin<T extends ILanguageServer> implements IL
182182
// First try to get completions from connection's getCompletionsForRawQuery method
183183
const connectionCompletions = await conn.getCompletionsForRawQuery(text, currentOffset);
184184
if (connectionCompletions !== null) {
185-
log.debug('using connection completions, count: %d', connectionCompletions.length);
185+
log.debug('Got completions from raw the query, count: %d', connectionCompletions.length);
186186
return connectionCompletions;
187187
}
188-
189188

190189
// Fallback to hue AST-based completions
191-
log.debug('falling back to hue AST completions');
190+
log.debug('Using completions based on hue SQL parser');
192191
const completions = await this.getCompletionsFromHueAst({ currentWord, conn, text, currentOffset });
193192
log.debug('total completions %d', completions.length);
194193
return completions;

packages/types/index.d.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ErrorHandler as LanguageClientErrorHandler, LanguageClient } from 'vscode-languageclient';
2-
import { IConnection as LSIConnection, TextDocuments } from 'vscode-languageserver';
2+
import { IConnection as LSIConnection, TextDocuments, CompletionItem } from 'vscode-languageserver';
33
import { RequestType, RequestType0 } from 'vscode-languageserver-protocol';
44

55
export declare namespace NodeJS {
@@ -207,7 +207,7 @@ export interface IConnection<DriverOptions = any> {
207207
* @memberof IConnection
208208
*/
209209
ssh?: 'Enabled' | 'Disabled';
210-
210+
211211
/**
212212
* SSH connection options. Required when ssh is 'Enabled'
213213
* @type {object}
@@ -220,30 +220,30 @@ export interface IConnection<DriverOptions = any> {
220220
* @memberof IConnection.sshOptions
221221
*/
222222
host: string;
223-
223+
224224
/**
225225
* SSH port
226226
* @type {number}
227227
* @default 22
228228
* @memberof IConnection.sshOptions
229229
*/
230230
port: number;
231-
231+
232232
/**
233233
* SSH username
234234
* @type {string}
235235
* @memberof IConnection.sshOptions
236236
*/
237237
username: string;
238-
238+
239239
/**
240240
* SSH password. You can use option askForPassword to prompt password before connect
241241
* @type {string}
242242
* @default null
243243
* @memberof IConnection.sshOptions
244244
*/
245245
password?: string;
246-
246+
247247
/**
248248
* Path to private key file
249249
* @type {string}
@@ -284,10 +284,6 @@ export interface IQueryOptions {
284284
export interface IConnectionDriverConstructor {
285285
new(credentials: IConnection<any>, getWorkspaceFolders?: LSIConnection['workspace']['getWorkspaceFolders']): IConnectionDriver;
286286
}
287-
export interface IRawCompletionItem {
288-
// TODO add more fields or maybe reuse existing types
289-
label: string;
290-
}
291287
export interface IConnectionDriver {
292288
connection: any;
293289
credentials: IConnection<any>;
@@ -315,7 +311,7 @@ export interface IConnectionDriver {
315311
port: number;
316312
}
317313
): Promise<{ port: number }>;
318-
getCompletionsForRawQuery?(text: string, currentOffset: number): Promise<IRawCompletionItem[]>;
314+
getCompletionsForRawQuery?(text: string, currentOffset: number): Promise<CompletionItem[]>;
319315
}
320316

321317
export declare enum ContextValue {
@@ -752,13 +748,13 @@ export interface ISettings {
752748
*/
753749
useNodeRuntime?: null | boolean | string;
754750

755-
/**
756-
* Disable node runtime detection notifications.
757-
* @default false
758-
* @type {boolean}
759-
* @memberof ISettings
760-
*/
761-
disableNodeDetectNotifications?: boolean;
751+
/**
752+
* Disable node runtime detection notifications.
753+
* @default false
754+
* @type {boolean}
755+
* @memberof ISettings
756+
*/
757+
disableNodeDetectNotifications?: boolean;
762758

763759
/**
764760
* Columns sort order

0 commit comments

Comments
 (0)