1use crate::{
2 display_map::{DisplayMap, DisplaySnapshot, ToDisplayPoint},
3 multi_buffer::ToPointUtf16,
4 AnchorRangeExt, Autoscroll, DisplayPoint, Editor, EditorMode, MultiBuffer, ToPoint,
5};
6use anyhow::Result;
7use collections::BTreeMap;
8use futures::{Future, StreamExt};
9use gpui::{
10 json, keymap::Keystroke, AppContext, ModelContext, ModelHandle, ViewContext, ViewHandle,
11};
12use indoc::indoc;
13use itertools::Itertools;
14use language::{point_to_lsp, Buffer, BufferSnapshot, FakeLspAdapter, Language, LanguageConfig};
15use lsp::{notification, request};
16use parking_lot::RwLock;
17use project::Project;
18use settings::Settings;
19use std::{
20 any::TypeId,
21 ops::{Deref, DerefMut, Range},
22 sync::{
23 atomic::{AtomicUsize, Ordering},
24 Arc,
25 },
26};
27use util::{
28 assert_set_eq, set_eq,
29 test::{generate_marked_text, marked_text_offsets, marked_text_ranges},
30};
31use workspace::{pane, AppState, Workspace, WorkspaceHandle};
32
33#[cfg(test)]
34#[ctor::ctor]
35fn init_logger() {
36 if std::env::var("RUST_LOG").is_ok() {
37 env_logger::init();
38 }
39}
40
41// Returns a snapshot from text containing '|' character markers with the markers removed, and DisplayPoints for each one.
42pub fn marked_display_snapshot(
43 text: &str,
44 cx: &mut gpui::MutableAppContext,
45) -> (DisplaySnapshot, Vec<DisplayPoint>) {
46 let (unmarked_text, markers) = marked_text_offsets(text);
47
48 let family_id = cx.font_cache().load_family(&["Helvetica"]).unwrap();
49 let font_id = cx
50 .font_cache()
51 .select_font(family_id, &Default::default())
52 .unwrap();
53 let font_size = 14.0;
54
55 let buffer = MultiBuffer::build_simple(&unmarked_text, cx);
56 let display_map =
57 cx.add_model(|cx| DisplayMap::new(buffer, font_id, font_size, None, 1, 1, cx));
58 let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
59 let markers = markers
60 .into_iter()
61 .map(|offset| offset.to_display_point(&snapshot))
62 .collect();
63
64 (snapshot, markers)
65}
66
67pub fn select_ranges(editor: &mut Editor, marked_text: &str, cx: &mut ViewContext<Editor>) {
68 let (umarked_text, text_ranges) = marked_text_ranges(marked_text, true);
69 assert_eq!(editor.text(cx), umarked_text);
70 editor.change_selections(None, cx, |s| s.select_ranges(text_ranges));
71}
72
73pub fn assert_text_with_selections(
74 editor: &mut Editor,
75 marked_text: &str,
76 cx: &mut ViewContext<Editor>,
77) {
78 let (unmarked_text, text_ranges) = marked_text_ranges(marked_text, true);
79 assert_eq!(editor.text(cx), unmarked_text);
80 assert_eq!(editor.selections.ranges(cx), text_ranges);
81}
82
83pub(crate) fn build_editor(
84 buffer: ModelHandle<MultiBuffer>,
85 cx: &mut ViewContext<Editor>,
86) -> Editor {
87 Editor::new(EditorMode::Full, buffer, None, None, cx)
88}
89
90pub struct EditorTestContext<'a> {
91 pub cx: &'a mut gpui::TestAppContext,
92 pub window_id: usize,
93 pub editor: ViewHandle<Editor>,
94 pub assertion_context: AssertionContextManager,
95}
96
97impl<'a> EditorTestContext<'a> {
98 pub fn new(cx: &'a mut gpui::TestAppContext) -> EditorTestContext<'a> {
99 let (window_id, editor) = cx.update(|cx| {
100 cx.set_global(Settings::test(cx));
101 crate::init(cx);
102
103 let (window_id, editor) = cx.add_window(Default::default(), |cx| {
104 build_editor(MultiBuffer::build_simple("", cx), cx)
105 });
106
107 editor.update(cx, |_, cx| cx.focus_self());
108
109 (window_id, editor)
110 });
111
112 Self {
113 cx,
114 window_id,
115 editor,
116 assertion_context: AssertionContextManager::new(),
117 }
118 }
119
120 pub fn add_assertion_context(&self, context: String) -> ContextHandle {
121 self.assertion_context.add_context(context)
122 }
123
124 pub fn condition(
125 &self,
126 predicate: impl FnMut(&Editor, &AppContext) -> bool,
127 ) -> impl Future<Output = ()> {
128 self.editor.condition(self.cx, predicate)
129 }
130
131 pub fn editor<F, T>(&self, read: F) -> T
132 where
133 F: FnOnce(&Editor, &AppContext) -> T,
134 {
135 self.editor.read_with(self.cx, read)
136 }
137
138 pub fn update_editor<F, T>(&mut self, update: F) -> T
139 where
140 F: FnOnce(&mut Editor, &mut ViewContext<Editor>) -> T,
141 {
142 self.editor.update(self.cx, update)
143 }
144
145 pub fn multibuffer<F, T>(&self, read: F) -> T
146 where
147 F: FnOnce(&MultiBuffer, &AppContext) -> T,
148 {
149 self.editor(|editor, cx| read(editor.buffer().read(cx), cx))
150 }
151
152 pub fn update_multibuffer<F, T>(&mut self, update: F) -> T
153 where
154 F: FnOnce(&mut MultiBuffer, &mut ModelContext<MultiBuffer>) -> T,
155 {
156 self.update_editor(|editor, cx| editor.buffer().update(cx, update))
157 }
158
159 pub fn buffer_text(&self) -> String {
160 self.multibuffer(|buffer, cx| buffer.snapshot(cx).text())
161 }
162
163 pub fn buffer<F, T>(&self, read: F) -> T
164 where
165 F: FnOnce(&Buffer, &AppContext) -> T,
166 {
167 self.multibuffer(|multibuffer, cx| {
168 let buffer = multibuffer.as_singleton().unwrap().read(cx);
169 read(buffer, cx)
170 })
171 }
172
173 pub fn update_buffer<F, T>(&mut self, update: F) -> T
174 where
175 F: FnOnce(&mut Buffer, &mut ModelContext<Buffer>) -> T,
176 {
177 self.update_multibuffer(|multibuffer, cx| {
178 let buffer = multibuffer.as_singleton().unwrap();
179 buffer.update(cx, update)
180 })
181 }
182
183 pub fn buffer_snapshot(&self) -> BufferSnapshot {
184 self.buffer(|buffer, _| buffer.snapshot())
185 }
186
187 pub fn simulate_keystroke(&mut self, keystroke_text: &str) {
188 let keystroke = Keystroke::parse(keystroke_text).unwrap();
189 self.cx.dispatch_keystroke(self.window_id, keystroke, false);
190 }
191
192 pub fn simulate_keystrokes<const COUNT: usize>(&mut self, keystroke_texts: [&str; COUNT]) {
193 for keystroke_text in keystroke_texts.into_iter() {
194 self.simulate_keystroke(keystroke_text);
195 }
196 }
197
198 pub fn ranges(&self, marked_text: &str) -> Vec<Range<usize>> {
199 let (unmarked_text, ranges) = marked_text_ranges(marked_text, false);
200 assert_eq!(self.buffer_text(), unmarked_text);
201 ranges
202 }
203
204 pub fn display_point(&mut self, marked_text: &str) -> DisplayPoint {
205 let ranges = self.ranges(marked_text);
206 let snapshot = self
207 .editor
208 .update(self.cx, |editor, cx| editor.snapshot(cx));
209 ranges[0].start.to_display_point(&snapshot)
210 }
211
212 // Returns anchors for the current buffer using `«` and `»`
213 pub fn text_anchor_range(&self, marked_text: &str) -> Range<language::Anchor> {
214 let ranges = self.ranges(marked_text);
215 let snapshot = self.buffer_snapshot();
216 snapshot.anchor_before(ranges[0].start)..snapshot.anchor_after(ranges[0].end)
217 }
218
219 /// Change the editor's text and selections using a string containing
220 /// embedded range markers that represent the ranges and directions of
221 /// each selection.
222 ///
223 /// See the `util::test::marked_text_ranges` function for more information.
224 pub fn set_state(&mut self, marked_text: &str) {
225 let (unmarked_text, selection_ranges) = marked_text_ranges(marked_text, true);
226 self.editor.update(self.cx, |editor, cx| {
227 editor.set_text(unmarked_text, cx);
228 editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
229 s.select_ranges(selection_ranges)
230 })
231 })
232 }
233
234 /// Make an assertion about the editor's text and the ranges and directions
235 /// of its selections using a string containing embedded range markers.
236 ///
237 /// See the `util::test::marked_text_ranges` function for more information.
238 pub fn assert_editor_state(&mut self, marked_text: &str) {
239 let (unmarked_text, expected_selections) = marked_text_ranges(marked_text, true);
240 let buffer_text = self.buffer_text();
241 assert_eq!(
242 buffer_text, unmarked_text,
243 "Unmarked text doesn't match buffer text"
244 );
245 self.assert_selections(expected_selections, marked_text.to_string())
246 }
247
248 pub fn assert_editor_background_highlights<Tag: 'static>(&mut self, marked_text: &str) {
249 let expected_ranges = self.ranges(marked_text);
250 let actual_ranges: Vec<Range<usize>> = self.update_editor(|editor, cx| {
251 let snapshot = editor.snapshot(cx);
252 editor
253 .background_highlights
254 .get(&TypeId::of::<Tag>())
255 .map(|h| h.1.clone())
256 .unwrap_or_default()
257 .into_iter()
258 .map(|range| range.to_offset(&snapshot.buffer_snapshot))
259 .collect()
260 });
261 assert_set_eq!(actual_ranges, expected_ranges);
262 }
263
264 pub fn assert_editor_text_highlights<Tag: ?Sized + 'static>(&mut self, marked_text: &str) {
265 let expected_ranges = self.ranges(marked_text);
266 let snapshot = self.update_editor(|editor, cx| editor.snapshot(cx));
267 let actual_ranges: Vec<Range<usize>> = snapshot
268 .highlight_ranges::<Tag>()
269 .map(|ranges| ranges.as_ref().clone().1)
270 .unwrap_or_default()
271 .into_iter()
272 .map(|range| range.to_offset(&snapshot.buffer_snapshot))
273 .collect();
274 assert_set_eq!(actual_ranges, expected_ranges);
275 }
276
277 pub fn assert_editor_selections(&mut self, expected_selections: Vec<Range<usize>>) {
278 let expected_marked_text =
279 generate_marked_text(&self.buffer_text(), &expected_selections, true);
280 self.assert_selections(expected_selections, expected_marked_text)
281 }
282
283 fn assert_selections(
284 &mut self,
285 expected_selections: Vec<Range<usize>>,
286 expected_marked_text: String,
287 ) {
288 let actual_selections = self
289 .editor
290 .read_with(self.cx, |editor, cx| editor.selections.all::<usize>(cx))
291 .into_iter()
292 .map(|s| {
293 if s.reversed {
294 s.end..s.start
295 } else {
296 s.start..s.end
297 }
298 })
299 .collect::<Vec<_>>();
300 let actual_marked_text =
301 generate_marked_text(&self.buffer_text(), &actual_selections, true);
302 if expected_selections != actual_selections {
303 panic!(
304 indoc! {"
305 Editor has unexpected selections.
306
307 Expected selections:
308 {}
309
310 Actual selections:
311 {}
312 "},
313 expected_marked_text, actual_marked_text,
314 );
315 }
316 }
317}
318
319impl<'a> Deref for EditorTestContext<'a> {
320 type Target = gpui::TestAppContext;
321
322 fn deref(&self) -> &Self::Target {
323 self.cx
324 }
325}
326
327impl<'a> DerefMut for EditorTestContext<'a> {
328 fn deref_mut(&mut self) -> &mut Self::Target {
329 &mut self.cx
330 }
331}
332
333pub struct EditorLspTestContext<'a> {
334 pub cx: EditorTestContext<'a>,
335 pub lsp: lsp::FakeLanguageServer,
336 pub workspace: ViewHandle<Workspace>,
337 pub buffer_lsp_url: lsp::Url,
338}
339
340impl<'a> EditorLspTestContext<'a> {
341 pub async fn new(
342 mut language: Language,
343 capabilities: lsp::ServerCapabilities,
344 cx: &'a mut gpui::TestAppContext,
345 ) -> EditorLspTestContext<'a> {
346 use json::json;
347
348 cx.update(|cx| {
349 crate::init(cx);
350 pane::init(cx);
351 });
352
353 let params = cx.update(AppState::test);
354
355 let file_name = format!(
356 "file.{}",
357 language
358 .path_suffixes()
359 .first()
360 .unwrap_or(&"txt".to_string())
361 );
362
363 let mut fake_servers = language
364 .set_fake_lsp_adapter(Arc::new(FakeLspAdapter {
365 capabilities,
366 ..Default::default()
367 }))
368 .await;
369
370 let project = Project::test(params.fs.clone(), [], cx).await;
371 project.update(cx, |project, _| project.languages().add(Arc::new(language)));
372
373 params
374 .fs
375 .as_fake()
376 .insert_tree("/root", json!({ "dir": { file_name: "" }}))
377 .await;
378
379 let (window_id, workspace) =
380 cx.add_window(|cx| Workspace::new(project.clone(), |_, _| unimplemented!(), cx));
381 project
382 .update(cx, |project, cx| {
383 project.find_or_create_local_worktree("/root", true, cx)
384 })
385 .await
386 .unwrap();
387 cx.read(|cx| workspace.read(cx).worktree_scans_complete(cx))
388 .await;
389
390 let file = cx.read(|cx| workspace.file_project_paths(cx)[0].clone());
391 let item = workspace
392 .update(cx, |workspace, cx| workspace.open_path(file, true, cx))
393 .await
394 .expect("Could not open test file");
395
396 let editor = cx.update(|cx| {
397 item.act_as::<Editor>(cx)
398 .expect("Opened test file wasn't an editor")
399 });
400 editor.update(cx, |_, cx| cx.focus_self());
401
402 let lsp = fake_servers.next().await.unwrap();
403
404 Self {
405 cx: EditorTestContext {
406 cx,
407 window_id,
408 editor,
409 assertion_context: AssertionContextManager::new(),
410 },
411 lsp,
412 workspace,
413 buffer_lsp_url: lsp::Url::from_file_path("/root/dir/file.rs").unwrap(),
414 }
415 }
416
417 pub async fn new_rust(
418 capabilities: lsp::ServerCapabilities,
419 cx: &'a mut gpui::TestAppContext,
420 ) -> EditorLspTestContext<'a> {
421 let language = Language::new(
422 LanguageConfig {
423 name: "Rust".into(),
424 path_suffixes: vec!["rs".to_string()],
425 ..Default::default()
426 },
427 Some(tree_sitter_rust::language()),
428 );
429
430 Self::new(language, capabilities, cx).await
431 }
432
433 // Constructs lsp range using a marked string with '[', ']' range delimiters
434 pub fn lsp_range(&mut self, marked_text: &str) -> lsp::Range {
435 let ranges = self.ranges(marked_text);
436 self.to_lsp_range(ranges[0].clone())
437 }
438
439 pub fn to_lsp_range(&mut self, range: Range<usize>) -> lsp::Range {
440 let snapshot = self.update_editor(|editor, cx| editor.snapshot(cx));
441 let start_point = range.start.to_point(&snapshot.buffer_snapshot);
442 let end_point = range.end.to_point(&snapshot.buffer_snapshot);
443
444 self.editor(|editor, cx| {
445 let buffer = editor.buffer().read(cx);
446 let start = point_to_lsp(
447 buffer
448 .point_to_buffer_offset(start_point, cx)
449 .unwrap()
450 .1
451 .to_point_utf16(&buffer.read(cx)),
452 );
453 let end = point_to_lsp(
454 buffer
455 .point_to_buffer_offset(end_point, cx)
456 .unwrap()
457 .1
458 .to_point_utf16(&buffer.read(cx)),
459 );
460
461 lsp::Range { start, end }
462 })
463 }
464
465 pub fn to_lsp(&mut self, offset: usize) -> lsp::Position {
466 let snapshot = self.update_editor(|editor, cx| editor.snapshot(cx));
467 let point = offset.to_point(&snapshot.buffer_snapshot);
468
469 self.editor(|editor, cx| {
470 let buffer = editor.buffer().read(cx);
471 point_to_lsp(
472 buffer
473 .point_to_buffer_offset(point, cx)
474 .unwrap()
475 .1
476 .to_point_utf16(&buffer.read(cx)),
477 )
478 })
479 }
480
481 pub fn update_workspace<F, T>(&mut self, update: F) -> T
482 where
483 F: FnOnce(&mut Workspace, &mut ViewContext<Workspace>) -> T,
484 {
485 self.workspace.update(self.cx.cx, update)
486 }
487
488 pub fn handle_request<T, F, Fut>(
489 &self,
490 mut handler: F,
491 ) -> futures::channel::mpsc::UnboundedReceiver<()>
492 where
493 T: 'static + request::Request,
494 T::Params: 'static + Send,
495 F: 'static + Send + FnMut(lsp::Url, T::Params, gpui::AsyncAppContext) -> Fut,
496 Fut: 'static + Send + Future<Output = Result<T::Result>>,
497 {
498 let url = self.buffer_lsp_url.clone();
499 self.lsp.handle_request::<T, _, _>(move |params, cx| {
500 let url = url.clone();
501 handler(url, params, cx)
502 })
503 }
504
505 pub fn notify<T: notification::Notification>(&self, params: T::Params) {
506 self.lsp.notify::<T>(params);
507 }
508}
509
510impl<'a> Deref for EditorLspTestContext<'a> {
511 type Target = EditorTestContext<'a>;
512
513 fn deref(&self) -> &Self::Target {
514 &self.cx
515 }
516}
517
518impl<'a> DerefMut for EditorLspTestContext<'a> {
519 fn deref_mut(&mut self) -> &mut Self::Target {
520 &mut self.cx
521 }
522}
523
524#[derive(Clone)]
525pub struct AssertionContextManager {
526 id: Arc<AtomicUsize>,
527 contexts: Arc<RwLock<BTreeMap<usize, String>>>,
528}
529
530impl AssertionContextManager {
531 pub fn new() -> Self {
532 Self {
533 id: Arc::new(AtomicUsize::new(0)),
534 contexts: Arc::new(RwLock::new(BTreeMap::new())),
535 }
536 }
537
538 pub fn add_context(&self, context: String) -> ContextHandle {
539 let id = self.id.fetch_add(1, Ordering::Relaxed);
540 let mut contexts = self.contexts.write();
541 contexts.insert(id, context);
542 ContextHandle {
543 id,
544 manager: self.clone(),
545 }
546 }
547
548 pub fn context(&self) -> String {
549 let contexts = self.contexts.read();
550 format!("\n{}\n", contexts.values().join("\n"))
551 }
552}
553
554pub struct ContextHandle {
555 id: usize,
556 manager: AssertionContextManager,
557}
558
559impl Drop for ContextHandle {
560 fn drop(&mut self) {
561 let mut contexts = self.manager.contexts.write();
562 contexts.remove(&self.id);
563 }
564}