on master help sign in
Extract a method for view alignment.
commit1be8b2005d41dc5b396e2b46fbc83dc98c3e0416 authorBlaž Hrastnik <blaz@mxxn.io> dateSat 8 May 2021, 15:36 (5 years ago) parentadaf861
1 file changed+32−27
helix-term/src/commands.rs+32−27
helix-term/src/commands.rs+32−27
@@ -98,6 +98,25 @@ impl<'a> Context<'a> {
9898 }
9999}
100100
101+enum Align {
102+ Top,
103+ Center,
104+ Bottom,
105+}
106+
107+fn align_view(doc: &Document, view: &mut View, align: Align) {
108+ let pos = doc.selection(view.id).cursor();
109+ let line = doc.text().char_to_line(pos);
110+
111+ let relative = match align {
112+ Align::Center => view.area.height as usize / 2,
113+ Align::Top => 0,
114+ Align::Bottom => view.area.height as usize,
115+ };
116+
117+ view.first_line = line.saturating_sub(relative);
118+}
119+
101120/// A command is a function that takes the current state and a count, and does a side-effect on the
102121/// state (usually by creating and applying a transaction).
103122pub type Command = fn(cx: &mut Context);
@@ -616,6 +635,7 @@ fn _search(doc: &mut Document, view: &mut View, contents: &str, regex: &Regex, e
616635 let mat = regex
617636 .find_at(contents, start)
618637 .or_else(|| regex.find(contents));
638+ // TODO: message on wraparound
619639 if let Some(mat) = mat {
620640 let start = text.byte_to_char(mat.start());
621641 let end = text.byte_to_char(mat.end());
@@ -630,9 +650,7 @@ fn _search(doc: &mut Document, view: &mut View, contents: &str, regex: &Regex, e
630650
631651 // TODO: (first_match, regex) stuff in register?
632652 doc.set_selection(view.id, selection);
633− // TODO: extract this centering into a function to share with _goto?
634− let line = doc.text().char_to_line(head);
635− view.first_line = line.saturating_sub(view.area.height as usize / 2);
653+ align_view(doc, view, Align::Center);
636654 };
637655}
638656
@@ -1243,8 +1261,7 @@ fn _goto(
12431261 // TODO: convert inside server
12441262 let new_pos = lsp_pos_to_pos(doc.text(), definition_pos, offset_encoding);
12451263 doc.set_selection(view.id, Selection::point(new_pos));
1246− let line = doc.text().char_to_line(new_pos);
1247− view.first_line = line.saturating_sub(view.area.height as usize / 2);
1264+ align_view(doc, view, Align::Center);
12481265 }
12491266
12501267 match locations.as_slice() {
@@ -1946,7 +1963,6 @@ pub fn save(cx: &mut Context) {
19461963 // Spawns an async task to actually do the saving. This way we prevent blocking.
19471964
19481965 // TODO: handle save errors somehow?
1949− // TODO: don't block
19501966 tokio::spawn(cx.doc().save());
19511967}
19521968
@@ -2051,8 +2067,6 @@ pub fn hover(cx: &mut Context) {
20512067
20522068 // TODO: factor out a doc.position_identifier() that returns lsp::TextDocumentPositionIdentifier
20532069
2054− // TODO: blocking here is not ideal, make commands async fn?
2055− // not like we can process additional input meanwhile though
20562070 let pos = pos_to_lsp_pos(
20572071 doc.text(),
20582072 doc.selection(view.id).cursor(),
@@ -2137,11 +2151,9 @@ pub fn jump_forward(cx: &mut Context) {
21372151 if let Some((id, selection)) = view.jumps.forward(count) {
21382152 view.doc = *id;
21392153 let selection = selection.clone();
2140− let cursor = selection.cursor();
21412154 doc.set_selection(view.id, selection);
2142− // TODO: extract this centering into a function to share with _goto?
2143− let line = doc.text().char_to_line(cursor);
2144− view.first_line = line.saturating_sub(view.area.height as usize / 2);
2155+
2156+ align_view(doc, view, Align::Center);
21452157 };
21462158}
21472159
@@ -2151,12 +2163,9 @@ pub fn jump_backward(cx: &mut Context) {
21512163
21522164 if let Some((id, selection)) = view.jumps.backward(count) {
21532165 view.doc = *id;
2154− let selection = selection.clone();
2155− let cursor = selection.cursor();
2156− doc.set_selection(view.id, selection);
2157− // TODO: extract this centering into a function to share with _goto?
2158− let line = doc.text().char_to_line(cursor);
2159− view.first_line = line.saturating_sub(view.area.height as usize / 2);
2166+ doc.set_selection(view.id, selection.clone());
2167+
2168+ align_view(doc, view, Align::Center);
21602169 };
21612170}
21622171
@@ -2216,17 +2225,13 @@ pub fn view_mode(cx: &mut Context) {
22162225 // bottom
22172226 | 'b' => {
22182227 let (view, doc) = cx.current();
2219− let pos = doc.selection(view.id).cursor();
2220− // TODO: extract this centering into a function to share with _goto?
2221− let line = doc.text().char_to_line(pos);
22222228
2223− let relative = match ch {
2224− 'z' | 'c' => view.area.height as usize / 2,
2225− 't' => 0,
2226− 'b' => view.area.height as usize,
2229+ align_view(doc, view, match ch {
2230+ 'z' | 'c' => Align::Center,
2231+ 't' => Align::Top,
2232+ 'b' => Align::Bottom,
22272233 _ => unreachable!()
2228− };
2229− view.first_line = line.saturating_sub(relative);
2234+ });
22302235 }
22312236 'm' => {
22322237 let (view, doc) = cx.current();