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

Commit 41bf9af

Browse files
authored
Merge pull request #603 from Xanewok/simplify-out-notifications
Simplify outgoing notifications, remove NoParams
2 parents 76e170f + 3f41009 commit 41bf9af

File tree

6 files changed

+80
-60
lines changed

6 files changed

+80
-60
lines changed

src/actions/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ use span;
2020
use Span;
2121

2222
use actions::post_build::{BuildResults, PostBuildHandler};
23+
use actions::notifications::BeginBuild;
2324
use build::*;
2425
use lsp_data::*;
25-
use server::Output;
26+
use server::{Output, Notification, NoParams};
2627

2728
use std::collections::HashMap;
2829
use std::path::{Path, PathBuf};
@@ -194,7 +195,7 @@ impl InitActionContext {
194195
}
195196
};
196197

197-
out.notify(NotificationMessage::new(NOTIFICATION_BUILD_BEGIN, None));
198+
out.notify(Notification::<BeginBuild>::new(NoParams {}));
198199
self.build_queue
199200
.request_build(project_path, priority, move |result| pbh.handle(result));
200201
}

src/actions/notifications.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,54 @@ impl<'a> BlockingNotificationAction<'a> for DidChangeWatchedFiles {
369369
Ok(())
370370
}
371371
}
372+
373+
/// Notification sent to client, used to publish file diagnostics
374+
/// (warnings, errors) from the server.
375+
#[derive(Debug)]
376+
pub struct PublishDiagnostics;
377+
378+
impl Action for PublishDiagnostics {
379+
type Params = PublishDiagnosticsParams;
380+
const METHOD: &'static str = NOTIFICATION__PublishDiagnostics;
381+
}
382+
383+
/// Notification sent to client, asking to show a specified message with a given type.
384+
#[derive(Debug)]
385+
pub struct ShowMessage;
386+
387+
impl Action for ShowMessage {
388+
type Params = ShowMessageParams;
389+
const METHOD: &'static str = NOTIFICATION__ShowMessage;
390+
}
391+
392+
/// Custom LSP notification sent to client indicating that the server is currently
393+
/// processing data and may publish new diagnostics on `rustDocument/diagnosticsEnd`.
394+
#[derive(Debug)]
395+
pub struct DiagnosticsBegin;
396+
397+
impl Action for DiagnosticsBegin {
398+
type Params = NoParams;
399+
const METHOD: &'static str = "rustDocument/diagnosticsBegin";
400+
}
401+
402+
/// Custom LSP notification sent to client indicating that data processing started
403+
/// by a `rustDocument`/diagnosticsBegin` has ended.
404+
/// For each `diagnosticsBegin` message, there is a single `diagnosticsEnd` message.
405+
/// This means that for multiple active `diagnosticsBegin` messages, there will
406+
/// be sent multiple `diagnosticsEnd` notifications.
407+
#[derive(Debug)]
408+
pub struct DiagnosticsEnd;
409+
410+
impl Action for DiagnosticsEnd {
411+
type Params = NoParams;
412+
const METHOD: &'static str = "rustDocument/diagnosticsEnd";
413+
}
414+
415+
/// Custom LSP notification sent to client indicating that a build process has begun.
416+
#[derive(Debug)]
417+
pub struct BeginBuild;
418+
419+
impl Action for BeginBuild {
420+
type Params = NoParams;
421+
const METHOD: &'static str = "rustDocument/beginBuild";
422+
}

src/actions/post_build.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ use std::sync::{Arc, Mutex};
1414
use std::thread;
1515

1616
use build::BuildResult;
17-
use lsp_data::{ls_util, NotificationMessage, PublishDiagnosticsParams};
18-
use lsp_data::{NOTIFICATION_DIAGNOSTICS_BEGIN, NOTIFICATION_DIAGNOSTICS_END};
19-
use server::Output;
17+
use lsp_data::{ls_util, PublishDiagnosticsParams};
18+
use actions::notifications::{DiagnosticsBegin, DiagnosticsEnd, PublishDiagnostics};
19+
use server::{Notification, Output, NoParams};
2020
use CRATE_BLACKLIST;
2121
use Span;
2222

2323
use analysis::AnalysisHost;
2424
use data::Analysis;
25-
use ls_types::{self, Diagnostic, DiagnosticSeverity, NumberOrString, Range};
25+
use ls_types::{Diagnostic, DiagnosticSeverity, NumberOrString, Range};
2626
use serde_json;
2727
use span::compiler::DiagnosticSpan;
2828
use url::Url;
@@ -41,41 +41,34 @@ pub struct PostBuildHandler<O: Output> {
4141

4242
impl<O: Output> PostBuildHandler<O> {
4343
pub fn handle(self, result: BuildResult) {
44-
// We use `rustDocument` document here since these notifications are
45-
// custom to the RLS and not part of the LS protocol.
46-
self.out.notify(NotificationMessage::new(
47-
NOTIFICATION_DIAGNOSTICS_BEGIN,
48-
None,
49-
));
44+
self.out.notify(Notification::<DiagnosticsBegin>::new(NoParams {}));
5045

5146
match result {
5247
BuildResult::Success(messages, new_analysis) => {
5348
thread::spawn(move || {
5449
trace!("build - Success");
5550

51+
// Emit appropriate diagnostics using the ones from build.
5652
self.handle_messages(messages);
5753

58-
// Handle the analysis data.
54+
// Reload the analysis data.
5955
debug!("reload analysis: {:?}", self.project_path);
6056
if new_analysis.is_empty() {
6157
self.reload_analysis_from_disk();
6258
} else {
6359
self.reload_analysis_from_memory(new_analysis);
6460
}
6561

66-
self.out
67-
.notify(NotificationMessage::new(NOTIFICATION_DIAGNOSTICS_END, None));
62+
self.out.notify(Notification::<DiagnosticsEnd>::new(NoParams{}));
6863
});
6964
}
7065
BuildResult::Squashed => {
7166
trace!("build - Squashed");
72-
self.out
73-
.notify(NotificationMessage::new(NOTIFICATION_DIAGNOSTICS_END, None));
67+
self.out.notify(Notification::<DiagnosticsEnd>::new(NoParams{}));
7468
}
7569
BuildResult::Err => {
7670
trace!("build - Error");
77-
self.out
78-
.notify(NotificationMessage::new(NOTIFICATION_DIAGNOSTICS_END, None));
71+
self.out.notify(Notification::<DiagnosticsEnd>::new(NoParams{}));
7972
}
8073
}
8174
}
@@ -252,9 +245,6 @@ fn emit_notifications<O: Output>(build_results: &BuildResults, show_warnings: bo
252245
.collect(),
253246
};
254247

255-
out.notify(NotificationMessage::new(
256-
ls_types::NOTIFICATION__PublishDiagnostics,
257-
Some(params),
258-
));
248+
out.notify(Notification::<PublishDiagnostics>::new(params));
259249
}
260250
}

src/lsp_data.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ use racer;
2323
use vfs::FileContents;
2424

2525
pub use ls_types::*;
26-
use jsonrpc_core::version;
27-
28-
/// Notification string for beginning diagnostics.
29-
pub const NOTIFICATION_DIAGNOSTICS_BEGIN: &'static str = "rustDocument/diagnosticsBegin";
30-
/// Notification string for ending diagnostics.
31-
pub const NOTIFICATION_DIAGNOSTICS_END: &'static str = "rustDocument/diagnosticsEnd";
32-
/// Notification string for when a build begins.
33-
pub const NOTIFICATION_BUILD_BEGIN: &'static str = "rustDocument/beginBuild";
3426

3527
/// Errors that can occur when parsing a file URI.
3628
#[derive(Debug)]
@@ -270,27 +262,6 @@ impl Default for InitializationOptions {
270262
}
271263
}
272264

273-
/// An event-like (no response needed) notification message.
274-
#[derive(Debug, Serialize)]
275-
pub struct NotificationMessage {
276-
jsonrpc: version::Version,
277-
/// The well-known language server protocol notification method string.
278-
pub method: &'static str,
279-
/// Extra notification parameters.
280-
pub params: Option<PublishDiagnosticsParams>,
281-
}
282-
283-
impl NotificationMessage {
284-
/// Construct a new notification message.
285-
pub fn new(method: &'static str, params: Option<PublishDiagnosticsParams>) -> Self {
286-
NotificationMessage {
287-
jsonrpc: version::Version::V2,
288-
method,
289-
params,
290-
}
291-
}
292-
}
293-
294265
/// A JSON language server protocol request that will have a matching response.
295266
#[derive(Debug, Serialize)]
296267
pub struct RequestMessage<T>

src/server/io.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use serde_json;
1212

13-
use lsp_data::*;
13+
use super::{Action, Notification};
1414

1515
use std::fmt;
1616
use std::io::{self, Read, Write};
@@ -143,8 +143,8 @@ pub trait Output: Sync + Send + Clone + 'static {
143143
}
144144

145145
/// Send a notification along the output.
146-
fn notify(&self, notification: NotificationMessage) {
147-
self.response(serde_json::to_string(&notification).unwrap());
146+
fn notify<A: Action>(&self, notification: Notification<A>) {
147+
self.response(format!("{}", notification));
148148
}
149149
}
150150

src/server/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ pub struct Notification<A: Action> {
153153
pub _action: PhantomData<A>,
154154
}
155155

156+
impl<A: Action> Notification<A> {
157+
/// Creates a `Notification` structure with given `params`.
158+
pub fn new(params: A::Params) -> Notification<A> {
159+
Notification {
160+
params,
161+
_action: PhantomData
162+
}
163+
}
164+
}
165+
156166
impl<'a, A: BlockingRequestAction<'a>> Request<A> {
157167
fn blocking_dispatch<O: Output>(
158168
self,
@@ -191,7 +201,7 @@ impl<'a, A: Action> fmt::Display for Request<A> {
191201
}
192202
}
193203

194-
impl<'a, A: BlockingNotificationAction<'a>> fmt::Display for Notification<A> {
204+
impl<'a, A: Action> fmt::Display for Notification<A> {
195205
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
196206
json!({
197207
"jsonrpc": "2.0",
@@ -678,10 +688,7 @@ mod test {
678688

679689
assert_eq!(
680690
notification,
681-
Ok(Notification::<notifications::Initialized> {
682-
params: NoParams {},
683-
_action: PhantomData,
684-
})
691+
Ok(Notification::<notifications::Initialized>::new(NoParams {}))
685692
);
686693
}
687694
}

0 commit comments

Comments
 (0)