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