Skip to content

Commit

Permalink
fix(completion): Give completion for local variables when pointing to…
Browse files Browse the repository at this point in the history
… whitespace

Before the closest identifier to the left of the location would be completed which meant
```f#
test   abc
//   ^ completion here would complete `test` instead of listing local variables
```
  • Loading branch information
Marwes committed Aug 22, 2016
1 parent 55e93ac commit 5c59a79
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
21 changes: 20 additions & 1 deletion check/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ trait OnFound {
fn expr(&mut self, expr: &SpannedExpr<TcIdent<Symbol>>);

fn ident(&mut self, context: &SpannedExpr<TcIdent<Symbol>>, ident: &TcIdent<Symbol>);

/// Location points to whitespace
fn nothing(&mut self);
}

struct GetType<E> {
Expand All @@ -37,6 +40,7 @@ impl<E: TypeEnv> OnFound for GetType<E> {
fn ident(&mut self, _context: &SpannedExpr<TcIdent<Symbol>>, ident: &TcIdent<Symbol>) {
self.typ = Some(ident.env_type_of(&self.env));
}
fn nothing(&mut self) {}
}

struct Suggest<E> {
Expand Down Expand Up @@ -101,6 +105,15 @@ impl<E: TypeEnv> OnFound for Suggest<E> {
}
}
}

fn nothing(&mut self) {
self.result.extend(self.stack.iter().map(|(name, typ)| {
TcIdent {
name: name.clone(),
typ: typ.clone(),
}
}));
}
}

struct FindVisitor<F> {
Expand Down Expand Up @@ -155,7 +168,13 @@ impl<F> FindVisitor<F>
use base::ast::Expr::*;

match current.value {
Identifier(_) | Literal(_) => self.on_found.expr(current),
Identifier(_) | Literal(_) => {
if current.span(self.env).containment(&self.location) == Ordering::Equal {
self.on_found.expr(current)
} else {
self.on_found.nothing()
}
}
Call(ref func, ref args) => {
self.visit_one(once(&**func).chain(args));
}
Expand Down
39 changes: 36 additions & 3 deletions check/tests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ fn identifier() {
column: CharPos(19),
absolute: BytePos(0),
});
let expected = Ok(typ("Int"));
assert_eq!(result, expected);
assert_eq!(result, Err(()));
}

#[test]
Expand Down Expand Up @@ -285,7 +284,7 @@ a
"#,
Location {
line: 3,
column: CharPos(7),
column: CharPos(1),
absolute: BytePos(0),
});
let expected = Ok(vec!["aa".into()]);
Expand All @@ -308,3 +307,37 @@ record.aa

assert_eq!(result, expected);
}

#[test]
fn suggest_end_of_identifier() {
let result = suggest(r#"
let abc = 1
let abb = 2
abc
"#,
Location {
line: 4,
column: CharPos(3),
absolute: BytePos(0),
});
let expected = Ok(vec!["abc".into()]);

assert_eq!(result, expected);
}

#[test]
fn suggest_after_identifier() {
let result = suggest(r#"
let abc = 1
let abb = 2
abc
"#,
Location {
line: 4,
column: CharPos(5),
absolute: BytePos(0),
});
let expected = Ok(vec!["abb".into(), "abc".into()]);

assert_eq!(result, expected);
}

0 comments on commit 5c59a79

Please sign in to comment.