Skip to content

Commit de4d890

Browse files
committed
Add a new Flutter: Attach to Process option that prompts for VM Service
Fixes #1638.
1 parent f34a2af commit de4d890

3 files changed

Lines changed: 48 additions & 28 deletions

File tree

‎package.json‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@
119119
},
120120
{
121121
"command": "flutter.attach",
122+
"title": "Attach to Flutter on Device",
123+
"category": "Debug"
124+
},
125+
{
126+
"command": "flutter.attachProcess",
122127
"title": "Attach to Flutter Process",
123128
"category": "Debug"
124129
},
@@ -451,6 +456,10 @@
451456
"command": "flutter.attach",
452457
"when": "dart-code:anyFlutterProjectLoaded && !inDebugMode && dart-code:flutterSupportsAttach"
453458
},
459+
{
460+
"command": "flutter.attachProcess",
461+
"when": "dart-code:anyFlutterProjectLoaded && !inDebugMode && dart-code:flutterSupportsAttach"
462+
},
454463
{
455464
"command": "dart.sortMembers",
456465
"when": "dart-code:dartProjectLoaded && editorLangId == dart"
@@ -1379,10 +1388,10 @@
13791388
}
13801389
},
13811390
{
1382-
"label": "Flutter: Attach",
1383-
"description": "Attach to Flutter in an already-running mobile application",
1391+
"label": "Flutter: Attach to Device",
1392+
"description": "Attach to Flutter on a device.",
13841393
"body": {
1385-
"name": "Flutter: Attach to Process",
1394+
"name": "Flutter: Attach to Device",
13861395
"type": "dart",
13871396
"request": "attach"
13881397
}

‎src/commands/debug.ts‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ export class DebugCommands {
6363
context.subscriptions.push(vs.commands.registerCommand("flutter.inspectWidget", () => this.flutterExtensions.toggle(FlutterServiceExtension.InspectorSelectMode, true, true)));
6464
context.subscriptions.push(vs.commands.registerCommand("flutter.cancelInspectWidget", () => this.flutterExtensions.toggle(FlutterServiceExtension.InspectorSelectMode, false, false)));
6565

66-
// Open Observatory.
6766
context.subscriptions.push(vs.commands.registerCommand("dart.openObservatory", async () => {
6867
if (!debugSessions.length)
6968
return;
@@ -187,13 +186,45 @@ export class DebugCommands {
187186
type: "dart",
188187
});
189188
}));
190-
context.subscriptions.push(vs.commands.registerCommand("flutter.attach", () => {
189+
context.subscriptions.push(vs.commands.registerCommand("flutter.attachProcess", () => {
191190
vs.debug.startDebugging(undefined, {
192191
name: "Flutter: Attach to Process",
192+
observatoryUri: "${command:dart.promptForVmService}",
193+
request: "attach",
194+
type: "dart",
195+
});
196+
}));
197+
context.subscriptions.push(vs.commands.registerCommand("flutter.attach", () => {
198+
vs.debug.startDebugging(undefined, {
199+
name: "Flutter: Attach to Device",
193200
request: "attach",
194201
type: "dart",
195202
});
196203
}));
204+
context.subscriptions.push(vs.commands.registerCommand("dart.promptForVmService", async (defaultValueOrConfig: string | vs.DebugConfiguration | undefined): Promise<string | undefined> => {
205+
const defaultValue = typeof defaultValueOrConfig === "string" ? defaultValueOrConfig : undefined;
206+
return vs.window.showInputBox({
207+
ignoreFocusOut: true, // Don't close the window if the user tabs away to get the uri
208+
placeHolder: "Paste an Observatory URI",
209+
prompt: "Enter Observatory URI",
210+
validateInput: (input) => {
211+
if (!input)
212+
return;
213+
214+
input = input.trim();
215+
216+
if (Number.isInteger(parseFloat(input)))
217+
return;
218+
219+
// Uri.parse doesn't seem to work as expected, so do our own basic validation
220+
// https://github.com/Microsoft/vscode/issues/49818
221+
222+
if (!input.startsWith("http://") && !input.startsWith("https://"))
223+
return "Please enter a valid Observatory URI";
224+
},
225+
value: defaultValue,
226+
});
227+
}));
197228
}
198229

199230
public handleDebugSessionStart(s: vs.DebugSession): void {

‎src/providers/debug_config_provider.ts‎

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export class DebugConfigProvider implements DebugConfigurationProvider {
221221
if (isAttachRequest) {
222222
// For attaching, the Observatory address must be specified. If it's not provided already, prompt for it.
223223
if (!isStandardFlutter) { // TEMP Condition because there's no point asking yet as the user doesn't know how to get this..
224-
debugConfig.observatoryUri = await this.getObservatoryUri(debugConfig.observatoryUri/*, mostRecentAttachedProbablyReusableObservatoryUri*/);
224+
debugConfig.observatoryUri = await this.getFullVmServiceUri(debugConfig.observatoryUri/*, mostRecentAttachedProbablyReusableObservatoryUri*/);
225225
}
226226

227227
if (!debugConfig.observatoryUri && !isStandardFlutter) {
@@ -367,28 +367,8 @@ export class DebugConfigProvider implements DebugConfigurationProvider {
367367
}
368368
}
369369

370-
private async getObservatoryUri(observatoryUri: string, defaultValue?: string): Promise<string> {
371-
observatoryUri = observatoryUri || await vs.window.showInputBox({
372-
ignoreFocusOut: true, // Don't close the window if the user tabs away to get the uri
373-
placeHolder: "Paste an Observatory URI",
374-
prompt: "Enter Observatory URI",
375-
validateInput: (input) => {
376-
if (!input)
377-
return;
378-
379-
input = input.trim();
380-
381-
if (Number.isInteger(parseFloat(input)))
382-
return;
383-
384-
// Uri.parse doesn't seem to work as expected, so do our own basic validation
385-
// https://github.com/Microsoft/vscode/issues/49818
386-
387-
if (!input.startsWith("http://") && !input.startsWith("https://"))
388-
return "Please enter a valid Observatory URI";
389-
},
390-
value: defaultValue,
391-
});
370+
private async getFullVmServiceUri(observatoryUri: string, defaultValue?: string): Promise<string> {
371+
observatoryUri = observatoryUri || await vs.commands.executeCommand("dart.promptForVmService", defaultValue);
392372
observatoryUri = observatoryUri && observatoryUri.trim();
393373

394374
// If the input is just a number, treat is as a localhost port.

0 commit comments

Comments
 (0)