UNPKG

@cocalc/project

Version:
116 lines 4.75 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.run_formatter_string = exports.run_formatter = void 0; const { math_escape, math_unescape } = require("@cocalc/util/markdown-utils"); const latex_format_1 = require("./latex-format"); const python_format_1 = require("./python-format"); const xml_format_1 = require("./xml-format"); const bib_format_1 = require("./bib-format"); const r_format_1 = require("./r-format"); const generic_format_1 = __importDefault(require("./generic-format")); const clang_format_1 = require("./clang-format"); const gofmt_1 = require("./gofmt"); const rust_format_1 = require("./rust-format"); const misc_1 = require("@cocalc/util/misc"); const util_1 = require("@cocalc/sync/editor/generic/util"); // mathjax-utils is from upstream project Jupyter const mathjax_utils_1 = require("@cocalc/util/mathjax-utils"); const prettier_lib_1 = require("./prettier-lib"); const async_utils_1 = require("@cocalc/util/async-utils"); async function run_formatter(client, path, options, logger) { // What we do is edit the syncstring with the given path to be "prettier" if possible... const syncstring = client.syncdoc({ path }); if (syncstring == null || syncstring.get_state() == "closed") { return { status: "error", error: "document not fully opened", phase: "format", }; } if (syncstring.get_state() != "ready") { await (0, async_utils_1.once)(syncstring, "ready"); } const doc = syncstring.get_doc(); let formatted, math, input0; let input = (input0 = doc.to_str()); if (options.parser === "markdown") { [input, math] = (0, mathjax_utils_1.remove_math)(math_escape(input)); } try { formatted = await run_formatter_string(path, input, options, logger); } catch (err) { logger.debug(`run_formatter error: ${err.message}`); return { status: "error", phase: "format", error: err.message }; } if (options.parser === "markdown") { formatted = math_unescape((0, mathjax_utils_1.replace_math)(formatted, math)); } // NOTE: the code used to make the change here on the backend. // See https://github.com/sagemathinc/cocalc/issues/4335 for why // that leads to confusion. const patch = (0, util_1.make_patch)(input0, formatted); return { status: "ok", patch }; } exports.run_formatter = run_formatter; async function run_formatter_string(path, input, options, logger) { let formatted; logger.debug(`run_formatter options.parser: "${options.parser}"`); switch (options.parser) { case "latex": case "latexindent": formatted = await (0, latex_format_1.latex_format)(input, options); break; case "python": case "yapf": formatted = await (0, python_format_1.python_format)(input, options, logger); break; case "zig": formatted = await (0, generic_format_1.default)({ command: "zig", args: (tmp) => ["fmt", tmp], input, timeout_s: 30, }); break; case "r": case "formatR": formatted = await (0, r_format_1.r_format)(input, options, logger); break; case "xml-tidy": formatted = await (0, xml_format_1.xml_format)(input, options, logger); break; case "bib-biber": formatted = await (0, bib_format_1.bib_format)(input, options, logger); break; case "clang-format": const ext = (0, misc_1.filename_extension)(path != null ? path : ""); formatted = await (0, clang_format_1.clang_format)(input, ext, options, logger); break; case "gofmt": formatted = await (0, gofmt_1.gofmt)(input, options, logger); break; case "rust": case "rustfmt": formatted = await (0, rust_format_1.rust_format)(input, options, logger); break; default: const prettier = (0, prettier_lib_1.get_prettier)(); if (prettier != null) { formatted = prettier.format(input, options); } else { throw Error("Could not load 'prettier'"); } } return formatted; } exports.run_formatter_string = run_formatter_string; //# sourceMappingURL=index.js.map