Skip to content

Commit

Permalink
Merge pull request racer-rust#825 from kngwyu/issue-785
Browse files Browse the repository at this point in the history
Issue 785
  • Loading branch information
nrc authored May 9, 2018
2 parents 03415e4 + b1af755 commit ab5d67c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/racer/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ struct ExprTypeVisitor<'c: 's, 's> {

impl<'c, 's> visit::Visitor for ExprTypeVisitor<'c, 's> {
fn visit_expr(&mut self, expr: &ast::Expr) {
debug!("visit_expr {:?}", expr);
debug!("ExprTypeVisitor::visit_expr {:?}(kind: {:?})", expr, expr.node);
//walk_expr(self, ex, e)
match expr.node {
ExprKind::Unary(_, ref expr) |
Expand All @@ -546,7 +546,7 @@ impl<'c, 's> visit::Visitor for ExprTypeVisitor<'c, 's> {
match m.mtype {
MatchType::Function => typeinf::get_return_type_of_function(&m, &m, self.session)
.and_then(|ty| path_to_match(ty, self.session)),
MatchType::Struct => Some(Ty::Match(m)),
MatchType::Struct | MatchType::Enum => Some(Ty::Match(m)),
_ => {
debug!("ExprTypeVisitor: Cannot handle ExprCall of {:?} type", m.mtype);
None
Expand Down
5 changes: 3 additions & 2 deletions src/racer/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use util;
/// Within a [`Match`], specifies what was matched
///
/// [`Match`]: struct.Match.html
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub enum MatchType {
Struct,
Module,
Expand All @@ -37,7 +37,8 @@ pub enum MatchType {
Impl,
TraitImpl,
Enum,
EnumVariant,
/// EnumVariant needs to have Enum type to complete methods
EnumVariant(Option<Box<Match>>),
Type,
FnArg,
Trait,
Expand Down
4 changes: 2 additions & 2 deletions src/racer/matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn match_pattern_let(msrc: &str, blobstart: Point, blobend: Point,
point: blobstart + start,
coords: None,
local: local,
mtype: mtype,
mtype: mtype.clone(),
contextstr: first_line(blob),
generic_args: Vec::new(),
generic_types: Vec::new(),
Expand Down Expand Up @@ -498,7 +498,7 @@ pub fn match_enum_variants(msrc: &str, blobstart: Point, blobend: Point,
point: blobstart + offset,
coords: None,
local: local,
mtype: EnumVariant,
mtype: EnumVariant(None),
contextstr: first_line(&blob[offset..]),
generic_args: Vec::new(),
generic_types: Vec::new(),
Expand Down
11 changes: 6 additions & 5 deletions src/racer/nameres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::{Path, PathBuf};
use {core, ast, matchers, scopes, typeinf};
use core::SearchType::{self, ExactMatch, StartsWith};
use core::{Match, Src, Session, Coordinate, SessionExt, Ty, Point};
use core::MatchType::{Module, Function, Struct, Enum, FnArg, Trait, StructField,
use core::MatchType::{Module, Function, Struct, Enum, EnumVariant, FnArg, Trait, StructField,
Impl, TraitImpl, MatchArm, Builtin};
use core::Namespace;
use ast::{GenericsVisitor, ImplVisitor};
Expand Down Expand Up @@ -1339,17 +1339,18 @@ pub fn resolve_path(path: &core::Path, filepath: &Path, pos: Point,
Enum => {
let pathseg = &path.segments[len-1];
debug!("searching an enum '{}' (whole path: {:?}) searchtype: {:?}", m.matchstr, path, search_type);

let filesrc = session.load_file(&m.filepath);
let scopestart = scopes::find_stmt_start(filesrc.as_src(), m.point).unwrap();
let scopesrc = filesrc.from(scopestart);
scopesrc.iter_stmts().nth(0).map(|(blobstart,blobend)| {
for m in matchers::match_enum_variants(&filesrc,
for mut enum_var in matchers::match_enum_variants(&filesrc,
scopestart+blobstart,
scopestart+blobend,
&pathseg.name, &m.filepath, search_type, true) {
debug!("Found enum variant: {}", m.matchstr);
out.push(m);
debug!("Found enum variant {} with enum type {}", enum_var.matchstr, m.matchstr);
// return Match which has enum simultaneously, for method completion
enum_var.mtype = EnumVariant(Some(Box::new(m.clone())));
out.push(enum_var);
}
});

Expand Down
10 changes: 9 additions & 1 deletion src/racer/typeinf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub fn get_struct_field_type(fieldname: &str, structmatch: &Match, session: &Ses
pub fn get_tuplestruct_field_type(fieldnum: usize, structmatch: &Match, session: &Session) -> Option<core::Ty> {
let src = session.load_file(&structmatch.filepath);

let structsrc = if let core::MatchType::EnumVariant = structmatch.mtype {
let structsrc = if let core::MatchType::EnumVariant(_) = structmatch.mtype {
// decorate the enum variant src to make it look like a tuple struct
let to = src[structmatch.point..].find('(')
.map(|n| scopes::find_closing_paren(&src, structmatch.point + n+1))
Expand Down Expand Up @@ -281,6 +281,14 @@ pub fn get_type_of_match(m: Match, msrc: Src, session: &Session) -> Option<core:
core::MatchType::Enum |
core::MatchType::Function |
core::MatchType::Module => Some(core::Ty::Match(m)),
core::MatchType::EnumVariant(Some(boxed_enum)) => {
if boxed_enum.mtype == core::MatchType::Enum {
Some(core::Ty::Match(*boxed_enum))
} else {
debug!("EnumVariant has not-enum type: {:?}", boxed_enum.mtype);
None
}
}
_ => { debug!("!!! WARNING !!! Can't get type of {:?}", m.mtype); None }
}
}
Expand Down
31 changes: 31 additions & 0 deletions tests/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4181,6 +4181,37 @@ fn completes_for_let_destracted_var_over_comment() {
assert_eq!(get_only_completion(src, None).matchstr, "variable");
}

// For issue 785
#[test]
fn completes_methods_for_global_enum() {
let _lock = sync!();
let src = r#"
fn main() {
let bar = Some("Hello");
bar.unwrap_or_def~
}
"#;
assert_eq!(get_only_completion(src, None).matchstr, "unwrap_or_default");
}

#[test]
fn completes_methods_for_local_enum() {
let _lock = sync!();
let src = "
fn main() {
enum MyEnum {
A
}
impl MyEnum {
fn method(&self) {}
}
let bar = MyEnum::A;
bar.met~
}
";
assert_eq!(get_only_completion(src, None).matchstr, "method");
}

// For Issue #815
#[test]
fn completes_methods_after_raw_string() {
Expand Down

0 comments on commit ab5d67c

Please sign in to comment.