Skip to content

Commit

Permalink
Fix issue with ensuring the webview is activated
Browse files Browse the repository at this point in the history
Signed-off-by: Liam Barry Allan <[email protected]>
  • Loading branch information
worksofliam committed Dec 12, 2022
1 parent 6ede425 commit 5c19b41
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"url": "https://github.com/halcyon-tech/vscode-db2i"
},
"activationEvents": [
"onStartupFinished"
"onStartupFinished",
"onLanguage:sql"
],
"extensionDependencies": [
"halcyontechltd.code-for-ibmi"
Expand Down
23 changes: 20 additions & 3 deletions src/language/results/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const getInstance = require(`../../base`);
const Configuration = require(`../../configuration`);
const html = require(`./html`);

function delay(t, v) {
return new Promise(resolve => setTimeout(resolve, t, v));
}

class ResultSetPanelProvider {
constructor() {
/** @type {vscode.WebviewView} */
Expand Down Expand Up @@ -56,16 +60,25 @@ class ResultSetPanelProvider {
});
}

async ensureActivation() {
let currentLoop = 0;
while (!this._view && currentLoop < 15) {
await this.focus();
await delay(100);
currentLoop += 1;
}
}

async focus() {
if (!this._view) {
// Weird one. Kind of a hack. _view.show doesn't work yet because it's not initialized.
// But, we can call a VS Code API to focus on the tab, which then
// 1. calls resolveWebviewView
// 2. sets this._view
await vscode.commands.executeCommand(`vscode-db2i.resultset.focus`);
} else {
this._view.show(true);
}

this._view.show(true);
}

async setLoadingText(content) {
Expand Down Expand Up @@ -112,7 +125,9 @@ exports.initialise = (context) => {
let resultSetProvider = new ResultSetPanelProvider();

context.subscriptions.push(
vscode.window.registerWebviewViewProvider(`vscode-db2i.resultset`, resultSetProvider),
vscode.window.registerWebviewViewProvider(`vscode-db2i.resultset`, resultSetProvider, {
webviewOptions: {retainContextWhenHidden: true},
}),

vscode.commands.registerCommand(`vscode-db2i.runEditorStatement`,
/**
Expand All @@ -129,6 +144,8 @@ exports.initialise = (context) => {
const editor = vscode.window.activeTextEditor;

if (optionsIsValid || (editor && editor.document.languageId === `sql`)) {
await resultSetProvider.ensureActivation();

/** @type {StatementInfo} */
const statement = (optionsIsValid ? options : this.parseStatement(editor));

Expand Down

0 comments on commit 5c19b41

Please sign in to comment.