How to map cursor position after formatting code

hi, marijn. This is how I format my code, I don’t replace the whole document,I use the Diff package to compare the differences。
usage1:(prettier API by Node server)

// code represents the old document data, result.code represents the new document data。
const items = diffChars(code, result.code);
    console.log('items: ', items);
    if (items.length > 1) {
      const transaction: Array<TransactionSpec> = [];
      let allCount = 0;
      items.forEach((item) => {
        if (item.removed) {
          const end = allCount + (item.count ?? 0);
          const trans = insertCompletionText(state, '', allCount, end);
          transaction.push(trans);
          allCount = end;
        } else if (item.added) {
          const trans = insertCompletionText(
            state,
            item.value,
            allCount,
            allCount,
          );
          transaction.push(trans);
        } else {
          allCount = allCount + (item.count ?? 0);
        }
      });
      view.dispatch(...transaction);

usage2:(language-server-protocol)

      result = await lspClient.textDocumentFormatting({
          textDocument: { uri: `file://${rootUri}/${openedPath}` },
          options: {
            tabSize: state.tabSize,
            insertSpaces,
          },
        });

if (result && result.result?.length) {
  const items = result.result;
  // $ 先存储更改数据,再一次性插入
  const transaction: Array<TransactionSpec> = [];
  items.forEach((item) => {
    const from = posToOffset(state.doc, item.range.start);
    // to为undefined表示在原有的基础上新增加了一行
    const to = posToOffset(state.doc, item.range.end) ?? state.doc.length;
    if (from !== undefined && to !== undefined) {
      const trans = insertCompletionText(state, item.newText, from, to);
      transaction.push(trans);
    }
  });
 view.dispatch(...transaction);
}

You are right, codemirror does have a built-in selection map, but it does not meet my needs(Not quite consistent with vscode)

The effect of not doing any processing on the cursor is as follows:
codemirror-cursor
This cursor should be focused in front of the display in the first d42-visible, not the second(Maybe I’m using it wrong?)