Debian Codegraph @

helix-term/tests/test/splits.rs

raw symbols SCIP
1 use super::*;
2
3 use helix_stdx::path;
4
5 #[tokio::test(flavor = "multi_thread")]
6 async fn test_split_write_quit_all() -> anyhow::Result<()> {
7 let mut file1 = tempfile::NamedTempFile::new()?;
8 let mut file2 = tempfile::NamedTempFile::new()?;
9 let mut file3 = tempfile::NamedTempFile::new()?;
10
11 let mut app = helpers::AppBuilder::new()
12 .with_file(file1.path(), None)
13 .build()?;
14
15 test_key_sequences(
16 &mut app,
17 vec![
18 (
19 Some(&format!(
20 "ihello1<esc>:sp<ret>:o {}<ret>ihello2<esc>:sp<ret>:o {}<ret>ihello3<esc>",
21 file2.path().to_string_lossy(),
22 file3.path().to_string_lossy()
23 )),
24 Some(&|app| {
25 let docs: Vec<_> = app.editor.documents().collect();
26 assert_eq!(3, docs.len());
27
28 let doc1 = docs
29 .iter()
30 .find(|doc| doc.path().unwrap() == &path::normalize(file1.path()))
31 .unwrap();
32
33 assert_eq!("hello1", doc1.text().to_string());
34
35 let doc2 = docs
36 .iter()
37 .find(|doc| doc.path().unwrap() == &path::normalize(file2.path()))
38 .unwrap();
39
40 assert_eq!("hello2", doc2.text().to_string());
41
42 let doc3 = docs
43 .iter()
44 .find(|doc| doc.path().unwrap() == &path::normalize(file3.path()))
45 .unwrap();
46
47 assert_eq!("hello3", doc3.text().to_string());
48
49 helpers::assert_status_not_error(&app.editor);
50 assert_eq!(3, app.editor.tree.views().count());
51 }),
52 ),
53 (
54 Some(":wqa<ret>"),
55 Some(&|app| {
56 helpers::assert_status_not_error(&app.editor);
57 assert_eq!(0, app.editor.tree.views().count());
58 }),
59 ),
60 ],
61 true,
62 )
63 .await?;
64
65 helpers::assert_file_has_content(&mut file1, &LineFeedHandling::Native.apply("hello1"))?;
66 helpers::assert_file_has_content(&mut file2, &LineFeedHandling::Native.apply("hello2"))?;
67 helpers::assert_file_has_content(&mut file3, &LineFeedHandling::Native.apply("hello3"))?;
68
69 Ok(())
70 }
71
72 #[tokio::test(flavor = "multi_thread")]
73 async fn test_split_write_quit_same_file() -> anyhow::Result<()> {
74 let mut file = tempfile::NamedTempFile::new()?;
75 let mut app = helpers::AppBuilder::new()
76 .with_file(file.path(), None)
77 .build()?;
78
79 test_key_sequences(
80 &mut app,
81 vec![
82 (
83 Some("O<esc>ihello<esc>:sp<ret>ogoodbye<esc>"),
84 Some(&|app| {
85 assert_eq!(2, app.editor.tree.views().count());
86 helpers::assert_status_not_error(&app.editor);
87
88 let mut docs: Vec<_> = app.editor.documents().collect();
89 assert_eq!(1, docs.len());
90
91 let doc = docs.pop().unwrap();
92
93 assert_eq!(
94 LineFeedHandling::Native.apply("hello\ngoodbye"),
95 doc.text().to_string()
96 );
97
98 assert!(doc.is_modified());
99 }),
100 ),
101 (
102 Some(":wq<ret>"),
103 Some(&|app| {
104 helpers::assert_status_not_error(&app.editor);
105 assert_eq!(1, app.editor.tree.views().count());
106
107 let mut docs: Vec<_> = app.editor.documents().collect();
108 assert_eq!(1, docs.len());
109
110 let doc = docs.pop().unwrap();
111
112 assert_eq!(
113 LineFeedHandling::Native.apply("hello\ngoodbye"),
114 doc.text().to_string()
115 );
116
117 assert!(!doc.is_modified());
118 }),
119 ),
120 ],
121 false,
122 )
123 .await?;
124
125 helpers::assert_file_has_content(&mut file, &LineFeedHandling::Native.apply("hello\ngoodbye"))?;
126
127 Ok(())
128 }
129
130 #[tokio::test(flavor = "multi_thread")]
131 async fn test_changes_in_splits_apply_to_all_views() -> anyhow::Result<()> {
132 // See <https://github.com/helix-editor/helix/issues/4732>.
133 // Transactions must be applied to any view that has the changed document open.
134 // This sequence would panic since the jumplist entry would be modified in one
135 // window but not the other. Attempting to update the changelist in the other
136 // window would cause a panic since it would point outside of the document.
137
138 // The key sequence here:
139 // * <C-w>v Create a vertical split of the current buffer.
140 // Both views look at the same doc.
141 // * [<space> Add a line ending to the beginning of the document.
142 // The cursor is now at line 2 in window 2.
143 // * <C-s> Save that selection to the jumplist in window 2.
144 // * <C-w>w Switch to window 1.
145 // * kd Delete line 1 in window 1.
146 // * <C-w>q Close window 1, focusing window 2.
147 // * d Delete line 1 in window 2.
148 //
149 // This panicked in the past because the jumplist entry on line 2 of window 2
150 // was not updated and after the `kd` step, pointed outside of the document.
151 test((
152 "#[|]#",
153 "<C-w>v[<space><C-s><C-w>wkd<C-w>qd",
154 "#[|]#",
155 LineFeedHandling::AsIs,
156 ))
157 .await?;
158
159 // Transactions are applied to the views for windows lazily when they are focused.
160 // This case panics if the transactions and inversions are not applied in the
161 // correct order as we switch between windows.
162 test((
163 "#[|]#",
164 "[<space>[<space>[<space><C-w>vuuu<C-w>wUUU<C-w>quuu",
165 "#[|]#",
166 LineFeedHandling::AsIs,
167 ))
168 .await?;
169
170 // See <https://github.com/helix-editor/helix/issues/4957>.
171 // This sequence undoes part of the history and then adds new changes, creating a
172 // new branch in the history tree. `View::sync_changes` applies transactions down
173 // and up to the lowest common ancestor in the path between old and new revision
174 // numbers. If we apply these up/down transactions in the wrong order, this case
175 // panics.
176 // The key sequence:
177 // * 3[<space> Create three empty lines so we are at the end of the document.
178 // * <C-w>v<C-s> Create a split and save that point at the end of the document
179 // in the jumplist.
180 // * <C-w>w Switch back to the first window.
181 // * uu Undo twice (not three times which would bring us back to the
182 // root of the tree).
183 // * 3[<space> Create three empty lines. Now the end of the document is past
184 // where it was on step 1.
185 // * <C-w>q Close window 1, focusing window 2 and causing a sync. This step
186 // panics if we don't apply in the right order.
187 // * %d Clean up the buffer.
188 test((
189 "#[|]#",
190 "3[<space><C-w>v<C-s><C-w>wuu3[<space><C-w>q%d",
191 "#[|]#",
192 LineFeedHandling::AsIs,
193 ))
194 .await?;
195
196 Ok(())
197 }
198
199 #[tokio::test(flavor = "multi_thread")]
200 async fn test_changes_in_splits_jumplist_sync() -> anyhow::Result<()> {
201 // See <https://github.com/helix-editor/helix/issues/9833>
202 // When jumping backwards (<C-o>) switches between two documents, we need to
203 // ensure that the current view has been synced with all changes to the
204 // document that occurred since the last time the view focused this document.
205 // If the view isn't synced then this case panics since we try to form a
206 // selection on "test" (which was deleted in the other view).
207 test((
208 "#[test|]#",
209 "<C-w>sgf<C-w>wd<C-w>w<C-o><C-w>qd",
210 "#[|]#",
211 LineFeedHandling::AsIs,
212 ))
213 .await?;
214
215 Ok(())
216 }
217
218 #[tokio::test(flavor = "multi_thread")]
219 async fn test_reload_all_with_split_jumplist() -> anyhow::Result<()> {
220 // See reproduction from <https://github.com/helix-editor/helix/issues/9830>
221 //
222 // The key sequence:
223 // * <C-w>s Horizontal split: two views on the same document.
224 // * ]<space> Add an empty line below, growing the document.
225 // * % Select the whole document.
226 // * 2G Go to line 2. `goto_line` calls `push_jump`, recording a jump
227 // whose selection is valid at the *current* (grown) revision.
228 // * ms/ Surround-add `/`, growing the document again.
229 // * :rla reload-all: re-reads the file from disk (shrinking the buffer
230 // back to its original contents) but only syncs the first view of
231 // each document, leaving the other split's `doc_revisions` stale.
232 // * %J Select-all and join, forcing a sync of the stale view.
233 //
234 // On the unfixed code the jumplist entry recorded by `2G` is left ahead of
235 // the stale view's `doc_revisions`; once that view is synced, the entry is
236 // mapped through a changeset whose pre-image predates it and
237 // `ChangeSet::update_positions` panics.
238 use std::io::Write;
239 let mut file = tempfile::NamedTempFile::new()?;
240 // `:reload-all` re-reads from disk, so the file must have on-disk contents
241 // for the reload to shrink the (grown) buffer back down.
242 file.write_all(b"line1\nline2\nline3\n")?;
243 file.flush()?;
244
245 let mut app = helpers::AppBuilder::new()
246 .with_file(file.path(), None)
247 .build()?;
248
249 test_key_sequence(
250 &mut app,
251 // The trailing `<C-w>q` closes the split so a single window remains for
252 // the harness's automatic `:q!` teardown. It also exercises the sync
253 // that runs when a window is closed.
254 Some("<C-w>s]<space>%2Gms/:rla<ret>%J<C-w>q"),
255 Some(&|app| {
256 helpers::assert_status_not_error(&app.editor);
257 }),
258 false,
259 )
260 .await?;
261
262 Ok(())
263 }