1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use crate::{
    buffer::BufferHandle,
    buffer_position::BufferPosition,
    buffer_view::{BufferView, BufferViewCollection},
    client::Client,
    cursor::Cursor,
    editor::Editor,
};

#[derive(Clone, Copy)]
pub enum NavigationMovement {
    Forward,
    Backward,
}

#[derive(Clone)]
struct NavigationHistorySnapshot {
    pub buffer_handle: BufferHandle,
    pub position: BufferPosition,
}

#[derive(Default)]
pub struct NavigationHistory {
    snapshots: Vec<NavigationHistorySnapshot>,
    current_snapshot_index: u32,
    on_previous_buffer: bool,
}

impl NavigationHistory {
    pub fn clear(&mut self) {
        self.snapshots.clear();
        self.current_snapshot_index = 0;
        self.on_previous_buffer = false;
    }

    pub fn save_snapshot(client: &mut Client, buffer_views: &BufferViewCollection) {
        let buffer_view_handle = match client.buffer_view_handle() {
            Some(handle) => handle,
            None => return,
        };
        let buffer_view = buffer_views.get(buffer_view_handle);

        let this = &mut client.navigation_history;
        if this.on_previous_buffer {
            this.current_snapshot_index = this.snapshots.len() as _;
        }
        this.snapshots.truncate(this.current_snapshot_index as _);
        this.on_previous_buffer = false;

        let buffer_handle = buffer_view.buffer_handle;
        let position = buffer_view.cursors.main_cursor().position;

        if this
            .snapshots
            .last()
            .map(|s| s.buffer_handle == buffer_handle && s.position == position)
            .unwrap_or(false)
        {
            return;
        }

        this.snapshots.push(NavigationHistorySnapshot {
            buffer_handle,
            position,
        });
        this.current_snapshot_index = this.snapshots.len() as _;
    }

    pub fn move_in_history(client: &mut Client, editor: &mut Editor, movement: NavigationMovement) {
        match movement {
            NavigationMovement::Forward => {
                if client.navigation_history.current_snapshot_index + 1
                    >= client.navigation_history.snapshots.len() as _
                {
                    return;
                }

                client.navigation_history.current_snapshot_index += 1;
            }
            NavigationMovement::Backward => {
                if client.navigation_history.current_snapshot_index == 0 {
                    return;
                }

                if client.navigation_history.current_snapshot_index
                    == client.navigation_history.snapshots.len() as _
                {
                    Self::save_snapshot(client, &editor.buffer_views);
                    if client.navigation_history.current_snapshot_index > 1 {
                        client.navigation_history.current_snapshot_index -= 1;
                    }
                }

                client.navigation_history.current_snapshot_index -= 1;
            }
        }

        let snapshot = &client.navigation_history.snapshots
            [client.navigation_history.current_snapshot_index as usize];

        let position = editor
            .buffers
            .get(snapshot.buffer_handle)
            .content()
            .saturate_position(snapshot.position);

        let buffer_view_handle = editor
            .buffer_views
            .buffer_view_handle_from_buffer_handle(client.handle(), snapshot.buffer_handle);

        let mut cursors = editor
            .buffer_views
            .get_mut(buffer_view_handle)
            .cursors
            .mut_guard();
        cursors.clear();
        cursors.add(Cursor {
            anchor: position,
            position,
        });

        client.set_buffer_view_handle_no_history(Some(buffer_view_handle));
        client.navigation_history.on_previous_buffer = false;
    }

    pub fn move_to_previous_buffer(client: &mut Client, editor: &mut Editor) {
        fn save_snapshot_if_current_buffer_is_different_from_last(
            client: &mut Client,
            buffer_view: &BufferView,
        ) {
            if let Some(current_snapshot) = client
                .navigation_history
                .snapshots
                .get(client.navigation_history.current_snapshot_index as usize)
            {
                if current_snapshot.buffer_handle == buffer_view.buffer_handle {
                    return;
                }
            }

            client
                .navigation_history
                .snapshots
                .push(NavigationHistorySnapshot {
                    buffer_handle: buffer_view.buffer_handle,
                    position: buffer_view.cursors.main_cursor().position,
                });
        }

        let current_buffer_view = client
            .buffer_view_handle()
            .map(|h| editor.buffer_views.get(h));

        if let Some(current_buffer_view) = current_buffer_view {
            save_snapshot_if_current_buffer_is_different_from_last(client, current_buffer_view);
        }

        let current_buffer_handle = current_buffer_view.map(|v| v.buffer_handle);

        for (i, snapshot) in client.navigation_history.snapshots.iter().enumerate().rev() {
            if current_buffer_handle != Some(snapshot.buffer_handle) {
                let buffer_view_handle = editor
                    .buffer_views
                    .buffer_view_handle_from_buffer_handle(client.handle(), snapshot.buffer_handle);
                client.set_buffer_view_handle_no_history(Some(buffer_view_handle));
                client.navigation_history.current_snapshot_index = i as _;
                client.navigation_history.on_previous_buffer = true;
                break;
            }
        }
    }

    pub fn remove_snapshots_with_buffer_handle(&mut self, buffer_handle: BufferHandle) {
        for i in (0..self.snapshots.len()).rev() {
            let snapshot = self.snapshots[i].clone();
            if snapshot.buffer_handle == buffer_handle {
                self.snapshots.remove(i);

                if self.current_snapshot_index > 0 && i <= self.current_snapshot_index as _ {
                    self.current_snapshot_index -= 1;
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::path::PathBuf;

    fn setup() -> (Editor, Client) {
        let mut client = Client::new();
        let mut editor = Editor::new(PathBuf::new(), String::new());

        let buffer_a = editor.buffers.add_new();
        assert_eq!(0, buffer_a.handle().0);
        let buffer_b = editor.buffers.add_new();
        assert_eq!(1, buffer_b.handle().0);
        let buffer_c = editor.buffers.add_new();
        assert_eq!(2, buffer_c.handle().0);

        let view_a = editor
            .buffer_views
            .add_new(client.handle(), BufferHandle(0));
        let view_b = editor
            .buffer_views
            .add_new(client.handle(), BufferHandle(1));
        let view_c = editor
            .buffer_views
            .add_new(client.handle(), BufferHandle(2));

        NavigationHistory::save_snapshot(&mut client, &editor.buffer_views);
        client.set_buffer_view_handle_no_history(Some(view_a));
        NavigationHistory::save_snapshot(&mut client, &editor.buffer_views);
        client.set_buffer_view_handle_no_history(Some(view_b));
        NavigationHistory::save_snapshot(&mut client, &editor.buffer_views);
        client.set_buffer_view_handle_no_history(Some(view_c));

        (editor, client)
    }

    fn buffer_index(client: &Client, editor: &Editor) -> usize {
        let buffer_view_handle = client.buffer_view_handle().unwrap();
        let buffer_view = editor.buffer_views.get(buffer_view_handle);
        buffer_view.buffer_handle.0 as _
    }

    #[test]
    fn move_back_and_forward_in_history() {
        let (mut editor, mut client) = setup();

        assert_eq!(2, client.navigation_history.current_snapshot_index);
        assert_eq!(2, buffer_index(&client, &editor));

        NavigationHistory::move_in_history(&mut client, &mut editor, NavigationMovement::Forward);
        assert_eq!(2, client.navigation_history.current_snapshot_index);
        assert_eq!(2, buffer_index(&client, &editor));

        NavigationHistory::move_in_history(&mut client, &mut editor, NavigationMovement::Backward);
        assert_eq!(1, client.navigation_history.current_snapshot_index);
        assert_eq!(1, buffer_index(&client, &editor));

        NavigationHistory::move_in_history(&mut client, &mut editor, NavigationMovement::Backward);
        assert_eq!(0, client.navigation_history.current_snapshot_index);
        assert_eq!(0, buffer_index(&client, &editor));

        NavigationHistory::move_in_history(&mut client, &mut editor, NavigationMovement::Backward);
        assert_eq!(0, client.navigation_history.current_snapshot_index);
        assert_eq!(0, buffer_index(&client, &editor));

        NavigationHistory::move_in_history(&mut client, &mut editor, NavigationMovement::Forward);
        assert_eq!(1, client.navigation_history.current_snapshot_index);
        assert_eq!(1, buffer_index(&client, &editor));

        assert_eq!(3, client.navigation_history.snapshots.len());
    }

    #[test]
    fn move_to_previous_buffer_three_times() {
        let (mut editor, mut client) = setup();

        assert_eq!(2, buffer_index(&client, &editor));

        NavigationHistory::move_to_previous_buffer(&mut client, &mut editor);
        assert_eq!(1, buffer_index(&client, &editor));

        NavigationHistory::move_to_previous_buffer(&mut client, &mut editor);
        assert_eq!(2, buffer_index(&client, &editor));

        NavigationHistory::move_to_previous_buffer(&mut client, &mut editor);
        assert_eq!(1, buffer_index(&client, &editor));
    }

    #[test]
    fn navigate_back_then_move_to_previous_buffer() {
        let (mut editor, mut client) = setup();

        NavigationHistory::move_in_history(&mut client, &mut editor, NavigationMovement::Backward);
        assert_eq!(1, client.navigation_history.current_snapshot_index);
        assert_eq!(1, buffer_index(&client, &editor));

        NavigationHistory::move_in_history(&mut client, &mut editor, NavigationMovement::Backward);
        assert_eq!(0, client.navigation_history.current_snapshot_index);
        assert_eq!(0, buffer_index(&client, &editor));

        NavigationHistory::move_in_history(&mut client, &mut editor, NavigationMovement::Forward);
        assert_eq!(1, client.navigation_history.current_snapshot_index);
        assert_eq!(1, buffer_index(&client, &editor));

        assert_eq!(3, client.navigation_history.snapshots.len());

        NavigationHistory::move_to_previous_buffer(&mut client, &mut editor);
        assert_eq!(2, client.navigation_history.current_snapshot_index);
        assert_eq!(2, buffer_index(&client, &editor));

        assert_eq!(3, client.navigation_history.snapshots.len());

        NavigationHistory::move_to_previous_buffer(&mut client, &mut editor);
        assert_eq!(1, client.navigation_history.current_snapshot_index);
        assert_eq!(1, buffer_index(&client, &editor));

        NavigationHistory::move_to_previous_buffer(&mut client, &mut editor);
        assert_eq!(2, client.navigation_history.current_snapshot_index);
        assert_eq!(2, buffer_index(&client, &editor));

        assert_eq!(3, client.navigation_history.snapshots.len());
    }
}