Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,16 @@ Also, you could set the executor per file extension:
}
```

**Note:** All of `code-runner.executorMapByFileExtension`, `code-runner.executorMapByGlob`, and `code-runner.executorMap` merge the default setting with what you set. If you want to remove one of the defaults, you can switch your setting to use `code-runner.executorMapByFileExtensionOverride`, `code-runner.executorMapByGlobOverride`, and `code-runner.executorMapOverride` respectively.

To set the custom command to run:
```json
{
"code-runner.customCommand": "echo Hello"
}
```

To set the the working directory:
To set the working directory:
```json
{
"code-runner.cwd": "path/to/working/directory"
Expand Down
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@
"description": "Set the executor by glob.",
"scope": "resource"
},
"code-runner.executorMapByGlobOverride": {
"type": "object",
"default": {},
"description": "If set, overrides `code-runner.executorMapByGlob` without merging the default settings.",
"scope": "resource"
},
"code-runner.executorMap": {
"type": "object",
"default": {
Expand Down Expand Up @@ -193,6 +199,12 @@
"description": "Set the executor of each language.",
"scope": "resource"
},
"code-runner.executorMapOverride": {
"type": "object",
"default": {},
"description": "If set, overrides `code-runner.executorMap` without merging the default settings.",
"scope": "resource"
},
"code-runner.executorMapByFileExtension": {
"type": "object",
"default": {
Expand Down Expand Up @@ -236,6 +248,12 @@
"description": "Set the executor of each file extension.",
"scope": "resource"
},
"code-runner.executorMapByFileExtensionOverride": {
"type": "object",
"default": {},
"description": "If set, overrides `code-runner.executorMapByFileExtension` without merging the default settings.",
"scope": "resource"
},
"code-runner.customCommand": {
"type": "string",
"default": "echo Hello",
Expand All @@ -252,6 +270,12 @@
"description": "Set the mapping of languageId to file extension.",
"scope": "resource"
},
"code-runner.languageIdToFileExtensionMapOverride": {
"type": "object",
"default": {},
"description": "If set, overrides `code-runner.languageIdToFileExtensionMap` without merging the default settings.",
"scope": "resource"
},
"code-runner.defaultLanguage": {
"type": "string",
"default": "",
Expand Down
54 changes: 34 additions & 20 deletions src/codeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class CodeManager implements vscode.Disposable {
public runByLanguage(): void {
this._appInsightsClient.sendEvent("runByLanguage");
const config = this.getConfiguration("code-runner");
const executorMap = config.get<any>("executorMap");
const executorMap = this.getExecutorMap(config);
vscode.window.showQuickPick(Object.keys(executorMap), { placeHolder: "Type or select language to run" }).then((languageId) => {
if (languageId !== undefined) {
this.run(languageId);
Expand Down Expand Up @@ -220,15 +220,15 @@ export class CodeManager implements vscode.Disposable {

private createRandomFile(content: string, folder: string, fileExtension: string) {
let fileType = "";
const languageIdToFileExtensionMap = this._config.get<any>("languageIdToFileExtensionMap");
if (this._languageId && languageIdToFileExtensionMap[this._languageId]) {
fileType = languageIdToFileExtensionMap[this._languageId];
} else {
if (fileExtension) {
fileType = fileExtension;
} else {
fileType = "." + this._languageId;
if (this._languageId && this._languageId !== "") {
let languageIdToFileExtensionMap = this._config.get<any>("languageIdToFileExtensionMapOverride", {});
if (Object.keys(languageIdToFileExtensionMap).length === 0) {
languageIdToFileExtensionMap = this._config.get<any>("languageIdToFileExtensionMap", {});
}

fileType = languageIdToFileExtensionMap[this._languageId] ?? fileExtension ?? ("." + this._languageId);
} else {
fileType = fileExtension ?? ("." + (this._languageId || ""));
}
const temporaryFileName = this._config.get<string>("temporaryFileName");
const tmpFileNameWithoutExt = temporaryFileName ? temporaryFileName : "temp" + this.rndName();
Expand All @@ -251,27 +251,33 @@ export class CodeManager implements vscode.Disposable {
}

if (executor == null) {
const executorMapByGlob = this._config.get<any>("executorMapByGlob");
if (executorMapByGlob) {
const fileBasename = basename(this._document.fileName);
for (const glob of Object.keys(executorMapByGlob)) {
if (micromatch.isMatch(fileBasename, glob)) {
executor = executorMapByGlob[glob];
break;
}
let executorMapByGlob = this._config.get<any>("executorMapByGlobOverride", {});
if (Object.keys(executorMapByGlob).length === 0) {
executorMapByGlob = this._config.get<any>("executorMapByGlob", {});
}

executorMapByGlob = Object.fromEntries(Object.entries(executorMapByGlob).filter(([key]) => key !== ""))
const fileBasename = basename(this._document.fileName);
for (const glob of Object.keys(executorMapByGlob)) {
if (micromatch.isMatch(fileBasename, glob)) {
executor = executorMapByGlob[glob];
break;
}
}
}

const executorMap = this._config.get<any>("executorMap");
const executorMap = this.getExecutorMap(this._config);

if (executor == null) {
executor = executorMap[this._languageId];
}

// executor is undefined or null
if (executor == null && fileExtension) {
const executorMapByFileExtension = this._config.get<any>("executorMapByFileExtension");
if (executor == null && fileExtension && fileExtension !== "") {
let executorMapByFileExtension = this._config.get<any>("executorMapByFileExtensionOverride", {});
if (Object.keys(executorMapByFileExtension).length === 0) {
executorMapByFileExtension = this._config.get<any>("executorMapByFileExtension", {});
}
executor = executorMapByFileExtension[fileExtension];
if (executor != null) {
this._languageId = fileExtension;
Expand All @@ -285,6 +291,14 @@ export class CodeManager implements vscode.Disposable {
return executor;
}

private getExecutorMap(config: vscode.WorkspaceConfiguration): any {
let executorMap = config.get<any>("executorMapOverride", {});
if (Object.keys(executorMap).length === 0) {
executorMap = config.get<any>("executorMap", {});
}
return Object.fromEntries(Object.entries(executorMap).filter(([key]) => key !== ""));
}

private executeCommand(executor: string, appendFile: boolean = true) {
if (this._config.get<boolean>("runInTerminal")) {
this.executeCommandInTerminal(executor, appendFile);
Expand Down