|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 |
| -use serde_json::{self, json}; |
| 11 | +use serde_json::{self, json, Value as JsonValue}; |
12 | 12 |
|
13 | 13 | use std::io::Write;
|
14 | 14 | use std::time::Duration;
|
@@ -648,7 +648,7 @@ fn cmd_test_complete_self_crate_name() {
|
648 | 648 | std::thread::sleep(Duration::from_millis(50));
|
649 | 649 | continue;
|
650 | 650 | }
|
651 |
| - }; |
| 651 | + } |
652 | 652 | assert_eq!(json["result"][0]["detail"], "pub fn function() -> usize");
|
653 | 653 |
|
654 | 654 | rls.shutdown(rls_timeout());
|
@@ -1364,3 +1364,148 @@ fn cmd_lens_run() {
|
1364 | 1364 |
|
1365 | 1365 | rls.shutdown(rls_timeout());
|
1366 | 1366 | }
|
| 1367 | + |
| 1368 | +#[test] |
| 1369 | +fn test_find_definitions() { |
| 1370 | + const SRC: &str = r#" |
| 1371 | + struct Foo { |
| 1372 | + } |
| 1373 | +
|
| 1374 | + impl Foo { |
| 1375 | + fn new() { |
| 1376 | + } |
| 1377 | + } |
| 1378 | +
|
| 1379 | + fn main() { |
| 1380 | + Foo::new(); |
| 1381 | + } |
| 1382 | + "#; |
| 1383 | + |
| 1384 | + let p = project("simple_workspace") |
| 1385 | + .file("Cargo.toml", &basic_bin_manifest("bar")) |
| 1386 | + .file("src/main.rs", SRC) |
| 1387 | + .build(); |
| 1388 | + |
| 1389 | + let root_path = p.root(); |
| 1390 | + let mut rls = p.spawn_rls(); |
| 1391 | + |
| 1392 | + rls.request( |
| 1393 | + 0, |
| 1394 | + "initialize", |
| 1395 | + Some(json!({ |
| 1396 | + "rootPath": root_path, |
| 1397 | + "capabilities": {}, |
| 1398 | + "initializationOptions": { |
| 1399 | + "settings": { |
| 1400 | + "rust": { |
| 1401 | + "racer_completion": false |
| 1402 | + } |
| 1403 | + } |
| 1404 | + } |
| 1405 | + })), |
| 1406 | + ) |
| 1407 | + .unwrap(); |
| 1408 | + |
| 1409 | + let _ = rls.wait_until_json_id(0, rls_timeout()); |
| 1410 | + |
| 1411 | + let uri = format!("file://{}/src/main.rs", root_path.display()); |
| 1412 | + |
| 1413 | + let mut results = vec![]; |
| 1414 | + let mut request_id = 1; |
| 1415 | + for (line_index, line) in SRC.lines().enumerate() { |
| 1416 | + for i in 0..line.len() { |
| 1417 | + rls.request( |
| 1418 | + request_id, |
| 1419 | + "textDocument/definition", |
| 1420 | + Some(json!({ |
| 1421 | + "position": { |
| 1422 | + "character": i, |
| 1423 | + "line": line_index |
| 1424 | + }, |
| 1425 | + "textDocument": { |
| 1426 | + "uri": uri, |
| 1427 | + "version": 1 |
| 1428 | + } |
| 1429 | + })), |
| 1430 | + ) |
| 1431 | + .unwrap(); |
| 1432 | + |
| 1433 | + let json = rls.wait_until_json_id(request_id, rls_timeout()); |
| 1434 | + let result = json["result"].as_array().unwrap(); |
| 1435 | + |
| 1436 | + request_id += 1; |
| 1437 | + |
| 1438 | + if result.is_empty() { |
| 1439 | + continue; |
| 1440 | + } |
| 1441 | + |
| 1442 | + results.push((line_index, i, result.clone())); |
| 1443 | + } |
| 1444 | + } |
| 1445 | + |
| 1446 | + rls.shutdown(rls_timeout()); |
| 1447 | + |
| 1448 | + // Foo |
| 1449 | + let foo_definition: JsonValue = json!({ |
| 1450 | + "uri": uri, |
| 1451 | + "range": { |
| 1452 | + "start": { |
| 1453 | + "line": 1, |
| 1454 | + "character": 15, |
| 1455 | + }, |
| 1456 | + "end": { |
| 1457 | + "line": 1, |
| 1458 | + "character": 18, |
| 1459 | + } |
| 1460 | + } |
| 1461 | + }); |
| 1462 | + |
| 1463 | + // Foo::new |
| 1464 | + let foo_new_definition: JsonValue = json!({ |
| 1465 | + "uri": uri, |
| 1466 | + "range": { |
| 1467 | + "start": { |
| 1468 | + "line": 5, |
| 1469 | + "character": 15, |
| 1470 | + }, |
| 1471 | + "end": { |
| 1472 | + "line": 5, |
| 1473 | + "character": 18, |
| 1474 | + } |
| 1475 | + } |
| 1476 | + }); |
| 1477 | + |
| 1478 | + |
| 1479 | + // main |
| 1480 | + let main_definition: JsonValue = json!({ |
| 1481 | + "uri": uri, |
| 1482 | + "range": { |
| 1483 | + "start": { |
| 1484 | + "line": 9, |
| 1485 | + "character": 11, |
| 1486 | + }, |
| 1487 | + "end": { |
| 1488 | + "line": 9, |
| 1489 | + "character": 15, |
| 1490 | + } |
| 1491 | + } |
| 1492 | + }); |
| 1493 | + |
| 1494 | + let expected = [ |
| 1495 | + (9, 11, vec![main_definition.clone()]), |
| 1496 | + (9, 12, vec![main_definition.clone()]), |
| 1497 | + (9, 13, vec![main_definition.clone()]), |
| 1498 | + (9, 14, vec![main_definition.clone()]), |
| 1499 | + (9, 15, vec![main_definition.clone()]), |
| 1500 | + (10, 12, vec![foo_definition.clone()]), |
| 1501 | + (10, 13, vec![foo_definition.clone()]), |
| 1502 | + (10, 14, vec![foo_definition.clone()]), |
| 1503 | + (10, 15, vec![foo_definition.clone()]), |
| 1504 | + (10, 17, vec![foo_new_definition.clone()]), |
| 1505 | + (10, 18, vec![foo_new_definition.clone()]), |
| 1506 | + (10, 19, vec![foo_new_definition.clone()]), |
| 1507 | + (10, 20, vec![foo_new_definition.clone()]), |
| 1508 | + ]; |
| 1509 | + |
| 1510 | + assert_eq!(results, expected); |
| 1511 | +} |
0 commit comments