Skip to content

Commit

Permalink
feat(completion): Provide completion when writing fields before anoth…
Browse files Browse the repository at this point in the history
…er field

Since you are likely to write the comma after the field we need to produce an AST when the comma is missing or else we have nothing to provide completion on.
  • Loading branch information
Marwes committed Dec 6, 2017
1 parent 43abb03 commit e89f8df
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
7 changes: 5 additions & 2 deletions completion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ where
.merge_by(fields.iter().map(Either::Right), |l, r| {
l.left().unwrap().name.span.start < r.right().unwrap().name.span.start
});
let (_, selected) = self.select_spanned(iter, |either| {
let (on_whitespace, selected) = self.select_spanned(iter, |either| {
either.either(
|type_field| type_field.name.span,
|field| {
Expand All @@ -353,7 +353,10 @@ where
},
)
});
if let Some(either) = selected {

if on_whitespace {
self.found = Some(None);
} else if let Some(either) = selected {
match either {
Either::Left(type_field) => {
if type_field.name.span.containment(&self.pos) == Ordering::Equal {
Expand Down
14 changes: 14 additions & 0 deletions completion/tests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,20 @@ let { ab } = { x = 1, abc = "", abcd = 2 }
assert_eq!(result, expected);
}

#[test]
fn suggest_record_field_in_pattern_before_field() {
let _ = env_logger::init();

let text = r#"
let { a abc } = { x = 1, abc = "", abcd = 2 }
()
"#;
let result = suggest_loc(text, 1, 7);
let expected = Ok(vec!["abcd".into()]);

assert_eq!(result, expected);
}

#[test]
fn suggest_alias_field_in_pattern() {
let _ = env_logger::init();
Expand Down
8 changes: 7 additions & 1 deletion parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ extern {

// Utils

SingleComma: () = {
"," => (),
// Necessar
<!> => errors.push(<>.error)
};

Comma<Rule>: Vec<Rule> =
<rules: (<Rule> ",")*> <last: Rule?> => {
<rules: (<Rule> SingleComma)*> <last: Rule?> => {
let mut rules = rules;
rules.extend(last);
rules
Expand Down

0 comments on commit e89f8df

Please sign in to comment.