Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dart support #174

Merged
merged 6 commits into from
Dec 26, 2022
Merged
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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, Golang, Groovy, Kotlin, Java, JavaScript, TypeScript, Lean, Lua, CSharp, Prolog, Rust, Python, R, Wolfram Mathematica, Haskell, Scala, Racket, F#, Shell & Powershell.
The following [languages are supported](#supported-programming-languages): C, CPP, Dart, Golang, Groovy, Kotlin, Java, JavaScript, TypeScript, Lean, Lua, CSharp, Prolog, Rust, Python, R, Wolfram Mathematica, Haskell, Scala, Racket, F#, Shell & Powershell.


Python and Rust 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 Down Expand Up @@ -58,6 +58,18 @@ Console.WriteLine("Hello, World!");
```
</details>

<details>
<summary>Dart</summary>

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

```dart
void main() {
print("Hello World");
}
```
</details>

<details>
<summary>Python</summary>

Expand Down
9 changes: 8 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import runAllCodeBlocks from './runAllCodeBlocks';

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"] as const;
"go", "rust", "java", "powershell", "kotlin", "mathematica", "haskell", "scala", "racket", "fsharp", "c", "dart"] as const;
export const supportedLanguages = [...languageAliases, ...canonicalLanguages] as const;


Expand Down Expand Up @@ -304,6 +304,13 @@ export default class ExecuteCodePlugin extends Plugin {
this.runCodeInShell(transformedCode, out, button, this.settings.luaPath, this.settings.luaArgs, "lua", language, file);
});

} else if (language === "dart") {
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.dartPath, this.settings.dartArgs, "dart", language, file);
});

} else if (language === "cs") {
button.addEventListener("click", async () => {
button.className = runButtonDisabledClass;
Expand Down
8 changes: 8 additions & 0 deletions src/settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export interface ExecutorSettings {
luaPath: string;
luaArgs: string;
luaInject: string;
dartPath: string;
dartArgs: string;
dartInject: string;
csPath: string;
csArgs: string;
csInject: string;
Expand Down Expand Up @@ -99,6 +102,7 @@ export interface ExecutorSettings {
csInteractive: boolean;
leanInteractive: boolean;
luaInteractive: boolean;
dartInteractive: boolean;
pythonInteractive: boolean;
cppInteractive: boolean;
prologInteractive: boolean;
Expand Down Expand Up @@ -141,6 +145,9 @@ export const DEFAULT_SETTINGS: ExecutorSettings = {
luaPath: "lua",
luaArgs: "",
luaInject: "",
dartPath: "dart",
dartArgs: "",
dartInject: "",
csPath: "dotnet-script",
csArgs: "",
csInject: "",
Expand Down Expand Up @@ -219,6 +226,7 @@ export const DEFAULT_SETTINGS: ExecutorSettings = {
csInteractive: false,
leanInteractive: false,
luaInteractive: false,
dartInteractive: false,
pythonInteractive: true,
cppInteractive: false,
prologInteractive: false,
Expand Down
4 changes: 4 additions & 0 deletions src/settings/SettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import makeJsSettings from "./per-lang/makeJsSettings";
import makeKotlinSettings from "./per-lang/makeKotlinSettings";
import makeLeanSettings from "./per-lang/makeLeanSettings";
import makeLuaSettings from "./per-lang/makeLuaSettings";
import makeDartSettings from "./per-lang/makeDartSettings";
import makeMathematicaSettings from "./per-lang/makeMathematicaSettings";
import makePowershellSettings from "./per-lang/makePowershellSettings";
import makePrologSettings from "./per-lang/makePrologSettings";
Expand Down Expand Up @@ -126,6 +127,9 @@ export class SettingsTab extends PluginSettingTab {
// ========== Lua ==========
makeLuaSettings(this, this.makeContainerFor("lua"));

// ========== Dart ==========
makeDartSettings(this, this.makeContainerFor("dart"));

// ========== CSharp ==========
makeCsSettings(this, this.makeContainerFor("cs"));

Expand Down
1 change: 1 addition & 0 deletions src/settings/languageDisplayName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { LanguageId } from "src/main";
export const DISPLAY_NAMES: Record<LanguageId, string> = {
cpp: "C++",
cs: "C#",
dart: "Dart",
go: "Golang",
groovy: "Groovy",
haskell: "Haskell",
Expand Down
26 changes: 26 additions & 0 deletions src/settings/per-lang/makeDartSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Setting } from "obsidian";
import { SettingsTab } from "../SettingsTab";

export default (tab: SettingsTab, containerEl: HTMLElement) => {
containerEl.createEl('h3', { text: 'Dart Settings' });
new Setting(containerEl)
.setName('dart path')
.addText(text => text
.setValue(tab.plugin.settings.dartPath)
.onChange(async (value) => {
const sanitized = tab.sanitizePath(value);
tab.plugin.settings.dartPath = sanitized;
console.log('dart path set to: ' + sanitized);
await tab.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Dart arguments')
.addText(text => text
.setValue(tab.plugin.settings.dartArgs)
.onChange(async (value) => {
tab.plugin.settings.dartArgs = value;
console.log('Dart args set to: ' + value);
await tab.plugin.saveSettings();
}));
tab.makeInjectSetting(containerEl, "dart");
}