1pub mod editor_lsp_test_context;
2pub mod editor_test_context;
3
4use crate::{
5 display_map::{DisplayMap, DisplaySnapshot, ToDisplayPoint},
6 DisplayPoint, Editor, EditorMode, MultiBuffer,
7};
8
9use gpui::{Context, Font, FontFeatures, FontStyle, FontWeight, Model, Pixels, ViewContext};
10
11use project::Project;
12use util::test::{marked_text_offsets, marked_text_ranges};
13
14#[cfg(test)]
15#[ctor::ctor]
16fn init_logger() {
17 if std::env::var("RUST_LOG").is_ok() {
18 env_logger::init();
19 }
20}
21
22// Returns a snapshot from text containing '|' character markers with the markers removed, and DisplayPoints for each one.
23pub fn marked_display_snapshot(
24 text: &str,
25 cx: &mut gpui::AppContext,
26) -> (DisplaySnapshot, Vec<DisplayPoint>) {
27 let (unmarked_text, markers) = marked_text_offsets(text);
28
29 let font = Font {
30 family: "Courier".into(),
31 features: FontFeatures::default(),
32 weight: FontWeight::default(),
33 style: FontStyle::default(),
34 };
35 let font_size: Pixels = 14usize.into();
36
37 let buffer = MultiBuffer::build_simple(&unmarked_text, cx);
38 let display_map = cx.new_model(|cx| DisplayMap::new(buffer, font, font_size, None, 1, 1, cx));
39 let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
40 let markers = markers
41 .into_iter()
42 .map(|offset| offset.to_display_point(&snapshot))
43 .collect();
44
45 (snapshot, markers)
46}
47
48pub fn select_ranges(editor: &mut Editor, marked_text: &str, cx: &mut ViewContext<Editor>) {
49 let (unmarked_text, text_ranges) = marked_text_ranges(marked_text, true);
50 assert_eq!(editor.text(cx), unmarked_text);
51 editor.change_selections(None, cx, |s| s.select_ranges(text_ranges));
52}
53
54pub fn assert_text_with_selections(
55 editor: &mut Editor,
56 marked_text: &str,
57 cx: &mut ViewContext<Editor>,
58) {
59 let (unmarked_text, text_ranges) = marked_text_ranges(marked_text, true);
60 assert_eq!(editor.text(cx), unmarked_text);
61 assert_eq!(editor.selections.ranges(cx), text_ranges);
62}
63
64// RA thinks this is dead code even though it is used in a whole lot of tests
65#[allow(dead_code)]
66#[cfg(any(test, feature = "test-support"))]
67pub(crate) fn build_editor(buffer: Model<MultiBuffer>, cx: &mut ViewContext<Editor>) -> Editor {
68 Editor::new(EditorMode::Full, buffer, None, cx)
69}
70
71pub(crate) fn build_editor_with_project(
72 project: Model<Project>,
73 buffer: Model<MultiBuffer>,
74 cx: &mut ViewContext<Editor>,
75) -> Editor {
76 Editor::new(EditorMode::Full, buffer, Some(project), cx)
77}
78
79#[cfg(any(test, feature = "test-support"))]
80pub fn editor_hunks(
81 editor: &Editor,
82 snapshot: &DisplaySnapshot,
83 cx: &mut ViewContext<'_, Editor>,
84) -> Vec<(String, git::diff::DiffHunkStatus, core::ops::Range<u32>)> {
85 use text::Point;
86
87 snapshot
88 .buffer_snapshot
89 .git_diff_hunks_in_range(0..u32::MAX)
90 .map(|hunk| {
91 let display_range = Point::new(hunk.associated_range.start, 0)
92 .to_display_point(snapshot)
93 .row()
94 ..Point::new(hunk.associated_range.end, 0)
95 .to_display_point(snapshot)
96 .row();
97 let (_, buffer, _) = editor
98 .buffer()
99 .read(cx)
100 .excerpt_containing(Point::new(hunk.associated_range.start, 0), cx)
101 .expect("no excerpt for expanded buffer's hunk start");
102 let diff_base = buffer
103 .read(cx)
104 .diff_base()
105 .expect("should have a diff base for expanded hunk")
106 .slice(hunk.diff_base_byte_range.clone())
107 .to_string();
108 (diff_base, hunk.status(), display_range)
109 })
110 .collect()
111}
112
113#[cfg(any(test, feature = "test-support"))]
114pub fn expanded_hunks(
115 editor: &Editor,
116 snapshot: &DisplaySnapshot,
117 cx: &mut ViewContext<'_, Editor>,
118) -> Vec<(String, git::diff::DiffHunkStatus, core::ops::Range<u32>)> {
119 editor
120 .expanded_hunks
121 .hunks(false)
122 .map(|expanded_hunk| {
123 let hunk_display_range = expanded_hunk
124 .hunk_range
125 .start
126 .to_display_point(snapshot)
127 .row()
128 ..expanded_hunk
129 .hunk_range
130 .end
131 .to_display_point(snapshot)
132 .row();
133 let (_, buffer, _) = editor
134 .buffer()
135 .read(cx)
136 .excerpt_containing(expanded_hunk.hunk_range.start, cx)
137 .expect("no excerpt for expanded buffer's hunk start");
138 let diff_base = buffer
139 .read(cx)
140 .diff_base()
141 .expect("should have a diff base for expanded hunk")
142 .slice(expanded_hunk.diff_base_byte_range.clone())
143 .to_string();
144 (diff_base, expanded_hunk.status, hunk_display_range)
145 })
146 .collect()
147}
148
149#[cfg(any(test, feature = "test-support"))]
150pub fn expanded_hunks_background_highlights(
151 editor: &Editor,
152 snapshot: &DisplaySnapshot,
153) -> Vec<core::ops::Range<u32>> {
154 use itertools::Itertools;
155
156 editor
157 .highlighted_rows::<crate::DiffRowHighlight>()
158 .into_iter()
159 .flatten()
160 .map(|(range, _)| {
161 range.start.to_display_point(snapshot).row()..range.end.to_display_point(snapshot).row()
162 })
163 .unique()
164 .collect()
165}