From 396d8b384c4368b4948af5f408afd52183b1fc0b Mon Sep 17 00:00:00 2001 From: jneem Date: Thu, 13 Jul 2023 17:04:18 +0200 Subject: [PATCH] Use the Types AST variant for completion (#1450) * Use the Types AST variant for completion * Add missing snapshot --- lsp/nls/src/linearization/building.rs | 2 +- lsp/nls/src/linearization/interface.rs | 3 ++- lsp/nls/src/linearization/mod.rs | 10 ++++++++++ lsp/nls/src/requests/completion.rs | 1 + lsp/nls/tests/inputs/completion-dict.ncl | 17 +++++++++++++++++ ...nls__tests__inputs__completion-dict.ncl.snap | 7 +++++++ 6 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 lsp/nls/tests/inputs/completion-dict.ncl create mode 100644 lsp/nls/tests/snapshots/main__lsp__nls__tests__inputs__completion-dict.ncl.snap diff --git a/lsp/nls/src/linearization/building.rs b/lsp/nls/src/linearization/building.rs index a2745308a4..0c41d43df5 100644 --- a/lsp/nls/src/linearization/building.rs +++ b/lsp/nls/src/linearization/building.rs @@ -90,7 +90,7 @@ impl<'b> Building<'b> { TermKind::Record(_) => (), // unreachable()!: add_usage can only called on let binding, functions and record // fields, only referring to items which support usages. - TermKind::Structure | TermKind::Usage(_) => unreachable!(), + TermKind::Types(_) | TermKind::Structure | TermKind::Usage(_) => unreachable!(), TermKind::Declaration { ref mut usages, .. } | TermKind::RecordField { ref mut usages, .. } => usages.push(usage), }; diff --git a/lsp/nls/src/linearization/interface.rs b/lsp/nls/src/linearization/interface.rs index 96fb44ecdb..36a45e8db2 100644 --- a/lsp/nls/src/linearization/interface.rs +++ b/lsp/nls/src/linearization/interface.rs @@ -21,7 +21,7 @@ impl ResolutionState for Resolved {} /// 3. Records, listing their fields /// 4. wildcard (Structure) for any other kind of term. /// Can be extended later to represent Contracts, Records, etc. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq)] pub enum TermKind { Declaration { id: Ident, @@ -43,6 +43,7 @@ pub enum TermKind { usages: Vec, value: ValueState, }, + Types(Types), Structure, } diff --git a/lsp/nls/src/linearization/mod.rs b/lsp/nls/src/linearization/mod.rs index 5ed87453c2..f5237e1058 100644 --- a/lsp/nls/src/linearization/mod.rs +++ b/lsp/nls/src/linearization/mod.rs @@ -493,6 +493,16 @@ impl<'a> Linearizer for AnalysisHost<'a> { metadata: self.meta.take(), }) } + Term::Types(t) => { + lin.push(LinearizationItem { + env: self.env.clone(), + id, + pos, + ty, + kind: TermKind::Types(t.clone()), + metadata: self.meta.take(), + }); + } other => { debug!("Add wildcard item: {:?}", other); diff --git a/lsp/nls/src/requests/completion.rs b/lsp/nls/src/requests/completion.rs index 5c237b2597..7cb1ba715b 100644 --- a/lsp/nls/src/requests/completion.rs +++ b/lsp/nls/src/requests/completion.rs @@ -186,6 +186,7 @@ fn find_fields_from_term_kind( TermKind::Usage(UsageState::Resolved(new_id)) => { find_fields_from_term_kind(new_id, path, info) } + TermKind::Types(ref ty) => find_fields_from_type(ty, path, info), _ => Vec::new(), }; result.into_iter().chain(contract_result).collect() diff --git a/lsp/nls/tests/inputs/completion-dict.ncl b/lsp/nls/tests/inputs/completion-dict.ncl new file mode 100644 index 0000000000..c4c31bd9d4 --- /dev/null +++ b/lsp/nls/tests/inputs/completion-dict.ncl @@ -0,0 +1,17 @@ +### /input-inline.ncl +let x | { _ : { foo : Number | default = 1 } } = {} in +x.PATH. +### /input-assign.ncl +let Dict = { _ : { foo : Number | default = 1 } } in +let x | Dict = {} in +x.PATH. +### [[request]] +### type = "Completion" +### textDocument.uri = "file:///input-inline.ncl" +### position = { line = 1, character = 7 } +### context = { triggerKind = 2, triggerCharacter = "." } +### [[request]] +### type = "Completion" +### textDocument.uri = "file:///input-assign.ncl" +### position = { line = 2, character = 7 } +### context = { triggerKind = 2, triggerCharacter = "." } diff --git a/lsp/nls/tests/snapshots/main__lsp__nls__tests__inputs__completion-dict.ncl.snap b/lsp/nls/tests/snapshots/main__lsp__nls__tests__inputs__completion-dict.ncl.snap new file mode 100644 index 0000000000..4ca1c4b19c --- /dev/null +++ b/lsp/nls/tests/snapshots/main__lsp__nls__tests__inputs__completion-dict.ncl.snap @@ -0,0 +1,7 @@ +--- +source: lsp/nls/tests/main.rs +expression: output +--- +[foo] +[foo] +