Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Aug 29, 2023
1 parent 4d921f9 commit bc847d1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
3 changes: 2 additions & 1 deletion po/POTFILES
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ src/DocumentationViewer.js
src/langs/css/css.js

src/langs/rust/Compiler.js
troll/src/async.js
troll/src/async.js
src/lib/prettier-estree.js
7 changes: 7 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export default [
},
},

{
input: "node_modules/prettier/plugins/estree.mjs",
output: {
file: "src/lib/prettier-estree.js",
},
},

{
input: "node_modules/prettier/plugins/postcss.mjs",
output: {
Expand Down
6 changes: 5 additions & 1 deletion src/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export default function Document({ session, code_view, file, lang }) {
saveSourceBuffer({ source_file, buffer })
.catch(logError)
.finally(() => {
session.settings.set_boolean("edited", true);
try {
session.settings.set_boolean("edited", true);
} catch (err) {
logError(err);
}
});
}

Expand Down
37 changes: 37 additions & 0 deletions src/lib/prettier-estree.js

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Devtools from "./Devtools.js";
import prettier from "./lib/prettier.js";
import prettier_babel from "./lib/prettier-babel.js";
import prettier_postcss from "./lib/prettier-postcss.js";
import prettier_estree from "./lib/prettier-estree.js";
import Previewer from "./Previewer/Previewer.js";
import ValaCompiler from "./langs/vala/Compiler.js";
import RustCompiler from "./langs/rust/Compiler.js";
Expand Down Expand Up @@ -94,6 +95,7 @@ export default function Window({ application, session }) {
code_view: builder.get_object("code_view_rust"),
file: file.get_child("code.rs"),
lang: langs.rust,
session,
});
langs.rust.document = document_rust;

Expand Down Expand Up @@ -193,13 +195,13 @@ export default function Window({ application, session }) {
previewer.openInspector().catch(logError);
});

function format(code_view, formatter) {
async function format(code_view, formatter) {
let code;

const { buffer } = code_view;

try {
code = formatter(buffer.text.trim());
code = await formatter(buffer.text.trim());
} catch (err) {
logError(err);
return;
Expand Down Expand Up @@ -242,25 +244,24 @@ export default function Window({ application, session }) {
return stdout;
}

function formatCode() {
async function formatCode() {
if (panel_code.panel.visible) {
if (panel_code.language === "JavaScript") {
format(langs.javascript.document.code_view, (text) => {
await format(langs.javascript.document.code_view, (text) => {
return prettier.format(text, {
parser: "babel",
plugins: [prettier_babel],
trailingComma: "all",
plugins: [prettier_babel, prettier_estree],
});
});
} else if (panel_code.language === "Rust") {
format(langs.rust.document.code_view, (text) => {
await format(langs.rust.document.code_view, (text) => {
return formatRustCode(text);
});
}
}

if (builder.get_object("panel_style").visible) {
format(langs.css.document.code_view, (text) => {
await format(langs.css.document.code_view, (text) => {
return prettier.format(text, {
parser: "css",
plugins: [prettier_postcss],
Expand All @@ -269,7 +270,7 @@ export default function Window({ application, session }) {
}

if (panel_ui.panel.visible) {
format(langs.xml.document.code_view, (text) => {
await format(langs.xml.document.code_view, (text) => {
return xml.format(text, 2);
});
}
Expand All @@ -290,7 +291,7 @@ export default function Window({ application, session }) {
await panel_ui.update();

if (format) {
formatCode();
await formatCode();
}

if (language === "JavaScript") {
Expand Down

0 comments on commit bc847d1

Please sign in to comment.