Skip to content

Commit 7c8b3c9

Browse files
Merge development to main (#421)
Co-authored-by: Lokesh Goel <113521973+lokesh-couchbase@users.noreply.github.com> Co-authored-by: Lokesh Goel <lokesh.goel@couchbase.com>
1 parent 4e6b81e commit 7c8b3c9

7 files changed

Lines changed: 108 additions & 31 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-couchbase",
33
"displayName": "Couchbase",
44
"description": "",
5-
"version": "2.1.2",
5+
"version": "2.1.4",
66
"engines": {
77
"vscode": "^1.63.1"
88
},

src/extension.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export function activate(context: vscode.ExtensionContext) {
104104
const uriToCasMap = new Map<string, string>();
105105
const workbench = new QueryWorkbench();
106106
const searchWorkbench = new SearchWorkbench();
107+
const queryKernel = new QueryKernel()
107108

108109
let currentSearchIndexNode: SearchIndexNode;
109110
let currentSearchWorkbench: SearchWorkbench;
@@ -725,21 +726,33 @@ context.subscriptions.push(disposable);
725726
)
726727
);
727728

729+
// Query Context for workbench
728730
subscriptions.push(
729731
vscode.commands.registerCommand(Commands.queryContext, () => {
730-
fetchQueryContext(workbench, context, globalStatusBarItem);
732+
fetchQueryContext(workbench, queryKernel, context, globalStatusBarItem);
731733
})
732734
);
733735

734-
// subscription to make sure query context status bar is only visible on sqlpp files
736+
// Subscription to handle changes in active workbench editor
735737
subscriptions.push(
736738
vscode.window.onDidChangeActiveTextEditor(async (editor) => {
737-
await handleQueryContextStatusbar(editor, workbench, globalStatusBarItem);
739+
const activeNotebookEditor = vscode.window.activeNotebookEditor;
740+
await handleQueryContextStatusbar(editor, activeNotebookEditor, workbench, queryKernel, globalStatusBarItem);
738741
})
739742
);
740-
// // Handle initial view of context status bar
743+
744+
// Subscription to handle changes in active notebook editor
745+
subscriptions.push(
746+
vscode.window.onDidChangeActiveNotebookEditor(async (notebookEditor) => {
747+
const activeEditor = vscode.window.activeTextEditor;
748+
await handleQueryContextStatusbar(activeEditor, notebookEditor, workbench, queryKernel, globalStatusBarItem);
749+
})
750+
);
751+
752+
// Handle initial view of context status bar
741753
const activeEditor = vscode.window.activeTextEditor;
742-
handleQueryContextStatusbar(activeEditor, workbench, globalStatusBarItem);
754+
const activeNotebookEditor = vscode.window.activeNotebookEditor;
755+
handleQueryContextStatusbar(activeEditor, activeNotebookEditor, workbench, queryKernel, globalStatusBarItem);
743756

744757
subscriptions.push(
745758
vscode.commands.registerCommand(
@@ -859,7 +872,7 @@ context.subscriptions.push(disposable);
859872
vscode.workspace.registerNotebookSerializer(
860873
Constants.notebookType, new QueryContentSerializer(), { transientOutputs: true }
861874
),
862-
new QueryKernel()
875+
queryKernel
863876
);
864877

865878
context.subscriptions.push(
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import * as vscode from 'vscode';
22
import { QueryWorkbench } from '../workbench/queryWorkbench';
3-
import { showQueryContextStatusbar } from '../util/queryContextUtils';
4-
export const handleQueryContextStatusbar = async (editor: vscode.TextEditor | undefined, workbench: QueryWorkbench, globalStatusBarItem: vscode.StatusBarItem) => {
5-
if( editor && editor.document.languageId === "SQL++"){
6-
// Case 1: Show Status bar
7-
showQueryContextStatusbar(editor, workbench, globalStatusBarItem);
3+
import { showQueryContextStatusbar, showQueryContextStatusbarNotebook } from '../util/queryContextUtils';
4+
import { QueryKernel } from '../notebook/controller';
5+
6+
export async function handleQueryContextStatusbar(
7+
activeEditor: vscode.TextEditor | undefined,
8+
activeNotebookEditor: vscode.NotebookEditor | undefined,
9+
workbench: QueryWorkbench,
10+
queryKernel: QueryKernel,
11+
globalStatusBarItem: vscode.StatusBarItem
12+
) {
13+
if (activeNotebookEditor) {
14+
15+
showQueryContextStatusbarNotebook(activeNotebookEditor, queryKernel, globalStatusBarItem)
16+
17+
} else if (activeEditor && activeEditor.document.languageId === "SQL++") {
18+
showQueryContextStatusbar(activeEditor, workbench, globalStatusBarItem)
19+
820
} else {
9-
// Case 2: Don't show status bar
1021
globalStatusBarItem.hide();
1122
}
12-
};
23+
}

src/notebook/controller.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
import * as vscode from 'vscode';
1818
import { getActiveConnection } from '../util/connections';
19-
import { CouchbaseError } from 'couchbase';
19+
import { CouchbaseError, QueryOptions } from 'couchbase';
20+
import { IQueryContext } from '../types/IQueryContext';
2021

2122
/**
2223
* // This is a typescript class for creating and handling the execution of a new Couchbase Query Notebook,
@@ -33,6 +34,7 @@ export class QueryKernel {
3334

3435
private _executionOrder = 0;
3536
private readonly _controller: vscode.NotebookController;
37+
public notebookToContext: Map<string, IQueryContext>;
3638

3739
// Constructor used to create new QueryKernel objects
3840
constructor() {
@@ -44,6 +46,7 @@ export class QueryKernel {
4446
this._controller.supportedLanguages = this._supportedLanguages;
4547
this._controller.supportsExecutionOrder = true;
4648
this._controller.executeHandler = this._executeAll.bind(this);
49+
this.notebookToContext = new Map<string, IQueryContext>();
4750
}
4851

4952
// Method to release any resources being held, currently it just disposes the notebook controller
@@ -61,21 +64,37 @@ export class QueryKernel {
6164
const execution = this._controller.createNotebookCellExecution(cell);
6265
execution.executionOrder = ++this._executionOrder;
6366
execution.start(Date.now());
67+
let queryOptions: QueryOptions
68+
const activeTextEditor = vscode.window.activeNotebookEditor;
69+
if (activeTextEditor) {
70+
const notebookUri = activeTextEditor.notebook.uri.toString();
6471

72+
// Retrieve the query context using the notebook URI
73+
const queryContext = this.notebookToContext.get(notebookUri);
74+
const queryContextString = queryContext
75+
? `${queryContext.bucketName}.${queryContext.scopeName}`
76+
: '';
77+
78+
queryOptions = {
79+
metrics: true,
80+
queryContext: queryContextString
81+
};
82+
6583
try {
6684
const activeConnection = getActiveConnection();
6785
if (!activeConnection) {
6886
vscode.window.showErrorMessage("Connection Failure: Looks like you are not connected to cluster \nPlease check your cluster connection and try again", { modal: true });
6987
throw Error('Connection Failed');
7088
};
7189
const result = await activeConnection.cluster?.query(
72-
cell.document.getText()
90+
cell.document.getText(), queryOptions
7391
);
7492
execution.replaceOutput([new vscode.NotebookCellOutput([
7593
vscode.NotebookCellOutputItem.json(result?.rows)
7694
])]);
7795

7896
execution.end(true, Date.now());
97+
7998
} catch (err) {
8099
const errorArray = [];
81100
if (err instanceof CouchbaseError) {
@@ -100,4 +119,5 @@ export class QueryKernel {
100119
}
101120
execution.end(false, Date.now());
102121
}
122+
}
103123
}

src/pages/queryContext/queryContext.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import { Bucket, BucketSettings } from "couchbase";
66
import { QueryWorkbench } from "../../workbench/queryWorkbench";
77
import {
88
showQueryContextStatusbar,
9+
showQueryContextStatusbarNotebook,
910
showSearchContextStatusbar,
1011
} from "../../util/queryContextUtils";
1112
import { getActiveConnection } from "../../util/connections";
1213
import { SearchWorkbench } from "../../commands/fts/SearchWorkbench/searchWorkbench";
1314
import SearchIndexNode from "../../model/SearchIndexNode";
1415
import { Commands } from "../../commands/extensionCommands/commands";
16+
import { QueryKernel } from "../../notebook/controller";
1517

1618
const fetchBucketNames = (
1719
bucketsSettings: BucketSettings[] | undefined,
@@ -33,6 +35,7 @@ const fetchBucketNames = (
3335

3436
export async function fetchQueryContext(
3537
workbench: QueryWorkbench,
38+
queryKernel: QueryKernel,
3639
context: vscode.ExtensionContext,
3740
globalStatusBarItem: any,
3841
) {
@@ -47,9 +50,11 @@ export async function fetchQueryContext(
4750
try {
4851
// Fetch active editor
4952
const activeEditor = vscode.window.activeTextEditor;
50-
if (!(activeEditor && activeEditor.document.languageId === "SQL++")) {
53+
const activeNotebookEditor = vscode.window.activeNotebookEditor;
54+
const isNotebook = !!activeNotebookEditor;
55+
if (!activeNotebookEditor && !(activeEditor && activeEditor.document.languageId === "SQL++")) {
5156
vscode.window.showErrorMessage(
52-
"Please ensure that the workbench is open/active",
57+
"Please ensure that a SQL++ editor or a notebook is open and active"
5358
);
5459
return;
5560
}
@@ -96,14 +101,13 @@ export async function fetchQueryContext(
96101

97102
const bucketNameSelected = selectedItem.label;
98103
if (bucketNameSelected === "Clear Context") {
99-
workbench.editorToContext.delete(
100-
activeEditor.document.uri.toString(),
101-
);
102-
showQueryContextStatusbar(
103-
activeEditor,
104-
workbench,
105-
globalStatusBarItem,
106-
);
104+
if (isNotebook && activeNotebookEditor) {
105+
queryKernel.notebookToContext.delete(activeNotebookEditor.notebook.uri.toString());
106+
showQueryContextStatusbarNotebook(activeNotebookEditor, queryKernel, globalStatusBarItem);
107+
} else if (activeEditor) {
108+
workbench.editorToContext.delete(activeEditor.document.uri.toString());
109+
showQueryContextStatusbar(activeEditor, workbench, globalStatusBarItem);
110+
}
107111
return;
108112
}
109113
const scopes = await connection.cluster
@@ -128,11 +132,18 @@ export async function fetchQueryContext(
128132
vscode.window.showInformationMessage("No scope selected.");
129133
return;
130134
}
131-
workbench.editorToContext.set(activeEditor.document.uri.toString(), {
135+
const queryContext = {
132136
bucketName: bucketNameSelected,
133137
scopeName: scopeNameSelected.label,
134-
});
135-
showQueryContextStatusbar(activeEditor, workbench, globalStatusBarItem);
138+
};
139+
if (isNotebook && activeEditor) {
140+
const notebookUri = activeNotebookEditor.notebook.uri.toString();
141+
queryKernel.notebookToContext.set(notebookUri, queryContext);
142+
showQueryContextStatusbarNotebook(activeNotebookEditor, queryKernel, globalStatusBarItem);
143+
} else if (activeEditor) {
144+
workbench.editorToContext.set(activeEditor.document.uri.toString(), queryContext);
145+
showQueryContextStatusbar(activeEditor, workbench, globalStatusBarItem);
146+
}
136147
} catch (err) {
137148
logger.error(`failed to open and set query context: ${err}`);
138149
logger.debug(err);

src/util/queryContextUtils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Constants } from './constants';
55
import { Commands } from '../commands/extensionCommands/commands';
66
import { SearchWorkbench } from '../commands/fts/SearchWorkbench/searchWorkbench';
77
import SearchIndexNode from '../model/SearchIndexNode';
8+
import { QueryKernel } from '../notebook/controller';
89

910
export const showQueryContextStatusbar = async (editor: vscode.TextEditor, workbench: QueryWorkbench, globalStatusBarItem: vscode.StatusBarItem) => {
1011
let queryContext = workbench.editorToContext.get(editor.document.uri.toString());
@@ -26,6 +27,27 @@ export const showQueryContextStatusbar = async (editor: vscode.TextEditor, workb
2627
globalStatusBarItem.show();
2728
};
2829

30+
export const showQueryContextStatusbarNotebook = async (editor: vscode.NotebookEditor, queryKernel: QueryKernel, globalStatusBarItem: vscode.StatusBarItem) => {
31+
const notebookUri = editor.notebook.uri.toString();
32+
const queryContext = queryKernel.notebookToContext.get(notebookUri);
33+
if (!queryContext) {
34+
globalStatusBarItem.text = `$(group-by-ref-type) No Query Context Set`;
35+
} else {
36+
let bucketName = queryContext.bucketName;
37+
if (bucketName.length > 15) {
38+
bucketName = `${bucketName.substring(0, 13)}...`;
39+
}
40+
let scopeName = queryContext.scopeName;
41+
if (scopeName.length > 15) {
42+
scopeName = `${scopeName.substring(0, 13)}...`;
43+
}
44+
globalStatusBarItem.text = `$(group-by-ref-type) ${bucketName} > ${scopeName}`;
45+
}
46+
globalStatusBarItem.command = Commands.queryContext;
47+
globalStatusBarItem.tooltip = "Query Context";
48+
globalStatusBarItem.show();
49+
};
50+
2951

3052
export const showSearchContextStatusbar = async (editor: vscode.TextEditor, searchNode: SearchIndexNode, workbench: SearchWorkbench, globalStatusBarItem: vscode.StatusBarItem) => {
3153
let editorId = editor.document.uri.toString();

0 commit comments

Comments
 (0)