Skip to content

Commit

Permalink
replace is_type with ast::Layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank-III authored and lpil committed Oct 25, 2024
1 parent c8ff5a6 commit 48504fd
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions compiler-core/src/language_server/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,15 +1112,15 @@ pub struct QualifiedConstructor<'a> {
module_aliased: bool,
used_name: EcoString,
constructor: EcoString,
is_type: bool,
layer: ast::Layer,
}

impl<'a> QualifiedConstructor<'a> {
fn constructor_import(&self) -> String {
if self.is_type {
format!("type {}", self.constructor)
} else {
if self.layer.is_value() {
self.constructor.to_string()
} else {
format!("type {}", self.constructor)
}
}
}
Expand All @@ -1145,16 +1145,16 @@ impl<'a> QualifiedToUnqualifiedImportFirstPass<'a> {
&self,
module_name: &EcoString,
constructor: &EcoString,
is_type: bool,
layer: ast::Layer,
) -> Option<&'a ast::Import<EcoString>> {
let mut matching_import = None;

for def in &self.module.ast.definitions {
if let ast::Definition::Import(import) = def {
let imported = if is_type {
&import.unqualified_types
} else {
let imported = if layer.is_value() {
&import.unqualified_values
} else {
&import.unqualified_types
};

if import.module != *module_name
Expand Down Expand Up @@ -1223,7 +1223,7 @@ impl<'ast> ast::visit::Visit<'ast> for QualifiedToUnqualifiedImportFirstPass<'as
.and_then(|node| {
if let Located::Annotation(_, ty) = node {
if let Some((module, _)) = ty.named_type_name() {
return self.get_module_import(&module, name, true);
return self.get_module_import(&module, name, ast::Layer::Type);
}
}
None
Expand All @@ -1234,7 +1234,7 @@ impl<'ast> ast::visit::Visit<'ast> for QualifiedToUnqualifiedImportFirstPass<'as
module_aliased: import.as_name.is_some(),
used_name: module_alias.clone(),
constructor: name.clone(),
is_type: true,
layer: ast::Layer::Type,
});
}
}
Expand All @@ -1258,13 +1258,15 @@ impl<'ast> ast::visit::Visit<'ast> for QualifiedToUnqualifiedImportFirstPass<'as
..
} = constructor
{
if let Some(import) = self.get_module_import(module_name, constructor_name, false) {
if let Some(import) =
self.get_module_import(module_name, constructor_name, ast::Layer::Value)
{
self.qualified_constructor = Some(QualifiedConstructor {
import,
module_aliased: import.as_name.is_some(),
used_name: module_alias.clone(),
constructor: constructor_name.clone(),
is_type: false,
layer: ast::Layer::Value,
});
}
}
Expand Down Expand Up @@ -1294,13 +1296,15 @@ impl<'ast> ast::visit::Visit<'ast> for QualifiedToUnqualifiedImportFirstPass<'as
if overlaps(self.params.range, range) {
if let Some((module_alias, _)) = module {
if let crate::analyse::Inferred::Known(constructor) = constructor {
if let Some(import) = self.get_module_import(&constructor.module, name, false) {
if let Some(import) =
self.get_module_import(&constructor.module, name, ast::Layer::Value)
{
self.qualified_constructor = Some(QualifiedConstructor {
import,
module_aliased: import.as_name.is_some(),
used_name: module_alias.clone(),
constructor: name.clone(),
is_type: false,
layer: ast::Layer::Value,
});
}
}
Expand Down Expand Up @@ -1398,20 +1402,20 @@ impl<'a> QualifiedToUnqualifiedImportSecondPass<'a> {
fn edit_import(&mut self) {
let QualifiedConstructor {
constructor,
is_type,
layer,
import,
..
} = &self.qualified_constructor;
let is_imported = if *is_type {
let is_imported = if layer.is_value() {
import
.unqualified_types
.unqualified_values
.iter()
.any(|type_| type_.used_name() == constructor)
.any(|value| value.used_name() == constructor)
} else {
import
.unqualified_values
.iter()
.any(|value| value.used_name() == constructor)
.any(|type_| type_.used_name() == constructor)
};
if is_imported {
return;
Expand Down Expand Up @@ -1569,11 +1573,11 @@ impl<'ast> ast::visit::Visit<'ast> for QualifiedToUnqualifiedImportSecondPass<'a
let QualifiedConstructor {
used_name,
constructor,
is_type,
layer,
..
} = &self.qualified_constructor;

if *is_type && used_name == module_name && name == constructor {
if !layer.is_value() && used_name == module_name && name == constructor {
self.remove_module_qualifier(*location, QualifiedConstructorType::Type);
}
}
Expand All @@ -1593,11 +1597,11 @@ impl<'ast> ast::visit::Visit<'ast> for QualifiedToUnqualifiedImportSecondPass<'a
let QualifiedConstructor {
used_name,
constructor,
is_type,
layer,
..
} = &self.qualified_constructor;

if !*is_type && used_name == module_alias && name == constructor {
if layer.is_value() && used_name == module_alias && name == constructor {
self.remove_module_qualifier(*location, QualifiedConstructorType::RecordValue);
}
}
Expand Down Expand Up @@ -1627,11 +1631,11 @@ impl<'ast> ast::visit::Visit<'ast> for QualifiedToUnqualifiedImportSecondPass<'a
let QualifiedConstructor {
used_name,
constructor,
is_type,
layer,
..
} = &self.qualified_constructor;

if !*is_type && used_name == module_alias && name == constructor {
if layer.is_value() && used_name == module_alias && name == constructor {
self.remove_module_qualifier(
*location,
QualifiedConstructorType::PatternRecord,
Expand Down Expand Up @@ -1677,7 +1681,7 @@ pub fn code_action_convert_qualified_constructor_to_unqualified(
struct UnqualifiedConstructor<'a> {
module_name: EcoString,
constructor: &'a ast::UnqualifiedImport,
is_type: bool,
layer: ast::Layer,
}

struct UnqualifiedToQualifiedImportFirstPass<'a> {
Expand Down Expand Up @@ -1716,7 +1720,7 @@ impl<'a> UnqualifiedToQualifiedImportFirstPass<'a> {
Some(UnqualifiedConstructor {
constructor: value,
module_name: import.used_name()?,
is_type: false,
layer: ast::Layer::Value,
})
}),
_ => None,
Expand All @@ -1739,7 +1743,7 @@ impl<'a> UnqualifiedToQualifiedImportFirstPass<'a> {
return Some(UnqualifiedConstructor {
constructor: ty,
module_name: import.used_name()?,
is_type: true,
layer: ast::Layer::Type,
});
}
None
Expand Down Expand Up @@ -1998,11 +2002,9 @@ impl<'ast> ast::visit::Visit<'ast> for UnqualifiedToQualifiedImportSecondPass<'a
) {
if module.is_none() {
let UnqualifiedConstructor {
constructor,
is_type,
..
constructor, layer, ..
} = &self.unqualified_constructor;
if *is_type && constructor.used_name() == name {
if !layer.is_value() && constructor.used_name() == name {
self.add_module_qualifier(*location);
}
}
Expand All @@ -2017,10 +2019,10 @@ impl<'ast> ast::visit::Visit<'ast> for UnqualifiedToQualifiedImportSecondPass<'a
) {
let UnqualifiedConstructor {
constructor: wanted_constructor,
is_type,
layer,
..
} = &self.unqualified_constructor;
if !*is_type && wanted_constructor.used_name() == name {
if layer.is_value() && wanted_constructor.used_name() == name {
self.add_module_qualifier(*location);
}
ast::visit::visit_typed_expr_var(self, location, constructor, name);
Expand All @@ -2039,10 +2041,10 @@ impl<'ast> ast::visit::Visit<'ast> for UnqualifiedToQualifiedImportSecondPass<'a
if module.is_none() {
let UnqualifiedConstructor {
constructor: wanted_constructor,
is_type,
layer,
..
} = &self.unqualified_constructor;
if !*is_type && wanted_constructor.used_name() == name {
if layer.is_value() && wanted_constructor.used_name() == name {
self.add_module_qualifier(*location);
}
}
Expand Down

0 comments on commit 48504fd

Please sign in to comment.