Skip to content

Commit 9d8518d

Browse files
committed
Show a run/debug button in the editor toolbar for valid target files
Fixes #2676.
1 parent 41a7f4c commit 9d8518d

4 files changed

Lines changed: 29 additions & 5 deletions

File tree

‎package.json‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,14 @@
185185
{
186186
"command": "dart.startDebugging",
187187
"title": "Start Debugging",
188-
"category": "Dart"
188+
"category": "Dart",
189+
"icon": "$(debug-alt-small)"
189190
},
190191
{
191192
"command": "dart.startWithoutDebugging",
192193
"title": "Run Without Debugging",
193-
"category": "Dart"
194+
"category": "Dart",
195+
"icon": "$(play)"
194196
},
195197
{
196198
"command": "dart.startDebuggingTest",
@@ -848,6 +850,16 @@
848850
"when": "resourceFilename == pubspec.yaml && dart-code:anyFlutterProjectLoaded",
849851
"command": "flutter.packages.outdated",
850852
"group": "navigation@3"
853+
},
854+
{
855+
"when": "resourceLangId == dart && dart-code:dartProjectLoaded && dart-code:currentFileIsRunnable",
856+
"command": "dart.startWithoutDebugging",
857+
"group": "1_run@10"
858+
},
859+
{
860+
"when": "resourceLangId == dart && dart-code:dartProjectLoaded && dart-code:currentFileIsRunnable",
861+
"command": "dart.startDebugging",
862+
"group": "1_run@20"
851863
}
852864
],
853865
"editor/context": [

‎src/extension/commands/debug.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import { config } from "../config";
1414
import { ServiceExtensionArgs, timeDilationNormal, timeDilationSlow, VmServiceExtensions } from "../flutter/vm_service_extensions";
1515
import { PubGlobal } from "../pub/global";
1616
import { DevToolsManager } from "../sdk/dev_tools/manager";
17+
import { isValidEntryFile } from "../utils";
1718
import { DartDebugSessionInformation, ProgressMessage } from "../utils/vscode/debug";
1819

1920
export const debugSessions: DartDebugSessionInformation[] = [];
21+
const CURRENT_FILE_RUNNABLE = "dart-code:currentFileIsRunnable";
2022

2123
// Workaround for https://github.com/microsoft/vscode/issues/100115
2224
const dynamicDebugSessionName = "Dart ";
@@ -64,6 +66,7 @@ export class DebugCommands {
6466
context.subscriptions.push(vs.debug.onDidStartDebugSession((s) => this.handleDebugSessionStart(s)));
6567
context.subscriptions.push(vs.debug.onDidReceiveDebugSessionCustomEvent((e) => this.handleDebugSessionCustomEvent(e)));
6668
context.subscriptions.push(vs.debug.onDidTerminateDebugSession((s) => this.handleDebugSessionEnd(s)));
69+
context.subscriptions.push(vs.window.onDidChangeActiveTextEditor((e) => this.updateEditorContexts(e)));
6770

6871
context.subscriptions.push(vs.commands.registerCommand("flutter.togglePlatform", () => this.vmServices.toggle(VmServiceExtension.PlatformOverride, "iOS", "android")));
6972
context.subscriptions.push(vs.commands.registerCommand("flutter.toggleDebugPainting", () => this.vmServices.toggle(VmServiceExtension.DebugPaint)));
@@ -571,4 +574,9 @@ export class DebugCommands {
571574
session.session.customRequest("serviceExtension", args);
572575
});
573576
}
577+
578+
private updateEditorContexts(e: vs.TextEditor | undefined): void {
579+
const isRunnable = !!(e && e.document && e.document.uri.scheme === "file" && isValidEntryFile(fsPath(e.document.uri)));
580+
vs.commands.executeCommand("setContext", CURRENT_FILE_RUNNABLE, isRunnable);
581+
}
574582
}

‎src/extension/providers/debug_config_provider.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { config } from "../config";
2929
import { locateBestProjectRoot } from "../project";
3030
import { PubGlobal } from "../pub/global";
3131
import { WebDev } from "../pub/webdev";
32-
import { isDartFile, isFlutterProjectFolder, isFlutterWorkspaceFolder, isInsideFolderNamed, isTestFile, isTestFileOrFolder, isTestFolder, projectShouldUsePubForTests as shouldUsePubForTests } from "../utils";
32+
import { isFlutterProjectFolder, isFlutterWorkspaceFolder, isInsideFolderNamed, isTestFileOrFolder, isTestFolder, isValidEntryFile, projectShouldUsePubForTests as shouldUsePubForTests } from "../utils";
3333
import { getGlobalFlutterArgs, getToolEnv } from "../utils/processes";
3434
import { TestResultsProvider } from "../views/test_view";
3535

@@ -412,8 +412,7 @@ export class DebugConfigProvider implements DebugConfigurationProvider {
412412

413413
private guessBestEntryPoint(openFile: string | undefined, folder: string | undefined): string | undefined {
414414
// For certain open files, assume the user wants to run them.
415-
if (openFile && isDartFile(openFile) &&
416-
(isTestFile(openFile) || (isInsideFolderNamed(openFile, "bin") || isInsideFolderNamed(openFile, "tool") || isInsideFolderNamed(openFile, "test_driver")))) {
415+
if (isValidEntryFile(openFile)) {
417416
this.logger.info(`Using open file as entry point: ${openFile}`);
418417
return openFile;
419418
}

‎src/extension/utils.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ export function isInsideFolderNamed(file: string, folderName: string): boolean {
174174
return segments.indexOf(folderName.toLowerCase()) !== -1;
175175
}
176176

177+
export function isValidEntryFile(file: string | undefined) {
178+
return file && isDartFile(file) &&
179+
(isTestFile(file) || (isInsideFolderNamed(file, "bin") || isInsideFolderNamed(file, "tool") || isInsideFolderNamed(file, "test_driver")));
180+
}
181+
177182
export function getLatestSdkVersion(): Promise<string> {
178183
return new Promise<string>((resolve, reject) => {
179184
const options: https.RequestOptions = {

0 commit comments

Comments
 (0)