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

Commit

Permalink
Use generic Notifications when sending them to client
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Nov 29, 2017
1 parent 76e170f commit f9ed5a9
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 34 deletions.
9 changes: 7 additions & 2 deletions src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ 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};
use std::sync::{Arc, Mutex};
use std::thread;
use std::marker::PhantomData;


// TODO: Support non-`file` URI schemes in VFS. We're currently ignoring them because
Expand Down Expand Up @@ -194,7 +196,10 @@ impl InitActionContext {
}
};

out.notify(NotificationMessage::new(NOTIFICATION_BUILD_BEGIN, None));
out.notify(Notification::<BeginBuild> {
params: NoParams {},
_action: PhantomData,
});
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";
}
48 changes: 27 additions & 21 deletions src/actions/post_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::thread;
use std::marker::PhantomData;

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 +42,46 @@ 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> {
params: NoParams {},
_action: PhantomData,
});

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> {
params: NoParams {},
_action: PhantomData,
});
});
}
BuildResult::Squashed => {
trace!("build - Squashed");
self.out
.notify(NotificationMessage::new(NOTIFICATION_DIAGNOSTICS_END, None));
self.out.notify(Notification::<DiagnosticsEnd> {
params: NoParams {},
_action: PhantomData,
});
}
BuildResult::Err => {
trace!("build - Error");
self.out
.notify(NotificationMessage::new(NOTIFICATION_DIAGNOSTICS_END, None));
self.out.notify(Notification::<DiagnosticsEnd> {
params: NoParams {},
_action: PhantomData,
});
}
}
}
Expand Down Expand Up @@ -252,9 +258,9 @@ 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> {
params,
_action: PhantomData
});
}
}
7 changes: 0 additions & 7 deletions src/lsp_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ 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)]
pub enum UrlFileParseError {
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
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,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

0 comments on commit f9ed5a9

Please sign in to comment.