Skip to content

Commit

Permalink
New Version 1.11.0
Browse files Browse the repository at this point in the history
New Version 1.11.0
  • Loading branch information
twibiral authored Apr 14, 2024
2 parents 59c7409 + e7c727f commit 766f591
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 34 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),


## [1.11.0]
### Added
- Support for OCaml (Thanks to @nieomylnieja)
- Support for Swift (Thanks to @ihomway)

### Changed
- Improved support for C compiler gcc (Thanks to @melo-afk)


## [1.10.0]
### Added
- Support for zig (Thanks to @slar)
Expand Down
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The result is shown only after the execution is finished. It is not possible to
![Video that shows how the plugin works.](https://github.com/twibiral/obsidian-execute-code/blob/master/images/execute_code_example.gif?raw=true)


The following [languages are supported](#supported-programming-languages-): C, CPP, Dart, Golang, Groovy, Kotlin, Java, JavaScript, TypeScript, Lean, Lua, CSharp, Prolog, Rust, Python, R, Ruby, Wolfram Mathematica, Haskell, Scala, Racket, F#, Batch, Shell & Powershell, Octave, Maxima and Zig.
The following [languages are supported](#supported-programming-languages-): C, CPP, Dart, Golang, Groovy, Kotlin, Java, JavaScript, TypeScript, Lean, Lua, CSharp, Prolog, Rust, Python, R, Ruby, Wolfram Mathematica, Haskell, Scala, Racket, F#, Batch, Shell & Powershell, Octave, Maxima, Zig and OCaml.


Python, Rust, and Octave support embedded plots. All languages support ["magic" commands](#magic-commands-) that help you to access paths in obsidian or show images in your notes.
Expand All @@ -32,9 +32,15 @@ Take a look at the [changelog](CHANGELOG.md) to see what has changed in recent v


## Featured In
[![Video by I Versus AI](https://img.youtube.com/vi/eQz4eAW3ZDk/0.jpg)](https://www.youtube.com/watch?v=eQz4eAW3ZDk)

"Escape ChatGPT. Make your own Code Interpreter EASY" by I Versus AI
| [![Video by I Versus AI](https://img.youtube.com/vi/eQz4eAW3ZDk/0.jpg)](https://www.youtube.com/watch?v=eQz4eAW3ZDk) | ![![Video by Michel's Science Speedrun](https://www.youtube.com/watch?v=w7vyavrMYqw)](https://img.youtube.com/vi/w7vyavrMYqw/0.jpg) |
|---|---|
| "Escape ChatGPT. Make your own Code Interpreter EASY" by _I Versus AI_ | "Obsidian & quarto integration" by _Michel's Science Speedrun_ |

In blogs:
- ["Using Obsidian: Coding Notes" by _Kera Cudmore_](https://www.codu.co/articles/using-obsidian-coding-notes-pqjyljkh)

<small>Are you featuring this plugin in your content? Let me know and I will add it here.</small>


## Supported programming languages 💻
Expand Down Expand Up @@ -446,6 +452,15 @@ plot2d(sin(x), [x,0,%pi]);

</details>

<details>
<summary>OCaml</summary>

- Requirements: OCaml is installed and the correct path is set in the settings.

```ocaml
print_endline "Hello, OCaml!"
</details>
<details>
<summary>Swift</summary>
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "execute-code",
"name": "Execute Code",
"version": "1.10.0",
"version": "1.11.0",
"minAppVersion": "1.2.8",
"description": "Allows to execute code snippets within a note. Supported programming languages: C, CPP, Dart, Golang, Groovy, Kotlin, Java, JavaScript, TypeScript, Lean, Lua, CSharp, Prolog, Rust, Python, R, Ruby, Wolfram Mathematica, Haskell, Scala, Racket, F#, Batch, Shell & Powershell.",
"author": "twibiral",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "execute-code",
"version": "1.10.0",
"version": "1.11.0",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"main": "src/main.js",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion src/executors/CExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export default class CExecutor extends ClingExecutor {
}

override run(codeBlockContent: string, outputter: Outputter, cmd: string, cmdArgs: string, ext: string) {
return super.run(codeBlockContent, outputter, cmd, `-x c ${cmdArgs}`, "cpp");
const install_path = this.settings[`clingPath`];
if (install_path.endsWith("cling") || install_path.endsWith("cling.exe")) {
return super.run(codeBlockContent, outputter, cmd, this.settings[`cArgs`], "cpp");
} else {
return super.run(codeBlockContent, outputter, cmd, this.settings[`cArgs`], "c");
}
}
}
13 changes: 8 additions & 5 deletions src/executors/ClingExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ export default abstract class ClingExecutor extends NonInteractiveCodeExecutor {
}

override run(codeBlockContent: string, outputter: Outputter, cmd: string, args: string, ext: string) {

// Run code with a main block
if (this.settings[`${this.language}UseMain`]) {
// Generate a new temp file id and don't set to undefined to super.run() uses the same file id
this.getTempFile(ext);
// Cling expects the main function to have the same name as the file
const code = codeBlockContent.replace(/main\(\)/g, `temp_${this.tempFileId}()`);

// Cling expects the main function to have the same name as the file / the extension is only c when gcc is used
let code: string;
if (ext != "c") {
code = codeBlockContent.replace(/main\(\)/g, `temp_${this.tempFileId}()`);
} else {
code = codeBlockContent;
}
return super.run(code, outputter, this.settings.clingPath, args, ext);
}

// Run code without a main block
// Run code without a main block (cling only)
return new Promise<void>((resolve, reject) => {
const childArgs = [...args.split(" "), ...codeBlockContent.split("\n")];
const child = child_process.spawn(this.settings.clingPath, childArgs, {env: process.env, shell: this.usesShell});
Expand Down
2 changes: 1 addition & 1 deletion src/executors/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ export default abstract class Executor extends EventEmitter {
protected getTempFile(ext: string) {
if (this.tempFileId === undefined)
this.tempFileId = Date.now().toString();
return `${os.tmpdir()}/temp_${this.tempFileId}.${ext}`;
return `${os.tmpdir()}\\temp_${this.tempFileId}.${ext}`;
}
}
37 changes: 30 additions & 7 deletions src/executors/NonInteractiveCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Outputter} from "src/Outputter";
import {LanguageId} from "src/main";
import { ExecutorSettings } from "../settings/Settings.js";
import windowsPathToWsl from "../transforms/windowsPathToWsl.js";
import { error } from "console";

export default class NonInteractiveCodeExecutor extends Executor {
usesShell: boolean
Expand Down Expand Up @@ -44,16 +45,38 @@ export default class NonInteractiveCodeExecutor extends Executor {
} else {
args.push(tempFileName);
}

const child = child_process.spawn(cmd, args, {env: process.env, shell: this.usesShell});

this.handleChildOutput(child, outputter, tempFileName).then(() => {
this.tempFileId = undefined; // Reset the file id to use a new file next time
});


let child: child_process.ChildProcessWithoutNullStreams;

// check if compiled by gcc
if (cmd.endsWith("gcc") || cmd.endsWith("gcc.exe")) {
// remove .c from tempFileName and add .out for the compiled output and add output path to args
const tempFileNameWExe: string = tempFileName.slice(0, -2) + ".out";
args.push("-o", tempFileNameWExe);

// compile c file with gcc and handle possible output
const childGCC = child_process.spawn(cmd, args, {env: process.env, shell: this.usesShell});
this.handleChildOutput(childGCC, outputter, tempFileName);
childGCC.on('exit', (code) => {
if (code === 0) {
// executing the compiled file
child = child_process.spawn(tempFileNameWExe, { env: process.env, shell: this.usesShell });
this.handleChildOutput(child, outputter, tempFileNameWExe).then(() => {
this.tempFileId = undefined; // Reset the file id to use a new file next time
});
}
});
} else {
child = child_process.spawn(cmd, args, { env: process.env, shell: this.usesShell });
this.handleChildOutput(child, outputter, tempFileName).then(() => {
this.tempFileId = undefined; // Reset the file id to use a new file next time
});
}

// We don't resolve the promise here - 'handleChildOutput' registers a listener
// For when the child_process closes, and will resolve the promise there
this.resolveRun = resolve;
this.resolveRun = resolve;
}).catch((err) => {
this.notifyError(cmd, cmdArgs, tempFileName, err, outputter);
resolve();
Expand Down
19 changes: 16 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import ExecutorManagerView, {

import runAllCodeBlocks from './runAllCodeBlocks';

export const languageAliases = ["javascript", "typescript", "bash", "csharp", "wolfram", "nb", "wl", "hs", "py", "scpt"] as const;
export const languageAliases = ["javascript", "typescript", "bash", "csharp", "wolfram", "nb", "wl", "hs", "py"] as const;
export const canonicalLanguages = ["js", "ts", "cs", "lean", "lua", "python", "cpp", "prolog", "shell", "groovy", "r",
"go", "rust", "java", "powershell", "kotlin", "mathematica", "haskell", "scala", "racket", "fsharp", "c", "dart",
"ruby", "batch", "sql", "octave", "maxima", "applescript", "zig"] as const;
"go", "rust", "java", "powershell", "kotlin", "mathematica", "haskell", "scala", "swift", "racket", "fsharp", "c", "dart",
"ruby", "batch", "sql", "octave", "maxima", "applescript", "zig", "ocaml"] as const;
export const supportedLanguages = [...languageAliases, ...canonicalLanguages] as const;
export type LanguageId = typeof canonicalLanguages[number];

Expand Down Expand Up @@ -353,6 +353,13 @@ export default class ExecuteCodePlugin extends Plugin {
const transformedCode = await new CodeInjector(this.app, this.settings, language).injectCode(srcCode);
this.runCodeInShell(transformedCode, out, button, this.settings.scalaPath, this.settings.scalaArgs, this.settings.scalaFileExtension, language, file);
});
} else if (language === "swift") {
button.addEventListener("click", async () => {
button.className = runButtonDisabledClass;
const transformedCode = await new CodeInjector(this.app, this.settings, language).injectCode(srcCode);
this.runCodeInShell(transformedCode, out, button, this.settings.swiftPath, this.settings.swiftArgs, this.settings.swiftFileExtension, language, file);
});

} else if (language === "c") {
button.addEventListener("click", async () => {
button.className = runButtonDisabledClass;
Expand Down Expand Up @@ -403,6 +410,12 @@ export default class ExecuteCodePlugin extends Plugin {
const transformedCode = await new CodeInjector(this.app, this.settings, language).injectCode(srcCode);
this.runCodeInShell(transformedCode, out, button, this.settings.zigPath, this.settings.zigArgs, "zig", language, file);
})
} else if (language === "ocaml") {
button.addEventListener("click", async () => {
button.className = runButtonDisabledClass;
const transformedCode = await new CodeInjector(this.app, this.settings, language).injectCode(srcCode);
this.runCodeInShell(transformedCode, out, button, this.settings.ocamlPath, this.settings.ocamlArgs, "ocaml", language, file);
})
}

}
Expand Down
20 changes: 19 additions & 1 deletion src/settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export interface ExecutorSettings {
onlyCurrentBlock: boolean;
nodePath: string;
nodeArgs: string;
jsFileExtension: string;
jsInject: string;
jsFileExtension: string;
tsPath: string;
tsArgs: string;
tsInject: string;
Expand Down Expand Up @@ -94,6 +94,10 @@ export interface ExecutorSettings {
kotlinArgs: string;
kotlinFileExtension: string;
kotlinInject: string;
swiftPath: string;
swiftArgs: string;
swiftFileExtension: string;
swiftInject: string;
runghcPath: string;
ghcPath: string;
ghciPath: string;
Expand Down Expand Up @@ -132,6 +136,9 @@ export interface ExecutorSettings {
zigPath: string;
zigArgs: string;
zigInject: string;
ocamlPath: string;
ocamlArgs: string;
ocamlInject: string;

jsInteractive: boolean;
tsInteractive: boolean;
Expand All @@ -152,6 +159,7 @@ export interface ExecutorSettings {
javaInteractive: boolean;
powershellInteractive: boolean;
kotlinInteractive: boolean;
swiftInteractive: boolean;
mathematicaInteractive: boolean;
haskellInteractive: boolean;
scalaInteractive: boolean;
Expand All @@ -164,6 +172,7 @@ export interface ExecutorSettings {
maximaInteractive: boolean;
applescriptInteractive: boolean;
zigInteractive: boolean;
ocamlInteractive: boolean;
}


Expand Down Expand Up @@ -253,6 +262,10 @@ export const DEFAULT_SETTINGS: ExecutorSettings = {
kotlinArgs: "-script",
kotlinFileExtension: "kts",
kotlinInject: "",
swiftPath: "swift",
swiftArgs: "",
swiftFileExtension: "swift",
swiftInject: "",
runghcPath: "runghc",
ghcPath: "ghc",
ghciPath: "ghci",
Expand Down Expand Up @@ -299,6 +312,9 @@ export const DEFAULT_SETTINGS: ExecutorSettings = {
zigPath: "zig",
zigArgs: "run",
zigInject: "",
ocamlPath: "ocaml",
ocamlArgs: "",
ocamlInject: "",
jsInteractive: true,
tsInteractive: false,
csInteractive: false,
Expand All @@ -318,6 +334,7 @@ export const DEFAULT_SETTINGS: ExecutorSettings = {
javaInteractive: false,
powershellInteractive: false,
kotlinInteractive: false,
swiftInteractive: false,
mathematicaInteractive: false,
haskellInteractive: false,
scalaInteractive: false,
Expand All @@ -330,4 +347,5 @@ export const DEFAULT_SETTINGS: ExecutorSettings = {
maximaInteractive: false,
applescriptInteractive: false,
zigInteractive: false,
ocamlInteractive: false,
}
8 changes: 8 additions & 0 deletions src/settings/SettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import makeOctaviaSettings from "./per-lang/makeOctaveSettings";
import makeMaximaSettings from "./per-lang/makeMaximaSettings";
import makeApplescriptSettings from "./per-lang/makeApplescriptSettings";
import makeZigSettings from "./per-lang/makeZigSettings";
import makeOCamlSettings from "./per-lang/makeOCamlSettings";
import makeSwiftSettings from "./per-lang/makeSwiftSettings";


/**
Expand Down Expand Up @@ -198,6 +200,9 @@ export class SettingsTab extends PluginSettingTab {
// ========== Scala ===========
makeScalaSettings(this, this.makeContainerFor("scala"));

// ========== Swift ===========
makeSwiftSettings(this, this.makeContainerFor("swift"));

// ========== Racket ===========
makeRacketSettings(this, this.makeContainerFor("racket"));

Expand All @@ -222,6 +227,9 @@ export class SettingsTab extends PluginSettingTab {
// ========== Zig ============
makeZigSettings(this, this.makeContainerFor("zig"));

// ========== OCaml ============
makeOCamlSettings(this, this.makeContainerFor("ocaml"));

this.focusContainer(this.plugin.settings.lastOpenLanguageTab || canonicalLanguages[0]);
}

Expand Down
2 changes: 2 additions & 0 deletions src/settings/languageDisplayName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const DISPLAY_NAMES: Record<LanguageId, string> = {
batch: "Batch",
ts: "Typescript",
scala: "Scala",
swift: "Swift",
racket: "Racket",
c: "C",
fsharp: "F#",
Expand All @@ -31,4 +32,5 @@ export const DISPLAY_NAMES: Record<LanguageId, string> = {
maxima: "Maxima",
applescript: "Applescript",
zig: "Zig",
ocaml: "OCaml",
} as const;
Loading

0 comments on commit 766f591

Please sign in to comment.