@@ -65,14 +65,20 @@ fn handle_open(
6565) -> Transaction {
6666 let mut ranges = SmallVec::with_capacity(selection.len());
6767
68+ let mut offs = 0;
69+
6870 let mut transaction = Transaction::change_by_selection(doc, selection, |range| {
6971 let pos = range.head;
7072 let next = next_char(doc, pos);
7173
72− let head = pos + open.len_utf8();
74+ let head = pos + offs + open.len_utf8();
7375 // if selection, retain anchor, if cursor, move over
7476 ranges.push(Range::new(
75− if range.is_empty() { head } else { range.anchor },
77+ if range.is_empty() {
78+ head
79+ } else {
80+ range.anchor + offs
81+ },
7682 head,
7783 ));
7884
@@ -88,6 +94,8 @@ fn handle_open(
8894 pair.push_char(open);
8995 pair.push_char(close);
9096
97+ offs += 2;
98+
9199 (pos, pos, Some(pair))
92100 }
93101 }
@@ -99,14 +107,20 @@ fn handle_open(
99107fn handle_close(doc: &Rope, selection: &Selection, _open: char, close: char) -> Transaction {
100108 let mut ranges = SmallVec::with_capacity(selection.len());
101109
110+ let mut offs = 0;
111+
102112 let mut transaction = Transaction::change_by_selection(doc, selection, |range| {
103113 let pos = range.head;
104114 let next = next_char(doc, pos);
105115
106− let head = pos + close.len_utf8();
116+ let head = pos + offs + close.len_utf8();
107117 // if selection, retain anchor, if cursor, move over
108118 ranges.push(Range::new(
109− if range.is_empty() { head } else { range.anchor },
119+ if range.is_empty() {
120+ head
121+ } else {
122+ range.anchor + offs
123+ },
110124 head,
111125 ));
112126
@@ -114,6 +128,8 @@ fn handle_close(doc: &Rope, selection: &Selection, _open: char, close: char) ->
114128 // return transaction that moves past close
115129 (pos, pos, None) // no-op
116130 } else {
131+ offs += close.len_utf8();
132+
117133 // TODO: else return (use default handler that inserts close)
118134 (pos, pos, Some(Tendril::from_char(close)))
119135 }