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 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<Mutex<HashMap<ModuleId, Subs>>>,
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<AnalyzedDocument> {
.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::<HashMap<_, _>>(),
));
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<AnalyzedDocument> {
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<ModuleId, MutSet<ModuleId>>,
exposed_imports: HashMap<ModuleId, Vec<(Symbol, Variable)>>,
exposed: HashMap<ModuleId, Arc<Vec<(Symbol, Variable)>>>,
all_subs: Arc<Mutex<HashMap<ModuleId, Subs>>>,
}
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(),
};

View file

@ -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,

View file

@ -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<ModuleId, Arc<Vec<(Symbol, Variable)>>>,
aliases: &MutMap<Symbol, (bool, Alias)>,
all_subs: &Mutex<HashMap<ModuleId, Subs>>,
just_modules: bool,
) -> Vec<CompletionItem> {
//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::<Vec<_>>()