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

Simplify outgoing notifications, remove NoParams #603

Merged
merged 2 commits into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ use span;
use Span;

use actions::post_build::{BuildResults, PostBuildHandler};
use actions::notifications::BeginBuild;
use build::*;
use lsp_data::*;
use server::Output;
use server::{Output, Notification, NoParams};

use std::collections::HashMap;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -194,7 +195,7 @@ impl InitActionContext {
}
};

out.notify(NotificationMessage::new(NOTIFICATION_BUILD_BEGIN, None));
out.notify(Notification::<BeginBuild>::new(NoParams {}));
self.build_queue
.request_build(project_path, priority, move |result| pbh.handle(result));
}
Expand Down
51 changes: 51 additions & 0 deletions src/actions/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,54 @@ impl<'a> BlockingNotificationAction<'a> for DidChangeWatchedFiles {
Ok(())
}
}

/// Notification sent to client, used to publish file diagnostics
/// (warnings, errors) from the server.
#[derive(Debug)]
pub struct PublishDiagnostics;

impl Action for PublishDiagnostics {
type Params = PublishDiagnosticsParams;
const METHOD: &'static str = NOTIFICATION__PublishDiagnostics;
}

/// Notification sent to client, asking to show a specified message with a given type.
#[derive(Debug)]
pub struct ShowMessage;

impl Action for ShowMessage {
type Params = ShowMessageParams;
const METHOD: &'static str = NOTIFICATION__ShowMessage;
}

/// Custom LSP notification sent to client indicating that the server is currently
/// processing data and may publish new diagnostics on `rustDocument/diagnosticsEnd`.
#[derive(Debug)]
pub struct DiagnosticsBegin;

impl Action for DiagnosticsBegin {
type Params = NoParams;
const METHOD: &'static str = "rustDocument/diagnosticsBegin";
}

/// Custom LSP notification sent to client indicating that data processing started
/// by a `rustDocument`/diagnosticsBegin` has ended.
/// For each `diagnosticsBegin` message, there is a single `diagnosticsEnd` message.
/// This means that for multiple active `diagnosticsBegin` messages, there will
/// be sent multiple `diagnosticsEnd` notifications.
#[derive(Debug)]
pub struct DiagnosticsEnd;

impl Action for DiagnosticsEnd {
type Params = NoParams;
const METHOD: &'static str = "rustDocument/diagnosticsEnd";
}

/// Custom LSP notification sent to client indicating that a build process has begun.
#[derive(Debug)]
pub struct BeginBuild;

impl Action for BeginBuild {
type Params = NoParams;
const METHOD: &'static str = "rustDocument/beginBuild";
}
32 changes: 11 additions & 21 deletions src/actions/post_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ use std::sync::{Arc, Mutex};
use std::thread;

use build::BuildResult;
use lsp_data::{ls_util, NotificationMessage, PublishDiagnosticsParams};
use lsp_data::{NOTIFICATION_DIAGNOSTICS_BEGIN, NOTIFICATION_DIAGNOSTICS_END};
use server::Output;
use lsp_data::{ls_util, PublishDiagnosticsParams};
use actions::notifications::{DiagnosticsBegin, DiagnosticsEnd, PublishDiagnostics};
use server::{Notification, Output, NoParams};
use CRATE_BLACKLIST;
use Span;

use analysis::AnalysisHost;
use data::Analysis;
use ls_types::{self, Diagnostic, DiagnosticSeverity, NumberOrString, Range};
use ls_types::{Diagnostic, DiagnosticSeverity, NumberOrString, Range};
use serde_json;
use span::compiler::DiagnosticSpan;
use url::Url;
Expand All @@ -41,41 +41,34 @@ pub struct PostBuildHandler<O: Output> {

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

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

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

// Handle the analysis data.
// Reload the analysis data.
debug!("reload analysis: {:?}", self.project_path);
if new_analysis.is_empty() {
self.reload_analysis_from_disk();
} else {
self.reload_analysis_from_memory(new_analysis);
}

self.out
.notify(NotificationMessage::new(NOTIFICATION_DIAGNOSTICS_END, None));
self.out.notify(Notification::<DiagnosticsEnd>::new(NoParams{}));
});
}
BuildResult::Squashed => {
trace!("build - Squashed");
self.out
.notify(NotificationMessage::new(NOTIFICATION_DIAGNOSTICS_END, None));
self.out.notify(Notification::<DiagnosticsEnd>::new(NoParams{}));
}
BuildResult::Err => {
trace!("build - Error");
self.out
.notify(NotificationMessage::new(NOTIFICATION_DIAGNOSTICS_END, None));
self.out.notify(Notification::<DiagnosticsEnd>::new(NoParams{}));
}
}
}
Expand Down Expand Up @@ -252,9 +245,6 @@ fn emit_notifications<O: Output>(build_results: &BuildResults, show_warnings: bo
.collect(),
};

out.notify(NotificationMessage::new(
ls_types::NOTIFICATION__PublishDiagnostics,
Some(params),
));
out.notify(Notification::<PublishDiagnostics>::new(params));
}
}
29 changes: 0 additions & 29 deletions src/lsp_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ use racer;
use vfs::FileContents;

pub use ls_types::*;
use jsonrpc_core::version;

/// Notification string for beginning diagnostics.
pub const NOTIFICATION_DIAGNOSTICS_BEGIN: &'static str = "rustDocument/diagnosticsBegin";
/// Notification string for ending diagnostics.
pub const NOTIFICATION_DIAGNOSTICS_END: &'static str = "rustDocument/diagnosticsEnd";
/// Notification string for when a build begins.
pub const NOTIFICATION_BUILD_BEGIN: &'static str = "rustDocument/beginBuild";

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

/// An event-like (no response needed) notification message.
#[derive(Debug, Serialize)]
pub struct NotificationMessage {
jsonrpc: version::Version,
/// The well-known language server protocol notification method string.
pub method: &'static str,
/// Extra notification parameters.
pub params: Option<PublishDiagnosticsParams>,
}

impl NotificationMessage {
/// Construct a new notification message.
pub fn new(method: &'static str, params: Option<PublishDiagnosticsParams>) -> Self {
NotificationMessage {
jsonrpc: version::Version::V2,
method,
params,
}
}
}

/// A JSON language server protocol request that will have a matching response.
#[derive(Debug, Serialize)]
pub struct RequestMessage<T>
Expand Down
6 changes: 3 additions & 3 deletions src/server/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use serde_json;

use lsp_data::*;
use super::{Action, Notification};

use std::fmt;
use std::io::{self, Read, Write};
Expand Down Expand Up @@ -143,8 +143,8 @@ pub trait Output: Sync + Send + Clone + 'static {
}

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

Expand Down
17 changes: 12 additions & 5 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ pub struct Notification<A: Action> {
pub _action: PhantomData<A>,
}

impl<A: Action> Notification<A> {
/// Creates a `Notification` structure with given `params`.
pub fn new(params: A::Params) -> Notification<A> {
Notification {
params,
_action: PhantomData
}
}
}

impl<'a, A: BlockingRequestAction<'a>> Request<A> {
fn blocking_dispatch<O: Output>(
self,
Expand Down Expand Up @@ -191,7 +201,7 @@ impl<'a, A: Action> fmt::Display for Request<A> {
}
}

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

assert_eq!(
notification,
Ok(Notification::<notifications::Initialized> {
params: NoParams {},
_action: PhantomData,
})
Ok(Notification::<notifications::Initialized>::new(NoParams {}))
);
}
}