inserttab to use foreachselection

This commit is contained in:
Víctor Losada Hernández
2026-09-27 23:00:17 +02:00
parent 7b1c576eb7
commit 12c8bdf5cd
@@ -64,37 +64,26 @@ const forEachSelection = (view, fn)=>{
const insertTab = (view)=>{ const insertTab = (view)=>{
// If any selection spans multiple lines, delegates to CodeMirror's indentMore // If any selection spans multiple lines, delegates to CodeMirror's indentMore
// Otherwise inserts two spaces at each cursor/selection // Otherwise inserts two spaces at each cursor/selection
const shouldIndent = view.state.selection.ranges.some((range)=>view.state.doc.lineAt(range.from).number !== const shouldIndent = view.state.selection.ranges.some((range)=>
view.state.doc.lineAt(range.from).number !==
view.state.doc.lineAt(range.to).number view.state.doc.lineAt(range.to).number
); );
if(shouldIndent) return indentMore(view); if(shouldIndent) return indentMore(view);
const changes = []; return forEachSelection(view, (state, range)=>{
const { from, to } = range;
for (const range of view.state.selection.ranges) { return {
changes.push({ changes : {
from : range.from, from,
to : range.to, to,
insert : ' ' // Insert two spaces, not a tab char! insert : ' ' // Insert two spaces, not a tab char!
}); },
} range : EditorSelection.cursor(from + 2)
// Create a transaction so we can map old positions to };
// their new positions after the edits are applied
const mappedChanges = view.state.update({ changes });
view.dispatch({
changes,
selection : EditorSelection.create(
view.state.selection.ranges.map((range)=>EditorSelection.cursor(
mappedChanges.changes.mapPos(range.from, -1) + 2
)
)
)
}); });
return true;
}; };
const wrapSelection = (prefix, suffix)=>(view)=>forEachSelection( const wrapSelection = (prefix, suffix)=>(view)=>forEachSelection(