on master help sign in
Replace tendril with smartstring

Slightly smaller API surface, less dependencies.

commitf88c077f992bbfc5ea3623441a9e2b2a0e9ca2b2 authorBlaž Hrastnik <blaz@mxxn.io> dateMon 7 Feb 2022, 14:03 (5 years ago) parentfdb9a16
10 files changed+69−80
Cargo.lock+16−40 helix-core/Cargo.toml+1−1 helix-core/src/auto_pairs.rs+9−5 helix-core/src/increment/date_time.rs+1−1 helix-core/src/increment/number.rs+5−5 helix-core/src/lib.rs+4−1 helix-core/src/transaction.rs+13−11 helix-lsp/src/client.rs+1−1 helix-term/src/commands.rs+18−14 xtask/src/main.rs+1−1
Cargo.lock+16−40
@@ -246,16 +246,6 @@ dependencies = [
246246 "percent-encoding",
247247]
248248
249−[[package]]
250−name = "futf"
251−version = "0.1.5"
252−source = "registry+https://github.com/rust-lang/crates.io-index"
253−checksum = "df420e2e84819663797d1ec6544b13c5be84629e7bb00dc960d6917db2987843"
254−dependencies = [
255− "mac",
256− "new_debug_unreachable",
257−]
258−
259249[[package]]
260250name = "futures-core"
261251version = "0.3.21"
@@ -383,7 +373,7 @@ dependencies = [
383373 "similar",
384374 "slotmap",
385375 "smallvec",
386− "tendril",
376+ "smartstring",
387377 "toml",
388378 "tree-sitter",
389379 "unicode-general-category",
@@ -606,12 +596,6 @@ dependencies = [
606596 "url",
607597]
608598
609−[[package]]
610−name = "mac"
611−version = "0.1.1"
612−source = "registry+https://github.com/rust-lang/crates.io-index"
613−checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4"
614−
615599[[package]]
616600name = "matches"
617601version = "0.1.9"
@@ -655,12 +639,6 @@ dependencies = [
655639 "winapi",
656640]
657641
658−[[package]]
659−name = "new_debug_unreachable"
660−version = "1.0.4"
661−source = "registry+https://github.com/rust-lang/crates.io-index"
662−checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
663−
664642[[package]]
665643name = "ntapi"
666644version = "0.3.6"
@@ -1010,6 +988,21 @@ version = "1.8.0"
1010988source = "registry+https://github.com/rust-lang/crates.io-index"
1011989checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
1012990
991+[[package]]
992+name = "smartstring"
993+version = "0.2.9"
994+source = "registry+https://github.com/rust-lang/crates.io-index"
995+checksum = "31aa6a31c0c2b21327ce875f7e8952322acfcfd0c27569a6e18a647281352c9b"
996+dependencies = [
997+ "static_assertions",
998+]
999+
1000+[[package]]
1001+name = "static_assertions"
1002+version = "1.1.0"
1003+source = "registry+https://github.com/rust-lang/crates.io-index"
1004+checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
1005+
10131006[[package]]
10141007name = "str-buf"
10151008version = "1.0.5"
@@ -1027,17 +1020,6 @@ dependencies = [
10271020 "unicode-xid",
10281021]
10291022
1030−[[package]]
1031−name = "tendril"
1032−version = "0.4.2"
1033−source = "registry+https://github.com/rust-lang/crates.io-index"
1034−checksum = "a9ef557cb397a4f0a5a3a628f06515f78563f2209e64d47055d9dc6052bf5e33"
1035−dependencies = [
1036− "futf",
1037− "mac",
1038− "utf-8",
1039−]
1040−
10411023[[package]]
10421024name = "thiserror"
10431025version = "1.0.30"
@@ -1212,12 +1194,6 @@ dependencies = [
12121194 "serde",
12131195]
12141196
1215−[[package]]
1216−name = "utf-8"
1217−version = "0.7.6"
1218−source = "registry+https://github.com/rust-lang/crates.io-index"
1219−checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
1220−
12211197[[package]]
12221198name = "version_check"
12231199version = "0.9.4"
helix-core/Cargo.toml+1−1
@@ -17,7 +17,7 @@ helix-syntax = { version = "0.6", path = "../helix-syntax" }
1717
1818ropey = "1.3"
1919smallvec = "1.8"
20−tendril = "0.4.2"
20+smartstring = "0.2.9"
2121unicode-segmentation = "1.9"
2222unicode-width = "0.1"
2323unicode-general-category = "0.5"
helix-core/src/auto_pairs.rs+9−5
@@ -214,7 +214,9 @@ fn handle_open(
214214 let change = match next_char {
215215 Some(ch) if !close_before.contains(ch) => {
216216 len_inserted = open.len_utf8();
217− (cursor, cursor, Some(Tendril::from_char(open)))
217+ let mut tendril = Tendril::new();
218+ tendril.push(open);
219+ (cursor, cursor, Some(tendril))
218220 }
219221 // None | Some(ch) if close_before.contains(ch) => {}
220222 _ => {
@@ -252,7 +254,9 @@ fn handle_close(doc: &Rope, selection: &Selection, _open: char, close: char) ->
252254 (cursor, cursor, None) // no-op
253255 } else {
254256 len_inserted += close.len_utf8();
255− (cursor, cursor, Some(Tendril::from_char(close)))
257+ let mut tendril = Tendril::new();
258+ tendril.push(close);
259+ (cursor, cursor, Some(tendril))
256260 };
257261
258262 let next_range = get_next_range(doc, start_range, offs, close, len_inserted);
@@ -290,15 +294,15 @@ fn handle_same(
290294 // return transaction that moves past close
291295 (cursor, cursor, None) // no-op
292296 } else {
293− let mut pair = Tendril::with_capacity(2 * token.len_utf8() as u32);
294− pair.push_char(token);
297+ let mut pair = Tendril::new();
298+ pair.push(token);
295299
296300 // for equal pairs, don't insert both open and close if either
297301 // side has a non-pair char
298302 if (next_char.is_none() || close_before.contains(next_char.unwrap()))
299303 && (prev_char.is_none() || open_before.contains(prev_char.unwrap()))
300304 {
301− pair.push_char(token);
305+ pair.push(token);
302306 }
303307
304308 len_inserted += pair.len();
helix-core/src/increment/date_time.rs+1−1
@@ -451,7 +451,7 @@ mod test {
451451 .unwrap()
452452 .increment(amount)
453453 .1,
454− expected.into()
454+ Tendril::from(expected)
455455 );
456456 }
457457 }
helix-core/src/increment/number.rs+5−5
@@ -371,7 +371,7 @@ mod test {
371371 .unwrap()
372372 .increment(amount)
373373 .1,
374− expected.into()
374+ Tendril::from(expected)
375375 );
376376 }
377377 }
@@ -398,7 +398,7 @@ mod test {
398398 .unwrap()
399399 .increment(amount)
400400 .1,
401− expected.into()
401+ Tendril::from(expected)
402402 );
403403 }
404404 }
@@ -426,7 +426,7 @@ mod test {
426426 .unwrap()
427427 .increment(amount)
428428 .1,
429− expected.into()
429+ Tendril::from(expected)
430430 );
431431 }
432432 }
@@ -472,7 +472,7 @@ mod test {
472472 .unwrap()
473473 .increment(amount)
474474 .1,
475− expected.into()
475+ Tendril::from(expected)
476476 );
477477 }
478478 }
@@ -500,7 +500,7 @@ mod test {
500500 .unwrap()
501501 .increment(amount)
502502 .1,
503− expected.into()
503+ Tendril::from(expected)
504504 );
505505 }
506506 }
helix-core/src/lib.rs+4−1
@@ -212,7 +212,10 @@ use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
212212
213213pub use ropey::{Rope, RopeBuilder, RopeSlice};
214214
215−pub use tendril::StrTendril as Tendril;
215+// pub use tendril::StrTendril as Tendril;
216+pub use smartstring::SmartString;
217+
218+pub type Tendril = SmartString<smartstring::LazyCompact>;
216219
217220#[doc(inline)]
218221pub use {regex, tree_sitter};
helix-core/src/transaction.rs+13−11
@@ -85,7 +85,7 @@ impl ChangeSet {
8585
8686 let new_last = match self.changes.as_mut_slice() {
8787 [.., Insert(prev)] | [.., Insert(prev), Delete(_)] => {
88− prev.push_tendril(&fragment);
88+ prev.push_str(&fragment);
8989 return;
9090 }
9191 [.., last @ Delete(_)] => std::mem::replace(last, Insert(fragment)),
@@ -189,7 +189,7 @@ impl ChangeSet {
189189 // TODO: cover this with a test
190190 // figure out the byte index of the truncated string end
191191 let (pos, _) = s.char_indices().nth(j).unwrap();
192− s.pop_front(pos as u32);
192+ s.replace_range(0..pos, "");
193193 head_a = Some(Insert(s));
194194 head_b = changes_b.next();
195195 }
@@ -211,9 +211,11 @@ impl ChangeSet {
211211 Ordering::Greater => {
212212 // figure out the byte index of the truncated string end
213213 let (pos, _) = s.char_indices().nth(j).unwrap();
214− let pos = pos as u32;
215− changes.insert(s.subtendril(0, pos));
216− head_a = Some(Insert(s.subtendril(pos, s.len() as u32 - pos)));
214+ let mut before = s;
215+ let after = before.split_off(pos);
216+
217+ changes.insert(before);
218+ head_a = Some(Insert(after));
217219 head_b = changes_b.next();
218220 }
219221 }
@@ -277,7 +279,7 @@ impl ChangeSet {
277279 }
278280 Delete(n) => {
279281 let text = Cow::from(original_doc.slice(pos..pos + *n));
280− changes.insert(Tendril::from_slice(&text));
282+ changes.insert(Tendril::from(text.as_ref()));
281283 pos += n;
282284 }
283285 Insert(s) => {
@@ -710,19 +712,19 @@ mod test {
710712 #[test]
711713 fn optimized_composition() {
712714 let mut state = State::new("".into());
713− let t1 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('h'));
715+ let t1 = Transaction::insert(&state.doc, &state.selection, Tendril::from("h"));
714716 t1.apply(&mut state.doc);
715717 state.selection = state.selection.clone().map(t1.changes());
716− let t2 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('e'));
718+ let t2 = Transaction::insert(&state.doc, &state.selection, Tendril::from("e"));
717719 t2.apply(&mut state.doc);
718720 state.selection = state.selection.clone().map(t2.changes());
719− let t3 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('l'));
721+ let t3 = Transaction::insert(&state.doc, &state.selection, Tendril::from("l"));
720722 t3.apply(&mut state.doc);
721723 state.selection = state.selection.clone().map(t3.changes());
722− let t4 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('l'));
724+ let t4 = Transaction::insert(&state.doc, &state.selection, Tendril::from("l"));
723725 t4.apply(&mut state.doc);
724726 state.selection = state.selection.clone().map(t4.changes());
725− let t5 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('o'));
727+ let t5 = Transaction::insert(&state.doc, &state.selection, Tendril::from("o"));
726728 t5.apply(&mut state.doc);
727729 state.selection = state.selection.clone().map(t5.changes());
728730
helix-lsp/src/client.rs+1−1
@@ -438,7 +438,7 @@ impl Client {
438438
439439 changes.push(lsp::TextDocumentContentChangeEvent {
440440 range: Some(lsp::Range::new(start, end)),
441− text: s.into(),
441+ text: s.to_string(),
442442 range_length: None,
443443 });
444444 }
helix-term/src/commands.rs+18−14
@@ -2769,7 +2769,7 @@ pub mod cmd {
27692769
27702770 let mut fragments: Vec<_> = selection
27712771 .fragments(text)
2772− .map(|fragment| Tendril::from_slice(&fragment))
2772+ .map(|fragment| Tendril::from(fragment.as_ref()))
27732773 .collect();
27742774
27752775 fragments.sort_by(match reverse {
@@ -3921,7 +3921,7 @@ fn try_restore_indent(doc: &mut Document, view_id: ViewId) {
39213921 if let [Operation::Retain(move_pos), Operation::Insert(ref inserted_str), Operation::Retain(_)] =
39223922 changes
39233923 {
3924− move_pos + inserted_str.len32() as usize == pos
3924+ move_pos + inserted_str.len() == pos
39253925 && inserted_str.starts_with('\n')
39263926 && inserted_str.chars().skip(1).all(char_is_whitespace)
39273927 && pos == line_end_pos // ensure no characters exists after current position
@@ -4513,7 +4513,8 @@ pub mod insert {
45134513 #[allow(clippy::unnecessary_wraps)] // need to use Option<> because of the Hook signature
45144514 fn insert(doc: &Rope, selection: &Selection, ch: char) -> Option<Transaction> {
45154515 let cursors = selection.clone().cursors(doc.slice(..));
4516− let t = Tendril::from_char(ch);
4516+ let mut t = Tendril::new();
4517+ t.push(ch);
45174518 let transaction = Transaction::insert(doc, &cursors, t);
45184519 Some(transaction)
45194520 }
@@ -5015,12 +5016,12 @@ fn replace_with_yanked(cx: &mut Context) {
50155016 let repeat = std::iter::repeat(
50165017 values
50175018 .last()
5018− .map(|value| Tendril::from_slice(&value.repeat(count)))
5019+ .map(|value| Tendril::from(&value.repeat(count)))
50195020 .unwrap(),
50205021 );
50215022 let mut values = values
50225023 .iter()
5023− .map(|value| Tendril::from_slice(&value.repeat(count)))
5024+ .map(|value| Tendril::from(&value.repeat(count)))
50245025 .chain(repeat);
50255026 let selection = doc.selection(view.id);
50265027 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
@@ -5530,7 +5531,7 @@ fn rotate_selection_contents(cx: &mut Context, direction: Direction) {
55305531 let selection = doc.selection(view.id);
55315532 let mut fragments: Vec<_> = selection
55325533 .fragments(text)
5533− .map(|fragment| Tendril::from_slice(&fragment))
5534+ .map(|fragment| Tendril::from(fragment.as_ref()))
55345535 .collect();
55355536
55365537 let group = count
@@ -5891,8 +5892,12 @@ fn surround_add(cx: &mut Context) {
58915892
58925893 let mut changes = Vec::with_capacity(selection.len() * 2);
58935894 for range in selection.iter() {
5894− changes.push((range.from(), range.from(), Some(Tendril::from_char(open))));
5895− changes.push((range.to(), range.to(), Some(Tendril::from_char(close))));
5895+ let mut o = Tendril::new();
5896+ o.push(open);
5897+ let mut c = Tendril::new();
5898+ c.push(close);
5899+ changes.push((range.from(), range.from(), Some(o)));
5900+ changes.push((range.to(), range.to(), Some(c)));
58965901 }
58975902
58985903 let transaction = Transaction::change(doc.text(), changes.into_iter());
@@ -5921,11 +5926,9 @@ fn surround_replace(cx: &mut Context) {
59215926 let transaction = Transaction::change(
59225927 doc.text(),
59235928 change_pos.iter().enumerate().map(|(i, &pos)| {
5924− (
5925− pos,
5926− pos + 1,
5927− Some(Tendril::from_char(if i % 2 == 0 { open } else { close })),
5928− )
5929+ let mut t = Tendril::new();
5930+ t.push(if i % 2 == 0 { open } else { close });
5931+ (pos, pos + 1, Some(t))
59295932 }),
59305933 );
59315934 doc.apply(&transaction, view.id);
@@ -6065,8 +6068,9 @@ fn shell_impl(
60656068 log::error!("Shell error: {}", String::from_utf8_lossy(&output.stderr));
60666069 }
60676070
6068− let tendril = Tendril::try_from_byte_slice(&output.stdout)
6071+ let str = std::str::from_utf8(&output.stdout)
60696072 .map_err(|_| anyhow!("Process did not output valid UTF-8"))?;
6073+ let tendril = Tendril::from(str);
60706074 Ok((tendril, output.status.success()))
60716075}
60726076
xtask/src/main.rs+1−1
@@ -120,7 +120,7 @@ pub mod md_gen {
120120 }
121121
122122 fn md_table_row(cols: &[String]) -> String {
123− "| ".to_owned() + &cols.join(" | ") + " |\n"
123+ format!("| {} |\n", cols.join(" | "))
124124 }
125125
126126 fn md_mono(s: &str) -> String {