The rule is that if the current line is not deleted, the cursor remains on the current line, and if the current line is deleted, the cursor is displayed on the nearest line with content
You didn’t mention how you are doing the code formatting. The editor will automatically map the selection through any document changes. But if you are doing something like replacing the entire document with a reformatted version, it obviously doesn’t know which parts of the old doc correspond to which parts of the new doc, so it can’t keep the selection in place.
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);
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:
This cursor should be focused in front of the display in the first d42-visible, not the second(Maybe I’m using it wrong?)
That kind of cursor jump is not something the built-in selection mapping would do (assuming the changes are just the minimal changes needed to perform that update).