Function racer::find_definition[][src]

pub fn find_definition<P, C>(
    filepath: P,
    cursor: C,
    session: &Session<'_>
) -> Option<Match> where
    P: AsRef<Path>,
    C: Into<Location>, 
Expand description

Find the definition for item at given a file, source, and cursor index

Examples

extern crate racer;
extern crate env_logger;

use std::path::Path;

let _ = env_logger::init();
let cache = racer::FileCache::default();
let session = racer::Session::new(&cache, None);

// This is the file where we request completion from
let src = r"
   mod sub;
   use sub::foo;
   fn main() {
       foo();
   };
";

// This is the submodule where the definition is found
let sub = r"pub fn foo() {}";

// Load files into cache to prevent trying to read from disk
session.cache_file_contents("sub.rs", sub);
session.cache_file_contents("lib.rs", src);

// Search for the definition. 52 is the byte offset in `src`.
// Specifically, this asks for the definition of `foo()`.
let m = racer::find_definition("lib.rs", racer::Location::from(52), &session)
              .expect("find definition returns a match");

// Should have found definition in the "sub.rs" file
assert_eq!(m.filepath, Path::new("sub.rs"));
// The definition should be for foo
assert_eq!(&m.matchstr[..], "foo");
// The definition should be a function
assert_eq!(m.mtype, racer::MatchType::Function);