From cff48fd8d8f3febaede7f6ac1012fd3c293013fb Mon Sep 17 00:00:00 2001 From: Eli Dowling Date: Fri, 5 Jan 2024 13:35:37 +1000 Subject: [PATCH] types for external completions --- crates/lang_srv/src/analysis.rs | 11 +++++++++++ crates/lang_srv/src/analysis/analysed_doc.rs | 9 ++++++++- crates/lang_srv/src/analysis/completion.rs | 12 ++++++++++-- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/crates/lang_srv/src/analysis.rs b/crates/lang_srv/src/analysis.rs index 4a408cb433..36c299097a 100644 --- a/crates/lang_srv/src/analysis.rs +++ b/crates/lang_srv/src/analysis.rs @@ -6,6 +6,7 @@ use std::{ use bumpalo::Bump; use log::debug; +use parking_lot::Mutex; use roc_can::{abilities::AbilitiesStore, expr::Declarations}; use roc_collections::{MutMap, MutSet}; use roc_load::{CheckedModule, LoadedModule}; @@ -43,6 +44,7 @@ pub(super) struct AnalyzedModule { module_id: ModuleId, interns: Interns, subs: Subs, + other_subs: Arc>>, abilities: AbilitiesStore, declarations: Declarations, // We need this because ModuleIds are not stable between compilations, so a ModuleId visible to @@ -139,6 +141,12 @@ pub(crate) fn global_analysis(doc_info: DocInfo) -> Vec { .into_iter() .map(|(id, symbols)| (id, Arc::new(symbols))) .collect(); + let all_subs = Arc::new(Mutex::new( + typechecked + .iter() + .map(|(k, v)| (k.clone(), v.solved_subs.0.clone())) + .collect::>(), + )); let mut builder = AnalyzedDocumentBuilder { interns: &interns, module_id_to_url: module_id_to_url_from_sources(&sources), @@ -149,6 +157,7 @@ pub(crate) fn global_analysis(doc_info: DocInfo) -> Vec { root_module: &mut root_module, exposed_imports, imports: &mut imports, + all_subs, exposed, }; debug!("docs: {:#?}", docs_by_module); @@ -229,6 +238,7 @@ struct AnalyzedDocumentBuilder<'a> { imports: &'a mut MutMap>, exposed_imports: HashMap>, exposed: HashMap>>, + all_subs: Arc>>, } impl<'a> AnalyzedDocumentBuilder<'a> { @@ -278,6 +288,7 @@ impl<'a> AnalyzedDocumentBuilder<'a> { abilities, declarations, module_id, + other_subs: self.all_subs.clone(), interns: self.interns.clone(), module_id_to_url: self.module_id_to_url.clone(), }; diff --git a/crates/lang_srv/src/analysis/analysed_doc.rs b/crates/lang_srv/src/analysis/analysed_doc.rs index f6370a77d7..3f037ace69 100644 --- a/crates/lang_srv/src/analysis/analysed_doc.rs +++ b/crates/lang_srv/src/analysis/analysed_doc.rs @@ -1,4 +1,4 @@ -use log::debug; +use log::{debug, info}; use std::collections::HashMap; use bumpalo::Bump; @@ -228,6 +228,7 @@ impl AnalyzedDocument { exposed_imports, aliases, imports, + other_subs, .. } = self.module()?; @@ -241,6 +242,7 @@ impl AnalyzedDocument { .map(|a| a.chars().nth(0).unwrap().is_uppercase()) .unwrap_or(false); if is_module_completion { + info!("Getting module dot completion"); //TODO: this doesn't work with builtins for some reason Some(get_upper_case_completion_items( position, @@ -250,9 +252,11 @@ impl AnalyzedDocument { &mut subs.clone(), imports, aliases, + other_subs, true, )) } else { + info!("Getting record dot completion"); field_completion( position, symbol_prefix, @@ -265,6 +269,7 @@ impl AnalyzedDocument { } else { let is_module_or_type_completion = symbol_prefix.chars().nth(0).unwrap().is_uppercase(); if is_module_or_type_completion { + info!("Getting module completion"); let completions = get_upper_case_completion_items( position, symbol_prefix, @@ -273,10 +278,12 @@ impl AnalyzedDocument { &mut subs.clone(), imports, aliases, + other_subs, false, ); Some(completions) } else { + info!("Getting variable completion"); let completions = get_completion_items( position, symbol_prefix, diff --git a/crates/lang_srv/src/analysis/completion.rs b/crates/lang_srv/src/analysis/completion.rs index 4d23d32934..025a36a434 100644 --- a/crates/lang_srv/src/analysis/completion.rs +++ b/crates/lang_srv/src/analysis/completion.rs @@ -4,6 +4,7 @@ use std::{ }; use log::{debug, trace, warn}; +use parking_lot::Mutex; use roc_can::{ def::Def, expr::{ClosureData, Declarations, Expr, WhenBranch}, @@ -19,6 +20,8 @@ use roc_types::{ }; use tower_lsp::lsp_types::{CompletionItem, CompletionItemKind}; +use crate::registry::Registry; + use super::utils::format_var_type; pub struct CompletionVisitor<'a> { @@ -331,6 +334,7 @@ pub fn get_upper_case_completion_items( subs: &mut Subs, imported_modules: &HashMap>>, aliases: &MutMap, + all_subs: &Mutex>, just_modules: bool, ) -> Vec { //TODO! use a proper completion type instead of simple @@ -345,11 +349,15 @@ pub fn get_upper_case_completion_items( } else if prefix.starts_with(&mod_name) { vars.clone() .iter() - .map(|(sym, vars)| { + .map(|(sym, var)| { CompletionItem::new_simple( sym.as_str(interns).to_string(), //TODO! I need to get subs from the module we are completing from - "builtin".to_string(), + all_subs + .lock() + .get_mut(mod_id) + .map(|subs| format_var_type(*var, subs, module_id, interns)) + .unwrap(), // "builtin".to_string(), ) }) .collect::>()