Skip to content

Commit 2ca221e

Browse files
committed
Fix rust-lang#686: send showMessage when missing root Cargo.toml
1 parent b2dcaf3 commit 2ca221e

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

src/actions/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ impl ActionContext {
8383
client_capabilities: lsp_data::ClientCapabilities,
8484
out: O,
8585
) {
86+
let cargo_toml = current_project.join("Cargo.toml");
87+
if !cargo_toml.is_file() {
88+
let warning: Notification<ShowMessage> = Notification::new(ShowMessageParams {
89+
typ: MessageType::Warning,
90+
message: format!("A {:?} file is required to support all RLS features", cargo_toml)
91+
});
92+
out.notify(warning);
93+
}
94+
8695
let ctx = match *self {
8796
ActionContext::Uninit(ref uninit) => {
8897
let ctx = InitActionContext::new(

src/test/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern crate json;
1515

1616
#[macro_use]
1717
mod harness;
18+
mod warnings;
1819

1920
use analysis;
2021
use actions::{requests, notifications};

src/test/warnings.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use super::*;
2+
3+
#[test]
4+
fn warn_missing_root_cargo_toml() {
5+
let mut env = Environment::new("no_cargo_toml");
6+
7+
let root_path = env.cache.abs_path(Path::new("."));
8+
let messages = vec![
9+
initialize(0, root_path.as_os_str().to_str().map(|x| x.to_owned())).to_string(),
10+
];
11+
12+
let (mut server, results) = env.mock_server(messages);
13+
// Initialize and build.
14+
assert_eq!(
15+
ls_server::LsService::handle_message(&mut server),
16+
ls_server::ServerStateChange::Continue
17+
);
18+
{
19+
wait_for_n_results!(1, results);
20+
let response = json::parse(&results.lock().unwrap().remove(0)).unwrap();
21+
println!("{}", response.pretty(2));
22+
assert!(response["result"]["capabilities"].is_object());
23+
}
24+
{
25+
// showMessage warning
26+
wait_for_n_results!(1, results);
27+
let response = json::parse(&results.lock().unwrap().remove(0)).unwrap();
28+
println!("{}", response.pretty(2));
29+
assert_eq!(response["method"], "window/showMessage");
30+
assert_eq!(response["params"]["type"], 2, "Expected 'warning' message type");
31+
let message = response["params"]["message"].as_str().unwrap();
32+
assert!(message.contains("Cargo.toml"));
33+
}
34+
}

test_data/no_cargo_toml/src/main.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016 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+
struct Bar {
11+
x: u64,
12+
}
13+
14+
#[test]
15+
pub fn test_fn() {
16+
let bar = Bar { x: 4 };
17+
println!("bar: {}", bar.x);
18+
}
19+
20+
pub fn main() {
21+
let world = "world";
22+
println!("Hello, {}!", world);
23+
24+
let bar2 = Bar { x: 5 };
25+
println!("bar2: {}", bar2.x);
26+
}

0 commit comments

Comments
 (0)