1use std::{
2 borrow::Cow,
3 ops::{Deref, DerefMut, Range},
4 sync::Arc,
5};
6
7use anyhow::Result;
8
9use futures::Future;
10use gpui::{json, ViewContext, ViewHandle};
11use indoc::indoc;
12use language::{point_to_lsp, FakeLspAdapter, Language, LanguageConfig, LanguageQueries};
13use lsp::{notification, request};
14use project::Project;
15use smol::stream::StreamExt;
16use workspace::{pane, AppState, Workspace, WorkspaceHandle};
17
18use crate::{multi_buffer::ToPointUtf16, Editor, ToPoint};
19
20use super::editor_test_context::EditorTestContext;
21
22pub struct EditorLspTestContext<'a> {
23 pub cx: EditorTestContext<'a>,
24 pub lsp: lsp::FakeLanguageServer,
25 pub workspace: ViewHandle<Workspace>,
26 pub buffer_lsp_url: lsp::Url,
27}
28
29impl<'a> EditorLspTestContext<'a> {
30 pub async fn new(
31 mut language: Language,
32 capabilities: lsp::ServerCapabilities,
33 cx: &'a mut gpui::TestAppContext,
34 ) -> EditorLspTestContext<'a> {
35 use json::json;
36
37 cx.update(|cx| {
38 crate::init(cx);
39 pane::init(cx);
40 });
41
42 let params = cx.update(AppState::test);
43
44 let file_name = format!(
45 "file.{}",
46 language
47 .path_suffixes()
48 .first()
49 .unwrap_or(&"txt".to_string())
50 );
51
52 let mut fake_servers = language
53 .set_fake_lsp_adapter(Arc::new(FakeLspAdapter {
54 capabilities,
55 ..Default::default()
56 }))
57 .await;
58
59 let project = Project::test(params.fs.clone(), [], cx).await;
60 project.update(cx, |project, _| project.languages().add(Arc::new(language)));
61
62 params
63 .fs
64 .as_fake()
65 .insert_tree("/root", json!({ "dir": { file_name.clone(): "" }}))
66 .await;
67
68 let (window_id, workspace) = cx.add_window(|cx| {
69 Workspace::new(
70 Default::default(),
71 0,
72 project.clone(),
73 |_, _| unimplemented!(),
74 cx,
75 )
76 });
77 project
78 .update(cx, |project, cx| {
79 project.find_or_create_local_worktree("/root", true, cx)
80 })
81 .await
82 .unwrap();
83 cx.read(|cx| workspace.read(cx).worktree_scans_complete(cx))
84 .await;
85
86 let file = cx.read(|cx| workspace.file_project_paths(cx)[0].clone());
87 let item = workspace
88 .update(cx, |workspace, cx| {
89 workspace.open_path(file, None, true, cx)
90 })
91 .await
92 .expect("Could not open test file");
93
94 let editor = cx.update(|cx| {
95 item.act_as::<Editor>(cx)
96 .expect("Opened test file wasn't an editor")
97 });
98 editor.update(cx, |_, cx| cx.focus_self());
99
100 let lsp = fake_servers.next().await.unwrap();
101
102 Self {
103 cx: EditorTestContext {
104 cx,
105 window_id,
106 editor,
107 },
108 lsp,
109 workspace,
110 buffer_lsp_url: lsp::Url::from_file_path(format!("/root/dir/{file_name}")).unwrap(),
111 }
112 }
113
114 pub async fn new_rust(
115 capabilities: lsp::ServerCapabilities,
116 cx: &'a mut gpui::TestAppContext,
117 ) -> EditorLspTestContext<'a> {
118 let language = Language::new(
119 LanguageConfig {
120 name: "Rust".into(),
121 path_suffixes: vec!["rs".to_string()],
122 ..Default::default()
123 },
124 Some(tree_sitter_rust::language()),
125 );
126
127 Self::new(language, capabilities, cx).await
128 }
129
130 pub async fn new_typescript(
131 capabilities: lsp::ServerCapabilities,
132 cx: &'a mut gpui::TestAppContext,
133 ) -> EditorLspTestContext<'a> {
134 let language = Language::new(
135 LanguageConfig {
136 name: "Typescript".into(),
137 path_suffixes: vec!["ts".to_string()],
138 ..Default::default()
139 },
140 Some(tree_sitter_typescript::language_typescript()),
141 )
142 .with_queries(LanguageQueries {
143 brackets: Some(Cow::from(indoc! {r#"
144 ("(" @open ")" @close)
145 ("[" @open "]" @close)
146 ("{" @open "}" @close)
147 ("<" @open ">" @close)
148 ("\"" @open "\"" @close)"#})),
149 ..Default::default()
150 })
151 .expect("Could not parse brackets");
152
153 Self::new(language, capabilities, cx).await
154 }
155
156 // Constructs lsp range using a marked string with '[', ']' range delimiters
157 pub fn lsp_range(&mut self, marked_text: &str) -> lsp::Range {
158 let ranges = self.ranges(marked_text);
159 self.to_lsp_range(ranges[0].clone())
160 }
161
162 pub fn to_lsp_range(&mut self, range: Range<usize>) -> lsp::Range {
163 let snapshot = self.update_editor(|editor, cx| editor.snapshot(cx));
164 let start_point = range.start.to_point(&snapshot.buffer_snapshot);
165 let end_point = range.end.to_point(&snapshot.buffer_snapshot);
166
167 self.editor(|editor, cx| {
168 let buffer = editor.buffer().read(cx);
169 let start = point_to_lsp(
170 buffer
171 .point_to_buffer_offset(start_point, cx)
172 .unwrap()
173 .1
174 .to_point_utf16(&buffer.read(cx)),
175 );
176 let end = point_to_lsp(
177 buffer
178 .point_to_buffer_offset(end_point, cx)
179 .unwrap()
180 .1
181 .to_point_utf16(&buffer.read(cx)),
182 );
183
184 lsp::Range { start, end }
185 })
186 }
187
188 pub fn to_lsp(&mut self, offset: usize) -> lsp::Position {
189 let snapshot = self.update_editor(|editor, cx| editor.snapshot(cx));
190 let point = offset.to_point(&snapshot.buffer_snapshot);
191
192 self.editor(|editor, cx| {
193 let buffer = editor.buffer().read(cx);
194 point_to_lsp(
195 buffer
196 .point_to_buffer_offset(point, cx)
197 .unwrap()
198 .1
199 .to_point_utf16(&buffer.read(cx)),
200 )
201 })
202 }
203
204 pub fn update_workspace<F, T>(&mut self, update: F) -> T
205 where
206 F: FnOnce(&mut Workspace, &mut ViewContext<Workspace>) -> T,
207 {
208 self.workspace.update(self.cx.cx, update)
209 }
210
211 pub fn handle_request<T, F, Fut>(
212 &self,
213 mut handler: F,
214 ) -> futures::channel::mpsc::UnboundedReceiver<()>
215 where
216 T: 'static + request::Request,
217 T::Params: 'static + Send,
218 F: 'static + Send + FnMut(lsp::Url, T::Params, gpui::AsyncAppContext) -> Fut,
219 Fut: 'static + Send + Future<Output = Result<T::Result>>,
220 {
221 let url = self.buffer_lsp_url.clone();
222 self.lsp.handle_request::<T, _, _>(move |params, cx| {
223 let url = url.clone();
224 handler(url, params, cx)
225 })
226 }
227
228 pub fn notify<T: notification::Notification>(&self, params: T::Params) {
229 self.lsp.notify::<T>(params);
230 }
231}
232
233impl<'a> Deref for EditorLspTestContext<'a> {
234 type Target = EditorTestContext<'a>;
235
236 fn deref(&self) -> &Self::Target {
237 &self.cx
238 }
239}
240
241impl<'a> DerefMut for EditorLspTestContext<'a> {
242 fn deref_mut(&mut self) -> &mut Self::Target {
243 &mut self.cx
244 }
245}