Skip to content

Commit

Permalink
Merge pull request #535 from estebank/master
Browse files Browse the repository at this point in the history
Add function and struct docs to `Match` type
  • Loading branch information
phildawes committed May 3, 2016
2 parents c1cda2d + 94435cc commit ca9418b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 35 deletions.
25 changes: 17 additions & 8 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,25 @@ fn match_with_snippet_fn(m: Match, session: &core::Session, interface: Interface
let snippet = racer::snippets::snippet_for_match(&m, session);
match interface {
Interface::Text =>
println!("MATCH {};{};{};{};{};{:?};{}",
println!("MATCH {};{};{};{};{};{:?};{};{:?}",
m.matchstr,
snippet,
linenum.to_string(),
charnum.to_string(),
m.filepath.to_str().unwrap(),
m.mtype,
m.contextstr),
m.contextstr,
m.docs),
Interface::TabText =>
println!("MATCH\t{}\t{}\t{}\t{}\t{}\t{:?}\t{}",
println!("MATCH\t{}\t{}\t{}\t{}\t{}\t{:?}\t{}\t{:?}",
m.matchstr,
snippet,
linenum.to_string(),
charnum.to_string(),
m.filepath.to_str().unwrap(),
m.mtype,
m.contextstr),
m.contextstr,
m.docs),
}
}

Expand Down Expand Up @@ -89,7 +91,7 @@ fn match_fn(m: Match, session: &core::Session, interface: Interface) {
#[cfg(not(test))]
fn complete(cfg: Config, print_type: CompletePrinter) {
if cfg.fqn.is_some() {
return external_complete(cfg);
return external_complete(cfg, print_type);
}
complete_by_line_coords(cfg, print_type);
}
Expand All @@ -114,6 +116,7 @@ fn complete_by_line_coords(cfg: Config,
}

#[cfg(not(test))]
#[derive(Debug)]
enum CompletePrinter {
Normal,
WithSnippets
Expand Down Expand Up @@ -164,7 +167,7 @@ fn run_the_complete_fn(cfg: &Config, print_type: CompletePrinter) {


#[cfg(not(test))]
fn external_complete(cfg: Config) {
fn external_complete(cfg: Config, print_type: CompletePrinter) {
// input: a command line string passed in
let p: Vec<&str> = cfg.fqn.as_ref().unwrap().split("::").collect();
let cwd = Path::new(".");
Expand All @@ -173,12 +176,18 @@ fn external_complete(cfg: Config) {

for m in do_file_search(p[0], &Path::new(".")) {
if p.len() == 1 {
match_fn(m, &session, cfg.interface);
match print_type {
CompletePrinter::Normal => match_fn(m, &session, cfg.interface),
CompletePrinter::WithSnippets => match_with_snippet_fn(m, &session, cfg.interface),
}
} else {
for m in do_external_search(&p[1..], &m.filepath, m.point,
core::SearchType::StartsWith,
core::Namespace::BothNamespaces, &session) {
match_fn(m, &session, cfg.interface);
match print_type {
CompletePrinter::Normal => match_fn(m, &session, cfg.interface),
CompletePrinter::WithSnippets => match_with_snippet_fn(m, &session, cfg.interface),
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/racer/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub struct Match {
pub contextstr: String,
pub generic_args: Vec<String>,
pub generic_types: Vec<PathSearch>, // generic types are evaluated lazily
pub docs: String,
}


Expand All @@ -85,6 +86,7 @@ impl Match {
contextstr: self.contextstr.clone(),
generic_args: self.generic_args.clone(),
generic_types: generic_types,
docs: self.docs.clone(),
}
}
}
Expand Down
59 changes: 45 additions & 14 deletions src/racer/matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ fn match_pattern_start(src: &str, blobstart: usize, blobend: usize,
mtype: mtype,
contextstr: first_line(blob),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(),
})
}
}
Expand Down Expand Up @@ -158,7 +159,8 @@ fn match_pattern_let(msrc: &str, blobstart: usize, blobend: usize,
mtype: mtype,
contextstr: first_line(blob),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(),
});
if let ExactMatch = search_type {
break;
Expand Down Expand Up @@ -207,7 +209,9 @@ pub fn match_for(msrc: &str, blobstart: usize, blobend: usize,
mtype: For,
contextstr: first_line(blob),
generic_args: Vec::new(),
generic_types: Vec::new() });
generic_types: Vec::new(),
docs: String::new(),
});
}
}
out
Expand Down Expand Up @@ -262,7 +266,8 @@ pub fn match_extern_crate(msrc: &str, blobstart: usize, blobend: usize,
mtype: Module,
contextstr: cratepath.to_str().unwrap().to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(),
});
});
}
Expand Down Expand Up @@ -292,7 +297,8 @@ pub fn match_mod(msrc: Src, blobstart: usize, blobend: usize,
mtype: Module,
contextstr: filepath.to_str().unwrap().to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(), //TODO: fix
})
} else {
// get internal module nesting
Expand All @@ -313,7 +319,8 @@ pub fn match_mod(msrc: Src, blobstart: usize, blobend: usize,
mtype: Module,
contextstr: modpath.to_str().unwrap().to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(), //TODO: fix
})
}
}
Expand Down Expand Up @@ -352,7 +359,8 @@ pub fn match_struct(msrc: &str, blobstart: usize, blobend: usize,
mtype: Struct,
contextstr: first_line(blob),
generic_args: generics.generic_args,
generic_types: Vec::new()
generic_types: Vec::new(),
docs: find_doc(msrc, blobstart),
})
} else {
None
Expand All @@ -377,7 +385,8 @@ pub fn match_type(msrc: &str, blobstart: usize, blobend: usize,
mtype: Type,
contextstr: first_line(blob),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: find_doc(msrc, blobstart),
})
} else {
None
Expand All @@ -402,7 +411,8 @@ pub fn match_trait(msrc: &str, blobstart: usize, blobend: usize,
mtype: Trait,
contextstr: first_line(blob),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: find_doc(msrc, blobstart),
})
} else {
None
Expand All @@ -429,7 +439,8 @@ pub fn match_enum_variants(msrc: &str, blobstart: usize, blobend: usize,
mtype: EnumVariant,
contextstr: first_line(&blob[offset..]),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: find_doc(msrc, blobstart),
};
out.push(m);
}
Expand Down Expand Up @@ -461,7 +472,8 @@ pub fn match_enum(msrc: &str, blobstart: usize, blobend: usize,
mtype: Enum,
contextstr: first_line(blob),
generic_args: generics.generic_args,
generic_types: Vec::new()
generic_types: Vec::new(),
docs: find_doc(msrc, blobstart),
})
} else {
None
Expand Down Expand Up @@ -572,9 +584,11 @@ pub fn match_use(msrc: &str, blobstart: usize, blobend: usize,
pub fn match_fn(msrc: &str, blobstart: usize, blobend: usize,
searchstr: &str, filepath: &Path, search_type: SearchType,
local: bool) -> Option<Match> {

let blob = &msrc[blobstart..blobend];
let keyword = if is_const_fn(msrc, blobstart, blobend) { "const fn" } else { "fn" };
if let Some(start) = find_keyword(blob, keyword, searchstr, search_type, local) {
info!("{:?}", start);
if !typeinf::first_param_is_self(blob) {
debug!("found a fn starting {}", searchstr);
let l = match search_type {
Expand All @@ -590,7 +604,8 @@ pub fn match_fn(msrc: &str, blobstart: usize, blobend: usize,
mtype: Function,
contextstr: first_line(blob),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: find_doc(msrc, blobstart),
})
} else {
None
Expand All @@ -599,7 +614,7 @@ pub fn match_fn(msrc: &str, blobstart: usize, blobend: usize,
None
}
}

pub fn match_macro(msrc: &str, blobstart: usize, blobend: usize,
searchstr: &str, filepath: &Path, search_type: SearchType,
local: bool) -> Option<Match> {
Expand All @@ -621,9 +636,25 @@ pub fn match_macro(msrc: &str, blobstart: usize, blobend: usize,
mtype: Macro,
contextstr: first_line(blob),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(),
})
} else {
None
}
}

fn find_doc(msrc: &str, blobend: usize) -> String {
let blob = &msrc[0..blobend];

blob.lines()
.rev()
.skip(1)
.take_while(|line| line.trim().starts_with("///"))
.collect::<Vec<_>>() // These are needed because
.iter() // you cannot `rev`an `iter` that
.rev() // has already been `rev`ed.
.map(|line| String::from(line[3..].trim().to_owned())) // Remove "/// "
.collect::<Vec<_>>()
.join("\n")
}
46 changes: 34 additions & 12 deletions src/racer/nameres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ fn search_struct_fields(searchstr: &str, structmatch: &Match,
local: structmatch.local,
mtype: StructField,
contextstr: contextstr,
generic_args: Vec::new(), generic_types: Vec::new()
generic_args: Vec::new(),
generic_types: Vec::new(),
docs: String::new(),

});
}
}
Expand Down Expand Up @@ -104,7 +107,10 @@ fn search_scope_for_methods(point: usize, src: Src, searchstr: &str, filepath: &
local: true,
mtype: Function,
contextstr: signature.to_owned(),
generic_args: Vec::new(), generic_types: Vec::new()
generic_args: Vec::new(),
generic_types: Vec::new(),
docs: String::new(),

};
out.push(m);
}
Expand Down Expand Up @@ -146,7 +152,8 @@ pub fn search_for_impls(pos: usize, searchstr: &str, filepath: &Path, local: boo
mtype: Impl,
contextstr: "".into(),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(),
};
out.push(m);
}
Expand Down Expand Up @@ -282,7 +289,8 @@ fn search_scope_headers(point: usize, scopestart: usize, msrc: Src, searchstr: &
mtype: MatchArm,
contextstr: lhs.trim().to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(),
});
if let SearchType::ExactMatch = search_type {
break;
Expand Down Expand Up @@ -338,7 +346,8 @@ fn search_fn_args(fnstart: usize, open_brace_pos: usize, msrc: &str,
mtype: FnArg,
contextstr: s.to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(),
};
debug!("search_fn_args matched: {:?}", m);
out.push(m);
Expand Down Expand Up @@ -376,7 +385,8 @@ pub fn do_file_search(searchstr: &str, currentdir: &Path) -> vec::IntoIter<Match
mtype: Module,
contextstr: (&fname[3..]).to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(),
};
out.push(m);
}
Expand All @@ -395,7 +405,8 @@ pub fn do_file_search(searchstr: &str, currentdir: &Path) -> vec::IntoIter<Match
mtype: Module,
contextstr: filepath.to_str().unwrap().to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(),
};
out.push(m);
}
Expand All @@ -410,7 +421,9 @@ pub fn do_file_search(searchstr: &str, currentdir: &Path) -> vec::IntoIter<Match
mtype: Module,
contextstr: fpath_buf.to_str().unwrap().to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(),

};
out.push(m);
}
Expand Down Expand Up @@ -637,7 +650,9 @@ pub fn search_scope(start: usize, point: usize, src: Src,
mtype: Module,
contextstr: cratepath.to_str().unwrap().to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(),

});
});
}
Expand Down Expand Up @@ -809,7 +824,9 @@ pub fn resolve_path_with_str(path: &core::Path, filepath: &Path, pos: usize,
mtype: Builtin,
contextstr: "str".into(),
generic_args: vec![],
generic_types: vec![]
generic_types: vec![],
docs: String::new(),

});
}

Expand Down Expand Up @@ -866,7 +883,10 @@ pub fn resolve_name(pathseg: &core::PathSegment, filepath: &Path, pos: usize,
local: false,
mtype: Module,
contextstr: cratepath.to_str().unwrap().to_owned(),
generic_args: Vec::new(), generic_types: Vec::new()
generic_args: Vec::new(),
generic_types: Vec::new(),
docs: String::new(),

});
});

Expand Down Expand Up @@ -1060,7 +1080,9 @@ pub fn do_external_search(path: &[&str], filepath: &Path, pos: usize, search_typ
mtype: Module,
contextstr: path.to_str().unwrap().to_owned(),
generic_args: Vec::new(),
generic_types: Vec::new()
generic_types: Vec::new(),
docs: String::new(),

});
});
} else {
Expand Down
Loading

0 comments on commit ca9418b

Please sign in to comment.