Skip to content

Commit b654ec0

Browse files
committed
Initial work for peek for objects
Signed-off-by: worksofliam <[email protected]>
1 parent d254ba3 commit b654ec0

File tree

4 files changed

+96
-20
lines changed

4 files changed

+96
-20
lines changed

src/database/schemas.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
import { spec } from "node:test/reporters";
23
import { getInstance } from "../base";
34

45
import { JobManager } from "../config";
@@ -15,6 +16,22 @@ const typeMap = {
1516

1617
export const AllSQLTypes: SQLType[] = ["tables", "views", "aliases", "constraints", "functions", "variables", "indexes", "procedures", "sequences", "packages", "triggers", "types", "logicals"];
1718

19+
export const InternalTypes: {[t: string]: string} = {
20+
"tables": `table`,
21+
"views": `view`,
22+
"aliases": `alias`,
23+
"constraints": `constraint`,
24+
"functions": `function`,
25+
"variables": `variable`,
26+
"indexes": `index`,
27+
"procedures": `procedure`,
28+
"sequences": `sequence`,
29+
"packages": `package`,
30+
"triggers": `trigger`,
31+
"types": `type`,
32+
"logicals": `logical`
33+
}
34+
1835
export const SQL_ESCAPE_CHAR = `\\`;
1936

2037
type BasicColumnType = string|number;
@@ -211,7 +228,7 @@ export default class Schemas {
211228
schema: object.BASE_SCHEMA || undefined,
212229
name: object.BASE_OBJ || undefined
213230
}
214-
}));
231+
} as BasicSQLObject));
215232
}
216233

217234
/**

src/language/providers/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { completionProvider } from "./completionProvider";
22
import { formatProvider } from "./formatProvider";
33
import { hoverProvider, openProvider } from "./hoverProvider";
44
import { signatureProvider } from "./parameterProvider";
5+
import { peekProvider } from "./peekProvider";
56

67
export function languageInit() {
78
let functionality = [];
@@ -11,7 +12,8 @@ export function languageInit() {
1112
formatProvider,
1213
signatureProvider,
1314
hoverProvider,
14-
openProvider
15+
openProvider,
16+
peekProvider
1517
);
1618

1719
return functionality;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { env, Hover, languages, MarkdownString, workspace } from "vscode";
2+
import { getSqlDocument } from "./logic/parse";
3+
import { DbCache, LookupResult, RoutineDetail } from "./logic/cache";
4+
import { JobManager } from "../../config";
5+
import Statement from "../../database/statement";
6+
import { getParmAttributes, prepareParamType } from "./logic/completion";
7+
import { StatementType } from "../sql/types";
8+
import { remoteAssistIsEnabled } from "./logic/available";
9+
import { getPositionData } from "./logic/callable";
10+
import { CallableSignature } from "../../database/callable";
11+
import Schemas, { AllSQLTypes, InternalTypes, SQLType } from "../../database/schemas";
12+
13+
const standardObjects: SQLType[] = AllSQLTypes.filter(type => ![`functions`, `procedures`].includes(type));
14+
15+
export const peekProvider = languages.registerDefinitionProvider({ language: `sql` }, {
16+
async provideDefinition(document, position, token) {
17+
if (!remoteAssistIsEnabled()) return;
18+
console.log(`peekProvider`);
19+
20+
const defaultSchema = getDefaultSchema();
21+
const sqlDoc = getSqlDocument(document);
22+
const offset = document.offsetAt(position);
23+
24+
const tokAt = sqlDoc.getTokenByOffset(offset);
25+
const statementAt = sqlDoc.getStatementByOffset(offset);
26+
27+
if (statementAt) {
28+
const refs = statementAt.getObjectReferences();
29+
const possibleNames = refs.map(ref => ref.object.name).filter(name => name);
30+
31+
const ref = refs.find(ref => ref.tokens[0].range.start && offset <= ref.tokens[ref.tokens.length - 1].range.end);
32+
33+
if (ref) {
34+
const name = Statement.noQuotes(Statement.delimName(ref.object.name, true));
35+
const schema = Statement.noQuotes(Statement.delimName(ref.object.schema || defaultSchema, true));
36+
37+
let types: SQLType[] = standardObjects;
38+
39+
if (ref.isUDTF) {
40+
types = [`functions`];
41+
} else if (statementAt.type === StatementType.Call) {
42+
types = [`procedures`];
43+
}
44+
45+
const possibleObjects = await Schemas.getObjects(schema, types, {filter: name});
46+
47+
if (possibleObjects.length) {
48+
const lines: string[] = [];
49+
for (const obj of possibleObjects) {
50+
const type = InternalTypes[obj.type];
51+
if (type) {
52+
const contents = await Schemas.generateSQL(obj.schema, obj.name, type.toUpperCase());
53+
lines.push(contents, ``, ``);
54+
}
55+
}
56+
57+
const document = await workspace.openTextDocument({ content: lines.join(`\n`), language: `sql` });
58+
59+
return {
60+
uri: document.uri,
61+
range: document.lineAt(0).range,
62+
};
63+
}
64+
65+
}
66+
}
67+
}
68+
});
69+
70+
const getDefaultSchema = (): string => {
71+
const currentJob = JobManager.getSelection();
72+
return currentJob && currentJob.job.options.libraries[0] ? currentJob.job.options.libraries[0] : `QGPL`;
73+
}

src/views/schemaBrowser/index.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import { ThemeIcon, TreeItem } from "vscode"
33
import * as vscode from "vscode"
4-
import Schemas, { AllSQLTypes, SQL_ESCAPE_CHAR, SQLType } from "../../database/schemas";
4+
import Schemas, { AllSQLTypes, InternalTypes, SQL_ESCAPE_CHAR, SQLType } from "../../database/schemas";
55
import Table from "../../database/table";
66
import { getInstance, loadBase } from "../../base";
77

@@ -12,22 +12,6 @@ import Statement from "../../database/statement";
1212
import { copyUI } from "./copyUI";
1313
import { getAdvisedIndexesStatement, getIndexesStatement, getMTIStatement } from "./statements";
1414

15-
const viewItem = {
16-
"tables": `table`,
17-
"views": `view`,
18-
"aliases": `alias`,
19-
"constraints": `constraint`,
20-
"functions": `function`,
21-
"variables": `variable`,
22-
"indexes": `index`,
23-
"procedures": `procedure`,
24-
"sequences": `sequence`,
25-
"packages": `package`,
26-
"triggers": `trigger`,
27-
"types": `type`,
28-
"logicals": `logical`
29-
}
30-
3115
const itemIcons = {
3216
"table": `split-horizontal`,
3317
"procedure": `run`,
@@ -552,7 +536,7 @@ class SQLObject extends vscode.TreeItem {
552536
}
553537

554538
constructor(item: BasicSQLObject) {
555-
const type = viewItem[item.type];
539+
const type = InternalTypes[item.type];
556540
super(Statement.prettyName(item.name), Types[type] ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None);
557541

558542
this.contextValue = type;

0 commit comments

Comments
 (0)