Skip to content

Commit c15090d

Browse files
committed
Improve handling of ambiguous diffs in contenteditable input mode
1 parent 3842167 commit c15090d

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/input/ContentEditableInput.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ export default class ContentEditableInput {
279279
while (cutEnd < maxCutEnd &&
280280
newBot.charCodeAt(newBot.length - cutEnd - 1) == oldBot.charCodeAt(oldBot.length - cutEnd - 1))
281281
++cutEnd
282+
// Try to move start of change to start of selection if ambiguous
283+
if (newText.length == 1 && oldText.length == 1 && fromLine == from.line) {
284+
while (cutFront && cutFront > from.ch &&
285+
newBot.charCodeAt(newBot.length - cutEnd - 1) == oldBot.charCodeAt(oldBot.length - cutEnd - 1)) {
286+
cutFront--
287+
cutEnd++
288+
}
289+
}
282290

283291
newText[newText.length - 1] = newBot.slice(0, newBot.length - cutEnd).replace(/^\u200b+/, "")
284292
newText[0] = newText[0].slice(cutFront).replace(/\u200b+$/, "")

test/contenteditable_test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@
6060
eq(cm.getValue(), "one\ntwothree\nfour\n")
6161
}, {inputStyle: "contenteditable", value: "one\ntwo\nfoo\nbar\nthree\nfour\n"})
6262

63+
testCM("ambiguous_diff_middle", function(cm) {
64+
cm.setSelection(Pos(0, 2))
65+
findTextNode(cm, "baah").nodeValue = "baaah"
66+
cm.display.input.updateFromDOM()
67+
eqCharPos(cm.getCursor(), Pos(0, 3))
68+
}, {inputStyle: "contenteditable", value: "baah"})
69+
70+
testCM("ambiguous_diff_start", function(cm) {
71+
cm.setSelection(Pos(0, 1))
72+
findTextNode(cm, "baah").nodeValue = "baaah"
73+
cm.display.input.updateFromDOM()
74+
eqCharPos(cm.getCursor(), Pos(0, 2))
75+
}, {inputStyle: "contenteditable", value: "baah"})
76+
77+
testCM("ambiguous_diff_end", function(cm) {
78+
cm.setSelection(Pos(0, 3))
79+
findTextNode(cm, "baah").nodeValue = "baaah"
80+
cm.display.input.updateFromDOM()
81+
eqCharPos(cm.getCursor(), Pos(0, 4))
82+
}, {inputStyle: "contenteditable", value: "baah"})
83+
6384
testCM("force_redraw", function(cm) {
6485
findTextNode(cm, "foo").parentNode.appendChild(document.createElement("hr")).className = "inserted"
6586
cm.display.input.updateFromDOM()

0 commit comments

Comments
 (0)