2018-09-01 17:21:11 +00:00
|
|
|
mod support;
|
|
|
|
|
2019-01-10 21:37:10 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
time::Instant,
|
|
|
|
};
|
|
|
|
|
2019-01-14 10:55:56 +00:00
|
|
|
use lsp_types::{
|
2019-01-03 13:43:47 +00:00
|
|
|
CodeActionContext, DocumentFormattingParams, FormattingOptions, Position, Range,
|
|
|
|
};
|
|
|
|
use ra_lsp_server::req::{
|
2019-01-10 17:13:08 +00:00
|
|
|
CodeActionParams, CodeActionRequest, Formatting, Runnables, RunnablesParams, CompletionParams, Completion,
|
2019-01-03 13:43:47 +00:00
|
|
|
};
|
2018-12-06 20:07:31 +00:00
|
|
|
use serde_json::json;
|
2019-03-05 21:29:23 +00:00
|
|
|
use tempfile::TempDir;
|
2018-09-01 17:21:11 +00:00
|
|
|
|
2019-04-13 18:33:49 +00:00
|
|
|
use crate::support::{project, Project};
|
2018-09-01 17:21:11 +00:00
|
|
|
|
2018-09-02 13:36:03 +00:00
|
|
|
const LOG: &'static str = "";
|
2018-09-02 11:46:15 +00:00
|
|
|
|
2019-01-10 17:13:08 +00:00
|
|
|
#[test]
|
|
|
|
fn completes_items_from_standard_library() {
|
2019-01-10 21:37:10 +00:00
|
|
|
let project_start = Instant::now();
|
2019-01-10 17:13:08 +00:00
|
|
|
let server = project(
|
|
|
|
r#"
|
|
|
|
//- Cargo.toml
|
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.0"
|
|
|
|
|
|
|
|
//- src/lib.rs
|
2019-01-10 21:37:10 +00:00
|
|
|
use std::collections::Spam;
|
2019-01-10 17:13:08 +00:00
|
|
|
"#,
|
|
|
|
);
|
2019-03-07 14:46:17 +00:00
|
|
|
server.wait_until_workspace_is_loaded();
|
2019-01-10 21:37:10 +00:00
|
|
|
eprintln!("loading took {:?}", project_start.elapsed());
|
|
|
|
let completion_start = Instant::now();
|
|
|
|
let res = server.send_request::<Completion>(CompletionParams {
|
|
|
|
text_document: server.doc_id("src/lib.rs"),
|
|
|
|
context: None,
|
|
|
|
position: Position::new(0, 23),
|
|
|
|
});
|
|
|
|
assert!(format!("{}", res).contains("HashMap"));
|
|
|
|
eprintln!("completion took {:?}", completion_start.elapsed());
|
2019-01-10 17:13:08 +00:00
|
|
|
}
|
|
|
|
|
2018-09-01 17:21:11 +00:00
|
|
|
#[test]
|
2018-09-02 13:36:03 +00:00
|
|
|
fn test_runnables_no_project() {
|
2018-10-15 21:44:23 +00:00
|
|
|
let server = project(
|
|
|
|
r"
|
2018-09-01 17:21:11 +00:00
|
|
|
//- lib.rs
|
|
|
|
#[test]
|
|
|
|
fn foo() {
|
|
|
|
}
|
2018-10-15 21:44:23 +00:00
|
|
|
",
|
|
|
|
);
|
2019-03-07 14:46:17 +00:00
|
|
|
server.wait_until_workspace_is_loaded();
|
2018-09-01 17:21:11 +00:00
|
|
|
server.request::<Runnables>(
|
2019-02-08 11:49:43 +00:00
|
|
|
RunnablesParams { text_document: server.doc_id("lib.rs"), position: None },
|
2018-12-06 20:07:31 +00:00
|
|
|
json!([
|
2018-09-01 17:21:11 +00:00
|
|
|
{
|
|
|
|
"args": [ "test", "--", "foo", "--nocapture" ],
|
|
|
|
"bin": "cargo",
|
|
|
|
"env": { "RUST_BACKTRACE": "short" },
|
2019-04-13 17:45:21 +00:00
|
|
|
"cwd": null,
|
2018-09-01 17:21:11 +00:00
|
|
|
"label": "test foo",
|
|
|
|
"range": {
|
|
|
|
"end": { "character": 1, "line": 2 },
|
|
|
|
"start": { "character": 0, "line": 0 }
|
|
|
|
}
|
2018-10-25 10:42:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"args": [
|
|
|
|
"check",
|
|
|
|
"--all"
|
|
|
|
],
|
|
|
|
"bin": "cargo",
|
|
|
|
"env": {},
|
2019-04-13 17:45:21 +00:00
|
|
|
"cwd": null,
|
2018-10-25 10:42:53 +00:00
|
|
|
"label": "cargo check --all",
|
|
|
|
"range": {
|
|
|
|
"end": {
|
|
|
|
"character": 0,
|
|
|
|
"line": 0
|
|
|
|
},
|
|
|
|
"start": {
|
|
|
|
"character": 0,
|
|
|
|
"line": 0
|
|
|
|
}
|
|
|
|
}
|
2018-09-01 17:21:11 +00:00
|
|
|
}
|
2018-12-06 20:07:31 +00:00
|
|
|
]),
|
2018-09-01 17:21:11 +00:00
|
|
|
);
|
|
|
|
}
|
2018-09-02 11:46:15 +00:00
|
|
|
|
2018-09-02 13:36:03 +00:00
|
|
|
#[test]
|
|
|
|
fn test_runnables_project() {
|
2019-04-13 18:33:49 +00:00
|
|
|
let code = r#"
|
|
|
|
//- foo/Cargo.toml
|
2018-09-02 13:36:03 +00:00
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.0"
|
|
|
|
|
2019-04-13 18:33:49 +00:00
|
|
|
//- foo/src/lib.rs
|
2018-09-02 13:36:03 +00:00
|
|
|
pub fn foo() {}
|
|
|
|
|
2019-04-13 18:33:49 +00:00
|
|
|
//- foo/tests/spam.rs
|
2018-09-02 13:36:03 +00:00
|
|
|
#[test]
|
|
|
|
fn test_eggs() {}
|
2019-04-13 18:33:49 +00:00
|
|
|
|
|
|
|
//- bar/Cargo.toml
|
|
|
|
[package]
|
|
|
|
name = "bar"
|
|
|
|
version = "0.0.0"
|
|
|
|
|
|
|
|
//- bar/src/main.rs
|
|
|
|
fn main() {}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let server = Project::with_fixture(code).root("foo").root("bar").server();
|
|
|
|
|
2019-03-07 14:46:17 +00:00
|
|
|
server.wait_until_workspace_is_loaded();
|
2018-09-02 13:36:03 +00:00
|
|
|
server.request::<Runnables>(
|
|
|
|
RunnablesParams {
|
2019-04-13 18:33:49 +00:00
|
|
|
text_document: server.doc_id("foo/tests/spam.rs"),
|
2018-09-02 13:36:03 +00:00
|
|
|
position: None,
|
|
|
|
},
|
2018-12-06 20:07:31 +00:00
|
|
|
json!([
|
2018-09-02 13:36:03 +00:00
|
|
|
{
|
|
|
|
"args": [ "test", "--package", "foo", "--test", "spam", "--", "test_eggs", "--nocapture" ],
|
|
|
|
"bin": "cargo",
|
|
|
|
"env": { "RUST_BACKTRACE": "short" },
|
|
|
|
"label": "test test_eggs",
|
|
|
|
"range": {
|
|
|
|
"end": { "character": 17, "line": 1 },
|
|
|
|
"start": { "character": 0, "line": 0 }
|
2019-04-13 17:45:21 +00:00
|
|
|
},
|
2019-04-13 18:33:49 +00:00
|
|
|
"cwd": server.path().join("foo")
|
2018-10-25 10:42:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"args": [
|
|
|
|
"check",
|
|
|
|
"--package",
|
|
|
|
"foo",
|
|
|
|
"--test",
|
|
|
|
"spam"
|
|
|
|
],
|
|
|
|
"bin": "cargo",
|
|
|
|
"env": {},
|
2019-04-13 18:33:49 +00:00
|
|
|
"cwd": server.path().join("foo"),
|
2018-10-25 10:42:53 +00:00
|
|
|
"label": "cargo check -p foo",
|
|
|
|
"range": {
|
|
|
|
"end": {
|
|
|
|
"character": 0,
|
|
|
|
"line": 0
|
|
|
|
},
|
|
|
|
"start": {
|
|
|
|
"character": 0,
|
|
|
|
"line": 0
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 13:36:03 +00:00
|
|
|
}
|
2018-12-06 20:07:31 +00:00
|
|
|
])
|
2018-09-02 13:36:03 +00:00
|
|
|
);
|
|
|
|
}
|
2018-12-06 20:26:23 +00:00
|
|
|
|
2018-12-29 19:09:42 +00:00
|
|
|
#[test]
|
|
|
|
fn test_format_document() {
|
|
|
|
let server = project(
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.0"
|
|
|
|
|
|
|
|
//- src/lib.rs
|
|
|
|
mod bar;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|
|
|
|
|
|
|
|
pub use std::collections::HashMap;
|
|
|
|
"#,
|
|
|
|
);
|
2019-03-07 14:46:17 +00:00
|
|
|
server.wait_until_workspace_is_loaded();
|
2018-12-29 19:09:42 +00:00
|
|
|
|
|
|
|
server.request::<Formatting>(
|
|
|
|
DocumentFormattingParams {
|
|
|
|
text_document: server.doc_id("src/lib.rs"),
|
|
|
|
options: FormattingOptions {
|
|
|
|
tab_size: 4,
|
|
|
|
insert_spaces: false,
|
|
|
|
properties: HashMap::new(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
json!([
|
|
|
|
{
|
|
|
|
"newText": r#"mod bar;
|
|
|
|
|
|
|
|
fn main() {}
|
|
|
|
|
|
|
|
pub use std::collections::HashMap;
|
|
|
|
"#,
|
|
|
|
"range": {
|
|
|
|
"end": {
|
|
|
|
"character": 0,
|
|
|
|
"line": 6
|
|
|
|
},
|
|
|
|
"start": {
|
|
|
|
"character": 0,
|
|
|
|
"line": 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-06 20:26:23 +00:00
|
|
|
#[test]
|
|
|
|
fn test_missing_module_code_action() {
|
|
|
|
let server = project(
|
|
|
|
r#"
|
|
|
|
//- Cargo.toml
|
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.0"
|
|
|
|
|
|
|
|
//- src/lib.rs
|
|
|
|
mod bar;
|
|
|
|
|
|
|
|
fn main() {}
|
|
|
|
"#,
|
|
|
|
);
|
2019-03-07 14:46:17 +00:00
|
|
|
server.wait_until_workspace_is_loaded();
|
2019-02-08 11:49:43 +00:00
|
|
|
let empty_context = || CodeActionContext { diagnostics: Vec::new(), only: None };
|
2019-03-05 21:29:23 +00:00
|
|
|
server.request::<CodeActionRequest>(
|
|
|
|
CodeActionParams {
|
|
|
|
text_document: server.doc_id("src/lib.rs"),
|
|
|
|
range: Range::new(Position::new(0, 4), Position::new(0, 7)),
|
|
|
|
context: empty_context(),
|
|
|
|
},
|
|
|
|
json!([
|
|
|
|
{
|
|
|
|
"command": {
|
|
|
|
"arguments": [
|
|
|
|
{
|
|
|
|
"cursorPosition": null,
|
|
|
|
"label": "create module",
|
|
|
|
"workspaceEdit": {
|
|
|
|
"documentChanges": [
|
|
|
|
{
|
|
|
|
"kind": "create",
|
|
|
|
"uri": "file:///[..]/src/bar.rs"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"command": "rust-analyzer.applySourceChange",
|
|
|
|
"title": "create module"
|
|
|
|
},
|
|
|
|
"title": "create module"
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
|
|
|
server.request::<CodeActionRequest>(
|
|
|
|
CodeActionParams {
|
|
|
|
text_document: server.doc_id("src/lib.rs"),
|
|
|
|
range: Range::new(Position::new(2, 4), Position::new(2, 7)),
|
|
|
|
context: empty_context(),
|
|
|
|
},
|
|
|
|
json!([]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_missing_module_code_action_in_json_project() {
|
|
|
|
let tmp_dir = TempDir::new().unwrap();
|
2019-03-07 16:25:35 +00:00
|
|
|
|
|
|
|
let path = tmp_dir.path();
|
|
|
|
|
|
|
|
let project = json!({
|
|
|
|
"roots": [path],
|
|
|
|
"crates": [ { "root_module": path.join("src/lib.rs"), "deps": [], "edition": "2015" } ]
|
|
|
|
});
|
|
|
|
|
2019-03-05 21:29:23 +00:00
|
|
|
let code = format!(
|
|
|
|
r#"
|
|
|
|
//- rust-project.json
|
2019-03-07 16:25:35 +00:00
|
|
|
{PROJECT}
|
2019-03-05 21:29:23 +00:00
|
|
|
|
|
|
|
//- src/lib.rs
|
|
|
|
mod bar;
|
|
|
|
|
2019-03-07 16:25:35 +00:00
|
|
|
fn main() {{}}
|
2019-03-05 21:29:23 +00:00
|
|
|
"#,
|
2019-03-07 16:25:35 +00:00
|
|
|
PROJECT = project.to_string(),
|
2019-03-05 21:29:23 +00:00
|
|
|
);
|
2019-04-13 18:33:49 +00:00
|
|
|
|
|
|
|
let server = Project::with_fixture(&code).tmp_dir(tmp_dir).server();
|
|
|
|
|
2019-03-07 14:46:17 +00:00
|
|
|
server.wait_until_workspace_is_loaded();
|
2019-03-05 21:29:23 +00:00
|
|
|
let empty_context = || CodeActionContext { diagnostics: Vec::new(), only: None };
|
2018-12-06 20:26:23 +00:00
|
|
|
server.request::<CodeActionRequest>(
|
|
|
|
CodeActionParams {
|
|
|
|
text_document: server.doc_id("src/lib.rs"),
|
2018-12-27 09:54:59 +00:00
|
|
|
range: Range::new(Position::new(0, 4), Position::new(0, 7)),
|
2018-12-06 20:26:23 +00:00
|
|
|
context: empty_context(),
|
|
|
|
},
|
|
|
|
json!([
|
2019-02-24 10:53:35 +00:00
|
|
|
{
|
|
|
|
"command": {
|
2018-12-06 20:26:23 +00:00
|
|
|
"arguments": [
|
|
|
|
{
|
|
|
|
"cursorPosition": null,
|
2019-02-24 10:53:35 +00:00
|
|
|
"label": "create module",
|
2019-01-03 13:43:47 +00:00
|
|
|
"workspaceEdit": {
|
|
|
|
"documentChanges": [
|
|
|
|
{
|
|
|
|
"kind": "create",
|
2018-12-06 20:26:23 +00:00
|
|
|
"uri": "file:///[..]/src/bar.rs"
|
2019-01-03 13:43:47 +00:00
|
|
|
}
|
|
|
|
]
|
2019-02-24 10:53:35 +00:00
|
|
|
}
|
2018-12-06 20:26:23 +00:00
|
|
|
}
|
|
|
|
],
|
2019-01-28 11:43:07 +00:00
|
|
|
"command": "rust-analyzer.applySourceChange",
|
2018-12-06 20:26:23 +00:00
|
|
|
"title": "create module"
|
2019-02-24 10:53:35 +00:00
|
|
|
},
|
|
|
|
"title": "create module"
|
|
|
|
}
|
2018-12-06 20:26:23 +00:00
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
|
|
|
server.request::<CodeActionRequest>(
|
|
|
|
CodeActionParams {
|
|
|
|
text_document: server.doc_id("src/lib.rs"),
|
2018-12-27 09:54:59 +00:00
|
|
|
range: Range::new(Position::new(2, 4), Position::new(2, 7)),
|
2018-12-06 20:26:23 +00:00
|
|
|
context: empty_context(),
|
|
|
|
},
|
|
|
|
json!([]),
|
|
|
|
);
|
|
|
|
}
|