|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use std::sync::atomic::{AtomicUsize, Ordering}; |
| 12 | + |
| 13 | +use lsp_data::{ProgressParams, PublishDiagnosticsParams, Progress, ShowMessageParams, MessageType}; |
| 14 | +use server::{Output, Notification}; |
| 15 | +use ls_types::notification::{PublishDiagnostics, ShowMessage}; |
| 16 | + |
| 17 | +/// Trait for communication of build progress back to the client. |
| 18 | +pub trait ProgressNotifier: Send { |
| 19 | + fn notify_begin_progress(&self); |
| 20 | + fn notify_progress(&self, update: ProgressUpdate); |
| 21 | + fn notify_end_progress(&self); |
| 22 | +} |
| 23 | + |
| 24 | +/// Kinds of progress updates |
| 25 | +pub enum ProgressUpdate { |
| 26 | + Message(String), |
| 27 | + Percentage(f64), |
| 28 | +} |
| 29 | + |
| 30 | +/// Trait for communication of diagnostics (i.e. build results) back to the rest of |
| 31 | +/// the RLS (and on to the client). |
| 32 | +// This trait only really exists to work around the object safety rules (Output |
| 33 | +// is not object-safe). |
| 34 | +pub trait DiagnosticsNotifier: Send { |
| 35 | + fn notify_begin_diagnostics(&self); |
| 36 | + fn notify_publish_diagnostics(&self, PublishDiagnosticsParams); |
| 37 | + fn notify_end_diagnostics(&self); |
| 38 | + fn notify_error_diagnostics(&self, msg: &str); |
| 39 | +} |
| 40 | + |
| 41 | +/// Generate a new progress params with a unique ID and the given title. |
| 42 | +fn new_progress_params(title: String) -> ProgressParams { |
| 43 | + |
| 44 | + // counter to generate unique ID for each chain-of-progress notifications. |
| 45 | + lazy_static! { |
| 46 | + static ref PROGRESS_ID_COUNTER: AtomicUsize = { |
| 47 | + AtomicUsize::new(0) |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + ProgressParams { |
| 52 | + id: format!("progress_{}", PROGRESS_ID_COUNTER.fetch_add(1, Ordering::SeqCst)), |
| 53 | + title: Some(title), |
| 54 | + message: None, |
| 55 | + percentage: None, |
| 56 | + done: None, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// Notifier of progress for the build (window/progress notifications). |
| 61 | +/// the same instance is used for the entirety of one single build. |
| 62 | +pub struct BuildProgressNotifier<O: Output> { |
| 63 | + out: O, |
| 64 | + // these params are used as a template and are cloned for each |
| 65 | + // message that is actually notified. |
| 66 | + progress_params: ProgressParams, |
| 67 | +} |
| 68 | + |
| 69 | +impl<O: Output> BuildProgressNotifier<O> { |
| 70 | + pub fn new(out: O) -> BuildProgressNotifier<O> { |
| 71 | + BuildProgressNotifier { |
| 72 | + out, |
| 73 | + progress_params: new_progress_params("Build".into()), |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl<O: Output> ProgressNotifier for BuildProgressNotifier<O> { |
| 79 | + fn notify_begin_progress(&self) { |
| 80 | + let params = self.progress_params.clone(); |
| 81 | + self.out.notify(Notification::<Progress>::new(params)); |
| 82 | + } |
| 83 | + fn notify_progress(&self, update: ProgressUpdate) { |
| 84 | + let mut params = self.progress_params.clone(); |
| 85 | + match update { |
| 86 | + ProgressUpdate::Message(s) => params.message = Some(s), |
| 87 | + ProgressUpdate::Percentage(p) => params.percentage = Some(p), |
| 88 | + } |
| 89 | + self.out.notify(Notification::<Progress>::new(params)); |
| 90 | + } |
| 91 | + fn notify_end_progress(&self) { |
| 92 | + let mut params = self.progress_params.clone(); |
| 93 | + params.done = Some(true); |
| 94 | + self.out.notify(Notification::<Progress>::new(params)); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +/// Notifier of diagnostics after the build has completed. |
| 100 | +pub struct BuildDiagnosticsNotifier<O: Output> { |
| 101 | + out: O, |
| 102 | + // these params are used as a template and are cloned for each |
| 103 | + // message that is actually notified. |
| 104 | + progress_params: ProgressParams, |
| 105 | +} |
| 106 | + |
| 107 | +impl<O: Output> BuildDiagnosticsNotifier<O> { |
| 108 | + pub fn new(out: O) -> BuildDiagnosticsNotifier<O> { |
| 109 | + BuildDiagnosticsNotifier { |
| 110 | + out, |
| 111 | + progress_params: new_progress_params("Diagnostics".into()), |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl<O: Output> DiagnosticsNotifier for BuildDiagnosticsNotifier<O> { |
| 117 | + fn notify_begin_diagnostics(&self) { |
| 118 | + let params = self.progress_params.clone(); |
| 119 | + self.out.notify(Notification::<Progress>::new(params)); |
| 120 | + } |
| 121 | + fn notify_publish_diagnostics(&self, params: PublishDiagnosticsParams) { |
| 122 | + self.out.notify(Notification::<PublishDiagnostics>::new(params)); |
| 123 | + } |
| 124 | + fn notify_end_diagnostics(&self) { |
| 125 | + let mut params = self.progress_params.clone(); |
| 126 | + params.done = Some(true); |
| 127 | + self.out.notify(Notification::<Progress>::new(params)); |
| 128 | + } |
| 129 | + fn notify_error_diagnostics(&self, msg: &str) { |
| 130 | + self.out.notify(Notification::<ShowMessage>::new(ShowMessageParams { |
| 131 | + typ: MessageType::Error, |
| 132 | + message: msg.to_owned(), |
| 133 | + })); |
| 134 | + } |
| 135 | +} |
0 commit comments