src/main/core.js/formatRange
function formatRange(text, opts) {
  const parsed = parser.parse(text, opts);
  const { ast } = parsed;
  text = parsed.text;

  const { rangeStart, rangeEnd } = rangeUtil.calculateRange(text, opts, ast);
  const rangeString = text.slice(rangeStart, rangeEnd);

  // Try to extend the range backwards to the beginning of the line.
  // This is so we can detect indentation correctly and restore it.
  // Use `Math.min` since `lastIndexOf` returns 0 when `rangeStart` is 0
  const rangeStart2 = Math.min(
    rangeStart,
    text.lastIndexOf("\n", rangeStart) + 1
  );
  const indentString = text.slice(rangeStart2, rangeStart).match(/^\s*/)[0];

  const alignmentSize = getAlignmentSize(indentString, opts.tabWidth);

  const rangeResult = coreFormat(
    rangeString,
    {
      ...opts,
      rangeStart: 0,
      rangeEnd: Infinity,
      // track the cursor offset only if it's within our range
      cursorOffset:
        opts.cursorOffset >= rangeStart && opts.cursorOffset < rangeEnd
          ? opts.cursorOffset - rangeStart
          : -1,
    },
    alignmentSize
  );

  // Since the range contracts to avoid trailing whitespace,
  // we need to remove the newline that was inserted by the `format` call.
  const rangeTrimmed = rangeResult.formatted.trimEnd();
  const rangeLeft = text.slice(0, rangeStart);
  const rangeRight = text.slice(rangeEnd);

  let { cursorOffset } = opts;
  if (opts.cursorOffset >= rangeEnd) {
    // handle the case where the cursor was past the end of the range
    cursorOffset =
      opts.cursorOffset - rangeEnd + (rangeStart + rangeTrimmed.length);
  } else if (rangeResult.cursorOffset !== undefined) {
    // handle the case where the cursor was in the range
    cursorOffset = rangeResult.cursorOffset + rangeStart;
  }
  // keep the cursor as it was if it was before the start of the range

  let formatted;
  if (opts.endOfLine === "lf") {
    formatted = rangeLeft + rangeTrimmed + rangeRight;
  } else {
    const eol = convertEndOfLineToChars(opts.endOfLine);
    if (cursorOffset >= 0) {
      const parts = [rangeLeft, rangeTrimmed, rangeRight];
      let partIndex = 0;
      let partOffset = cursorOffset;
      while (partIndex < parts.length) {
        const part = parts[partIndex];
        if (partOffset < part.length) {
          parts[partIndex] =
            parts[partIndex].slice(0, partOffset) +
            PLACEHOLDERS.cursorOffset +
            parts[partIndex].slice(partOffset);
          break;
        }
        partIndex++;
        partOffset -= part.length;
      }
      const [newRangeLeft, newRangeTrimmed, newRangeRight] = parts;
      formatted = (
        newRangeLeft.replace(/\n/g, eol) +
        newRangeTrimmed +
        newRangeRight.replace(/\n/g, eol)
      ).replace(PLACEHOLDERS.cursorOffset, (_, index) => {
        cursorOffset = index;
        return "";
      });
    } else {
      formatted =
        rangeLeft.replace(/\n/g, eol) +
        rangeTrimmed +
        rangeRight.replace(/\n/g, eol);
    }
  }

  return { formatted, cursorOffset };
}