Skip to content

Commit d43f596

Browse files
authored
refactor: use inline completion snapshots (#580)
1 parent 8482286 commit d43f596

31 files changed

+271
-196
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/mun_hir/src/code_model.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod function;
22
mod r#impl;
33
mod module;
44
mod package;
5+
mod primitive_type;
56
pub(crate) mod src;
67
pub(crate) mod r#struct;
78
mod type_alias;
@@ -12,6 +13,7 @@ pub use self::{
1213
function::{Function, FunctionData},
1314
module::{Module, ModuleDef},
1415
package::Package,
16+
primitive_type::PrimitiveType,
1517
r#impl::{AssocItem, ImplData},
1618
r#struct::{Field, Struct, StructData, StructKind, StructMemoryKind},
1719
src::HasSource,

crates/mun_hir/src/code_model/module.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use mun_hir_input::{FileId, ModuleId};
22

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

66
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
77
pub struct Module {
@@ -203,7 +203,7 @@ impl From<ItemDefinitionId> for ModuleDef {
203203
ItemDefinitionId::FunctionId(id) => Function { id }.into(),
204204
ItemDefinitionId::StructId(id) => Struct { id }.into(),
205205
ItemDefinitionId::TypeAliasId(id) => TypeAlias { id }.into(),
206-
ItemDefinitionId::PrimitiveType(id) => id.into(),
206+
ItemDefinitionId::PrimitiveType(ty) => PrimitiveType { inner: ty }.into(),
207207
}
208208
}
209209
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::{name::AsName, ty::lower::type_for_primitive, Name, Ty};
2+
3+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4+
pub struct PrimitiveType {
5+
pub(crate) inner: crate::primitive_type::PrimitiveType,
6+
}
7+
8+
impl PrimitiveType {
9+
/// Returns the type of the primitive
10+
pub fn ty(self, _db: &dyn crate::HirDatabase) -> Ty {
11+
type_for_primitive(self)
12+
}
13+
14+
/// Returns the name of the primitive
15+
pub fn name(self) -> Name {
16+
self.inner.as_name()
17+
}
18+
}
19+
20+
impl From<crate::primitive_type::PrimitiveType> for PrimitiveType {
21+
fn from(inner: crate::primitive_type::PrimitiveType) -> Self {
22+
PrimitiveType { inner }
23+
}
24+
}

crates/mun_hir/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub use mun_hir_input::ModuleId;
1111
pub use salsa;
1212

1313
pub use self::code_model::{
14-
Field, Function, FunctionData, HasSource, Module, ModuleDef, Package, Struct, StructMemoryKind,
15-
TypeAlias,
14+
Field, Function, FunctionData, HasSource, Module, ModuleDef, Package, PrimitiveType, Struct,
15+
StructMemoryKind, TypeAlias,
1616
};
1717
pub use crate::{
1818
db::{

crates/mun_hir/src/primitive_type.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fmt;
22

3-
use crate::name::{name, Name};
3+
use crate::name::{name, AsName, Name};
44

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

7575
impl fmt::Display for PrimitiveType {
7676
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77-
let type_name = match self {
77+
f.write_str(self.as_str())
78+
}
79+
}
80+
81+
impl PrimitiveType {
82+
pub fn as_str(self) -> &'static str {
83+
match self {
7884
PrimitiveType::Bool => "bool",
7985
PrimitiveType::Int(PrimitiveInt {
8086
signedness,
@@ -98,8 +104,13 @@ impl fmt::Display for PrimitiveType {
98104
FloatBitness::X32 => "f32",
99105
FloatBitness::X64 => "f64",
100106
},
101-
};
102-
f.write_str(type_name)
107+
}
108+
}
109+
}
110+
111+
impl AsName for PrimitiveType {
112+
fn as_name(&self) -> Name {
113+
Name::new(self.as_str())
103114
}
104115
}
105116

crates/mun_hir/src/source_analyzer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::{
88
ids::DefWithBodyId,
99
resolver_for_scope,
1010
semantics::PathResolution,
11-
Body, ExprId, ExprScopes, HirDatabase, InFile, InferenceResult, Path, Resolver, Struct, Ty,
12-
TypeAlias, TypeNs,
11+
Body, ExprId, ExprScopes, HirDatabase, InFile, InferenceResult, Path, PrimitiveType, Resolver,
12+
Struct, Ty, TypeAlias, TypeNs,
1313
};
1414

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

221221
Some(res)

crates/mun_hir/src/ty/infer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ impl InferenceResultBuilder<'_> {
880880
TypeNs::SelfType(id) => self.db.type_for_impl_self(id),
881881
TypeNs::StructId(id) => type_for_def_fn(TypableDef::Struct(id.into())),
882882
TypeNs::TypeAliasId(id) => type_for_def_fn(TypableDef::TypeAlias(id.into())),
883-
TypeNs::PrimitiveType(id) => type_for_def_fn(TypableDef::PrimitiveType(id)),
883+
TypeNs::PrimitiveType(id) => type_for_def_fn(TypableDef::PrimitiveType(id.into())),
884884
};
885885

886886
// Resolve the value.

crates/mun_hir/src/ty/lower.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use crate::{
1111
diagnostics::DiagnosticSink,
1212
ids::ImplId,
1313
name_resolution::Namespace,
14-
primitive_type::PrimitiveType,
1514
resolve::{HasResolver, Resolver, TypeNs},
1615
ty::{FnSig, Substitution, Ty, TyKind},
1716
type_ref::{LocalTypeRefId, TypeRef, TypeRefMap, TypeRefSourceMap},
18-
Function, HasVisibility, HirDatabase, ModuleDef, Path, Struct, TypeAlias, Visibility,
17+
Function, HasVisibility, HirDatabase, ModuleDef, Path, PrimitiveType, Struct, TypeAlias,
18+
Visibility,
1919
};
2020

2121
/// A struct which holds resolved type references to `Ty`s.
@@ -138,7 +138,7 @@ impl Ty {
138138
TypeNs::SelfType(id) => Some(db.type_for_impl_self(id)),
139139
TypeNs::StructId(id) => type_for_def_fn(TypableDef::Struct(id.into())),
140140
TypeNs::TypeAliasId(id) => type_for_def_fn(TypableDef::TypeAlias(id.into())),
141-
TypeNs::PrimitiveType(id) => type_for_def_fn(TypableDef::PrimitiveType(id)),
141+
TypeNs::PrimitiveType(id) => type_for_def_fn(TypableDef::PrimitiveType(id.into())),
142142
}
143143
}
144144
}
@@ -260,11 +260,11 @@ pub(crate) fn type_for_impl_self(db: &dyn HirDatabase, i: ImplId) -> Ty {
260260
}
261261

262262
/// Build the declared type of a static.
263-
fn type_for_primitive(def: PrimitiveType) -> Ty {
264-
match def {
265-
PrimitiveType::Float(f) => TyKind::Float(f.into()),
266-
PrimitiveType::Int(i) => TyKind::Int(i.into()),
267-
PrimitiveType::Bool => TyKind::Bool,
263+
pub(crate) fn type_for_primitive(def: PrimitiveType) -> Ty {
264+
match def.inner {
265+
crate::primitive_type::PrimitiveType::Float(f) => TyKind::Float(f.into()),
266+
crate::primitive_type::PrimitiveType::Int(i) => TyKind::Int(i.into()),
267+
crate::primitive_type::PrimitiveType::Bool => TyKind::Bool,
268268
}
269269
.intern()
270270
}

crates/mun_language_server/src/completion.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ mod expr;
1414
mod name_ref;
1515
#[cfg(test)]
1616
mod test_utils;
17+
mod tests;
1718

1819
use context::{
1920
CompletionAnalysis, CompletionContext, DotAccess, NameRefContext, NameRefKind,
2021
PathCompletionContext, PathExprContext, PathKind, Qualified,
2122
};
22-
pub use item::{CompletionItem, CompletionItemKind, CompletionKind};
23+
pub use item::{CompletionItem, CompletionItemKind};
2324
use mun_hir::semantics::ScopeDef;
2425

2526
use crate::{
@@ -52,8 +53,6 @@ pub(crate) fn completions(db: &AnalysisDatabase, position: FilePosition) -> Opti
5253
}
5354
}
5455

55-
// unqualified_path::complete_unqualified_path(&mut result, &context);
56-
// dot::complete_dot(&mut result, &context);
5756
Some(result)
5857
}
5958

0 commit comments

Comments
 (0)