Skip to content

Commit

Permalink
refactor: use inline completion snapshots (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra authored Feb 10, 2025
1 parent 8482286 commit d43f596
Show file tree
Hide file tree
Showing 31 changed files with 271 additions and 196 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/mun_hir/src/code_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod function;
mod r#impl;
mod module;
mod package;
mod primitive_type;
pub(crate) mod src;
pub(crate) mod r#struct;
mod type_alias;
Expand All @@ -12,6 +13,7 @@ pub use self::{
function::{Function, FunctionData},
module::{Module, ModuleDef},
package::Package,
primitive_type::PrimitiveType,
r#impl::{AssocItem, ImplData},
r#struct::{Field, Struct, StructData, StructKind, StructMemoryKind},
src::HasSource,
Expand Down
6 changes: 3 additions & 3 deletions crates/mun_hir/src/code_model/module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use mun_hir_input::{FileId, ModuleId};

use super::{r#impl::Impl, AssocItem, Function, Package, Struct, TypeAlias};
use crate::{ids::ItemDefinitionId, primitive_type::PrimitiveType, DiagnosticSink, HirDatabase};
use super::{r#impl::Impl, AssocItem, Function, Package, PrimitiveType, Struct, TypeAlias};
use crate::{ids::ItemDefinitionId, DiagnosticSink, HirDatabase};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct Module {
Expand Down Expand Up @@ -203,7 +203,7 @@ impl From<ItemDefinitionId> for ModuleDef {
ItemDefinitionId::FunctionId(id) => Function { id }.into(),
ItemDefinitionId::StructId(id) => Struct { id }.into(),
ItemDefinitionId::TypeAliasId(id) => TypeAlias { id }.into(),
ItemDefinitionId::PrimitiveType(id) => id.into(),
ItemDefinitionId::PrimitiveType(ty) => PrimitiveType { inner: ty }.into(),
}
}
}
24 changes: 24 additions & 0 deletions crates/mun_hir/src/code_model/primitive_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::{name::AsName, ty::lower::type_for_primitive, Name, Ty};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PrimitiveType {
pub(crate) inner: crate::primitive_type::PrimitiveType,
}

impl PrimitiveType {
/// Returns the type of the primitive
pub fn ty(self, _db: &dyn crate::HirDatabase) -> Ty {
type_for_primitive(self)
}

/// Returns the name of the primitive
pub fn name(self) -> Name {
self.inner.as_name()
}
}

impl From<crate::primitive_type::PrimitiveType> for PrimitiveType {
fn from(inner: crate::primitive_type::PrimitiveType) -> Self {
PrimitiveType { inner }
}
}
4 changes: 2 additions & 2 deletions crates/mun_hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub use mun_hir_input::ModuleId;
pub use salsa;

pub use self::code_model::{
Field, Function, FunctionData, HasSource, Module, ModuleDef, Package, Struct, StructMemoryKind,
TypeAlias,
Field, Function, FunctionData, HasSource, Module, ModuleDef, Package, PrimitiveType, Struct,
StructMemoryKind, TypeAlias,
};
pub use crate::{
db::{
Expand Down
19 changes: 15 additions & 4 deletions crates/mun_hir/src/primitive_type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use crate::name::{name, Name};
use crate::name::{name, AsName, Name};

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Signedness {
Expand Down Expand Up @@ -74,7 +74,13 @@ impl PrimitiveType {

impl fmt::Display for PrimitiveType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let type_name = match self {
f.write_str(self.as_str())
}
}

impl PrimitiveType {
pub fn as_str(self) -> &'static str {
match self {
PrimitiveType::Bool => "bool",
PrimitiveType::Int(PrimitiveInt {
signedness,
Expand All @@ -98,8 +104,13 @@ impl fmt::Display for PrimitiveType {
FloatBitness::X32 => "f32",
FloatBitness::X64 => "f64",
},
};
f.write_str(type_name)
}
}
}

impl AsName for PrimitiveType {
fn as_name(&self) -> Name {
Name::new(self.as_str())
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/mun_hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::{
ids::DefWithBodyId,
resolver_for_scope,
semantics::PathResolution,
Body, ExprId, ExprScopes, HirDatabase, InFile, InferenceResult, Path, Resolver, Struct, Ty,
TypeAlias, TypeNs,
Body, ExprId, ExprScopes, HirDatabase, InFile, InferenceResult, Path, PrimitiveType, Resolver,
Struct, Ty, TypeAlias, TypeNs,
};

/// A `SourceAnalyzer` is a wrapper which exposes the HIR API in terms of the
Expand Down Expand Up @@ -215,7 +215,7 @@ fn resolve_hir_path_qualifier(
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
TypeNs::StructId(it) => PathResolution::Def(Struct::from(it).into()),
TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
TypeNs::PrimitiveType(it) => PathResolution::Def(it.into()),
TypeNs::PrimitiveType(it) => PathResolution::Def(PrimitiveType::from(it).into()),
};

Some(res)
Expand Down
2 changes: 1 addition & 1 deletion crates/mun_hir/src/ty/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ impl InferenceResultBuilder<'_> {
TypeNs::SelfType(id) => self.db.type_for_impl_self(id),
TypeNs::StructId(id) => type_for_def_fn(TypableDef::Struct(id.into())),
TypeNs::TypeAliasId(id) => type_for_def_fn(TypableDef::TypeAlias(id.into())),
TypeNs::PrimitiveType(id) => type_for_def_fn(TypableDef::PrimitiveType(id)),
TypeNs::PrimitiveType(id) => type_for_def_fn(TypableDef::PrimitiveType(id.into())),
};

// Resolve the value.
Expand Down
16 changes: 8 additions & 8 deletions crates/mun_hir/src/ty/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use crate::{
diagnostics::DiagnosticSink,
ids::ImplId,
name_resolution::Namespace,
primitive_type::PrimitiveType,
resolve::{HasResolver, Resolver, TypeNs},
ty::{FnSig, Substitution, Ty, TyKind},
type_ref::{LocalTypeRefId, TypeRef, TypeRefMap, TypeRefSourceMap},
Function, HasVisibility, HirDatabase, ModuleDef, Path, Struct, TypeAlias, Visibility,
Function, HasVisibility, HirDatabase, ModuleDef, Path, PrimitiveType, Struct, TypeAlias,
Visibility,
};

/// A struct which holds resolved type references to `Ty`s.
Expand Down Expand Up @@ -138,7 +138,7 @@ impl Ty {
TypeNs::SelfType(id) => Some(db.type_for_impl_self(id)),
TypeNs::StructId(id) => type_for_def_fn(TypableDef::Struct(id.into())),
TypeNs::TypeAliasId(id) => type_for_def_fn(TypableDef::TypeAlias(id.into())),
TypeNs::PrimitiveType(id) => type_for_def_fn(TypableDef::PrimitiveType(id)),
TypeNs::PrimitiveType(id) => type_for_def_fn(TypableDef::PrimitiveType(id.into())),
}
}
}
Expand Down Expand Up @@ -260,11 +260,11 @@ pub(crate) fn type_for_impl_self(db: &dyn HirDatabase, i: ImplId) -> Ty {
}

/// Build the declared type of a static.
fn type_for_primitive(def: PrimitiveType) -> Ty {
match def {
PrimitiveType::Float(f) => TyKind::Float(f.into()),
PrimitiveType::Int(i) => TyKind::Int(i.into()),
PrimitiveType::Bool => TyKind::Bool,
pub(crate) fn type_for_primitive(def: PrimitiveType) -> Ty {
match def.inner {
crate::primitive_type::PrimitiveType::Float(f) => TyKind::Float(f.into()),
crate::primitive_type::PrimitiveType::Int(i) => TyKind::Int(i.into()),
crate::primitive_type::PrimitiveType::Bool => TyKind::Bool,
}
.intern()
}
Expand Down
5 changes: 2 additions & 3 deletions crates/mun_language_server/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ mod expr;
mod name_ref;
#[cfg(test)]
mod test_utils;
mod tests;

use context::{
CompletionAnalysis, CompletionContext, DotAccess, NameRefContext, NameRefKind,
PathCompletionContext, PathExprContext, PathKind, Qualified,
};
pub use item::{CompletionItem, CompletionItemKind, CompletionKind};
pub use item::{CompletionItem, CompletionItemKind};
use mun_hir::semantics::ScopeDef;

use crate::{
Expand Down Expand Up @@ -52,8 +53,6 @@ pub(crate) fn completions(db: &AnalysisDatabase, position: FilePosition) -> Opti
}
}

// unqualified_path::complete_unqualified_path(&mut result, &context);
// dot::complete_dot(&mut result, &context);
Some(result)
}

Expand Down
1 change: 0 additions & 1 deletion crates/mun_language_server/src/completion/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ impl<'a> CompletionContext<'a> {
let sema = Semantics::new(db);

let original_file = sema.parse(position.file_id);

// Insert a fake identifier to get a valid parse tree. This tree will be used to
// determine context. The actual original_file will be used for
// completion.
Expand Down
26 changes: 13 additions & 13 deletions crates/mun_language_server/src/completion/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(super) fn complete_dot(

#[cfg(test)]
mod tests {
use crate::completion::{test_utils::completion_string, CompletionKind};
use crate::completion::test_utils::completion_string;

#[test]
fn test_struct_fields() {
Expand All @@ -42,8 +42,10 @@ mod tests {
bar.$0
}
"#,
Some(CompletionKind::Reference)
));
), @r###"
fd bar i32
fd foo i32
"###);
}

#[test]
Expand All @@ -57,8 +59,10 @@ mod tests {
bar.$0
}
"#,
Some(CompletionKind::Reference)
));
), @r###"
fd 0 i32
fd 1 i32
"###);
}

#[test]
Expand All @@ -73,8 +77,7 @@ mod tests {
bar.0.$0
}
"#,
Some(CompletionKind::Reference)
));
), @"fd baz i32");
}

#[test]
Expand All @@ -88,8 +91,7 @@ mod tests {
bar.$0
}
"#,
Some(CompletionKind::Reference)
));
), @"fd bar i32");
}

#[test]
Expand All @@ -102,8 +104,7 @@ mod tests {
bar.$0
}
"#,
Some(CompletionKind::Reference)
));
), @"fd bar i32");
}

#[test]
Expand All @@ -118,7 +119,6 @@ mod tests {
}
}
"#,
Some(CompletionKind::Reference)
));
), @"fd bar i32");
}
}
34 changes: 20 additions & 14 deletions crates/mun_language_server/src/completion/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(super) fn complete_expr_path(

#[cfg(test)]
mod tests {
use crate::completion::{test_utils::completion_string, CompletionKind};
use crate::completion::test_utils::completion_string;

#[test]
fn test_local_scope() {
Expand All @@ -57,9 +57,12 @@ mod tests {
let foo_bar = 0;
f$0
}
"#,
Some(CompletionKind::Reference)
));
"#
), @r###"
fn foo -> ()
lc bar i32
lc foo_bar i32
"###);
}

#[test]
Expand All @@ -77,9 +80,8 @@ mod tests {
fn foo() {
let bar = Foo::$0;
}
"#,
Some(CompletionKind::Reference)
));
"#
), @"fn new -> Foo");
}

#[test]
Expand All @@ -90,9 +92,11 @@ mod tests {
let a = 0;
foo(f$0)
}
"#,
Some(CompletionKind::Reference)
));
"#
), @r###"
fn bar -> ()
lc a i32
"###);
}

#[test]
Expand All @@ -107,8 +111,7 @@ mod tests {
}
}
"#,
Some(CompletionKind::Reference)
));
), @"fn foo -> ()");
}

#[test]
Expand All @@ -123,7 +126,10 @@ mod tests {
}
}
"#,
Some(CompletionKind::Reference)
));
), @r###"
lc self Foo
sp Self Foo
st Foo Foo
"###);
}
}
Loading

0 comments on commit d43f596

Please sign in to comment.