This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathnotifications.rs
334 lines (291 loc) · 11 KB
/
notifications.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! One-way notifications that the RLS receives from the client.
use crate::actions::{FileWatch, InitActionContext, VersionOrdering};
use crate::Span;
use log::{debug, trace, warn};
use rls_vfs::{Change, VfsSpan};
use serde::Deserialize;
use std::sync::atomic::Ordering;
use crate::build::*;
use crate::lsp_data::request::{RangeFormatting, RegisterCapability, UnregisterCapability};
use crate::lsp_data::*;
use crate::server::Request;
use lsp_types::notification::ShowMessage;
pub use crate::lsp_data::notification::{
Cancel, DidChangeConfiguration, DidChangeTextDocument, DidChangeWatchedFiles,
DidOpenTextDocument, DidSaveTextDocument, Initialized,
};
use crate::server::{BlockingNotificationAction, Notification, Output};
use std::thread;
impl BlockingNotificationAction for Initialized {
// Respond to the `initialized` notification. We take this opportunity to
// dynamically register some options.
fn handle<O: Output>(
_params: Self::Params,
ctx: &mut InitActionContext,
out: O,
) -> Result<(), ()> {
const WATCH_ID: &str = "rls-watch";
let id = out.provide_id();
let params = RegistrationParams {
registrations: vec![Registration {
id: WATCH_ID.to_owned(),
method: <DidChangeWatchedFiles as LSPNotification>::METHOD.to_owned(),
register_options: Some(FileWatch::new(&ctx).watchers_config()),
}],
};
let request = Request::<RegisterCapability>::new(id, params);
out.request(request);
Ok(())
}
}
impl BlockingNotificationAction for DidOpenTextDocument {
fn handle<O: Output>(
params: Self::Params,
ctx: &mut InitActionContext,
_out: O,
) -> Result<(), ()> {
trace!("on_open: {:?}", params.text_document.uri);
let file_path = parse_file_path!(¶ms.text_document.uri, "on_open")?;
ctx.reset_change_version(&file_path);
ctx.vfs.set_file(&file_path, ¶ms.text_document.text);
Ok(())
}
}
impl BlockingNotificationAction for DidChangeTextDocument {
fn handle<O: Output>(
params: Self::Params,
ctx: &mut InitActionContext,
out: O,
) -> Result<(), ()> {
trace!(
"on_change: {:?}, thread: {:?}",
params,
thread::current().id()
);
if params.content_changes.is_empty() {
return Ok(());
}
ctx.quiescent.store(false, Ordering::SeqCst);
let file_path = parse_file_path!(¶ms.text_document.uri, "on_change")?;
let version_num = params.text_document.version.unwrap();
match ctx.check_change_version(&file_path, version_num) {
VersionOrdering::Ok => {}
VersionOrdering::Duplicate => return Ok(()),
VersionOrdering::OutOfOrder => {
out.notify(Notification::<ShowMessage>::new(ShowMessageParams {
typ: MessageType::Warning,
message: format!("Out of order change in {:?}", file_path),
}));
return Ok(());
}
}
let changes: Vec<Change> = params
.content_changes
.iter()
.map(|i| {
if let Some(range) = i.range {
let range = ls_util::range_to_rls(range);
Change::ReplaceText {
// LSP sends UTF-16 code units based offsets and length
span: VfsSpan::from_utf16(
Span::from_range(range, file_path.clone()),
i.range_length
),
text: i.text.clone(),
}
} else {
Change::AddFile {
file: file_path.clone(),
text: i.text.clone(),
}
}
}).collect();
ctx.vfs
.on_changes(&changes)
.expect("error committing to VFS");
ctx.build_queue.mark_file_dirty(file_path, version_num);
if !ctx.config.lock().unwrap().build_on_save {
ctx.build_current_project(BuildPriority::Normal, &out);
}
Ok(())
}
}
impl BlockingNotificationAction for Cancel {
fn handle<O: Output>(
_params: CancelParams,
_ctx: &mut InitActionContext,
_out: O,
) -> Result<(), ()> {
// Nothing to do.
Ok(())
}
}
impl BlockingNotificationAction for DidChangeConfiguration {
fn handle<O: Output>(
params: DidChangeConfigurationParams,
ctx: &mut InitActionContext,
out: O,
) -> Result<(), ()> {
trace!("config change: {:?}", params.settings);
let settings = ChangeConfigSettings::deserialize(¶ms.settings);
let new_config = match settings {
Ok(mut value) => {
value.rust.normalise();
value.rust
}
Err(err) => {
warn!(
"Received unactionable config: {:?} (error: {:?})",
params.settings, err
);
return Err(());
}
};
let unstable_features = new_config.unstable_features;
{
let mut config = ctx.config.lock().unwrap();
// User may specify null (to be inferred) options, in which case
// we schedule further inference on a separate thread not to block
// the main thread
let needs_inference = new_config.needs_inference();
// In case of null options, we provide default values for now
config.update(new_config);
trace!("Updated config: {:?}", *config);
if needs_inference {
let project_dir = ctx.current_project.clone();
let config = ctx.config.clone();
// Will lock and access Config just outside the current scope
thread::spawn(move || {
let mut config = config.lock().unwrap();
if let Err(e) = config.infer_defaults(&project_dir) {
debug!(
"Encountered an error while trying to infer config \
defaults: {:?}",
e
);
}
});
}
}
// We do a clean build so that if we've changed any relevant options
// for Cargo, we'll notice them. But if nothing relevant changes
// then we don't do unnecessary building (i.e., we don't delete
// artifacts on disk).
ctx.build_current_project(BuildPriority::Cargo, &out);
const RANGE_FORMATTING_ID: &str = "rls-range-formatting";
// FIXME should handle the response
let id = out.provide_id();
if unstable_features {
let params = RegistrationParams {
registrations: vec![Registration {
id: RANGE_FORMATTING_ID.to_owned(),
method: <RangeFormatting as LSPRequest>::METHOD.to_owned(),
register_options: None,
}],
};
let request = Request::<RegisterCapability>::new(id, params);
out.request(request);
} else {
let params = UnregistrationParams {
unregisterations: vec![Unregistration {
id: RANGE_FORMATTING_ID.to_owned(),
method: <RangeFormatting as LSPRequest>::METHOD.to_owned(),
}],
};
let request = Request::<UnregisterCapability>::new(id, params);
out.request(request);
}
Ok(())
}
}
impl BlockingNotificationAction for DidSaveTextDocument {
fn handle<O: Output>(
params: DidSaveTextDocumentParams,
ctx: &mut InitActionContext,
out: O,
) -> Result<(), ()> {
let file_path = parse_file_path!(¶ms.text_document.uri, "on_save")?;
ctx.vfs.file_saved(&file_path).unwrap();
if !ctx.client_use_change_watched && FileWatch::new(&ctx).is_relevant_save_doc(¶ms) {
// support manifest change rebuilding for client's that don't send
// workspace/didChangeWatchedFiles notifications
ctx.build_current_project(BuildPriority::Cargo, &out);
ctx.invalidate_project_model();
} else if ctx.config.lock().unwrap().build_on_save {
ctx.build_current_project(BuildPriority::Normal, &out);
}
Ok(())
}
}
impl BlockingNotificationAction for DidChangeWatchedFiles {
fn handle<O: Output>(
params: DidChangeWatchedFilesParams,
ctx: &mut InitActionContext,
out: O,
) -> Result<(), ()> {
trace!("on_cargo_change: thread: {:?}", thread::current().id());
ctx.client_use_change_watched = true;
let file_watch = FileWatch::new(&ctx);
if params.changes.iter().any(|c| file_watch.is_relevant(c)) {
ctx.build_current_project(BuildPriority::Cargo, &out);
ctx.invalidate_project_model();
}
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::server::{Output, RequestId};
use rls_analysis::{AnalysisHost, Target};
use rls_vfs::Vfs;
use std::sync::Arc;
use url::Url;
#[derive(Clone, Copy)]
struct NoOutput;
impl Output for NoOutput {
/// Send a response string along the output.
fn response(&self, _: String) {}
fn provide_id(&self) -> RequestId {
RequestId::Num(0)
}
}
#[test]
fn learn_client_use_change_watched() {
let (project_root, lsp_project_manifest) = if cfg!(windows) {
("C:/some/dir", "file:c:/some/dir/Cargo.toml")
} else {
("/some/dir", "file:///some/dir/Cargo.toml")
};
let mut ctx = InitActionContext::new(
Arc::new(AnalysisHost::new(Target::Debug)),
Arc::new(Vfs::new()),
<_>::default(),
<_>::default(),
project_root.into(),
123,
false,
);
assert!(!ctx.client_use_change_watched);
let manifest_change = Url::parse(lsp_project_manifest).unwrap();
DidChangeWatchedFiles::handle(
DidChangeWatchedFilesParams {
changes: vec![FileEvent::new(manifest_change, FileChangeType::Changed)],
},
&mut ctx,
NoOutput,
)
.unwrap();
assert!(ctx.client_use_change_watched);
ctx.wait_for_concurrent_jobs();
}
}