Skip to content
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: 3 additions & 11 deletions src/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,13 @@ async function loadSourceBuffer({ source_file, buffer, lang }) {
buffer,
file: source_file,
});
let success;
try {
success = await file_loader.load_async(GLib.PRIORITY_DEFAULT, null, null);
await file_loader.load_async(GLib.PRIORITY_DEFAULT, null, null);
} catch (err) {
if (err.code === Gio.IOErrorEnum.NOT_FOUND) {
if (lang.placeholder) {
buffer.set_text(lang.placeholder, -1);
}
success = true;
} else {
if (err.code !== Gio.IOErrorEnum.NOT_FOUND) {
throw err;
}
}

if (success) {
buffer.set_modified(false);
}
buffer.set_modified(false);
}
6 changes: 1 addition & 5 deletions src/Library/EntryRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ class EntryRow extends Adw.PreferencesRow {
}

#createLanguageTags(demo) {
const language = getLanguage("javascript");
const language_tag = this.#createLanguageTag(language);
this._languages_box.append(language_tag);
Comment on lines -34 to -36
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was necessary because demos without Code didn't have a .js file (but they had Rust and Vala files that did nothing)

With this we don't show language for demos without code anymore


["vala", "rust"].forEach((id) => {
["javascript", "vala", "rust"].forEach((id) => {
const language = getLanguage(id);
if (!demo.languages.includes(language.id)) return;
const language_tag = this.#createLanguageTag(language);
Expand Down
17 changes: 13 additions & 4 deletions src/Library/Library.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
demos_dir,
getLanguage,
settings as global_settings,
languages,
} from "../util.js";
import Window from "../window.js";

Expand Down Expand Up @@ -71,7 +72,6 @@ export function getDemo(name) {
return demos.find((demo) => demo.name === name);
}

const lang_javascript = getLanguage("javascript");
async function openDemo({ application, demo_name, language }) {
const demo = getDemo(demo_name);
const session = createSessionFromDemo(demo);
Expand All @@ -86,9 +86,18 @@ async function openDemo({ application, demo_name, language }) {
session.settings.set_boolean("show-code", true);
}

const is_js =
session.settings.get_int("code-language") === lang_javascript.index;
const code_languge = session.settings.get_int("code-language");
const lang = languages.find((lang) => lang.index === code_languge);

// Override the user preferred language if the demo doesn't support it
if (demo.languages.length > 0 && !demo.languages.includes(lang.id)) {
session.settings.set_int(
"code-language",
getLanguage(demo.languages[0]).index,
);
}

const run = demo.autorun && lang.id === "javascript";
const { load } = Window({ application, session });
await load({ run: demo.autorun && is_js });
await load({ run });
}
1 change: 0 additions & 1 deletion src/Library/demos/Advanced Buttons/code.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/Library/demos/Advanced Buttons/main.vala

This file was deleted.

1 change: 0 additions & 1 deletion src/Library/demos/Boxed Lists/code.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/Library/demos/Boxed Lists/main.vala

This file was deleted.

1 change: 0 additions & 1 deletion src/Library/demos/Center Box/code.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/Library/demos/Center Box/main.vala

This file was deleted.

1 change: 0 additions & 1 deletion src/Library/demos/Editable Label/code.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/Library/demos/Header Bar/code.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/Library/demos/Header Bar/main.vala

This file was deleted.

1 change: 0 additions & 1 deletion src/Library/demos/Navigation Split View/code.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/Library/demos/Navigation Split View/main.vala

This file was deleted.

1 change: 0 additions & 1 deletion src/Library/demos/Pango Markup/code.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/Library/demos/Pango Markup/main.vala

This file was deleted.

1 change: 0 additions & 1 deletion src/Library/demos/Platform Tools/code.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/Library/demos/Platform Tools/main.vala

This file was deleted.

1 change: 0 additions & 1 deletion src/Library/demos/Window/code.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/Library/demos/Window/main.vala

This file was deleted.

2 changes: 0 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export const languages = [
extensions: [".vala"],
types: ["text/x-vala"],
document: null,
placeholder: "// Sorry, this demo is not available in Vala yet.",
default_file: "main.vala",
index: 1,
},
Expand All @@ -94,7 +93,6 @@ export const languages = [
extensions: [".rs"],
types: ["text/x-rust"],
document: null,
placeholder: "// Sorry, this demo is not available in Rust yet.",
default_file: "code.rs",
index: 2,
},
Expand Down
134 changes: 73 additions & 61 deletions src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,74 +276,14 @@ export default function Window({ application, session }) {
previewer.stop();
panel_ui.stop();

const { language } = panel_code;
try {
await panel_ui.update();

if (format) {
await formatCode();
}

if (language === "JavaScript") {
await previewer.update(true);

// We have to create a new file each time
// because gjs doesn't appear to use etag for module caching
// ?foo=Date.now() also does not work as expected
// TODO: File a bug
const [file_javascript] = Gio.File.new_tmp("workbench-XXXXXX.js");
await file_javascript.replace_contents_async(
new GLib.Bytes(document_javascript.code_view.buffer.text || " "),
null,
false,
Gio.FileCreateFlags.NONE,
null,
);
let exports;
try {
exports = await import(`file://${file_javascript.get_path()}`);
} catch (err) {
await previewer.update(true);
throw err;
} finally {
file_javascript
.delete_async(GLib.PRIORITY_DEFAULT, null)
.catch(console.error);
}
previewer.setSymbols(exports);
} else if (language === "Vala") {
if (!isValaEnabled()) {
action_extensions.activate(null);
return;
}

compiler_vala = compiler_vala || ValaCompiler({ session });
const success = await compiler_vala.compile();
if (success) {
await previewer.useExternal();
if (await compiler_vala.run()) {
await previewer.open();
} else {
await previewer.useInternal();
}
}
} else if (language === "Rust") {
if (!isRustEnabled()) {
action_extensions.activate(null);
return;
}

compiler_rust = compiler_rust || RustCompiler({ session });
const success = await compiler_rust.compile();
if (success) {
await previewer.useExternal();
if (await compiler_rust.run()) {
await previewer.open();
} else {
await previewer.useInternal();
}
}
}
Comment on lines -287 to -346
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was extracted into the compile function

await compile();
} catch (err) {
// prettier xml errors are not instances of Error
if (err instanceof Error || err instanceof GLib.Error) {
Expand All @@ -360,6 +300,78 @@ export default function Window({ application, session }) {
term_console.scrollToEnd();
}

async function compile() {
const { language } = panel_code;

const lang = langs[language.toLowerCase()];
// Do nothing if there is no code to avoid compile errors
const text = lang.document.code_view.buffer.text.trim();
if (text === "") {
return;
}

if (language === "JavaScript") {
await previewer.update(true);

// We have to create a new file each time
// because gjs doesn't appear to use etag for module caching
// ?foo=Date.now() also does not work as expected
// TODO: File a bug
const [file_javascript] = Gio.File.new_tmp("workbench-XXXXXX.js");
await file_javascript.replace_contents_async(
new GLib.Bytes(text),
null,
false,
Gio.FileCreateFlags.NONE,
null,
);
let exports;
try {
exports = await import(`file://${file_javascript.get_path()}`);
} catch (err) {
await previewer.update(true);
throw err;
} finally {
file_javascript
.delete_async(GLib.PRIORITY_DEFAULT, null)
.catch(console.error);
}
previewer.setSymbols(exports);
} else if (language === "Vala") {
if (!isValaEnabled()) {
action_extensions.activate(null);
return;
}

compiler_vala = compiler_vala || ValaCompiler({ session });
const success = await compiler_vala.compile();
if (success) {
await previewer.useExternal();
if (await compiler_vala.run()) {
await previewer.open();
} else {
await previewer.useInternal();
}
}
} else if (language === "Rust") {
if (!isRustEnabled()) {
action_extensions.activate(null);
return;
}

compiler_rust = compiler_rust || RustCompiler({ session });
const success = await compiler_rust.compile();
if (success) {
await previewer.useExternal();
if (await compiler_rust.run()) {
await previewer.open();
} else {
await previewer.useInternal();
}
}
}
}

const action_run = new Gio.SimpleAction({
name: "run",
});
Expand Down