Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 3486c78

Browse files
authored
Merge pull request #658 from Xanewok/update-ls-types
Update languageserver-types and clean up a bit
2 parents 72aba6e + 45ad2bd commit 3486c78

File tree

9 files changed

+125
-119
lines changed

9 files changed

+125
-119
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cargo = { git = "https://github.com/rust-lang/cargo" }
1414
env_logger = "0.4"
1515
failure = "0.1.1"
1616
jsonrpc-core = "8.0.1"
17-
languageserver-types = { git = "https://github.com/Xanewok/languageserver-types", branch = "backport-0.17-fix-params-types" }
17+
languageserver-types = "0.27"
1818
lazy_static = "0.2"
1919
log = "0.3"
2020
racer = "2.0.12"

src/actions/notifications.rs

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use Span;
2121

2222
use build::*;
2323
use lsp_data::*;
24-
use lsp_data::request::{Request, RangeFormatting};
25-
use lsp_data::notification::{Notification, RegisterCapability, UnregisterCapability};
24+
use lsp_data::request::{RangeFormatting, RegisterCapability, UnregisterCapability};
25+
use server::Request;
2626

2727
pub use lsp_data::notification::{
2828
Initialized,
@@ -46,21 +46,19 @@ impl BlockingNotificationAction for Initialized {
4646

4747
let ctx = ctx.inited();
4848

49-
let options = FileWatch::new(&ctx).watchers_config();
50-
let output = serde_json::to_string(&RequestMessage::new(
51-
out.provide_id(),
52-
<RegisterCapability as Notification>::METHOD.to_owned(),
53-
RegistrationParams {
54-
registrations: vec![
55-
Registration {
56-
id: WATCH_ID.to_owned(),
57-
method: <DidChangeWatchedFiles as Notification>::METHOD.to_owned(),
58-
register_options: options,
59-
},
60-
],
61-
},
62-
)).unwrap();
63-
out.response(output);
49+
let id = out.provide_id() as usize;
50+
let params = RegistrationParams {
51+
registrations: vec![
52+
Registration {
53+
id: WATCH_ID.to_owned(),
54+
method: <DidChangeWatchedFiles as LSPNotification>::METHOD.to_owned(),
55+
register_options: FileWatch::new(&ctx).watchers_config(),
56+
},
57+
],
58+
};
59+
60+
let request = Request::<RegisterCapability>::new(id, params);
61+
out.request(request);
6462
Ok(())
6563
}
6664
}
@@ -111,7 +109,7 @@ impl BlockingNotificationAction for DidChangeTextDocument {
111109
.expect("error committing to VFS");
112110
if !changes.is_empty() {
113111
ctx.build_queue
114-
.mark_file_dirty(file_path, params.text_document.version)
112+
.mark_file_dirty(file_path, params.text_document.version.unwrap())
115113
}
116114

117115
if !ctx.config.lock().unwrap().build_on_save {
@@ -198,35 +196,32 @@ impl BlockingNotificationAction for DidChangeConfiguration {
198196

199197
const RANGE_FORMATTING_ID: &'static str = "rls-range-formatting";
200198
// FIXME should handle the response
199+
let id = out.provide_id() as usize;
201200
if unstable_features {
202-
let output = serde_json::to_string(&RequestMessage::new(
203-
out.provide_id(),
204-
<RegisterCapability as Notification>::METHOD.to_owned(),
205-
RegistrationParams {
201+
let params = RegistrationParams {
206202
registrations: vec![
207203
Registration {
208204
id: RANGE_FORMATTING_ID.to_owned(),
209-
method: <RangeFormatting as Request>::METHOD.to_owned(),
205+
method: <RangeFormatting as LSPRequest>::METHOD.to_owned(),
210206
register_options: serde_json::Value::Null,
211207
},
212208
],
213-
},
214-
)).unwrap();
215-
out.response(output);
209+
};
210+
211+
let request = Request::<RegisterCapability>::new(id, params);
212+
out.request(request);
216213
} else {
217-
let output = serde_json::to_string(&RequestMessage::new(
218-
out.provide_id(),
219-
<UnregisterCapability as Notification>::METHOD.to_owned(),
220-
UnregistrationParams {
221-
unregisterations: vec![
222-
Unregistration {
223-
id: RANGE_FORMATTING_ID.to_owned(),
224-
method: <RangeFormatting as Request>::METHOD.to_owned(),
225-
},
226-
],
227-
},
228-
)).unwrap();
229-
out.response(output);
214+
let params = UnregistrationParams {
215+
unregisterations: vec![
216+
Unregistration {
217+
id: RANGE_FORMATTING_ID.to_owned(),
218+
method: <RangeFormatting as LSPRequest>::METHOD.to_owned(),
219+
},
220+
],
221+
};
222+
223+
let request = Request::<UnregisterCapability>::new(id, params);
224+
out.request(request);
230225
}
231226
Ok(())
232227
}

src/actions/requests.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ use rayon;
2727
use lsp_data;
2828
use lsp_data::*;
2929
use server;
30-
use server::{Ack, Output, RequestAction, ResponseError};
30+
use server::{Ack, Output, Request, RequestAction, ResponseError};
3131
use jsonrpc_core::types::ErrorCode;
3232

33+
use lsp_data::request::ApplyWorkspaceEdit;
3334
pub use lsp_data::request::{
3435
WorkspaceSymbol,
3536
DocumentSymbol as Symbols,
@@ -370,7 +371,8 @@ impl RequestAction for Rename {
370371

371372
fn fallback_response() -> Result<Self::Response, ResponseError> {
372373
Ok(WorkspaceEdit {
373-
changes: HashMap::new(),
374+
changes: None,
375+
document_changes: None,
374376
})
375377
}
376378

@@ -422,7 +424,7 @@ impl RequestAction for Rename {
422424
});
423425
}
424426

425-
Ok(WorkspaceEdit { changes: edits })
427+
Ok(WorkspaceEdit { changes: Some(edits), document_changes: None })
426428
}
427429
}
428430

@@ -437,14 +439,13 @@ impl server::Response for ExecuteCommandResponse {
437439
// FIXME should handle the client's responses
438440
match *self {
439441
ExecuteCommandResponse::ApplyEdit(ref params) => {
440-
let output = serde_json::to_string(&RequestMessage::new(
441-
out.provide_id(),
442-
"workspace/applyEdit".to_owned(),
443-
ApplyWorkspaceEditParams {
442+
let id = out.provide_id() as usize;
443+
let params = ApplyWorkspaceEditParams {
444444
edit: params.edit.clone(),
445-
},
446-
)).unwrap();
447-
out.response(output);
445+
};
446+
447+
let request = Request::<ApplyWorkspaceEdit>::new(id, params);
448+
out.request(request);
448449
}
449450
}
450451

@@ -515,11 +516,15 @@ fn apply_deglobs(args: Vec<serde_json::Value>) -> Result<ApplyWorkspaceEditParam
515516
}
516517
})
517518
.collect();
518-
let mut edit = WorkspaceEdit {
519-
changes: HashMap::new(),
520-
};
521519
// all deglob results will share the same URI
522-
edit.changes.insert(uri, text_edits);
520+
let changes: HashMap<_, _> = vec![(uri, text_edits)]
521+
.into_iter()
522+
.collect();
523+
524+
let edit = WorkspaceEdit {
525+
changes: Some(changes),
526+
document_changes: None,
527+
};
523528

524529
Ok(ApplyWorkspaceEditParams { edit })
525530
}

src/lsp_data.rs

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010

1111
//! Types, helpers, and conversions to and from LSP and `racer` types.
1212
13-
use std::collections::HashMap;
14-
use std::fmt::{self, Debug};
13+
use std::fmt;
1514
use std::path::PathBuf;
1615
use std::error::Error;
1716

1817
use analysis::DefKind;
1918
use url::Url;
20-
use serde::Serialize;
2119
use span;
2220
use racer;
2321
use vfs::FileContents;
2422
use ls_types;
2523

2624
pub use ls_types::*;
25+
pub use ls_types::request::Request as LSPRequest;
26+
pub use ls_types::notification::Notification as LSPNotification;
2727

2828
/// Errors that can occur when parsing a file URI.
2929
#[derive(Debug)]
@@ -64,21 +64,20 @@ pub fn parse_file_path(uri: &Url) -> Result<PathBuf, UrlFileParseError> {
6464

6565
/// Create an edit for the given location and text.
6666
pub fn make_workspace_edit(location: Location, new_text: String) -> WorkspaceEdit {
67-
let mut edit = WorkspaceEdit {
68-
changes: HashMap::new(),
69-
};
70-
71-
edit.changes.insert(
67+
let changes = vec![(
7268
location.uri,
7369
vec![
7470
TextEdit {
7571
range: location.range,
7672
new_text,
7773
},
7874
],
79-
);
75+
)].into_iter().collect();
8076

81-
edit
77+
WorkspaceEdit {
78+
changes: Some(changes),
79+
document_changes: None,
80+
}
8281
}
8382

8483
/// Utilities for working with the language server protocol.
@@ -294,44 +293,14 @@ impl ClientCapabilities {
294293
}
295294
}
296295

297-
/// A JSON language server protocol request that will have a matching response.
298-
#[derive(Debug, Serialize)]
299-
pub struct RequestMessage<T>
300-
where
301-
T: Debug + Serialize,
302-
{
303-
jsonrpc: &'static str,
304-
/// The request id. The response will have a matching id.
305-
pub id: u32,
306-
/// The well-known language server protocol request method string.
307-
pub method: String,
308-
/// Extra request parameters.
309-
pub params: T,
310-
}
311-
312-
impl<T> RequestMessage<T>
313-
where
314-
T: Debug + Serialize,
315-
{
316-
/// Construct a new request.
317-
pub fn new(id: u32, method: String, params: T) -> Self {
318-
RequestMessage {
319-
jsonrpc: "2.0",
320-
id,
321-
method: method,
322-
params: params,
323-
}
324-
}
325-
}
326-
327296
/* --------------- Custom JSON-RPC notifications -------------- */
328297

329298
/// Custom LSP notification sent to client indicating that the server is currently
330299
/// processing data and may publish new diagnostics on `rustDocument/diagnosticsEnd`.
331300
#[derive(Debug)]
332301
pub enum DiagnosticsBegin { }
333302

334-
impl notification::Notification for DiagnosticsBegin {
303+
impl LSPNotification for DiagnosticsBegin {
335304
type Params = ();
336305
const METHOD: &'static str = "rustDocument/diagnosticsBegin";
337306
}
@@ -344,7 +313,7 @@ impl notification::Notification for DiagnosticsBegin {
344313
#[derive(Debug)]
345314
pub enum DiagnosticsEnd { }
346315

347-
impl notification::Notification for DiagnosticsEnd {
316+
impl LSPNotification for DiagnosticsEnd {
348317
type Params = ();
349318
const METHOD: &'static str = "rustDocument/diagnosticsEnd";
350319
}
@@ -353,7 +322,7 @@ impl notification::Notification for DiagnosticsEnd {
353322
#[derive(Debug)]
354323
pub enum BeginBuild { }
355324

356-
impl notification::Notification for BeginBuild {
325+
impl LSPNotification for BeginBuild {
357326
type Params = ();
358327
const METHOD: &'static str = "rustDocument/beginBuild";
359328
}
@@ -364,9 +333,8 @@ impl notification::Notification for BeginBuild {
364333
#[derive(Debug)]
365334
pub enum FindImpls { }
366335

367-
impl request::Request for FindImpls {
336+
impl LSPRequest for FindImpls {
368337
type Params = TextDocumentPositionParams;
369338
type Result = Vec<Location>;
370339
const METHOD: &'static str = "rustDocument/implementations";
371340
}
372-

src/server/dispatch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use server;
1414
use server::{Request, Response};
1515
use server::io::Output;
1616
use actions::InitActionContext;
17-
use ls_types::request::Request as LSPRequest;
17+
use lsp_data::LSPRequest;
1818
use std::sync::mpsc;
1919
use std::thread;
2020
use std::time::Duration;

src/server/io.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use serde;
1212
use serde_json;
1313

14-
use super::Notification;
15-
use ls_types::notification::Notification as LSPNotification;
14+
use super::{Notification, Request};
15+
use lsp_data::{LSPNotification, LSPRequest};
1616

1717
use std::fmt;
1818
use std::io::{self, Read, Write};
@@ -145,13 +145,23 @@ pub trait Output: Sync + Send + Clone + 'static {
145145
}
146146

147147
/// Send a notification along the output.
148-
fn notify<A, P>(&self, notification: Notification<A>)
148+
fn notify<A>(&self, notification: Notification<A>)
149149
where
150-
A: LSPNotification<Params = P>,
151-
P: serde::Serialize,
150+
A: LSPNotification,
151+
<A as LSPNotification>::Params: serde::Serialize,
152152
{
153153
self.response(format!("{}", notification));
154154
}
155+
156+
/// Send a one-shot request along the output.
157+
/// Ignores any response associated with the request.
158+
fn request<A>(&self, request: Request<A>)
159+
where
160+
A: LSPRequest,
161+
<A as LSPRequest>::Params: serde::Serialize,
162+
{
163+
self.response(format!("{}", request));
164+
}
155165
}
156166

157167
/// An output that sends notifications and responses on `stdout`.

0 commit comments

Comments
 (0)