-
-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathextension.ts
More file actions
204 lines (195 loc) Β· 9.2 KB
/
extension.ts
File metadata and controls
204 lines (195 loc) Β· 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import * as vscode from 'vscode'
import { WorkspaceFolder, DebugConfiguration, CancellationToken } from 'vscode'
import { EvaluateExtendedArguments, LaunchRequestArguments } from './phpDebug'
import * as which from 'which'
import * as path from 'path'
import { resolveEnvVariables } from './envResolver'
import { DebugProtocol } from '@vscode/debugprotocol'
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.debug.registerDebugConfigurationProvider('php', {
async resolveDebugConfiguration(
folder: WorkspaceFolder | undefined,
debugConfiguration: DebugConfiguration & LaunchRequestArguments,
token?: CancellationToken
): Promise<DebugConfiguration | undefined> {
const isDynamic =
(!debugConfiguration.type || debugConfiguration.type === 'php') &&
!debugConfiguration.request &&
!debugConfiguration.name
if (isDynamic) {
const editor = vscode.window.activeTextEditor
if (editor && editor.document.languageId === 'php') {
debugConfiguration.type = 'php'
debugConfiguration.name = 'Launch (dynamic)'
debugConfiguration.request = 'launch'
debugConfiguration.program = debugConfiguration.program || '${file}'
debugConfiguration.cwd = debugConfiguration.cwd || '${fileDirname}'
debugConfiguration.port = 0
debugConfiguration.runtimeArgs = ['-dxdebug.start_with_request=yes']
debugConfiguration.env = {
XDEBUG_MODE: 'debug,develop',
XDEBUG_CONFIG: 'client_port=${port}',
}
// debugConfiguration.stopOnEntry = true
}
}
if (
(debugConfiguration.program || debugConfiguration.runtimeArgs) &&
!debugConfiguration.runtimeExecutable
) {
// See if we have runtimeExecutable configured
const conf = vscode.workspace.getConfiguration('php.debug')
const executablePath = conf.get<string>('executablePath')
if (executablePath) {
debugConfiguration.runtimeExecutable = executablePath
}
// See if it's in path
if (!debugConfiguration.runtimeExecutable) {
try {
await which.default('php')
} catch (e) {
const selected = await vscode.window.showErrorMessage(
'PHP executable not found. Install PHP and add it to your PATH or set the php.debug.executablePath setting',
'Open settings'
)
if (selected === 'Open settings') {
await vscode.commands.executeCommand('workbench.action.openGlobalSettings', {
query: 'php.debug.executablePath',
})
return undefined
}
}
}
}
if (debugConfiguration.proxy?.enable === true) {
// Proxy configuration
if (!debugConfiguration.proxy.key) {
const conf = vscode.workspace.getConfiguration('php.debug')
const ideKey = conf.get<string>('ideKey')
if (ideKey) {
debugConfiguration.proxy.key = ideKey
}
}
}
if (debugConfiguration.pathMappings) {
const resolvedMappings: { [index: string]: string } = {}
for (const [serverPath, localPath] of Object.entries(debugConfiguration.pathMappings)) {
const resolvedServerPath = resolveEnvVariables(serverPath)
let resolvedLocalPath = resolveEnvVariables(localPath)
if (folder && folder.uri.scheme !== 'file') {
resolvedLocalPath = resolvedLocalPath.replace('${workspaceFolder}', folder.uri.toString())
}
resolvedMappings[resolvedServerPath] = resolvedLocalPath
}
debugConfiguration.pathMappings = resolvedMappings
}
// The following path are currently NOT mapped
/*
debugConfiguration.skipEntryPaths = debugConfiguration.skipEntryPaths?.map(v =>
v.replace('${workspaceFolder}', folder.uri.toString())
)
debugConfiguration.skipFiles = debugConfiguration.skipFiles?.map(v =>
v.replace('${workspaceFolder}', folder.uri.toString())
)
debugConfiguration.ignore = debugConfiguration.ignore?.map(v =>
v.replace('${workspaceFolder}', folder.uri.toString())
)
*/
return debugConfiguration
},
})
)
context.subscriptions.push(
vscode.commands.registerCommand('extension.php-debug.runEditorContents', (resource: vscode.Uri) => {
let targetResource = resource
if (!targetResource && vscode.window.activeTextEditor) {
targetResource = vscode.window.activeTextEditor.document.uri
}
if (targetResource) {
void vscode.debug.startDebugging(undefined, {
type: 'php',
name: '',
request: '',
noDebug: true,
program: targetResource.fsPath,
cwd: path.dirname(targetResource.fsPath),
})
}
}),
vscode.commands.registerCommand('extension.php-debug.debugEditorContents', (resource: vscode.Uri) => {
let targetResource = resource
if (!targetResource && vscode.window.activeTextEditor) {
targetResource = vscode.window.activeTextEditor.document.uri
}
if (targetResource) {
void vscode.debug.startDebugging(undefined, {
type: 'php',
name: '',
request: '',
stopOnEntry: true,
program: targetResource.fsPath,
cwd: path.dirname(targetResource.fsPath),
})
}
})
)
context.subscriptions.push(
vscode.commands.registerCommand('extension.php-debug.startWithStopOnEntry', async (uri: vscode.Uri) => {
await vscode.commands.executeCommand('workbench.action.debug.start', {
config: {
stopOnEntry: true,
},
})
})
)
/* This is coppied from vscode/src/vs/workbench/contrib/debug/browser/variablesView.ts */
interface IVariablesContext {
sessionId: string | undefined
container: DebugProtocol.Variable | DebugProtocol.Scope | DebugProtocol.EvaluateArguments
variable: DebugProtocol.Variable
}
/* This is coppied from @vscode/debugprotocol/lib/debugProtocol.d.ts because customRequest returns the body of the response and not the response itself */
interface EvaluateResponse {
/** The result of the evaluate request. */
result: string
}
const copyVar = async (arg: IVariablesContext, context: string) => {
const aci = vscode.debug.activeStackItem
if (aci && aci instanceof vscode.DebugStackFrame) {
const ret = (await vscode.debug.activeDebugSession?.customRequest('evaluate', <EvaluateExtendedArguments>{
context,
expression: arg.variable.evaluateName,
frameId: aci.frameId,
variablesReference: arg.variable.variablesReference,
})) as EvaluateResponse
await vscode.env.clipboard.writeText(ret.result)
} else {
await vscode.window.showErrorMessage('Cannot derermine active debug session')
}
}
context.subscriptions.push(
vscode.commands.registerCommand(
'extension.php-debug.copyVarExport',
async (arg: IVariablesContext, p2: any, p3: any) => {
await copyVar(arg, 'clipboard-var_export')
}
)
)
context.subscriptions.push(
vscode.commands.registerCommand(
'extension.php-debug.copyJson',
async (arg: IVariablesContext, p2: any, p3: any) => {
await copyVar(arg, 'clipboard-json')
}
)
)
context.subscriptions.push(
vscode.commands.registerCommand(
'extension.php-debug.copyRaw',
async (arg: IVariablesContext, p2: any, p3: any) => {
await copyVar(arg, 'clipboard-raw')
}
)
)
}