Skip to content

Commit dec7908

Browse files
committed
Add setting to only show run button on files with executor
1 parent 97af108 commit dec7908

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ To set whether to preserve focus on code editor after code run is triggered (def
178178

179179
`code-runner.ignoreSelection`: Whether to ignore selection to always run entire file. (Default is **false**)
180180

181-
`code-runner.showRunIconInEditorTitleMenu`: Whether to show 'Run Code' icon in editor title menu. (Default is **true**)
181+
`code-runner.onlyShowRunIconIfExecutorExists`: Whether to only show 'Run Code' icon in editor title menu if the current file can be ran. If false, the icon will show for every file. (Default is **true**)
182+
183+
`code-runner.showRunIconInEditorTitleMenu`: Whether to show 'Run Code' icon in editor title menu. If false, overrides `code-runner.onlyShowRunIconIfExecutorExists` setting. (Default is **true**)
182184

183185
`code-runner.showRunCommandInEditorContextMenu`: Whether to show 'Run Code' command in editor context menu. (Default is **true**)
184186

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
],
9898
"editor/title/run": [
9999
{
100-
"when": "config.code-runner.showRunIconInEditorTitleMenu",
100+
"when": "code-runner.showRunButton",
101101
"command": "code-runner.run",
102102
"group": "navigation"
103103
}
@@ -324,10 +324,16 @@
324324
"description": "Whether to ignore selection to always run entire file.",
325325
"scope": "resource"
326326
},
327+
"code-runner.onlyShowRunIconIfExecutorExists": {
328+
"type": "boolean",
329+
"default": true,
330+
"description": "Whether to only show 'Run Code' icon in editor title menu if the current file can be ran. If false, the icon will show for every file.",
331+
"scope": "resource"
332+
},
327333
"code-runner.showRunIconInEditorTitleMenu": {
328334
"type": "boolean",
329335
"default": true,
330-
"description": "Whether to show 'Run Code' icon in editor title menu.",
336+
"description": "Whether to show 'Run Code' icon in editor title menu. If false, overrides `code-runner.onlyShowRunIconIfExecutorExists` setting.",
331337
"scope": "resource"
332338
},
333339
"code-runner.showStopIconInEditorTitleMenu": {

src/codeManager.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@ export class CodeManager implements vscode.Disposable {
112112
this.stopRunning();
113113
}
114114

115+
public fileHasExecutor(): boolean {
116+
const editor = vscode.window.activeTextEditor;
117+
if (editor) {
118+
this._document = editor.document;
119+
}
120+
121+
if (!this._document) {
122+
return false;
123+
}
124+
125+
this.initialize();
126+
const fileExtension = extname(this._document.fileName);
127+
const executor = this.getExecutor(this._document.languageId, fileExtension);
128+
return executor != null;
129+
}
130+
115131
private checkIsRunFromExplorer(fileUri: vscode.Uri): boolean {
116132
const editor = vscode.window.activeTextEditor;
117133
if (!fileUri || !fileUri.fsPath) {

src/extension.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ export function activate(context: vscode.ExtensionContext) {
3131
context.subscriptions.push(runByLanguage);
3232
context.subscriptions.push(stop);
3333
context.subscriptions.push(codeManager);
34+
35+
const updateContext = () => {
36+
const config = vscode.workspace.getConfiguration("code-runner");
37+
if (!config.get<boolean>("showRunIconInEditorTitleMenu", true)) {
38+
vscode.commands.executeCommand("setContext", "code-runner.showRunButton", false);
39+
return;
40+
}
41+
42+
if (!config.get<boolean>("onlyShowRunIconIfExecutorExists", true)) {
43+
vscode.commands.executeCommand("setContext", "code-runner.showRunButton", true);
44+
return;
45+
}
46+
47+
vscode.commands.executeCommand("setContext", "code-runner.showRunButton", codeManager.fileHasExecutor());
48+
};
49+
vscode.workspace.onDidChangeConfiguration(updateContext);
50+
vscode.window.onDidChangeActiveTextEditor(updateContext);
51+
52+
updateContext();
3453
}
3554

3655
export function deactivate() {

0 commit comments

Comments
 (0)