Skip to content

Commit

Permalink
add webview for named parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lokesh-couchbase committed May 24, 2024
1 parent 81e1f36 commit c523616
Show file tree
Hide file tree
Showing 10 changed files with 512 additions and 9 deletions.
30 changes: 25 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -478,12 +478,12 @@
},
{
"command": "vscode-couchbase.queryTypeDocumentFilter",
"title": "Add Query Type Document Filter",
"title": "Add Query Filter",
"category": "Couchbase"
},
{
"command": "vscode-couchbase.kvTypeDocumentFilter",
"title": "Add KV Range Type Document Filter",
"title": "Add KV Range Filter",
"category": "Couchbase"
},
{
Expand Down Expand Up @@ -595,6 +595,15 @@
"category": "Couchbase",
"icon": "$(star-empty)"
},
{
"title": "Show Named Parameters List",
"command": "vscode-couchbase.showNamedParameters",
"category": "Couchbase",
"icon": {
"light": "images/light/favorites-list.svg",
"dark": "images/dark/favorites-list.svg"
}
},
{
"title": "Apply Query History",
"command": "vscode-couchbase.applyQueryHistory",
Expand All @@ -619,17 +628,17 @@
"icon": "$(refresh)"
},
{
"title": "Edit Query Type Document Filter",
"title": "Edit Query Filter",
"command": "vscode-couchbase.editQueryTypeDocumentFilter",
"category": "Couchbase"
},
{
"title": "Edit KV Range Type Document Filter",
"title": "Edit KV Range Filter",
"command": "vscode-couchbase.editKvTypeDocumentFilter",
"category": "Couchbase"
},
{
"title": "Clear Document Filter",
"title": "Clear Filters",
"command": "vscode-couchbase.clearDocumentFilter",
"category": "Couchbase"
},
Expand Down Expand Up @@ -705,6 +714,13 @@
"group": "navigation@4",
"when": "(editorLangId == sqlpp || resourceFilename =~ /.sqlpp$/) && !isKVCluster"
},
{
"title": "Show Named Parameters",
"command": "vscode-couchbase.showNamedParameters",
"category": "Couchbase",
"group": "navigation@3",
"when": "(editorLangId == sqlpp || resourceFilename =~ /.sqlpp$/) && !isKVCluster"
},
{
"title": "Refresh Cluster Overview",
"command": "vscode-couchbase.refreshClusterOverview",
Expand Down Expand Up @@ -828,6 +844,10 @@
"command": "vscode-couchbase.markFavoriteQuery",
"when": "false"
},
{
"command": "vscode-couchbase.showNamedParameters",
"when": "false"
},
{
"command": "vscode-couchbase.editQueryTypeDocumentFilter",
"when": "false"
Expand Down
1 change: 1 addition & 0 deletions src/commands/extensionCommands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export namespace Commands {
export const queryContext: string = "vscode-couchbase.queryContext";
export const showFavoriteQueries: string = "vscode-couchbase.showFavoriteQueries";
export const markFavoriteQuery: string = "vscode-couchbase.markFavoriteQuery";
export const showNamedParameters: string = "vscode-couchbase.showNamedParameters";
export const applyQueryHistory: string = "vscode-couchbase.applyQueryHistory";
export const deleteQueryHistoryItem: string = "vscode-couchbase.deleteQueryHistoryItem";
export const copyQueryHistoryItem: string = "vscode-couchbase.copyQueryHistoryItem";
Expand Down
10 changes: 10 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import { secretUpdater } from "./util/secretUpdater";
import { newChatHandler } from "./commands/iq/chat/newChatHandler";
import { SecretService } from "./util/secretService";
import { kvTypeFilterDocuments } from "./commands/documents/documentFilters/kvTypeFilterDocuments";
import { fetchNamedParameters } from "./pages/namedParameters/namedParameters";

export function activate(context: vscode.ExtensionContext) {
Global.setState(context.globalState);
Expand Down Expand Up @@ -596,6 +597,15 @@ export function activate(context: vscode.ExtensionContext) {
)
);

subscriptions.push(
vscode.commands.registerCommand(
Commands.showNamedParameters,
() => {
fetchNamedParameters(context);
}
)
);

subscriptions.push(
vscode.commands.registerCommand(
Commands.applyQueryHistory,
Expand Down
1 change: 0 additions & 1 deletion src/pages/FavoriteQueries/FavoriteQueries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as vscode from 'vscode';
import * as path from 'path';
import { logger } from '../../logger/logger';
import { showFavoriteQueries } from '../../webViews/favoriteQueries.webiew';
import { applyQuery } from '../../commands/queryHistory/applyQuery';
Expand Down
77 changes: 77 additions & 0 deletions src/pages/namedParameters/namedParameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as vscode from 'vscode';
import { logger } from '../../logger/logger';
import { showNamedParameters } from '../../webViews/namedParameters.webview';
import { applyQuery } from '../../commands/queryHistory/applyQuery';
import { Memory, getUUID } from '../../util/util';
import { deleteNamedParameter } from '../../util/namedParameters';
import { IConnection } from '../../types/IConnection';
import { Constants } from '../../util/constants';

export interface INamedParametersWebviewState {
webviewPanel: vscode.WebviewPanel
}

export const fetchNamedParameters = (context: vscode.ExtensionContext) => {
const namedParametersWebviewDetails = Memory.state.get<INamedParametersWebviewState>(Constants.NAMED_PARAMETERS_WEBVIEW);
if (namedParametersWebviewDetails) {
// Named Parameters Webview already exists, Closing existing and creating new
namedParametersWebviewDetails.webviewPanel.dispose();
Memory.state.update(Constants.NAMED_PARAMETERS_WEBVIEW, null);
}
const currentPanel = vscode.window.createWebviewPanel(
"showNamedParameters",
"Named Parameters",
vscode.ViewColumn.Beside,
{
enableScripts: true,
enableForms: true,
retainContextWhenHidden: true,
},
);

try {
currentPanel.webview.html = showNamedParameters();
currentPanel.webview.onDidReceiveMessage(async (message) => {
switch (message.command) {
case 'vscode-couchbase.pasteQuery':
const dataFromWebview = message.query;
const connection = Memory.state.get<IConnection>(Constants.ACTIVE_CONNECTION);
if (!connection) {
vscode.window.showErrorMessage("Please connect to a cluster before pasting a query");
return;
}
currentPanel.dispose();
await new Promise((resolve) => setTimeout(resolve, 200));
applyQuery({ query: dataFromWebview, id: getUUID() });

break;
case 'vscode-couchbase.deleteQuery':
const queryId = message.id;
vscode.window.showWarningMessage(`Are you sure you want to delete the query from named parameters? Query: ${message.query}`,
"Yes",
"No").then((value)=>{
if(value === "Yes"){
deleteNamedParameter(queryId, context);
}
});
break;
case 'vscode-couchbase.queryNotSelected':
vscode.window.showErrorMessage("Please select a query before performing an action");
break;
}
});

currentPanel.onDidDispose(() => {
Memory.state.update(Constants.NAMED_PARAMETERS_WEBVIEW, null);
});

Memory.state.update(Constants.NAMED_PARAMETERS_WEBVIEW, {
webviewPanel: currentPanel
});


} catch (err) {
logger.error(`failed to open and set query context: ${err}`);
logger.debug(err);
}
};
2 changes: 2 additions & 0 deletions src/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export class Constants {
public static connectionKeys = "clusterConnections";
public static QUERY_HISTORY = "cluster.queryHistory";
public static FAVORITE_QUERY = "cluster.favoriteQueries";
public static NAMED_PARAMETER = "cluster.namedParameters";
public static ACTIVE_CONNECTION = "activeConnection";
public static FAVORITE_QUERIES_WEBVIEW = "favoriteQueriesWebview";
public static NAMED_PARAMETERS_WEBVIEW = "namedParametersWebview";
public static CLUSTER_OVERVIEW_WEBVIEW = "clusterOverviewWebview";
public static CLUSTER_OVERVIEW_DATA = "clusterOverviewData";
public static QUERY_CONTEXT_STATUS_BAR = "queryContextStatusBar";
Expand Down
3 changes: 1 addition & 2 deletions src/util/favoriteQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import { fetchFavoriteQueries } from "../pages/FavoriteQueries/FavoriteQueries";
import { favoriteQueryType } from "../types/FavoriteQueryType";
import { IKeyValuePair } from "../types/IKeyValuePair";
import { IQuery } from "../types/IQuery";
import { Constants } from "./constants";
import { Global, Memory } from "./util";
import { Global } from "./util";
import * as vscode from "vscode";

export function getFavoriteQueries(): favoriteQueryType {
Expand Down
76 changes: 76 additions & 0 deletions src/util/namedParameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { fetchNamedParameters } from "../pages/namedParameters/namedParameters";
import { IKeyValuePair } from "../types/IKeyValuePair";
import { Constants } from "./constants";
import { Global } from "./util";
import * as vscode from "vscode";
const fs = require('fs');
const path = require('path');

export function getNamedParameters(): IKeyValuePair[] {
let namedParameters = Global.state.get<IKeyValuePair[]>(Constants.NAMED_PARAMETER);
if (namedParameters === undefined) {
Global.state.update(Constants.NAMED_PARAMETER, []);
return [];
} else {
return namedParameters;
}
}

export function getProjectsNamedParameters(): IKeyValuePair[] {
const workspaceFolders = vscode.workspace.workspaceFolders;
if(workspaceFolders && workspaceFolders.length > 0) {
const rootPath = workspaceFolders[0].uri.fsPath;
const filePath = path.join(rootPath, '.cbNamedParams.properties');
let namedParameters: IKeyValuePair[] = [];
try {
const fileContent = fs.readFileSync(filePath, 'utf-8');
const lines = fileContent.split('\n');

for (let line of lines) {
const [key, value] = line.split('=');
namedParameters.push({ key: key.trim(), value: value.trim() });
}
} catch (error) {
console.error('Error reading .cbNamedParams.properties file:', error);
}
return namedParameters;
} else {
return [];
}
}

export async function saveNamedParameter(newParameter: IKeyValuePair): Promise<IKeyValuePair[]> {
let namedParameters = getNamedParameters();
for (let parameter of namedParameters) {
if (parameter.key === newParameter.key) {
vscode.window.showErrorMessage("Key already exists: please try again with a new key");
return namedParameters;
}
}
namedParameters.push({ key: newParameter.key, value: newParameter.value });
await Global.state.update(Constants.NAMED_PARAMETER, namedParameters);
vscode.window.showInformationMessage('Named Parameter Saved Successfully');
return namedParameters;
}

export async function deleteNamedParameter(key: string, context: vscode.ExtensionContext): Promise<IKeyValuePair[]> {
let namedParameters = getNamedParameters();
let lenOfNamedParameters = namedParameters.length;
let deleted = false;
for (let i = 0; i < lenOfNamedParameters; i++) {
if (namedParameters[i].key === key) {
namedParameters.splice(i, 1);
deleted = true;
break;
}
}
if (!deleted) {
vscode.window.showErrorMessage("No named parameter with the given key exists: Aborting deletion");
return namedParameters;
} else {
await Global.state.update(Constants.NAMED_PARAMETER, namedParameters);
fetchNamedParameters(context);
vscode.window.showInformationMessage("Parameter deleted successfully");
return namedParameters;
}
}
2 changes: 1 addition & 1 deletion src/webViews/favoriteQueries.webiew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getFavoriteQueries } from "../util/favoriteQuery";

export const showFavoriteQueries = (): string => {
let favQueries = getFavoriteQueries();
return `
return /*HTML*/`
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down
Loading

0 comments on commit c523616

Please sign in to comment.