|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +import * as vscode from 'vscode'; |
| 9 | +import * as nls from 'vscode-nls'; |
| 10 | +import { basename } from 'path'; |
| 11 | +import { pollProcesses, attachToProcess } from './nodeProcessTree'; |
| 12 | + |
| 13 | +const localize = nls.loadMessageBundle(); |
| 14 | +const ON_TEXT = localize('status.text.auto.attach.on', "Auto Attach: On"); |
| 15 | +const OFF_TEXT = localize('status.text.auto.attach.off', "Auto Attach: Off"); |
| 16 | + |
| 17 | +const TOGGLE_COMMAND = 'extension.node-debug.toggleAutoAttach'; |
| 18 | + |
| 19 | +let currentState: string; |
| 20 | +let autoAttacher: vscode.Disposable | undefined; |
| 21 | +let statusItem: vscode.StatusBarItem | undefined = undefined; |
| 22 | + |
| 23 | + |
| 24 | +export function activate(context: vscode.ExtensionContext): void { |
| 25 | + |
| 26 | + context.subscriptions.push(vscode.commands.registerCommand(TOGGLE_COMMAND, toggleAutoAttach)); |
| 27 | + |
| 28 | + context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(e => { |
| 29 | + if (e.affectsConfiguration('debug.node.autoAttach')) { |
| 30 | + updateAutoAttachInStatus(context); |
| 31 | + } |
| 32 | + })); |
| 33 | + |
| 34 | + updateAutoAttachInStatus(context); |
| 35 | +} |
| 36 | + |
| 37 | +export function deactivate(): void { |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +function toggleAutoAttach(context: vscode.ExtensionContext) { |
| 42 | + |
| 43 | + const conf = vscode.workspace.getConfiguration('debug.node'); |
| 44 | + |
| 45 | + let value = conf.get('autoAttach'); |
| 46 | + if (value === 'on') { |
| 47 | + value = 'off'; |
| 48 | + } else { |
| 49 | + value = 'on'; |
| 50 | + } |
| 51 | + |
| 52 | + const info = conf.inspect('autoAttach'); |
| 53 | + let target: vscode.ConfigurationTarget = vscode.ConfigurationTarget.Global; |
| 54 | + if (info) { |
| 55 | + if (info.workspaceFolderValue) { |
| 56 | + target = vscode.ConfigurationTarget.WorkspaceFolder; |
| 57 | + } else if (info.workspaceValue) { |
| 58 | + target = vscode.ConfigurationTarget.Workspace; |
| 59 | + } else if (info.globalValue) { |
| 60 | + target = vscode.ConfigurationTarget.Global; |
| 61 | + } else if (info.defaultValue) { |
| 62 | + // setting not yet used: store setting in workspace |
| 63 | + if (vscode.workspace.workspaceFolders) { |
| 64 | + target = vscode.ConfigurationTarget.Workspace; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + conf.update('autoAttach', value, target); |
| 69 | + |
| 70 | + updateAutoAttachInStatus(context); |
| 71 | +} |
| 72 | + |
| 73 | +function updateAutoAttachInStatus(context: vscode.ExtensionContext) { |
| 74 | + |
| 75 | + const newState = <string>vscode.workspace.getConfiguration('debug.node').get('autoAttach'); |
| 76 | + |
| 77 | + if (newState !== currentState) { |
| 78 | + |
| 79 | + currentState = newState; |
| 80 | + |
| 81 | + if (newState === 'disabled') { |
| 82 | + |
| 83 | + // turn everything off |
| 84 | + if (statusItem) { |
| 85 | + statusItem.hide(); |
| 86 | + statusItem.text = OFF_TEXT; |
| 87 | + } |
| 88 | + if (autoAttacher) { |
| 89 | + autoAttacher.dispose(); |
| 90 | + autoAttacher = undefined; |
| 91 | + } |
| 92 | + |
| 93 | + } else { // 'on' or 'off' |
| 94 | + |
| 95 | + // make sure status bar item exists and is visible |
| 96 | + if (!statusItem) { |
| 97 | + statusItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left); |
| 98 | + statusItem.command = TOGGLE_COMMAND; |
| 99 | + statusItem.text = OFF_TEXT; |
| 100 | + statusItem.tooltip = localize('status.tooltip.auto.attach', "Automatically attach to node.js processes in debug mode"); |
| 101 | + statusItem.show(); |
| 102 | + context.subscriptions.push(statusItem); |
| 103 | + } else { |
| 104 | + statusItem.show(); |
| 105 | + } |
| 106 | + |
| 107 | + if (newState === 'off') { |
| 108 | + statusItem.text = OFF_TEXT; |
| 109 | + if (autoAttacher) { |
| 110 | + autoAttacher.dispose(); |
| 111 | + autoAttacher = undefined; |
| 112 | + } |
| 113 | + } else if (newState === 'on') { |
| 114 | + statusItem.text = ON_TEXT; |
| 115 | + const vscode_pid = process.env['VSCODE_PID']; |
| 116 | + const rootPid = vscode_pid ? parseInt(vscode_pid) : 0; |
| 117 | + autoAttacher = startAutoAttach(rootPid); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +function startAutoAttach(rootPid: number): vscode.Disposable { |
| 124 | + |
| 125 | + return pollProcesses(rootPid, true, (pid, cmdPath, args) => { |
| 126 | + const cmdName = basename(cmdPath, '.exe'); |
| 127 | + if (cmdName === 'node') { |
| 128 | + const name = localize('process.with.pid.label', "Process {0}", pid); |
| 129 | + attachToProcess(undefined, name, pid, args); |
| 130 | + } |
| 131 | + }); |
| 132 | +} |
0 commit comments