-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81e1f36
commit c523616
Showing
10 changed files
with
512 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.