refactor modifyBy implementations

This commit is contained in:
Muki Kiboigo
2026-02-17 12:11:29 -08:00
parent 3822e3f8d9
commit c4391ff058

View File

@@ -386,32 +386,7 @@ fn modifyByCharacter(self: *Selection, alter: ModifyAlter, forward: bool, range:
var new_node = focus_node; var new_node = focus_node;
var new_offset = focus_offset; var new_offset = focus_offset;
if (!isTextNode(focus_node)) { if (isTextNode(focus_node)) {
if (forward) {
if (focus_node.getChildAt(focus_offset)) |child| {
if (isTextNode(child)) {
new_node = child;
new_offset = 0;
} else if (nextTextNode(child)) |t| {
new_node = t;
new_offset = 0;
}
}
} else {
var idx = focus_offset;
while (idx > 0) {
idx -= 1;
const child = focus_node.getChildAt(idx) orelse break;
var bottom = child;
while (bottom.lastChild()) |c| bottom = c;
if (isTextNode(bottom)) {
new_node = bottom;
new_offset = bottom.getLength();
break;
}
}
}
} else {
if (forward) { if (forward) {
const len = focus_node.getLength(); const len = focus_node.getLength();
if (focus_offset < len) { if (focus_offset < len) {
@@ -428,6 +403,32 @@ fn modifyByCharacter(self: *Selection, alter: ModifyAlter, forward: bool, range:
new_offset = prev.getLength(); new_offset = prev.getLength();
} }
} }
} else {
if (forward) {
if (focus_node.getChildAt(focus_offset)) |child| {
if (isTextNode(child)) {
new_node = child;
new_offset = 0;
} else if (nextTextNode(child)) |t| {
new_node = t;
new_offset = 0;
}
}
} else {
var idx = focus_offset;
while (idx > 0) {
idx -= 1;
const child = focus_node.getChildAt(idx) orelse break;
var bottom = child;
while (bottom.lastChild()) |c| bottom = c;
if (isTextNode(bottom)) {
new_node = bottom;
new_offset = bottom.getLength();
break;
}
}
}
} }
switch (alter) { switch (alter) {
@@ -481,35 +482,7 @@ fn modifyByWord(self: *Selection, alter: ModifyAlter, forward: bool, range: *Ran
var new_node = focus_node; var new_node = focus_node;
var new_offset = focus_offset; var new_offset = focus_offset;
if (!isTextNode(focus_node)) { if (isTextNode(focus_node)) {
if (forward) {
const child = focus_node.getChildAt(focus_offset) orelse {
if (nextTextNode(focus_node)) |next| {
new_node = next;
new_offset = nextWordEnd(next.getData(), 0);
}
return self.applyWordModify(alter, new_node, new_offset, page);
};
const t = if (isTextNode(child)) child else nextTextNode(child) orelse {
return self.applyWordModify(alter, new_node, new_offset, page);
};
new_node = t;
new_offset = nextWordEnd(t.getData(), 0);
} else {
var idx = focus_offset;
while (idx > 0) {
idx -= 1;
const child = focus_node.getChildAt(idx) orelse break;
var bottom = child;
while (bottom.lastChild()) |c| bottom = c;
if (isTextNode(bottom)) {
new_node = bottom;
new_offset = prevWordStart(bottom.getData(), bottom.getLength());
break;
}
}
}
} else {
if (forward) { if (forward) {
const i = nextWordEnd(new_node.getData(), new_offset); const i = nextWordEnd(new_node.getData(), new_offset);
if (i > new_offset) { if (i > new_offset) {
@@ -527,6 +500,39 @@ fn modifyByWord(self: *Selection, alter: ModifyAlter, forward: bool, range: *Ran
new_offset = prevWordStart(prev.getData(), @intCast(prev.getData().len)); new_offset = prevWordStart(prev.getData(), @intCast(prev.getData().len));
} }
} }
} else {
// Search and apply rules on the next Text Node.
// This is either next (on forward) or previous (on backward).
if (forward) {
const child = focus_node.getChildAt(focus_offset) orelse {
if (nextTextNode(focus_node)) |next| {
new_node = next;
new_offset = nextWordEnd(next.getData(), 0);
}
return self.applyWordModify(alter, new_node, new_offset, page);
};
const t = if (isTextNode(child)) child else nextTextNode(child) orelse {
return self.applyWordModify(alter, new_node, new_offset, page);
};
new_node = t;
new_offset = nextWordEnd(t.getData(), 0);
} else {
var idx = focus_offset;
while (idx > 0) {
idx -= 1;
const child = focus_node.getChildAt(idx) orelse break;
var bottom = child;
while (bottom.lastChild()) |c| bottom = c;
if (isTextNode(bottom)) {
new_node = bottom;
new_offset = prevWordStart(bottom.getData(), bottom.getLength());
break;
}
}
}
} }
try self.applyWordModify(alter, new_node, new_offset, page); try self.applyWordModify(alter, new_node, new_offset, page);