aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-03-07 11:36:04 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-03-07 11:36:04 +0000
commit5232099977d07492c1b6d5e6163c70d4f6eb07df (patch)
tree0660079034d3c2f30147e946f2c473d2f651eef2 /crates/ra_lsp_server
parentb94e1eee8341cb2a16b89711d65b382549fd2bde (diff)
parent4cd757c1e32f0cf1f281b82cd55615d0e47edb24 (diff)
Merge #939
939: Initial implementation of project-lock.json. r=davidtwco a=davidtwco Fixes #792. This PR adds a initial implementation of project-lock.json, a build system agnostic method of specifying the crate graph and roots. Co-authored-by: David Wood <[email protected]>
Diffstat (limited to 'crates/ra_lsp_server')
-rw-r--r--crates/ra_lsp_server/src/cargo_target_spec.rs20
-rw-r--r--crates/ra_lsp_server/src/server_world.rs9
-rw-r--r--crates/ra_lsp_server/tests/heavy_tests/main.rs65
-rw-r--r--crates/ra_lsp_server/tests/heavy_tests/support.rs6
4 files changed, 82 insertions, 18 deletions
diff --git a/crates/ra_lsp_server/src/cargo_target_spec.rs b/crates/ra_lsp_server/src/cargo_target_spec.rs
index e011eab7c..cdf2ec10b 100644
--- a/crates/ra_lsp_server/src/cargo_target_spec.rs
+++ b/crates/ra_lsp_server/src/cargo_target_spec.rs
@@ -1,5 +1,5 @@
1use crate::{ 1use crate::{
2 project_model::TargetKind, 2 project_model::{self, TargetKind},
3 server_world::ServerWorld, 3 server_world::ServerWorld,
4 Result 4 Result
5}; 5};
@@ -65,14 +65,16 @@ impl CargoTargetSpec {
65 }; 65 };
66 let file_id = world.analysis().crate_root(crate_id)?; 66 let file_id = world.analysis().crate_root(crate_id)?;
67 let path = world.vfs.read().file2path(ra_vfs::VfsFile(file_id.0.into())); 67 let path = world.vfs.read().file2path(ra_vfs::VfsFile(file_id.0.into()));
68 let res = world.workspaces.iter().find_map(|ws| { 68 let res = world.workspaces.iter().find_map(|ws| match ws {
69 let tgt = ws.cargo.target_by_root(&path)?; 69 project_model::ProjectWorkspace::Cargo { cargo, .. } => {
70 let res = CargoTargetSpec { 70 let tgt = cargo.target_by_root(&path)?;
71 package: tgt.package(&ws.cargo).name(&ws.cargo).to_string(), 71 Some(CargoTargetSpec {
72 target: tgt.name(&ws.cargo).to_string(), 72 package: tgt.package(&cargo).name(&cargo).to_string(),
73 target_kind: tgt.kind(&ws.cargo), 73 target: tgt.name(&cargo).to_string(),
74 }; 74 target_kind: tgt.kind(&cargo),
75 Some(res) 75 })
76 }
77 project_model::ProjectWorkspace::Json { .. } => None,
76 }); 78 });
77 Ok(res) 79 Ok(res)
78 } 80 }
diff --git a/crates/ra_lsp_server/src/server_world.rs b/crates/ra_lsp_server/src/server_world.rs
index 4a68c019f..7163568b9 100644
--- a/crates/ra_lsp_server/src/server_world.rs
+++ b/crates/ra_lsp_server/src/server_world.rs
@@ -40,12 +40,7 @@ impl ServerWorldState {
40 let mut roots = Vec::new(); 40 let mut roots = Vec::new();
41 roots.push(root.clone()); 41 roots.push(root.clone());
42 for ws in workspaces.iter() { 42 for ws in workspaces.iter() {
43 for pkg in ws.cargo.packages() { 43 roots.extend(ws.to_roots());
44 roots.push(pkg.root(&ws.cargo).to_path_buf());
45 }
46 for krate in ws.sysroot.crates() {
47 roots.push(krate.root_dir(&ws.sysroot).to_path_buf())
48 }
49 } 44 }
50 let (mut vfs, roots) = Vfs::new(roots); 45 let (mut vfs, roots) = Vfs::new(roots);
51 let roots_to_scan = roots.len(); 46 let roots_to_scan = roots.len();
@@ -185,7 +180,7 @@ impl ServerWorld {
185 } else { 180 } else {
186 res.push_str("workspaces:\n"); 181 res.push_str("workspaces:\n");
187 for w in self.workspaces.iter() { 182 for w in self.workspaces.iter() {
188 res += &format!("{} packages loaded\n", w.cargo.packages().count()); 183 res += &format!("{} packages loaded\n", w.count());
189 } 184 }
190 } 185 }
191 res.push_str("\nanalysis:\n"); 186 res.push_str("\nanalysis:\n");
diff --git a/crates/ra_lsp_server/tests/heavy_tests/main.rs b/crates/ra_lsp_server/tests/heavy_tests/main.rs
index 1c099a78f..41c240139 100644
--- a/crates/ra_lsp_server/tests/heavy_tests/main.rs
+++ b/crates/ra_lsp_server/tests/heavy_tests/main.rs
@@ -12,8 +12,9 @@ use ra_lsp_server::req::{
12 CodeActionParams, CodeActionRequest, Formatting, Runnables, RunnablesParams, CompletionParams, Completion, 12 CodeActionParams, CodeActionRequest, Formatting, Runnables, RunnablesParams, CompletionParams, Completion,
13}; 13};
14use serde_json::json; 14use serde_json::json;
15use tempfile::TempDir;
15 16
16use crate::support::project; 17use crate::support::{project, project_with_tmpdir};
17 18
18const LOG: &'static str = ""; 19const LOG: &'static str = "";
19 20
@@ -258,3 +259,65 @@ fn main() {}
258 json!([]), 259 json!([]),
259 ); 260 );
260} 261}
262
263#[test]
264fn test_missing_module_code_action_in_json_project() {
265 let tmp_dir = TempDir::new().unwrap();
266 let code = format!(
267 r#"
268//- rust-project.json
269{{
270 "roots": [ "{PATH}" ],
271 "crates": [ {{ "root_module": "{PATH}/src/lib.rs", "deps": [], "edition": "2015" }} ]
272}}
273
274//- src/lib.rs
275mod bar;
276
277fn main() {}
278"#,
279 PATH = tmp_dir.path().display()
280 );
281 let server = project_with_tmpdir(tmp_dir, &code);
282 server.wait_for_message("workspace loaded");
283 let empty_context = || CodeActionContext { diagnostics: Vec::new(), only: None };
284 server.request::<CodeActionRequest>(
285 CodeActionParams {
286 text_document: server.doc_id("src/lib.rs"),
287 range: Range::new(Position::new(0, 4), Position::new(0, 7)),
288 context: empty_context(),
289 },
290 json!([
291 {
292 "command": {
293 "arguments": [
294 {
295 "cursorPosition": null,
296 "label": "create module",
297 "workspaceEdit": {
298 "documentChanges": [
299 {
300 "kind": "create",
301 "uri": "file:///[..]/src/bar.rs"
302 }
303 ]
304 }
305 }
306 ],
307 "command": "rust-analyzer.applySourceChange",
308 "title": "create module"
309 },
310 "title": "create module"
311 }
312 ]),
313 );
314
315 server.request::<CodeActionRequest>(
316 CodeActionParams {
317 text_document: server.doc_id("src/lib.rs"),
318 range: Range::new(Position::new(2, 4), Position::new(2, 7)),
319 context: empty_context(),
320 },
321 json!([]),
322 );
323}
diff --git a/crates/ra_lsp_server/tests/heavy_tests/support.rs b/crates/ra_lsp_server/tests/heavy_tests/support.rs
index 8bfc8d622..e02d7858e 100644
--- a/crates/ra_lsp_server/tests/heavy_tests/support.rs
+++ b/crates/ra_lsp_server/tests/heavy_tests/support.rs
@@ -27,12 +27,16 @@ use ra_lsp_server::{
27}; 27};
28 28
29pub fn project(fixture: &str) -> Server { 29pub fn project(fixture: &str) -> Server {
30 let tmp_dir = TempDir::new().unwrap();
31 project_with_tmpdir(tmp_dir, fixture)
32}
33
34pub fn project_with_tmpdir(tmp_dir: TempDir, fixture: &str) -> Server {
30 static INIT: Once = Once::new(); 35 static INIT: Once = Once::new();
31 INIT.call_once(|| { 36 INIT.call_once(|| {
32 let _ = Logger::with_env_or_str(crate::LOG).start().unwrap(); 37 let _ = Logger::with_env_or_str(crate::LOG).start().unwrap();
33 }); 38 });
34 39
35 let tmp_dir = TempDir::new().unwrap();
36 let mut paths = vec![]; 40 let mut paths = vec![];
37 41
38 for entry in parse_fixture(fixture) { 42 for entry in parse_fixture(fixture) {