on master help sign in
fixed page up/down
commit88f93399fd4a451f529693269e4154421c8a6d06 authorJan Hrastnik <jan.hrastnik2@gmail.com> dateMon 5 Oct 2020, 17:58 (6 years ago) parent1035b2a
2 files changed+51−4
helix-view/src/commands.rs+40−2 helix-view/src/keymap.rs+11−2
helix-view/src/commands.rs+40−2
@@ -133,22 +133,60 @@ pub fn move_file_end(view: &mut View, _count: usize) {
133133 view.state.mode = Mode::Normal;
134134}
135135
136+pub fn check_cursor_in_view(view: &mut View) -> bool {
137+ let cursor = view.state.selection().cursor();
138+ let line = view.state.doc().char_to_line(cursor) as u16;
139+ let document_end = view.first_line + view.size.1.saturating_sub(1) - 1;
140+ let padding = 5u16;
141+
142+ if (line > document_end.saturating_sub(padding)) | (line < view.first_line + padding) {
143+ return false;
144+ }
145+ true
146+}
147+
136148pub fn page_up(view: &mut View, _count: usize) {
149+ let text = &view.state.doc;
137150 view.first_line = view.first_line.saturating_sub(view.size.1);
138− view.state.selection = Selection::single(view.first_line as usize, view.first_line as usize);
151+
152+ view.state.selection = Selection::single(
153+ text.line_to_char(view.first_line as usize),
154+ text.line_to_char(view.first_line as usize),
155+ );
139156}
140157
141158pub fn page_down(view: &mut View, _count: usize) {
159+ let text = &view.state.doc;
142160 view.first_line += view.size.1;
143− view.state.selection = Selection::single(view.first_line as usize, view.first_line as usize);
161+
162+ view.state.selection = Selection::single(
163+ text.line_to_char(view.first_line as usize),
164+ text.line_to_char(view.first_line as usize),
165+ );
144166}
145167
146168pub fn half_page_up(view: &mut View, _count: usize) {
147169 view.first_line = view.first_line.saturating_sub(view.size.1 / 2);
170+
171+ if !check_cursor_in_view(view) {
172+ let text = &view.state.doc;
173+ view.state.selection = Selection::single(
174+ text.line_to_char(view.first_line as usize),
175+ text.line_to_char(view.first_line as usize),
176+ );
177+ }
148178}
149179
150180pub fn half_page_down(view: &mut View, _count: usize) {
151181 view.first_line += view.size.1 / 2;
182+
183+ if !check_cursor_in_view(view) {
184+ let text = &view.state.doc;
185+ view.state.selection = Selection::single(
186+ text.line_to_char(view.first_line as usize),
187+ text.line_to_char(view.first_line as usize),
188+ );
189+ }
152190}
153191// avoid select by default by having a visual mode switch that makes movements into selects
154192
helix-view/src/keymap.rs+11−2
@@ -108,6 +108,15 @@ macro_rules! shift {
108108 };
109109}
110110
111+macro_rules! ctrl {
112+ ($ch:expr) => {
113+ Key {
114+ code: KeyCode::Char($ch),
115+ modifiers: Modifiers::CONTROL,
116+ }
117+ };
118+}
119+
111120pub fn default() -> Keymaps {
112121 hashmap!(
113122 state::Mode::Normal =>
@@ -148,8 +157,8 @@ pub fn default() -> Keymaps {
148157 code: KeyCode::PageDown,
149158 modifiers: Modifiers::NONE
150159 }] => commands::page_down,
151− vec![shift!('u')] => commands::half_page_up,
152− vec![shift!('d')] => commands::half_page_down,
160+ vec![ctrl!('u')] => commands::half_page_up,
161+ vec![ctrl!('d')] => commands::half_page_down,
153162 ),
154163 state::Mode::Insert => hashmap!(
155164 vec![Key {