5188: Implement StatusBar r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot]
2020-07-02 14:02:52 +00:00
committed by GitHub
10 changed files with 144 additions and 12 deletions

View File

@@ -130,6 +130,7 @@ pub struct ClientCapsConfig {
pub code_action_group: bool,
pub resolve_code_action: bool,
pub hover_actions: bool,
pub status_notification: bool,
}
impl Config {
@@ -365,6 +366,7 @@ impl Config {
self.client_caps.code_action_group = get_bool("codeActionGroup");
self.client_caps.resolve_code_action = get_bool("resolveCodeAction");
self.client_caps.hover_actions = get_bool("hoverActions");
self.client_caps.status_notification = get_bool("statusNotification");
}
}
}

View File

@@ -31,6 +31,8 @@ use crate::{
pub(crate) enum Status {
Loading,
Ready,
Invalid,
NeedsReload,
}
impl Default for Status {

View File

@@ -3,7 +3,7 @@
use std::{collections::HashMap, path::PathBuf};
use lsp_types::request::Request;
use lsp_types::{Position, Range, TextDocumentIdentifier};
use lsp_types::{notification::Notification, Position, Range, TextDocumentIdentifier};
use serde::{Deserialize, Serialize};
pub enum AnalyzerStatus {}
@@ -208,6 +208,22 @@ pub struct SsrParams {
pub parse_only: bool,
}
pub enum StatusNotification {}
#[serde(rename_all = "camelCase")]
#[derive(Serialize, Deserialize)]
pub enum Status {
Loading,
Ready,
NeedsReload,
Invalid,
}
impl Notification for StatusNotification {
type Params = Status;
const METHOD: &'static str = "rust-analyzer/status";
}
pub enum CodeActionRequest {}
impl Request for CodeActionRequest {

View File

@@ -111,6 +111,35 @@ impl GlobalState {
}
fn run(mut self, inbox: Receiver<lsp_server::Message>) -> Result<()> {
let registration_options = lsp_types::TextDocumentRegistrationOptions {
document_selector: Some(vec![
lsp_types::DocumentFilter {
language: None,
scheme: None,
pattern: Some("**/*.rs".into()),
},
lsp_types::DocumentFilter {
language: None,
scheme: None,
pattern: Some("**/Cargo.toml".into()),
},
lsp_types::DocumentFilter {
language: None,
scheme: None,
pattern: Some("**/Cargo.lock".into()),
},
]),
};
let registration = lsp_types::Registration {
id: "textDocument/didSave".to_string(),
method: "textDocument/didSave".to_string(),
register_options: Some(serde_json::to_value(registration_options).unwrap()),
};
self.send_request::<lsp_types::request::RegisterCapability>(
lsp_types::RegistrationParams { registrations: vec![registration] },
|_, _| (),
);
self.reload();
while let Some(event) = self.next_event(&inbox) {
@@ -169,16 +198,16 @@ impl GlobalState {
}
vfs::loader::Message::Progress { n_total, n_done } => {
if n_total == 0 {
self.status = Status::Ready;
self.transition(Status::Invalid);
} else {
let state = if n_done == 0 {
self.status = Status::Loading;
self.transition(Status::Loading);
Progress::Begin
} else if n_done < n_total {
Progress::Report
} else {
assert_eq!(n_done, n_total);
self.status = Status::Ready;
self.transition(Status::Ready);
Progress::End
};
self.report_progress(
@@ -274,6 +303,19 @@ impl GlobalState {
Ok(())
}
fn transition(&mut self, new_status: Status) {
self.status = Status::Ready;
if self.config.client_caps.status_notification {
let lsp_status = match new_status {
Status::Loading => lsp_ext::Status::Loading,
Status::Ready => lsp_ext::Status::Ready,
Status::Invalid => lsp_ext::Status::Invalid,
Status::NeedsReload => lsp_ext::Status::NeedsReload,
};
self.send_notification::<lsp_ext::StatusNotification>(lsp_status);
}
}
fn on_request(&mut self, request_received: Instant, req: Request) -> Result<()> {
self.register_request(&req, request_received);
@@ -383,10 +425,16 @@ impl GlobalState {
);
Ok(())
})?
.on::<lsp_types::notification::DidSaveTextDocument>(|this, _params| {
.on::<lsp_types::notification::DidSaveTextDocument>(|this, params| {
if let Some(flycheck) = &this.flycheck {
flycheck.handle.update();
}
let uri = params.text_document.uri.as_str();
if uri.ends_with("Cargo.toml") || uri.ends_with("Cargo.lock") {
if matches!(this.status, Status::Ready | Status::Invalid) {
this.transition(Status::NeedsReload);
}
}
Ok(())
})?
.on::<lsp_types::notification::DidChangeConfiguration>(|this, _params| {

View File

@@ -78,7 +78,7 @@ impl GlobalState {
.collect(),
};
let registration = lsp_types::Registration {
id: "file-watcher".to_string(),
id: "workspace/didChangeWatchedFiles".to_string(),
method: "workspace/didChangeWatchedFiles".to_string(),
register_options: Some(serde_json::to_value(registration_options).unwrap()),
};