diff --git a/vscode-extensions/vscode-spring-boot/lib/live-hover-connect-ui.ts b/vscode-extensions/vscode-spring-boot/lib/live-hover-connect-ui.ts index f9323282d..811cf3b82 100644 --- a/vscode-extensions/vscode-spring-boot/lib/live-hover-connect-ui.ts +++ b/vscode-extensions/vscode-spring-boot/lib/live-hover-connect-ui.ts @@ -2,6 +2,11 @@ import { LanguageClient } from "vscode-languageclient/node"; import { ActivatorOptions } from '@pivotal-tools/commons-vscode'; import { ExtensionContext, QuickPickItem, commands, window } from 'vscode'; +export const CONNECT_CMD = "sts/livedata/connect"; +export const DISCONNECT_CMD = "sts/livedata/disconnect"; +export const REFRESH_CMD = "sts/livedata/refresh"; +export const LIST_CMD = "sts/livedata/listProcesses"; + interface ProcessCommandInfo { processKey : string; label: string; @@ -21,7 +26,7 @@ export interface RemoteBootApp { projectName?: string; } -interface BootAppQuickPick extends QuickPickItem { +export interface BootAppQuickPick extends QuickPickItem { commandInfo: ProcessCommandInfo; } @@ -33,23 +38,22 @@ let state: BootAppState async function liveHoverConnectHandler() { const quickPick = window.createQuickPick(); quickPick.title = 'Searching for running Spring Boot Apps...'; - quickPick.canSelectMany = true; quickPick.canSelectMany = false; quickPick.busy = true; quickPick.show(); - const processData : ProcessCommandInfo[] = await commands.executeCommand('sts/livedata/listProcesses'); + const processData : ProcessCommandInfo[] = await commands.executeCommand(LIST_CMD); const items = processData.map(p => { let actionLabel = ""; switch (p.action) { - case "sts/livedata/connect": + case CONNECT_CMD: actionLabel = "Show" break; - case "sts/livedata/refresh": + case REFRESH_CMD: actionLabel = "Refresh"; break; - case "sts/livedata/disconnect": + case DISCONNECT_CMD: actionLabel = "Hide"; break; } @@ -92,10 +96,10 @@ async function liveHoverConnectHandler() { async function executeLiveProcessAction(commandInfo: ProcessCommandInfo) { if (activeBootApp?.jmxurl === commandInfo.processKey) { switch (commandInfo.action) { - case "sts/livedata/connect": + case CONNECT_CMD: await commands.executeCommand('vscode-spring-boot.live.show.active'); break; - case "sts/livedata/disconnect": + case DISCONNECT_CMD: await commands.executeCommand('vscode-spring-boot.live.hide.active'); break; default: @@ -144,7 +148,7 @@ export function activate( commands.registerCommand("vscode-spring-boot.live.show.active", async () => { try { updateBootAppState("connecting"); - await commands.executeCommand('sts/livedata/connect', { + await commands.executeCommand(CONNECT_CMD, { processKey: activeBootApp.jmxurl }); updateBootAppState("connected"); @@ -155,7 +159,7 @@ export function activate( }), commands.registerCommand("vscode-spring-boot.live.refresh.active", async () => { - await commands.executeCommand('sts/livedata/refresh', { + await commands.executeCommand(REFRESH_CMD, { processKey: activeBootApp.jmxurl }); }), @@ -163,7 +167,7 @@ export function activate( commands.registerCommand("vscode-spring-boot.live.hide.active", async () => { try { updateBootAppState("disconnecting"); - await commands.executeCommand('sts/livedata/disconnect', { + await commands.executeCommand(DISCONNECT_CMD, { processKey: activeBootApp.jmxurl }); updateBootAppState("disconnected"); diff --git a/vscode-extensions/vscode-spring-boot/lib/set-log-levels-ui.ts b/vscode-extensions/vscode-spring-boot/lib/set-log-levels-ui.ts index a37fd0819..4af8e76a9 100644 --- a/vscode-extensions/vscode-spring-boot/lib/set-log-levels-ui.ts +++ b/vscode-extensions/vscode-spring-boot/lib/set-log-levels-ui.ts @@ -1,9 +1,8 @@ -'use strict'; - -import * as VSCode from 'vscode'; import { LanguageClient } from "vscode-languageclient/node"; import { ActivatorOptions } from '@pivotal-tools/commons-vscode'; -import { LiveProcess, LiveProcessLogLevelUpdatedNotification, LiveProcessUpdatedLogLevel } from './notification'; +import { LiveProcessLogLevelUpdatedNotification, LiveProcessUpdatedLogLevel } from './notification'; +import { BootAppQuickPick, CONNECT_CMD, DISCONNECT_CMD, LIST_CMD } from './live-hover-connect-ui'; +import { ExtensionContext, ProgressLocation, commands, window } from "vscode"; interface ProcessCommandInfo { processKey : string; @@ -39,44 +38,57 @@ export interface LoggerItem { } async function setLogLevelHandler() { - - const processData : ProcessCommandInfo[] = await VSCode.commands.executeCommand('sts/livedata/listProcesses'); - const choiceMap = new Map(); - const choices : string[] = []; - processData.forEach(p => { - const slash = p.action.lastIndexOf('/'); - if (slash>=0) { - const choiceLabel = p.label; - if (!choiceMap.has(choiceLabel)) { - choiceMap.set(choiceLabel, p); - choices.push(choiceLabel); - } - } - }); - if (choices) { - const picked = await VSCode.window.showQuickPick(choices); - if (picked) { - const chosen = choiceMap.get(picked); - try { - const loggers: ProcessLoggersData = await getLoggers(chosen); - await displayLoggers(loggers, chosen.processKey); - } catch (error) { - VSCode.window.showErrorMessage("Failed to fetch loggers for the process " + chosen.processKey); - } + const processInfo = await selectRunningProcess(); + if (processInfo) { + try { + const loggers: ProcessLoggersData = await getLoggers(processInfo); + await displayLoggers(loggers, processInfo.processKey); + } catch (error) { + window.showErrorMessage("Failed to fetch loggers for the process " + processInfo.processKey); } } } +async function selectRunningProcess(): Promise { + const quickPick = window.createQuickPick(); + quickPick.title = 'Searching for running Spring Boot Apps...'; + quickPick.canSelectMany = false; + quickPick.busy = true; + quickPick.show(); + + const items = ((await commands.executeCommand(LIST_CMD)) as ProcessCommandInfo[]).filter(cp => CONNECT_CMD === cp.action || DISCONNECT_CMD === cp.action).map(cp => ({ + label: cp.label, + commandInfo: cp + } as BootAppQuickPick)); + + quickPick.busy = false; + + quickPick.title = items.length ? "Select running Spring Boot App" : "No running Spring Boot Apps found"; + + quickPick.items = items; + + if (!items.length) { + quickPick.hide(); + window.showInformationMessage("No running Spring Boot Apps found"); + return; + } + + return new Promise((resolve, reject) => { + quickPick.onDidChangeSelection(() => quickPick.hide()); + quickPick.onDidHide(async () => resolve(Array.isArray(quickPick.selectedItems) ? quickPick.selectedItems[0]?.commandInfo : undefined)) + }); +} + async function getLoggers(processInfo: ProcessCommandInfo): Promise { return new Promise(async (resolve, reject) => { - await VSCode.window.withProgress({ - location: VSCode.ProgressLocation.Window, + await window.withProgress({ + location: ProgressLocation.Window, title: "Fetching Loggers Data for process "+processInfo.processKey, cancellable: false }, async (progress) => { try { - const loggers: ProcessLoggersData = await VSCode.commands.executeCommand('sts/livedata/getLoggers', processInfo, {"endpoint": "loggers"}); + const loggers: ProcessLoggersData = await commands.executeCommand('sts/livedata/getLoggers', processInfo, {"endpoint": "loggers"}); progress.report({}); resolve(loggers); } catch (error) { @@ -102,17 +114,23 @@ async function displayLoggers(processLoggersData: ProcessLoggersData, processKey }); } if(items) { - const chosenPackage = await VSCode.window.showQuickPick(items); + const chosenPackage = await window.showQuickPick(items, { + canPickMany: false, + title: "Select Logger" + }); if (chosenPackage) { - const chosenlogLevel = await VSCode.window.showQuickPick(loggersData.levels); - await VSCode.commands.executeCommand('sts/livedata/configure/logLevel', {"processKey": processKey}, chosenPackage, {"configuredLevel":chosenlogLevel}); + const chosenlogLevel = await window.showQuickPick(loggersData.levels, { + canPickMany: false, + title: "Select Level" + }); + await commands.executeCommand('sts/livedata/configure/logLevel', {"processKey": processKey}, chosenPackage, {"configuredLevel":chosenlogLevel}); } } } async function logLevelUpdated(process: LiveProcessUpdatedLogLevel) { - VSCode.window.showInformationMessage("The Log level for " + process.packageName + " has been updated from " + + window.showInformationMessage("The Log level for " + process.packageName + " has been updated from " + process.effectiveLevel + " to " + process.configuredLevel); } @@ -120,15 +138,15 @@ async function logLevelUpdated(process: LiveProcessUpdatedLogLevel) { export function activate( client: LanguageClient, options: ActivatorOptions, - context: VSCode.ExtensionContext + context: ExtensionContext ) { client.onNotification(LiveProcessLogLevelUpdatedNotification.type, logLevelUpdated) context.subscriptions.push( - VSCode.commands.registerCommand('vscode-spring-boot.set.log-levels', () => { + commands.registerCommand('vscode-spring-boot.set.log-levels', () => { if (client.isRunning()) { return setLogLevelHandler(); } else { - VSCode.window.showErrorMessage("No Spring Boot project found. Action is only available for Spring Boot Projects"); + window.showErrorMessage("No Spring Boot project found. Action is only available for Spring Boot Projects"); } }) );