Skip to content

Commit

Permalink
feat(completion): Provide symbol information for enum definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Jul 17, 2020
1 parent b3b65c7 commit 04a5b20
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
16 changes: 14 additions & 2 deletions completion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,14 +1013,16 @@ pub fn all_symbols<'a, 'ast>(

fn visit_ast_type(&mut self, typ: &'a ast::AstType<'ast, Self::Ident>) {
match &**typ {
Type::Record(_) => {
Type::Record(_) | Type::Variant(_) | Type::Effect(_) => {
for field in base::types::row_iter(typ) {
let mut children = Vec::new();
idents_of(self.source_span, &field.typ, &mut children);
self.result.push(pos::spanned(
field.name.span,
CompletionSymbol {
name: &field.name.value,
content: CompletionSymbolContent::Type { typ: &field.typ },
children: Vec::new(),
children,
},
));
}
Expand Down Expand Up @@ -1131,6 +1133,16 @@ pub fn all_symbols<'a, 'ast>(
}
}

impl<'a, 'ast, I> Visit<'a, 'ast> for ast::AstType<'ast, I>
where
I: 'a + 'ast,
{
type Ident = I;
fn visit(&'a self, visitor: &mut impl Visitor<'a, 'ast, Ident = Self::Ident>) {
visitor.visit_ast_type(self)
}
}

impl<'a, 'ast, I> Visit<'a, 'ast> for ast::SpannedAlias<'ast, I>
where
I: 'a + 'ast,
Expand Down
63 changes: 54 additions & 9 deletions completion/tests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern crate gluon_completion as completion;
extern crate gluon_parser as parser;

use crate::base::{
ast::Sp,
kind::{ArcKind, Kind},
pos::{BytePos, Span},
types::{ArcType, Field, Type},
Expand Down Expand Up @@ -391,6 +392,31 @@ test #Int+ test #Int+ dummy
);
}

#[derive(PartialEq, Debug)]
struct Symbols {
name: String,
children: Vec<Symbols>,
}

impl From<&'_ str> for Symbols {
fn from(s: &str) -> Self {
Self {
name: s.into(),
children: Vec::new(),
}
}
}

fn simple_symbols(symbols: Vec<Sp<completion::CompletionSymbol<'_, '_>>>) -> Vec<Symbols> {
symbols
.into_iter()
.map(|s| Symbols {
name: s.value.name.to_string(),
children: simple_symbols(s.value.children),
})
.collect()
}

#[test]
fn all_symbols_test() {
let _ = env_logger::try_init();
Expand All @@ -400,7 +426,12 @@ let test = 1
let dummy a =
let test = 3
test
type Abc a = a Int
type Abc a = {
field: a,
}
type Enum =
| A { a: Int }
| B
// Unpacked values are not counted because they probably originated in another module
let { x, y } = { x = 1, y = 2 }
1
Expand All @@ -412,15 +443,29 @@ let { x, y } = { x = 1, y = 2 }

let symbols = completion::all_symbols(expr.span, &expr);

assert_eq!(symbols.len(), 3);
assert_eq!(
symbols[1]
.value
.children
.iter()
.map(|c| c.value.name.to_string())
.collect::<Vec<_>>(),
vec!["a".to_string(), "test".to_string()]
simple_symbols(symbols),
vec![
"test".into(),
Symbols {
name: "dummy".into(),
children: vec!["a".into(), "test".into()]
},
Symbols {
name: "test.Abc".into(),
children: vec!["field".into()]
},
Symbols {
name: "test.Enum".into(),
children: vec![
Symbols {
name: "A".into(),
children: vec!["a".into()]
},
"B".into()
],
}
]
);
}

Expand Down

0 comments on commit 04a5b20

Please sign in to comment.