Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go to Definition support #278

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/webpack_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '👋 A new build is available for this PR based on ${{ github.event.pull_request.head.sha }}.\n * [Download here.](https://github.com/codefori/vscode-ibmi/actions/runs/${{ github.run_id }})\n* [Read more about how to test](https://github.com/codefori/vscode-ibmi/blob/master/.github/pr_testing_template.md)'
body: '👋 A new build is available for this PR based on ${{ github.event.pull_request.head.sha }}.\n * [Download here.](https://github.com/codefori/vscode-db2i/actions/runs/${{ github.run_id }})\n* [Read more about how to test](https://github.com/codefori/vscode-db2i/blob/master/.github/pr_testing_template.md)'
})

- name: Update comment
Expand All @@ -69,5 +69,5 @@ jobs:
body: |
👋 A new build is available for this PR based on ${{ github.event.pull_request.head.sha }}.

* [Download here.](https://github.com/codefori/vscode-ibmi/actions/runs/${{ github.run_id }})
* [Download here.](https://github.com/codefori/vscode-db2i/actions/runs/${{ github.run_id }})
* [Read more about how to test](https://github.com/codefori/vscode-ibmi/blob/master/.github/pr_testing_template.md)
42 changes: 37 additions & 5 deletions src/database/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ const typeMap = {

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

export const InternalTypes: {[t: string]: string} = {
"tables": `table`,
"views": `view`,
"aliases": `alias`,
"constraints": `constraint`,
"functions": `function`,
"variables": `variable`,
"indexes": `index`,
"procedures": `procedure`,
"sequences": `sequence`,
"packages": `package`,
"triggers": `trigger`,
"types": `type`,
"logicals": `logical`
}

export const SQL_ESCAPE_CHAR = `\\`;

type BasicColumnType = string|number;
Expand Down Expand Up @@ -211,17 +227,33 @@ export default class Schemas {
schema: object.BASE_SCHEMA || undefined,
name: object.BASE_OBJ || undefined
}
}));
} as BasicSQLObject));
}

/**
* @param schema Not user input
* @param object Not user input
*/
static async generateSQL(schema: string, object: string, internalType: string): Promise<string> {
const lines = await JobManager.runSQL<{ SRCDTA: string }>([
`CALL QSYS2.GENERATE_SQL(?, ?, ?, CREATE_OR_REPLACE_OPTION => '1', PRIVILEGES_OPTION => '0')`
].join(` `), { parameters: [object, schema, internalType] });
static async generateSQL(schema: string, object: string, internalType: string, basic?: boolean): Promise<string> {
let statement = `CALL QSYS2.GENERATE_SQL(?, ?, ?, CREATE_OR_REPLACE_OPTION => '1', PRIVILEGES_OPTION => '0')`;

if (basic) {
let options: string[] = [
`CREATE_OR_REPLACE_OPTION => '0'`,
`PRIVILEGES_OPTION => '0'`,
`COMMENT_OPTION => '0'`,
`LABEL_OPTION => '0'`,
`HEADER_OPTION => '0'`,
`TRIGGER_OPTION => '0'`,
`CONSTRAINT_OPTION => '0'`,
// `PRIVILEGES_OPTION => '0'`,
// `ACTIVATE_ACCESS_CONTROL_OPTION => '0'`,
`MASK_AND_PERMISSION_OPTION => '0'`,
];
statement = `CALL QSYS2.GENERATE_SQL(?, ?, ?, ${options.join(`, `)})`;
}

const lines = await JobManager.runSQL<{ SRCDTA: string }>(statement, { parameters: [object, schema, internalType] });

const generatedStatement = lines.map(line => line.SRCDTA).join(`\n`);

Expand Down
4 changes: 3 additions & 1 deletion src/language/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { completionProvider } from "./completionProvider";
import { formatProvider } from "./formatProvider";
import { hoverProvider, openProvider } from "./hoverProvider";
import { signatureProvider } from "./parameterProvider";
import { peekProvider } from "./peekProvider";

export function languageInit() {
let functionality = [];
Expand All @@ -11,7 +12,8 @@ export function languageInit() {
formatProvider,
signatureProvider,
hoverProvider,
openProvider
openProvider,
peekProvider
);

return functionality;
Expand Down
73 changes: 73 additions & 0 deletions src/language/providers/peekProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { env, Hover, languages, MarkdownString, workspace } from "vscode";
import { getSqlDocument } from "./logic/parse";
import { DbCache, LookupResult, RoutineDetail } from "./logic/cache";
import { JobManager } from "../../config";
import Statement from "../../database/statement";
import { getParmAttributes, prepareParamType } from "./logic/completion";
import { StatementType } from "../sql/types";
import { remoteAssistIsEnabled } from "./logic/available";
import { getPositionData } from "./logic/callable";
import { CallableSignature } from "../../database/callable";
import Schemas, { AllSQLTypes, InternalTypes, SQLType } from "../../database/schemas";

const standardObjects: SQLType[] = AllSQLTypes.filter(type => ![`functions`, `procedures`].includes(type));

export const peekProvider = languages.registerDefinitionProvider({ language: `sql` }, {
async provideDefinition(document, position, token) {
if (!remoteAssistIsEnabled()) return;
console.log(`peekProvider`);

const defaultSchema = getDefaultSchema();
const sqlDoc = getSqlDocument(document);
const offset = document.offsetAt(position);

const tokAt = sqlDoc.getTokenByOffset(offset);
const statementAt = sqlDoc.getStatementByOffset(offset);

if (statementAt) {
const refs = statementAt.getObjectReferences();
const possibleNames = refs.map(ref => ref.object.name).filter(name => name);

const ref = refs.find(ref => ref.tokens[0].range.start && offset <= ref.tokens[ref.tokens.length - 1].range.end);

if (ref) {
const name = Statement.noQuotes(Statement.delimName(ref.object.name, true));
const schema = Statement.noQuotes(Statement.delimName(ref.object.schema || defaultSchema, true));

let types: SQLType[] = standardObjects;

if (ref.isUDTF) {
types = [`functions`];
} else if (statementAt.type === StatementType.Call) {
types = [`procedures`];
}

const possibleObjects = await Schemas.getObjects(schema, types, {filter: name});

if (possibleObjects.length) {
const lines: string[] = [`-- Condensed version of the object definition`];
for (const obj of possibleObjects) {
const type = InternalTypes[obj.type];
if (type) {
const contents = await Schemas.generateSQL(obj.schema, obj.name, type.toUpperCase(), true);
lines.push(Statement.format(contents), ``, ``);
}
}

const document = await workspace.openTextDocument({ content: lines.join(`\n`), language: `sql` });

return {
uri: document.uri,
range: document.lineAt(0).range,
};
}

}
}
}
});

const getDefaultSchema = (): string => {
const currentJob = JobManager.getSelection();
return currentJob && currentJob.job.options.libraries[0] ? currentJob.job.options.libraries[0] : `QGPL`;
}
20 changes: 2 additions & 18 deletions src/views/schemaBrowser/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { ThemeIcon, TreeItem } from "vscode"
import * as vscode from "vscode"
import Schemas, { AllSQLTypes, SQL_ESCAPE_CHAR, SQLType } from "../../database/schemas";
import Schemas, { AllSQLTypes, InternalTypes, SQL_ESCAPE_CHAR, SQLType } from "../../database/schemas";
import Table from "../../database/table";
import { getInstance, loadBase } from "../../base";

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

const viewItem = {
"tables": `table`,
"views": `view`,
"aliases": `alias`,
"constraints": `constraint`,
"functions": `function`,
"variables": `variable`,
"indexes": `index`,
"procedures": `procedure`,
"sequences": `sequence`,
"packages": `package`,
"triggers": `trigger`,
"types": `type`,
"logicals": `logical`
}

const itemIcons = {
"table": `split-horizontal`,
"procedure": `run`,
Expand Down Expand Up @@ -552,7 +536,7 @@ class SQLObject extends vscode.TreeItem {
}

constructor(item: BasicSQLObject) {
const type = viewItem[item.type];
const type = InternalTypes[item.type];
super(Statement.prettyName(item.name), Types[type] ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None);

this.contextValue = type;
Expand Down
Loading