Handle syncing with selections

This commit is contained in:
Andras Schmelczer 2025-05-11 15:50:24 +01:00
parent 35bb7f2405
commit 1598e3c4d5
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -55,20 +55,31 @@ export class ObsidianFileSystemOperations implements FileSystemOperations {
const view = this.workspace.getActiveViewOfType(MarkdownView); const view = this.workspace.getActiveViewOfType(MarkdownView);
if (view?.file?.path === path) { if (view?.file?.path === path) {
const cursor = view.editor.getCursor();
const text = view.editor.getValue(); const text = view.editor.getValue();
const result = updater({ const cursors = view.editor
text, .listSelections()
cursors: [ .flatMap(({ anchor, head }, i) => [
{ {
id: 0, id: 2 * i,
characterPosition: lineAndColumnToPosition( characterPosition: lineAndColumnToPosition(
text, text,
cursor.line, anchor.line,
cursor.ch anchor.ch
)
},
{
id: 2 * i + 1,
characterPosition: lineAndColumnToPosition(
text,
head.line,
head.ch
) )
} }
] ]);
const result = updater({
text,
cursors
}); });
if (result.text === text) { if (result.text === text) {
@ -77,13 +88,25 @@ export class ObsidianFileSystemOperations implements FileSystemOperations {
view.editor.setValue(result.text); view.editor.setValue(result.text);
result.cursors.forEach((movedCursor) => { const selections = [];
const { line, column } = positionToLineAndColumn( for (let i = 0; i < result.cursors.length / 2; i++) {
result.text, const from = result.cursors[2 * i];
movedCursor.characterPosition const to = result.cursors[2 * i + 1];
); const { line: fromLine, column: fromColumn } =
view.editor.setCursor(line, column); positionToLineAndColumn(
}); result.text,
from.characterPosition
);
const { line: toLine, column: toColumn } =
positionToLineAndColumn(result.text, to.characterPosition);
selections.push({
anchor: { line: fromLine, ch: fromColumn },
head: { line: toLine, ch: toColumn }
});
}
view.editor.setSelections(selections);
return result.text; return result.text;
} }