on master help sign in
Simplify some code.
commitcc058ad78fa6462b3bbe79035b10653f4c7691b6 authorBlaž Hrastnik <blaz@mxxn.io> dateThu 1 Apr 2021, 11:01 (6 years ago) parent0dbd5b6
3 files changed+40−52
helix-core/src/lib.rs+1 helix-term/src/commands.rs+30−48 helix-term/src/ui/mod.rs+9−4
helix-core/src/lib.rs+1
@@ -53,6 +53,7 @@ pub use {regex, tree_sitter};
5353
5454pub use position::{coords_at_pos, pos_at_coords, Position};
5555pub use selection::{Range, Selection};
56+pub use smallvec::SmallVec;
5657pub use syntax::Syntax;
5758
5859pub use diagnostic::Diagnostic;
helix-term/src/commands.rs+30−48
@@ -1,10 +1,10 @@
11use helix_core::{
2− comment, coords_at_pos, graphemes, match_brackets,
2+ comment, coords_at_pos, graphemes, indent, match_brackets,
33 movement::{self, Direction},
44 object, pos_at_coords,
55 regex::{self, Regex},
66 register, search, selection, Change, ChangeSet, Position, Range, Rope, RopeSlice, Selection,
7− Tendril, Transaction,
7+ SmallVec, Tendril, Transaction,
88};
99
1010use once_cell::sync::Lazy;
@@ -951,26 +951,29 @@ pub fn open_below(cx: &mut Context) {
951951 let (view, doc) = cx.current();
952952 enter_insert_mode(doc);
953953
954+ let text = doc.text().slice(..);
954955 let lines = selection_lines(doc.text(), doc.selection(view.id));
955956
956− let positions = lines.into_iter().map(|index| {
957− // adjust all positions to the end of the line (next line minus one)
958− doc.text().line_to_char(index + 1).saturating_sub(1)
959− });
957+ let mut ranges = SmallVec::with_capacity(lines.len());
960958
961− let text = doc.text().slice(..);
959+ let changes: Vec<Change> = lines
960+ .into_iter()
961+ .map(|line| {
962+ // adjust all positions to the end of the line (next line minus one)
963+ let index = doc.text().line_to_char(line + 1).saturating_sub(1);
962964
963− let changes: Vec<Change> = positions
964− .map(|index| {
965965 // TODO: share logic with insert_newline for indentation
966− let indent_level =
967− helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
966+ let indent_level = indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
968967 let indent = doc.indent_unit().repeat(indent_level);
969968 let mut text = String::with_capacity(1 + indent.len());
970969 text.push('\n');
971970 text.push_str(&indent);
972971 let text = text.repeat(count);
973972
973+ // calculate new selection range
974+ let pos = index + text.len();
975+ ranges.push(Range::new(pos, pos));
976+
974977 (index, index, Some(text.into()))
975978 })
976979 .collect();
@@ -978,17 +981,7 @@ pub fn open_below(cx: &mut Context) {
978981 // TODO: count actually inserts "n" new lines and starts editing on all of them.
979982 // TODO: append "count" newlines and modify cursors to those lines
980983
981− let selection = Selection::new(
982− changes
983− .iter()
984− .map(|(start, _end, text): &Change| {
985− let len = text.as_ref().map(|text| text.len()).unwrap(); // minus newline
986− let pos = start + len;
987− Range::new(pos, pos)
988− })
989− .collect(),
990− 0,
991− );
984+ let selection = Selection::new(ranges, 0);
992985
993986 let transaction =
994987 Transaction::change(doc.text(), changes.into_iter()).with_selection(selection);
@@ -1002,26 +995,29 @@ pub fn open_above(cx: &mut Context) {
1002995 let (view, doc) = cx.current();
1003996 enter_insert_mode(doc);
1004997
998+ let text = doc.text().slice(..);
1005999 let lines = selection_lines(doc.text(), doc.selection(view.id));
10061000
1007− let positions = lines.into_iter().map(|index| {
1008− // adjust all positions to the end of the previous line
1009− doc.text().line_to_char(index).saturating_sub(1)
1010− });
1001+ let mut ranges = SmallVec::with_capacity(lines.len());
10111002
1012− let text = doc.text().slice(..);
1003+ let changes: Vec<Change> = lines
1004+ .into_iter()
1005+ .map(|line| {
1006+ // adjust all positions to the end of the previous line
1007+ let index = doc.text().line_to_char(line).saturating_sub(1);
10131008
1014− let changes: Vec<Change> = positions
1015− .map(|index| {
10161009 // TODO: share logic with insert_newline for indentation
1017− let indent_level =
1018− helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
1010+ let indent_level = indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
10191011 let indent = doc.indent_unit().repeat(indent_level);
10201012 let mut text = String::with_capacity(1 + indent.len());
10211013 text.push('\n');
10221014 text.push_str(&indent);
10231015 let text = text.repeat(count);
10241016
1017+ // calculate new selection range
1018+ let pos = index + text.len();
1019+ ranges.push(Range::new(pos, pos));
1020+
10251021 // generate changes
10261022 (index, index, Some(text.into()))
10271023 })
@@ -1030,17 +1026,7 @@ pub fn open_above(cx: &mut Context) {
10301026 // TODO: count actually inserts "n" new lines and starts editing on all of them.
10311027 // TODO: append "count" newlines and modify cursors to those lines
10321028
1033− let selection = Selection::new(
1034− changes
1035− .iter()
1036− .map(|(start, _end, text): &Change| {
1037− let len = text.as_ref().map(|text| text.len()).unwrap(); // minus newline
1038− let pos = start + len;
1039− Range::new(pos, pos)
1040− })
1041− .collect(),
1042− 0,
1043− );
1029+ let selection = Selection::new(ranges, 0);
10441030
10451031 let transaction =
10461032 Transaction::change(doc.text(), changes.into_iter()).with_selection(selection);
@@ -1359,12 +1345,8 @@ pub mod insert {
13591345 let transaction =
13601346 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
13611347 // TODO: offset range.head by 1? when calculating?
1362− let indent_level = helix_core::indent::suggested_indent_for_pos(
1363− doc.syntax(),
1364− text,
1365− range.head,
1366− true,
1367− );
1348+ let indent_level =
1349+ indent::suggested_indent_for_pos(doc.syntax(), text, range.head, true);
13681350 let indent = doc.indent_unit().repeat(indent_level);
13691351 let mut text = String::with_capacity(1 + indent.len());
13701352 text.push('\n');
helix-term/src/ui/mod.rs+9−4
@@ -82,7 +82,7 @@ pub fn file_picker(root: PathBuf) -> Picker<PathBuf> {
8282 let files = Walk::new(root.clone()).filter_map(|entry| match entry {
8383 Ok(entry) => {
8484 // filter dirs, but we might need special handling for symlinks!
85− if !entry.file_type().unwrap().is_dir() {
85+ if !entry.file_type().map_or(false, |entry| entry.is_dir()) {
8686 Some(entry.into_path())
8787 } else {
8888 None
@@ -97,7 +97,11 @@ pub fn file_picker(root: PathBuf) -> Picker<PathBuf> {
9797 files.take(MAX).collect(),
9898 move |path: &PathBuf| {
9999 // format_fn
100− path.strip_prefix(&root).unwrap().to_str().unwrap().into()
100+ path.strip_prefix(&root)
101+ .unwrap_or(path)
102+ .to_str()
103+ .unwrap()
104+ .into()
101105 },
102106 move |editor: &mut Editor, path: &PathBuf, action| {
103107 let document_id = editor
@@ -144,12 +148,13 @@ pub mod completers {
144148 file.ok().map(|entry| {
145149 let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir());
146150
147− let mut path = entry.path().strip_prefix(&dir).unwrap().to_path_buf();
151+ let path = entry.path();
152+ let mut path = path.strip_prefix(&dir).unwrap_or(path).to_path_buf();
148153
149154 if is_dir {
150155 path.push("");
151156 }
152− let path = path.to_str().unwrap().to_string();
157+ let path = path.to_str().unwrap().to_owned();
153158 (end.clone(), Cow::from(path))
154159 })
155160 }) // TODO: unwrap or skip