src/main/core.js/format
function format(text, opts) {
  const selectedParser = parser.resolveParser(opts);
  const hasPragma = !selectedParser.hasPragma || selectedParser.hasPragma(text);
  if (opts.requirePragma && !hasPragma) {
    return { formatted: text };
  }

  if (opts.endOfLine === "auto") {
    opts.endOfLine = guessEndOfLine(text);
  }

  const hasCursor = opts.cursorOffset >= 0;
  const hasRangeStart = opts.rangeStart > 0;
  const hasRangeEnd = opts.rangeEnd < text.length;

  // get rid of CR/CRLF parsing
  if (text.includes("\r")) {
    const offsetKeys = [
      hasCursor && "cursorOffset",
      hasRangeStart && "rangeStart",
      hasRangeEnd && "rangeEnd",
    ]
      .filter(Boolean)
      .sort((aKey, bKey) => opts[aKey] - opts[bKey]);

    for (let i = offsetKeys.length - 1; i >= 0; i--) {
      const key = offsetKeys[i];
      text =
        text.slice(0, opts[key]) + PLACEHOLDERS[key] + text.slice(opts[key]);
    }

    text = text.replace(/\r\n?/g, "\n");

    for (let i = 0; i < offsetKeys.length; i++) {
      const key = offsetKeys[i];
      text = text.replace(PLACEHOLDERS[key], (_, index) => {
        opts[key] = index;
        return "";
      });
    }
  }

  const hasUnicodeBOM = text.charAt(0) === BOM;
  if (hasUnicodeBOM) {
    text = text.slice(1);
    if (hasCursor) {
      opts.cursorOffset++;
    }
    if (hasRangeStart) {
      opts.rangeStart++;
    }
    if (hasRangeEnd) {
      opts.rangeEnd++;
    }
  }

  if (!hasCursor) {
    opts.cursorOffset = -1;
  }
  if (opts.rangeStart < 0) {
    opts.rangeStart = 0;
  }
  if (opts.rangeEnd > text.length) {
    opts.rangeEnd = text.length;
  }

  const result =
    hasRangeStart || hasRangeEnd
      ? formatRange(text, opts)
      : coreFormat(
          opts.insertPragma && opts.printer.insertPragma && !hasPragma
            ? opts.printer.insertPragma(text)
            : text,
          opts
        );

  if (hasUnicodeBOM) {
    result.formatted = BOM + result.formatted;

    if (hasCursor) {
      result.cursorOffset++;
    }
  }

  return result;
}