Skip to content

Commit

Permalink
chore: minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Desdaemon committed Nov 21, 2024
1 parent 5d65aec commit 377271a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ impl Backend {
let attr = String::from_utf8_lossy(attr);
self.index.models.populate_field_names(model, &[])?;
let attr = interner().get(attr.as_ref())?;
let relation = self.index.models.normalize_field_relation(attr.into(), model.into())?;
let relation = self.index.models.resolve_related_field(attr.into(), model.into())?;
Some(Type::Model(interner().resolve(&relation).into()))
}
pub fn has_attribute(&self, type_: &Type, attr: &[u8], scope: &Scope) -> bool {
Expand Down
1 change: 1 addition & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ pub fn index_models(contents: &[u8]) -> miette::Result<Vec<Model>> {
(false, None) => Some(Model {
range,
byte_range,
// if _name is not defined and _inherit = [A, B, C], A is considered the inherit base by default
type_: ModelType::Inherit(inherits),
}),
(true, None) => None,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ impl LanguageServer for Backend {
drop(entry);
if let Some(field_entry) = field_entry {
let type_ = interner().resolve(&field_entry.type_);
completion.detail = match self.index.models.normalize_field_relation(field.into(), model) {
completion.detail = match self.index.models.resolve_related_field(field.into(), model) {
None => Some(format!("{type_}(…)")),
Some(relation) => {
let relation = interner().resolve(&relation);
Expand Down
22 changes: 11 additions & 11 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl ModelIndex {
if replace {
for inherit in inherits {
let Some(inherit) = interner.get(inherit) else { continue };
if let Some(mut entry) = self.try_get_mut(&inherit.into()).expect(format_loc!("deadlock")) {
if let Some(mut entry) = self.get_mut(&inherit.into()) {
entry.descendants.retain(|loc| loc.0.path != path)
}
}
Expand Down Expand Up @@ -397,15 +397,15 @@ impl ModelIndex {
) -> Result<(), ResolveMappedError> {
while let Some((lhs, rhs)) = needle.split_once('.') {
trace!("(resolved_mapped) `{needle}` model=`{}`", interner().resolve(model));
let mut normalized = interner()
let mut resolved = interner()
.get(lhs)
.and_then(|key| self.normalize_field_relation(key.into(), *model));
if let Some(normalized) = &normalized {
.and_then(|key| self.resolve_related_field(key.into(), *model));
if let Some(normalized) = &resolved {
trace!("(resolved_mapped) prenormalized: {}", interner().resolve(normalized));
}
// lhs: foo
// rhs: ba
if normalized.is_none() {
if resolved.is_none() {
let Some(fields) = self.populate_field_names((*model).into(), &[]) else {
debug!(
"tried to resolve before fields are populated for `{}`",
Expand All @@ -416,15 +416,15 @@ impl ModelIndex {
let field = interner().get(lhs);
let field = field.and_then(|field| fields.fields.as_ref()?.get(&field.into()));
match field.as_ref().map(|f| &f.kind) {
Some(FieldKind::Relational(rel)) => normalized = Some(*rel),
Some(FieldKind::Relational(rel)) => resolved = Some(*rel),
None | Some(FieldKind::Value) => return Err(ResolveMappedError::NonRelational),
Some(FieldKind::Related(..)) => {
drop(fields);
normalized = self.normalize_field_relation(interner().get(lhs).unwrap().into(), *model);
resolved = self.resolve_related_field(interner().get(lhs).unwrap().into(), *model);
}
}
}
let Some(rel) = normalized else {
let Some(rel) = resolved else {
warn!("unresolved field `{}`.`{lhs}`", interner().resolve(model));
*needle = lhs;
if let Some(range) = range.as_mut() {
Expand All @@ -444,9 +444,9 @@ impl ModelIndex {
}
Ok(())
}
/// Turns related fields ([`FieldKind::Related`]) into concrete fields, and return the field's type itself.
/// Turns related fields ([`FieldKind::Related`]) into concrete fields, and return the field's type itself if successful.
#[must_use = "normalized relation might not have been updated back to the central index"]
pub fn normalize_field_relation(&self, field: Symbol<Field>, model: Spur) -> Option<Spur> {
pub fn resolve_related_field(&self, field: Symbol<Field>, model: Spur) -> Option<Spur> {
// Why populate?
// If we came from a long chain of relations, we might encounter a field on a model
// that hasn't been populated yet. This is because we only populate fields when they're
Expand All @@ -469,7 +469,7 @@ impl ModelIndex {
if self.resolve_mapped(&mut field_model, &mut related, None).is_ok() {
// resolved_mapped took us to the final field, now we need to resolve it to a model
let related_key = interner().get(related)?;
let field_model = self.normalize_field_relation(related_key.into(), field_model)?;
let field_model = self.resolve_related_field(related_key.into(), field_model)?;

kind = FieldKind::Relational(field_model);
let mut model_entry = self.try_get_mut(&model.into()).expect(format_loc!("deadlock"))?;
Expand Down

0 comments on commit 377271a

Please sign in to comment.