types for external completions

This commit is contained in:
Eli Dowling 2024-01-05 13:35:37 +10:00 committed by Eli Dowling
parent afb5c6b440
commit cff48fd8d8
3 changed files with 29 additions and 3 deletions

View file

@ -6,6 +6,7 @@ use std::{
use bumpalo::Bump; use bumpalo::Bump;
use log::debug; use log::debug;
use parking_lot::Mutex;
use roc_can::{abilities::AbilitiesStore, expr::Declarations}; use roc_can::{abilities::AbilitiesStore, expr::Declarations};
use roc_collections::{MutMap, MutSet}; use roc_collections::{MutMap, MutSet};
use roc_load::{CheckedModule, LoadedModule}; use roc_load::{CheckedModule, LoadedModule};
@ -43,6 +44,7 @@ pub(super) struct AnalyzedModule {
module_id: ModuleId, module_id: ModuleId,
interns: Interns, interns: Interns,
subs: Subs, subs: Subs,
other_subs: Arc<Mutex<HashMap<ModuleId, Subs>>>,
abilities: AbilitiesStore, abilities: AbilitiesStore,
declarations: Declarations, declarations: Declarations,
// We need this because ModuleIds are not stable between compilations, so a ModuleId visible to // 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<AnalyzedDocument> {
.into_iter() .into_iter()
.map(|(id, symbols)| (id, Arc::new(symbols))) .map(|(id, symbols)| (id, Arc::new(symbols)))
.collect(); .collect();
let all_subs = Arc::new(Mutex::new(
typechecked
.iter()
.map(|(k, v)| (k.clone(), v.solved_subs.0.clone()))
.collect::<HashMap<_, _>>(),
));
let mut builder = AnalyzedDocumentBuilder { let mut builder = AnalyzedDocumentBuilder {
interns: &interns, interns: &interns,
module_id_to_url: module_id_to_url_from_sources(&sources), module_id_to_url: module_id_to_url_from_sources(&sources),
@ -149,6 +157,7 @@ pub(crate) fn global_analysis(doc_info: DocInfo) -> Vec<AnalyzedDocument> {
root_module: &mut root_module, root_module: &mut root_module,
exposed_imports, exposed_imports,
imports: &mut imports, imports: &mut imports,
all_subs,
exposed, exposed,
}; };
debug!("docs: {:#?}", docs_by_module); debug!("docs: {:#?}", docs_by_module);
@ -229,6 +238,7 @@ struct AnalyzedDocumentBuilder<'a> {
imports: &'a mut MutMap<ModuleId, MutSet<ModuleId>>, imports: &'a mut MutMap<ModuleId, MutSet<ModuleId>>,
exposed_imports: HashMap<ModuleId, Vec<(Symbol, Variable)>>, exposed_imports: HashMap<ModuleId, Vec<(Symbol, Variable)>>,
exposed: HashMap<ModuleId, Arc<Vec<(Symbol, Variable)>>>, exposed: HashMap<ModuleId, Arc<Vec<(Symbol, Variable)>>>,
all_subs: Arc<Mutex<HashMap<ModuleId, Subs>>>,
} }
impl<'a> AnalyzedDocumentBuilder<'a> { impl<'a> AnalyzedDocumentBuilder<'a> {
@ -278,6 +288,7 @@ impl<'a> AnalyzedDocumentBuilder<'a> {
abilities, abilities,
declarations, declarations,
module_id, module_id,
other_subs: self.all_subs.clone(),
interns: self.interns.clone(), interns: self.interns.clone(),
module_id_to_url: self.module_id_to_url.clone(), module_id_to_url: self.module_id_to_url.clone(),
}; };

View file

@ -1,4 +1,4 @@
use log::debug; use log::{debug, info};
use std::collections::HashMap; use std::collections::HashMap;
use bumpalo::Bump; use bumpalo::Bump;
@ -228,6 +228,7 @@ impl AnalyzedDocument {
exposed_imports, exposed_imports,
aliases, aliases,
imports, imports,
other_subs,
.. ..
} = self.module()?; } = self.module()?;
@ -241,6 +242,7 @@ impl AnalyzedDocument {
.map(|a| a.chars().nth(0).unwrap().is_uppercase()) .map(|a| a.chars().nth(0).unwrap().is_uppercase())
.unwrap_or(false); .unwrap_or(false);
if is_module_completion { if is_module_completion {
info!("Getting module dot completion");
//TODO: this doesn't work with builtins for some reason //TODO: this doesn't work with builtins for some reason
Some(get_upper_case_completion_items( Some(get_upper_case_completion_items(
position, position,
@ -250,9 +252,11 @@ impl AnalyzedDocument {
&mut subs.clone(), &mut subs.clone(),
imports, imports,
aliases, aliases,
other_subs,
true, true,
)) ))
} else { } else {
info!("Getting record dot completion");
field_completion( field_completion(
position, position,
symbol_prefix, symbol_prefix,
@ -265,6 +269,7 @@ impl AnalyzedDocument {
} else { } else {
let is_module_or_type_completion = symbol_prefix.chars().nth(0).unwrap().is_uppercase(); let is_module_or_type_completion = symbol_prefix.chars().nth(0).unwrap().is_uppercase();
if is_module_or_type_completion { if is_module_or_type_completion {
info!("Getting module completion");
let completions = get_upper_case_completion_items( let completions = get_upper_case_completion_items(
position, position,
symbol_prefix, symbol_prefix,
@ -273,10 +278,12 @@ impl AnalyzedDocument {
&mut subs.clone(), &mut subs.clone(),
imports, imports,
aliases, aliases,
other_subs,
false, false,
); );
Some(completions) Some(completions)
} else { } else {
info!("Getting variable completion");
let completions = get_completion_items( let completions = get_completion_items(
position, position,
symbol_prefix, symbol_prefix,

View file

@ -4,6 +4,7 @@ use std::{
}; };
use log::{debug, trace, warn}; use log::{debug, trace, warn};
use parking_lot::Mutex;
use roc_can::{ use roc_can::{
def::Def, def::Def,
expr::{ClosureData, Declarations, Expr, WhenBranch}, expr::{ClosureData, Declarations, Expr, WhenBranch},
@ -19,6 +20,8 @@ use roc_types::{
}; };
use tower_lsp::lsp_types::{CompletionItem, CompletionItemKind}; use tower_lsp::lsp_types::{CompletionItem, CompletionItemKind};
use crate::registry::Registry;
use super::utils::format_var_type; use super::utils::format_var_type;
pub struct CompletionVisitor<'a> { pub struct CompletionVisitor<'a> {
@ -331,6 +334,7 @@ pub fn get_upper_case_completion_items(
subs: &mut Subs, subs: &mut Subs,
imported_modules: &HashMap<ModuleId, Arc<Vec<(Symbol, Variable)>>>, imported_modules: &HashMap<ModuleId, Arc<Vec<(Symbol, Variable)>>>,
aliases: &MutMap<Symbol, (bool, Alias)>, aliases: &MutMap<Symbol, (bool, Alias)>,
all_subs: &Mutex<HashMap<ModuleId, Subs>>,
just_modules: bool, just_modules: bool,
) -> Vec<CompletionItem> { ) -> Vec<CompletionItem> {
//TODO! use a proper completion type instead of simple //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) { } else if prefix.starts_with(&mod_name) {
vars.clone() vars.clone()
.iter() .iter()
.map(|(sym, vars)| { .map(|(sym, var)| {
CompletionItem::new_simple( CompletionItem::new_simple(
sym.as_str(interns).to_string(), sym.as_str(interns).to_string(),
//TODO! I need to get subs from the module we are completing from //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::<Vec<_>>() .collect::<Vec<_>>()