|
1 | 1 | use lsp_server::{Connection, Message, Notification, Request, RequestId, Response}; |
2 | 2 | use lsp_types::request::{Initialize, Shutdown}; |
3 | 3 | use lsp_types::{ |
4 | | - ClientCapabilities, DidChangeConfigurationParams, InitializeParams, InitializedParams, |
| 4 | + ClientCapabilities, DidChangeConfigurationParams, DidChangeWatchedFilesParams, |
| 5 | + FileChangeType, FileEvent, InitializeParams, InitializedParams, Url, |
5 | 6 | }; |
6 | 7 |
|
7 | 8 | use analysis::config::Config; |
8 | 9 |
|
9 | 10 | use dialects::DialectName; |
10 | 11 |
|
11 | | -use integration_tests::config; |
| 12 | +use integration_tests::{config, get_script_path}; |
12 | 13 |
|
13 | | -use lsp_types::notification::{DidChangeConfiguration, Initialized}; |
| 14 | +use lsp_types::notification::{DidChangeConfiguration, DidChangeWatchedFiles, Initialized}; |
14 | 15 |
|
15 | 16 | use move_language_server::global_state::{initialize_new_global_state, GlobalState}; |
16 | | -use move_language_server::main_loop::{main_loop, notification_new, request_new}; |
| 17 | +use move_language_server::main_loop::{ |
| 18 | + main_loop, notification_new, request_new, FileSystemEvent, |
| 19 | +}; |
17 | 20 | use move_language_server::server::run_server; |
18 | 21 |
|
19 | 22 | const SHUTDOWN_REQ_ID: u64 = 10; |
@@ -58,20 +61,28 @@ fn response(req_id: usize, contents: serde_json::Value) -> Message { |
58 | 61 | trait MessageType { |
59 | 62 | fn into_request(self) -> Request; |
60 | 63 | fn into_response(self) -> Response; |
| 64 | + fn into_notification(self) -> Notification; |
61 | 65 | } |
62 | 66 |
|
63 | 67 | impl MessageType for Message { |
64 | 68 | fn into_request(self) -> Request { |
65 | 69 | match self { |
66 | 70 | Message::Request(req) => req, |
67 | | - _ => panic!(), |
| 71 | + _ => panic!("not a request"), |
68 | 72 | } |
69 | 73 | } |
70 | 74 |
|
71 | 75 | fn into_response(self) -> Response { |
72 | 76 | match self { |
73 | 77 | Message::Response(resp) => resp, |
74 | | - _ => panic!(), |
| 78 | + _ => panic!("not a response"), |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + fn into_notification(self) -> Notification { |
| 83 | + match self { |
| 84 | + Message::Notification(notification) => notification, |
| 85 | + _ => panic!("not a notification"), |
75 | 86 | } |
76 | 87 | } |
77 | 88 | } |
@@ -115,13 +126,18 @@ fn test_server_initialization() { |
115 | 126 | assert_eq!(init_finished_resp.id, RequestId::from(1)); |
116 | 127 | assert_eq!( |
117 | 128 | init_finished_resp.result.unwrap()["capabilities"]["textDocumentSync"], |
118 | | - 1 |
| 129 | + serde_json::json!({"change": 1, "openClose": true}) |
119 | 130 | ); |
120 | | - let shutdown_req = client_conn.receiver.try_recv().unwrap(); |
| 131 | + let registration_req = client_conn.receiver.try_recv().unwrap().into_request(); |
| 132 | + assert_eq!(registration_req.method, "client/registerCapability"); |
121 | 133 | assert_eq!( |
122 | | - shutdown_req.into_response().id, |
123 | | - RequestId::from(SHUTDOWN_REQ_ID) |
| 134 | + registration_req.params["registrations"][0]["method"], |
| 135 | + "workspace/didChangeWatchedFiles" |
124 | 136 | ); |
| 137 | + |
| 138 | + let shutdown_resp = client_conn.receiver.try_recv().unwrap().into_response(); |
| 139 | + assert_eq!(shutdown_resp.id, RequestId::from(SHUTDOWN_REQ_ID)); |
| 140 | + |
125 | 141 | client_conn.receiver.try_recv().unwrap_err(); |
126 | 142 | } |
127 | 143 |
|
@@ -157,3 +173,31 @@ fn test_server_config_change() { |
157 | 173 | ); |
158 | 174 | assert_eq!(global_state.config().dialect_name, DialectName::DFinance); |
159 | 175 | } |
| 176 | + |
| 177 | +#[test] |
| 178 | +fn test_removed_file_not_present_in_the_diagnostics() { |
| 179 | + let (client_conn, server_conn) = Connection::memory(); |
| 180 | + |
| 181 | + let script_text = r"script { |
| 182 | + use 0x0::Unknown; |
| 183 | + fun main() {} |
| 184 | + }"; |
| 185 | + let script_file = (get_script_path(), script_text.to_string()); |
| 186 | + |
| 187 | + let mut global_state = global_state(config!()); |
| 188 | + global_state.update_from_events(vec![FileSystemEvent::AddFile(script_file)]); |
| 189 | + |
| 190 | + let delete_event = FileEvent::new( |
| 191 | + Url::from_file_path(get_script_path()).unwrap(), |
| 192 | + FileChangeType::Deleted, |
| 193 | + ); |
| 194 | + let files_changed_notification = |
| 195 | + notification::<DidChangeWatchedFiles>(DidChangeWatchedFilesParams { |
| 196 | + changes: vec![delete_event], |
| 197 | + }); |
| 198 | + send_messages(&client_conn, vec![files_changed_notification]); |
| 199 | + |
| 200 | + main_loop(&mut global_state, &server_conn).unwrap(); |
| 201 | + |
| 202 | + assert!(global_state.analysis().db().available_files.is_empty()); |
| 203 | +} |
0 commit comments