Skip to content

Commit ee4f495

Browse files
committed
Improvements for looking up pasted code
Signed-off-by: worksofliam <[email protected]>
1 parent ac4c7d6 commit ee4f495

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

src/language/providers/hoverProvider.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const openProvider = workspace.onDidOpenTextDocument(async (document) =>
2525
if (first.object.name) {
2626
const name = Statement.noQuotes(Statement.delimName(first.object.name, true));
2727
const schema = Statement.noQuotes(Statement.delimName(first.object.schema || defaultSchema, true));
28-
const result = await DbCache.getType(schema, name, `PROCEDURE`);
28+
const result = await DbCache.getRoutine(schema, name, `PROCEDURE`);
2929
if (result) {
3030
await DbCache.getSignaturesFor(schema, name, result.specificNames);
3131
}
@@ -37,7 +37,7 @@ export const openProvider = workspace.onDidOpenTextDocument(async (document) =>
3737
const name = Statement.noQuotes(Statement.delimName(ref.object.name, true));
3838
const schema = Statement.noQuotes(Statement.delimName(ref.object.schema || defaultSchema, true));
3939
if (ref.isUDTF) {
40-
const result = await DbCache.getType(schema, name, `FUNCTION`);
40+
const result = await DbCache.getRoutine(schema, name, `FUNCTION`);
4141
if (result) {
4242
await DbCache.getSignaturesFor(schema, name, result.specificNames);
4343
}
@@ -74,9 +74,11 @@ export const hoverProvider = languages.registerHoverProvider({ language: `sql` }
7474

7575
if (atRef) {
7676
const schema = ref.object.schema || defaultSchema;
77-
const result = lookupSymbol(ref.object.name, schema, possibleNames);
77+
const result = await lookupSymbol(ref.object.name, schema, possibleNames);
7878
if (result) {
7979
addSymbol(md, result);
80+
} else {
81+
8082
}
8183

8284
if (systemSchemas.includes(schema.toUpperCase())) {
@@ -87,7 +89,7 @@ export const hoverProvider = languages.registerHoverProvider({ language: `sql` }
8789

8890
// If no symbol found, check if we can find a symbol by name
8991
if (md.value.length === 0 && tokAt && tokAt.type === `word` && tokAt.value) {
90-
const result = lookupSymbol(tokAt.value, defaultSchema, possibleNames);
92+
const result = await lookupSymbol(tokAt.value, undefined, possibleNames);
9193
if (result) {
9294
addSymbol(md, result);
9395
}
@@ -147,9 +149,9 @@ function addSymbol(base: MarkdownString, symbol: LookupResult) {
147149
}
148150
}
149151

150-
function lookupSymbol(name: string, schema: string, possibleNames: string[]) {
152+
function lookupSymbol(name: string, schema: string|undefined, possibleNames: string[]) {
151153
name = Statement.noQuotes(Statement.delimName(name, true));
152-
schema = Statement.noQuotes(Statement.delimName(schema, true));
154+
schema = schema ? Statement.noQuotes(Statement.delimName(schema, true)) : undefined
153155

154156
return DbCache.lookupSymbol(name, schema, possibleNames);
155157
}

src/language/providers/logic/cache.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,37 @@ export class DbCache {
4040
return false;
4141
}
4242

43-
static lookupSymbol(name: string, schema: string, objectFilter: string[]): LookupResult {
44-
const routine = this.getCachedType(schema, name, `FUNCTION`) || this.getCachedType(schema, name, `PROCEDURE`);
45-
if (routine) {
46-
const signatures = this.getCachedSignatures(schema, name);
47-
return { routine, signatures } as RoutineDetail;
48-
}
49-
50-
objectFilter = objectFilter.map(o => o.toLowerCase());
51-
52-
const included = (name: string) => {
43+
static async lookupSymbol(name: string, schema: string|undefined, objectFilter: string[]): Promise<LookupResult> {
44+
const included = (lookupName: string) => {
5345
if (objectFilter) {
54-
return objectFilter.includes(name.toLowerCase());
46+
return objectFilter.includes(lookupName.toLowerCase());
5547
}
5648
return true;
5749
}
5850

59-
// Search objects
60-
for (const currentSchema of this.schemaObjects.values()) {
61-
const chosenObject = currentSchema.find(sqlObject => included(sqlObject.name) && sqlObject.name === name && sqlObject.schema === schema);
62-
if (chosenObject) {
63-
return chosenObject;
51+
if (schema) {
52+
// Looking routine
53+
const routine = this.getCachedRoutine(schema, name, `FUNCTION`) || this.getCachedRoutine(schema, name, `PROCEDURE`);
54+
if (routine) {
55+
const signatures = this.getCachedSignatures(schema, name);
56+
return { routine, signatures } as RoutineDetail;
57+
}
58+
59+
objectFilter = objectFilter.map(o => o.toLowerCase());
60+
61+
// Search objects
62+
for (const currentSchema of this.schemaObjects.values()) {
63+
const chosenObject = currentSchema.find(sqlObject => included(sqlObject.name) && sqlObject.name === name && sqlObject.schema === schema);
64+
if (chosenObject) {
65+
return chosenObject;
66+
}
67+
}
68+
69+
// Finally, let's do a last lookup
70+
const lookupRoutine = await this.getRoutine(schema, name, `FUNCTION`) || await this.getRoutine(schema, name, `PROCEDURE`);
71+
if (lookupRoutine) {
72+
const signatures = await this.getSignaturesFor(schema, name, lookupRoutine.specificNames);
73+
return { routine: lookupRoutine, signatures } as RoutineDetail;
6474
}
6575
}
6676

@@ -111,7 +121,7 @@ export class DbCache {
111121
return this.schemaObjects.get(key) || [];
112122
}
113123

114-
static async getType(schema: string, name: string, type: CallableType) {
124+
static async getRoutine(schema: string, name: string, type: CallableType) {
115125
const key = getKey(type, schema, name);
116126

117127
if (!this.routines.has(key) || this.shouldReset(name)) {
@@ -127,7 +137,7 @@ export class DbCache {
127137
return this.routines.get(key) || undefined;
128138
}
129139

130-
static getCachedType(schema: string, name: string, type: CallableType) {
140+
static getCachedRoutine(schema: string, name: string, type: CallableType) {
131141
const key = getKey(type, schema, name);
132142
return this.routines.get(key) || undefined
133143
}

src/language/providers/logic/callable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export async function isCallableType(ref: ObjectRef, type: CallableType) {
1414
ref.object.schema = Statement.noQuotes(Statement.delimName(ref.object.schema, true));
1515
ref.object.name = Statement.noQuotes(Statement.delimName(ref.object.name, true));
1616

17-
const callableRoutine = await DbCache.getType(ref.object.schema, ref.object.name, type);
17+
const callableRoutine = await DbCache.getRoutine(ref.object.schema, ref.object.name, type);
1818

1919
if (callableRoutine) {
2020
await DbCache.getSignaturesFor(ref.object.schema, ref.object.name, callableRoutine.specificNames);

0 commit comments

Comments
 (0)