From d6788eddf13ab76809c299d1afa80652e37dac41 Mon Sep 17 00:00:00 2001 From: Jakko Sikkar Date: Mon, 11 Jan 2016 16:17:41 +0200 Subject: [PATCH 1/6] package reading --- src/racer/cargo.rs | 111 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 89 insertions(+), 22 deletions(-) diff --git a/src/racer/cargo.rs b/src/racer/cargo.rs index 1e6b9858..ba63584f 100644 --- a/src/racer/cargo.rs +++ b/src/racer/cargo.rs @@ -2,10 +2,18 @@ use std::fs::File; use std::io::Read; use std::env; use std::path::{Path,PathBuf}; +use std::collections::BTreeMap; use util::{path_exists, is_dir}; use std::fs::{read_dir}; use toml; +#[derive(Debug)] +struct PackageInfo { + name: String, + version: String, + source: Option +} + // otry is 'option try' macro_rules! otry { ($e:expr) => (match $e { Some(e) => e, None => return None }) @@ -61,45 +69,104 @@ fn empty_if_no_branch() { } fn find_src_via_lockfile(kratename: &str, cargofile: &Path) -> Option { - let mut file = otry2!(File::open(cargofile)); + if let Some(packages) = get_cargo_packages(cargofile) { + for package in packages { + if let Some(package_source) = package.source.clone() { + if let Some(tomlfile) = find_cargo_tomlfile(package_source.as_path()) { + let package_name = get_package_name(tomlfile.as_path()); + + debug!("find_src_via_lockfile package_name: {}", package_name); + + if package_name == kratename { + return package.source; + } + } + } + } + } + + None +} + +fn parse_toml_file(toml_file: &Path) -> Option> { + let mut file = otry2!(File::open(toml_file)); let mut string = String::new(); otry2!(file.read_to_string(&mut string)); let mut parser = toml::Parser::new(&string); - let lock_table = parser.parse().unwrap(); - debug!("find_src_via_lockfile found lock table {:?}", lock_table); + parser.parse() +} + +fn get_cargo_packages(cargofile: &Path) -> Option> { + let lock_table = parse_toml_file(cargofile).unwrap(); - let t = match lock_table.get("package") { + debug!("get_cargo_packages found lock_table {:?}", lock_table); + + let packages_array = match lock_table.get("package") { Some(&toml::Value::Array(ref t1)) => t1, _ => return None }; - for item in t { - if let &toml::Value::Table(ref t) = item { - if let Some(&toml::Value::String(ref name)) = t.get("name") { - if name.replace("-", "_") == kratename { - debug!("found matching crate {:?}", t); - let version = otry!(getstr(t, "version")); - let source = otry!(getstr(t, "source")); - - if Some("registry") == source.split("+").nth(0) { - return get_versioned_cratefile(name, &version, cargofile); - } else if Some("git") == source.split("+").nth(0) { - let sha1 = otry!(source.split("#").last()); + let mut result = Vec::new(); + + for package_element in packages_array { + if let &toml::Value::Table(ref package_table) = package_element { + if let Some(&toml::Value::String(ref package_name)) = package_table.get("name") { + let package_version = otry!(getstr(package_table, "version")); + let package_source = otry!(getstr(package_table, "source")); + + let package_source = match package_source.split("+").nth(0) { + Some("registry") => { + get_versioned_cratefile(package_name, &package_version, cargofile) + }, + Some("git") => { + let sha1 = otry!(package_source.split("#").last()); let mut d = otry!(get_cargo_rootdir(cargofile)); - let branch = get_branch_from_source(&source); + let branch = get_branch_from_source(&package_source); d.push("git"); d.push("checkouts"); - d = otry!(find_git_src_dir(d, name, &sha1, branch)); + d = otry!(find_git_src_dir(d, package_name, &sha1, branch)); d.push("src"); d.push("lib.rs"); - return Some(d); - } - } + + Some(d) + }, + _ => return None + }; + + result.push(PackageInfo{ + name: package_name.to_owned(), + version: package_version, + source: package_source + }); } } } - None + + Some(result) +} + +fn get_package_name(cargofile: &Path) -> String { + let lock_table = parse_toml_file(cargofile).unwrap(); + + debug!("get_package_name found lock_table {:?}", lock_table); + + let mut name = String::new(); + + if let Some(&toml::Value::Table(ref lib_table)) = lock_table.get("lib") { + if let Some(&toml::Value::String(ref package_name)) = lib_table.get("name") { + name.push_str(package_name); + return name; + } + } + + if let Some(&toml::Value::Table(ref package_table)) = lock_table.get("package") { + if let Some(&toml::Value::String(ref package_name)) = package_table.get("name") { + name.push_str(package_name); + } + } + + name } fn get_cargo_rootdir(cargofile: &Path) -> Option { From 985e9dca595be7796ecc3cd8d8c5bfb9e79223a2 Mon Sep 17 00:00:00 2001 From: Jakko Sikkar Date: Tue, 12 Jan 2016 01:59:02 +0200 Subject: [PATCH 2/6] package reading for local files --- src/racer/cargo.rs | 92 ++++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/src/racer/cargo.rs b/src/racer/cargo.rs index ba63584f..a84e3798 100644 --- a/src/racer/cargo.rs +++ b/src/racer/cargo.rs @@ -10,7 +10,7 @@ use toml; #[derive(Debug)] struct PackageInfo { name: String, - version: String, + version: Option, source: Option } @@ -84,7 +84,6 @@ fn find_src_via_lockfile(kratename: &str, cargofile: &Path) -> Option { } } } - None } @@ -136,13 +135,12 @@ fn get_cargo_packages(cargofile: &Path) -> Option> { result.push(PackageInfo{ name: package_name.to_owned(), - version: package_version, + version: Some(package_version), source: package_source }); } } } - Some(result) } @@ -298,13 +296,7 @@ fn get_versioned_cratefile(kratename: &str, version: &str, cargofile: &Path) -> fn find_src_via_tomlfile(kratename: &str, cargofile: &Path) -> Option { // only look for 'path' references here. // We find the git and crates.io stuff via the lockfile - - let mut file = otry2!(File::open(cargofile)); - let mut string = String::new(); - otry2!(file.read_to_string(&mut string)); - let mut parser = toml::Parser::new(&string); - let table = otry!(parser.parse()); - + let table = parse_toml_file(cargofile).unwrap(); // is it this lib? (e.g. you're searching from tests to find the main library crate) if let Some(&toml::Value::Table(ref t)) = table.get("lib") { @@ -321,36 +313,66 @@ fn find_src_via_tomlfile(kratename: &str, cargofile: &Path) -> Option { } // otherwise search the dependencies - let t = match table.get("dependencies") { + let local_packages = get_local_packages(&table, cargofile, "dependencies").unwrap(); + let local_packages_dev = get_local_packages(&table, cargofile, "dev-dependencies").unwrap(); + + debug!("find_src_via_tomlfile found local packages: {:?}", local_packages); + debug!("find_src_via_tomlfile found local packages dev: {:?}", local_packages_dev); + + for package in local_packages.iter().chain(local_packages_dev.iter()) { + if let Some(package_source) = package.source.clone() { + if let Some(tomlfile) = find_cargo_tomlfile(package_source.as_path()) { + let package_name = get_package_name(tomlfile.as_path()); + + debug!("find_src_via_tomlfile package_name: {}", package_name); + + if package_name == kratename { + return Some(package_source); + } + } + } + } + + None +} + +fn get_local_packages(table: &BTreeMap, cargofile: &Path, section_name: &str) -> Option> { + debug!("get_local_packages found table {:?}", table); + + let t = match table.get(section_name) { Some(&toml::Value::Table(ref t)) => t, _ => return None }; - let mut name = kratename; - let value = if kratename.contains('_') { - t.iter().find(|&(k, _)| k.replace("-", "_") == name).map(|(k,v)| { - name = k; - v - }) - } else { - t.get(kratename) - }; + let mut result = Vec::new(); - match value { - Some(&toml::Value::Table(ref t)) => { - // local directory - let relative_path = otry!(getstr(t, "path")); - Some(otry!(cargofile.parent()) - .join(relative_path) - .join("src") - .join("lib.rs")) - }, - Some(&toml::Value::String(ref version)) => { - // versioned crate - get_versioned_cratefile(name, version, cargofile) - } - _ => None + for (package_name, value) in t.iter() { + let mut package_version = None; + + let package_source = match *value { + toml::Value::Table(ref t) => { + // local directory + let relative_path = otry!(getstr(t, "path")); + Some(otry!(cargofile.parent()) + .join(relative_path) + .join("src") + .join("lib.rs")) + }, + toml::Value::String(ref version) => { + // versioned crate + package_version = Some(version.to_owned()); + get_versioned_cratefile(package_name, version, cargofile) + } + _ => continue + }; + + result.push(PackageInfo { + name: package_name.to_owned(), + version: package_version, + source: package_source + }); } + Some(result) } fn find_cratesio_src_dirs(d: PathBuf) -> Vec { From a5042aa458ab3ba56d513f09c635986ea5915f05 Mon Sep 17 00:00:00 2001 From: Jakko Sikkar Date: Wed, 13 Jan 2016 23:02:30 +0200 Subject: [PATCH 3/6] add few tests --- Cargo.lock | 5 +++++ Cargo.toml | 3 +++ src/test_fixtures/Cargo.toml | 8 ++++++++ src/test_fixtures/src/foo.rs | 4 ++++ src/test_fixtures/src/lib.rs | 2 ++ tests/system.rs | 40 +++++++++++++++++++++++++++++++++++- tests/test.rs | 2 +- 7 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/test_fixtures/Cargo.toml create mode 100644 src/test_fixtures/src/foo.rs create mode 100644 src/test_fixtures/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 25991eb4..c88d9e2a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,7 @@ dependencies = [ "env_logger 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.24.0 (registry+https://github.com/rust-lang/crates.io-index)", + "test_fixtures 0.1.0", "toml 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "typed-arena 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -125,6 +126,10 @@ dependencies = [ "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "test_fixtures" +version = "0.1.0" + [[package]] name = "toml" version = "0.1.25" diff --git a/Cargo.toml b/Cargo.toml index 85f548d8..34bc10d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,5 +25,8 @@ env_logger = "~0.3.2" typed-arena = "~1.0.1" clap = "~1.5.3" +[dev-dependencies] +test_fixtures = { path = "src/test_fixtures" } + [features] nightly = [] diff --git a/src/test_fixtures/Cargo.toml b/src/test_fixtures/Cargo.toml new file mode 100644 index 00000000..033ac94d --- /dev/null +++ b/src/test_fixtures/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "test_fixtures" +version = "0.1.0" +authors = ["jaxx"] + +[lib] +name = "fixtures" +path = "src/lib.rs" \ No newline at end of file diff --git a/src/test_fixtures/src/foo.rs b/src/test_fixtures/src/foo.rs new file mode 100644 index 00000000..ef03aa01 --- /dev/null +++ b/src/test_fixtures/src/foo.rs @@ -0,0 +1,4 @@ + +pub fn test() { + println!("Hello from test function"); +} \ No newline at end of file diff --git a/src/test_fixtures/src/lib.rs b/src/test_fixtures/src/lib.rs new file mode 100644 index 00000000..5bc09d8c --- /dev/null +++ b/src/test_fixtures/src/lib.rs @@ -0,0 +1,2 @@ + +pub mod foo; \ No newline at end of file diff --git a/tests/system.rs b/tests/system.rs index c3882d9e..199b7eba 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -1,11 +1,11 @@ extern crate racer; + use racer::core::complete_from_file; use racer::core::find_definition; use racer::core; use racer::scopes; use racer::util; - use std::env; use std::io::Write; use std::fs::{self, File}; @@ -103,6 +103,27 @@ fn completes_pub_fn_locally_precached() { assert_eq!("apple".to_string(), got.matchstr.to_string()); } +#[test] +fn completes_pub_fn_from_local_package() { + let src=" + extern crate fixtures; + + use fixtures::foo; + + fn main() { + let x = foo:: + } + "; + + let path = tmpname(); + write_file(&path, src); + let pos = scopes::coords_to_point(src, 7, 21); + let cache = core::FileCache::new(); + let got = complete_from_file(src, &path, pos, &core::Session::from_path(&cache, &path, &path)).nth(0); + fs::remove_file(&path).unwrap(); + assert_eq!(got.unwrap().matchstr, "test".to_owned()); +} + #[test] fn overwriting_cached_files() { let src1 = "src1"; @@ -293,6 +314,23 @@ fn follows_use_glob() { assert_eq!(got.matchstr, "myfn".to_string()); } +#[test] +fn follows_use_local_package() { + let src=" + extern crate fixtures; + + use fixtures:: + "; + + let path = tmpname(); + write_file(&path, src); + let pos = scopes::coords_to_point(src, 4, 18); + let cache = core::FileCache::new(); + let got = complete_from_file(src, &path, pos, &core::Session::from_path(&cache, &path, &path)).nth(0); + fs::remove_file(&path).unwrap(); + assert_eq!(got.unwrap().matchstr, "foo".to_owned()); +} + #[test] fn completes_struct_field_via_assignment() { let src=" diff --git a/tests/test.rs b/tests/test.rs index bed8b730..a9077de3 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,4 +1,4 @@ extern crate racer; + #[cfg(test)] pub mod system; #[cfg(test)] pub mod bench; - From 4a4b327709d8ccd237a452e52d2ddb8637d503a1 Mon Sep 17 00:00:00 2001 From: Jakko Sikkar Date: Mon, 11 Jan 2016 16:17:41 +0200 Subject: [PATCH 4/6] package reading --- src/racer/cargo.rs | 111 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 89 insertions(+), 22 deletions(-) diff --git a/src/racer/cargo.rs b/src/racer/cargo.rs index 6af73c99..52b30d8a 100644 --- a/src/racer/cargo.rs +++ b/src/racer/cargo.rs @@ -2,10 +2,18 @@ use std::fs::File; use std::io::Read; use std::env; use std::path::{Path,PathBuf}; +use std::collections::BTreeMap; use util::{path_exists, is_dir}; use std::fs::{read_dir}; use toml; +#[derive(Debug)] +struct PackageInfo { + name: String, + version: String, + source: Option +} + // otry is 'option try' macro_rules! otry { ($e:expr) => (match $e { Some(e) => e, None => return None }) @@ -61,45 +69,104 @@ fn empty_if_no_branch() { } fn find_src_via_lockfile(kratename: &str, cargofile: &Path) -> Option { - let mut file = otry2!(File::open(cargofile)); + if let Some(packages) = get_cargo_packages(cargofile) { + for package in packages { + if let Some(package_source) = package.source.clone() { + if let Some(tomlfile) = find_cargo_tomlfile(package_source.as_path()) { + let package_name = get_package_name(tomlfile.as_path()); + + debug!("find_src_via_lockfile package_name: {}", package_name); + + if package_name == kratename { + return package.source; + } + } + } + } + } + + None +} + +fn parse_toml_file(toml_file: &Path) -> Option> { + let mut file = otry2!(File::open(toml_file)); let mut string = String::new(); otry2!(file.read_to_string(&mut string)); let mut parser = toml::Parser::new(&string); - let lock_table = parser.parse().unwrap(); - debug!("find_src_via_lockfile found lock table {:?}", lock_table); + parser.parse() +} - let t = match lock_table.get("package") { +fn get_cargo_packages(cargofile: &Path) -> Option> { + let lock_table = parse_toml_file(cargofile).unwrap(); + + debug!("get_cargo_packages found lock_table {:?}", lock_table); + + let packages_array = match lock_table.get("package") { Some(&toml::Value::Array(ref t1)) => t1, _ => return None }; - for item in t { - if let toml::Value::Table(ref t) = *item { - if let Some(&toml::Value::String(ref name)) = t.get("name") { - if name.replace("-", "_") == kratename { - debug!("found matching crate {:?}", t); - let version = otry!(getstr(t, "version")); - let source = otry!(getstr(t, "source")); - - if Some("registry") == source.split("+").nth(0) { - return get_versioned_cratefile(name, &version, cargofile); - } else if Some("git") == source.split("+").nth(0) { - let sha1 = otry!(source.split("#").last()); + let mut result = Vec::new(); + + for package_element in packages_array { + if let &toml::Value::Table(ref package_table) = package_element { + if let Some(&toml::Value::String(ref package_name)) = package_table.get("name") { + let package_version = otry!(getstr(package_table, "version")); + let package_source = otry!(getstr(package_table, "source")); + + let package_source = match package_source.split("+").nth(0) { + Some("registry") => { + get_versioned_cratefile(package_name, &package_version, cargofile) + }, + Some("git") => { + let sha1 = otry!(package_source.split("#").last()); let mut d = otry!(get_cargo_rootdir(cargofile)); - let branch = get_branch_from_source(&source); + let branch = get_branch_from_source(&package_source); d.push("git"); d.push("checkouts"); - d = otry!(find_git_src_dir(d, name, &sha1, branch)); + d = otry!(find_git_src_dir(d, package_name, &sha1, branch)); d.push("src"); d.push("lib.rs"); - return Some(d); - } - } + + Some(d) + }, + _ => return None + }; + + result.push(PackageInfo{ + name: package_name.to_owned(), + version: package_version, + source: package_source + }); } } } - None + + Some(result) +} + +fn get_package_name(cargofile: &Path) -> String { + let lock_table = parse_toml_file(cargofile).unwrap(); + + debug!("get_package_name found lock_table {:?}", lock_table); + + let mut name = String::new(); + + if let Some(&toml::Value::Table(ref lib_table)) = lock_table.get("lib") { + if let Some(&toml::Value::String(ref package_name)) = lib_table.get("name") { + name.push_str(package_name); + return name; + } + } + + if let Some(&toml::Value::Table(ref package_table)) = lock_table.get("package") { + if let Some(&toml::Value::String(ref package_name)) = package_table.get("name") { + name.push_str(package_name); + } + } + + name } fn get_cargo_rootdir(cargofile: &Path) -> Option { From 492bfdd48ac5d21ef4a1db7571941415ffead325 Mon Sep 17 00:00:00 2001 From: Jakko Sikkar Date: Tue, 12 Jan 2016 01:59:02 +0200 Subject: [PATCH 5/6] package reading for local files --- src/racer/cargo.rs | 92 ++++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/src/racer/cargo.rs b/src/racer/cargo.rs index 52b30d8a..0ba4e0fd 100644 --- a/src/racer/cargo.rs +++ b/src/racer/cargo.rs @@ -10,7 +10,7 @@ use toml; #[derive(Debug)] struct PackageInfo { name: String, - version: String, + version: Option, source: Option } @@ -84,7 +84,6 @@ fn find_src_via_lockfile(kratename: &str, cargofile: &Path) -> Option { } } } - None } @@ -136,13 +135,12 @@ fn get_cargo_packages(cargofile: &Path) -> Option> { result.push(PackageInfo{ name: package_name.to_owned(), - version: package_version, + version: Some(package_version), source: package_source }); } } } - Some(result) } @@ -298,13 +296,7 @@ fn get_versioned_cratefile(kratename: &str, version: &str, cargofile: &Path) -> fn find_src_via_tomlfile(kratename: &str, cargofile: &Path) -> Option { // only look for 'path' references here. // We find the git and crates.io stuff via the lockfile - - let mut file = otry2!(File::open(cargofile)); - let mut string = String::new(); - otry2!(file.read_to_string(&mut string)); - let mut parser = toml::Parser::new(&string); - let table = otry!(parser.parse()); - + let table = parse_toml_file(cargofile).unwrap(); // is it this lib? (e.g. you're searching from tests to find the main library crate) { @@ -340,36 +332,66 @@ fn find_src_via_tomlfile(kratename: &str, cargofile: &Path) -> Option { } // otherwise search the dependencies - let t = match table.get("dependencies") { + let local_packages = get_local_packages(&table, cargofile, "dependencies").unwrap(); + let local_packages_dev = get_local_packages(&table, cargofile, "dev-dependencies").unwrap(); + + debug!("find_src_via_tomlfile found local packages: {:?}", local_packages); + debug!("find_src_via_tomlfile found local packages dev: {:?}", local_packages_dev); + + for package in local_packages.iter().chain(local_packages_dev.iter()) { + if let Some(package_source) = package.source.clone() { + if let Some(tomlfile) = find_cargo_tomlfile(package_source.as_path()) { + let package_name = get_package_name(tomlfile.as_path()); + + debug!("find_src_via_tomlfile package_name: {}", package_name); + + if package_name == kratename { + return Some(package_source); + } + } + } + } + + None +} + +fn get_local_packages(table: &BTreeMap, cargofile: &Path, section_name: &str) -> Option> { + debug!("get_local_packages found table {:?}", table); + + let t = match table.get(section_name) { Some(&toml::Value::Table(ref t)) => t, _ => return None }; - let mut name = kratename; - let value = if kratename.contains('_') { - t.iter().find(|&(k, _)| k.replace("-", "_") == name).map(|(k,v)| { - name = k; - v - }) - } else { - t.get(kratename) - }; + let mut result = Vec::new(); - match value { - Some(&toml::Value::Table(ref t)) => { - // local directory - let relative_path = otry!(getstr(t, "path")); - Some(otry!(cargofile.parent()) - .join(relative_path) - .join("src") - .join("lib.rs")) - }, - Some(&toml::Value::String(ref version)) => { - // versioned crate - get_versioned_cratefile(name, version, cargofile) - } - _ => None + for (package_name, value) in t.iter() { + let mut package_version = None; + + let package_source = match *value { + toml::Value::Table(ref t) => { + // local directory + let relative_path = otry!(getstr(t, "path")); + Some(otry!(cargofile.parent()) + .join(relative_path) + .join("src") + .join("lib.rs")) + }, + toml::Value::String(ref version) => { + // versioned crate + package_version = Some(version.to_owned()); + get_versioned_cratefile(package_name, version, cargofile) + } + _ => continue + }; + + result.push(PackageInfo { + name: package_name.to_owned(), + version: package_version, + source: package_source + }); } + Some(result) } fn find_cratesio_src_dirs(d: PathBuf) -> Vec { From cf612e9d405d75f659e3521d4521ea276cf14d6f Mon Sep 17 00:00:00 2001 From: Jakko Sikkar Date: Wed, 13 Jan 2016 23:02:30 +0200 Subject: [PATCH 6/6] add few tests --- Cargo.lock | 5 +++++ Cargo.toml | 3 +++ src/test_fixtures/Cargo.toml | 8 ++++++++ src/test_fixtures/src/foo.rs | 4 ++++ src/test_fixtures/src/lib.rs | 2 ++ tests/system.rs | 40 +++++++++++++++++++++++++++++++++++- tests/test.rs | 2 +- 7 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/test_fixtures/Cargo.toml create mode 100644 src/test_fixtures/src/foo.rs create mode 100644 src/test_fixtures/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index a395619a..43aa6062 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,7 @@ dependencies = [ "env_logger 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.24.0 (registry+https://github.com/rust-lang/crates.io-index)", + "test_fixtures 0.1.0", "toml 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "typed-arena 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -125,6 +126,10 @@ dependencies = [ "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "test_fixtures" +version = "0.1.0" + [[package]] name = "toml" version = "0.1.25" diff --git a/Cargo.toml b/Cargo.toml index 274081d6..03e08784 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,5 +25,8 @@ env_logger = "~0.3.2" typed-arena = "~1.0.1" clap = "~1.5.3" +[dev-dependencies] +test_fixtures = { path = "src/test_fixtures" } + [features] nightly = [] diff --git a/src/test_fixtures/Cargo.toml b/src/test_fixtures/Cargo.toml new file mode 100644 index 00000000..033ac94d --- /dev/null +++ b/src/test_fixtures/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "test_fixtures" +version = "0.1.0" +authors = ["jaxx"] + +[lib] +name = "fixtures" +path = "src/lib.rs" \ No newline at end of file diff --git a/src/test_fixtures/src/foo.rs b/src/test_fixtures/src/foo.rs new file mode 100644 index 00000000..ef03aa01 --- /dev/null +++ b/src/test_fixtures/src/foo.rs @@ -0,0 +1,4 @@ + +pub fn test() { + println!("Hello from test function"); +} \ No newline at end of file diff --git a/src/test_fixtures/src/lib.rs b/src/test_fixtures/src/lib.rs new file mode 100644 index 00000000..5bc09d8c --- /dev/null +++ b/src/test_fixtures/src/lib.rs @@ -0,0 +1,2 @@ + +pub mod foo; \ No newline at end of file diff --git a/tests/system.rs b/tests/system.rs index c3882d9e..199b7eba 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -1,11 +1,11 @@ extern crate racer; + use racer::core::complete_from_file; use racer::core::find_definition; use racer::core; use racer::scopes; use racer::util; - use std::env; use std::io::Write; use std::fs::{self, File}; @@ -103,6 +103,27 @@ fn completes_pub_fn_locally_precached() { assert_eq!("apple".to_string(), got.matchstr.to_string()); } +#[test] +fn completes_pub_fn_from_local_package() { + let src=" + extern crate fixtures; + + use fixtures::foo; + + fn main() { + let x = foo:: + } + "; + + let path = tmpname(); + write_file(&path, src); + let pos = scopes::coords_to_point(src, 7, 21); + let cache = core::FileCache::new(); + let got = complete_from_file(src, &path, pos, &core::Session::from_path(&cache, &path, &path)).nth(0); + fs::remove_file(&path).unwrap(); + assert_eq!(got.unwrap().matchstr, "test".to_owned()); +} + #[test] fn overwriting_cached_files() { let src1 = "src1"; @@ -293,6 +314,23 @@ fn follows_use_glob() { assert_eq!(got.matchstr, "myfn".to_string()); } +#[test] +fn follows_use_local_package() { + let src=" + extern crate fixtures; + + use fixtures:: + "; + + let path = tmpname(); + write_file(&path, src); + let pos = scopes::coords_to_point(src, 4, 18); + let cache = core::FileCache::new(); + let got = complete_from_file(src, &path, pos, &core::Session::from_path(&cache, &path, &path)).nth(0); + fs::remove_file(&path).unwrap(); + assert_eq!(got.unwrap().matchstr, "foo".to_owned()); +} + #[test] fn completes_struct_field_via_assignment() { let src=" diff --git a/tests/test.rs b/tests/test.rs index bed8b730..a9077de3 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,4 +1,4 @@ extern crate racer; + #[cfg(test)] pub mod system; #[cfg(test)] pub mod bench; -