More type safety around names
This commit is contained in:
parent
a85c4280bf
commit
3b1a648539
@ -158,7 +158,7 @@ impl ChangeFixture {
|
|||||||
let crate_id = crate_graph.add_crate_root(
|
let crate_id = crate_graph.add_crate_root(
|
||||||
file_id,
|
file_id,
|
||||||
meta.edition,
|
meta.edition,
|
||||||
Some(crate_name.clone()),
|
Some(crate_name.clone().into()),
|
||||||
meta.cfg,
|
meta.cfg,
|
||||||
meta.env,
|
meta.env,
|
||||||
Default::default(),
|
Default::default(),
|
||||||
@ -187,7 +187,7 @@ impl ChangeFixture {
|
|||||||
crate_graph.add_crate_root(
|
crate_graph.add_crate_root(
|
||||||
crate_root,
|
crate_root,
|
||||||
Edition::Edition2018,
|
Edition::Edition2018,
|
||||||
Some(CrateName::new("test").unwrap()),
|
Some(CrateName::new("test").unwrap().into()),
|
||||||
default_cfg,
|
default_cfg,
|
||||||
Env::default(),
|
Env::default(),
|
||||||
Default::default(),
|
Default::default(),
|
||||||
|
@ -108,24 +108,37 @@ impl ops::Deref for CrateName {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct CrateDisplayName(CrateName);
|
pub struct CrateDisplayName {
|
||||||
|
// The name we use to display various paths (with `_`).
|
||||||
|
crate_name: CrateName,
|
||||||
|
// The name as specified in Cargo.toml (with `-`).
|
||||||
|
canonical_name: String,
|
||||||
|
}
|
||||||
|
|
||||||
impl From<CrateName> for CrateDisplayName {
|
impl From<CrateName> for CrateDisplayName {
|
||||||
fn from(inner: CrateName) -> CrateDisplayName {
|
fn from(crate_name: CrateName) -> CrateDisplayName {
|
||||||
CrateDisplayName(inner)
|
let canonical_name = crate_name.to_string();
|
||||||
|
CrateDisplayName { crate_name, canonical_name }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for CrateDisplayName {
|
impl fmt::Display for CrateDisplayName {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{}", self.0)
|
write!(f, "{}", self.crate_name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ops::Deref for CrateDisplayName {
|
impl ops::Deref for CrateDisplayName {
|
||||||
type Target = str;
|
type Target = str;
|
||||||
fn deref(&self) -> &str {
|
fn deref(&self) -> &str {
|
||||||
&*self.0
|
&*self.crate_name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CrateDisplayName {
|
||||||
|
pub fn from_canonical_name(canonical_name: String) -> CrateDisplayName {
|
||||||
|
let crate_name = CrateName::normalize_dashes(&canonical_name);
|
||||||
|
CrateDisplayName { crate_name, canonical_name }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,7 +168,7 @@ pub struct CrateData {
|
|||||||
///
|
///
|
||||||
/// For purposes of analysis, crates are anonymous (only names in
|
/// For purposes of analysis, crates are anonymous (only names in
|
||||||
/// `Dependency` matters), this name should only be used for UI.
|
/// `Dependency` matters), this name should only be used for UI.
|
||||||
pub display_name: Option<CrateName>,
|
pub display_name: Option<CrateDisplayName>,
|
||||||
pub cfg_options: CfgOptions,
|
pub cfg_options: CfgOptions,
|
||||||
pub env: Env,
|
pub env: Env,
|
||||||
pub dependencies: Vec<Dependency>,
|
pub dependencies: Vec<Dependency>,
|
||||||
@ -184,7 +197,7 @@ impl CrateGraph {
|
|||||||
&mut self,
|
&mut self,
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
edition: Edition,
|
edition: Edition,
|
||||||
display_name: Option<CrateName>,
|
display_name: Option<CrateDisplayName>,
|
||||||
cfg_options: CfgOptions,
|
cfg_options: CfgOptions,
|
||||||
env: Env,
|
env: Env,
|
||||||
proc_macro: Vec<(SmolStr, Arc<dyn tt::TokenExpander>)>,
|
proc_macro: Vec<(SmolStr, Arc<dyn tt::TokenExpander>)>,
|
||||||
|
@ -13,8 +13,8 @@ pub use crate::{
|
|||||||
cancellation::Canceled,
|
cancellation::Canceled,
|
||||||
change::Change,
|
change::Change,
|
||||||
input::{
|
input::{
|
||||||
CrateData, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, FileId, ProcMacroId,
|
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, Dependency, Edition, Env,
|
||||||
SourceRoot, SourceRootId,
|
FileId, ProcMacroId, SourceRoot, SourceRootId,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
pub use salsa;
|
pub use salsa;
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
use std::{iter, sync::Arc};
|
use std::{iter, sync::Arc};
|
||||||
|
|
||||||
use arrayvec::ArrayVec;
|
use arrayvec::ArrayVec;
|
||||||
use base_db::{CrateId, CrateName, Edition, FileId};
|
use base_db::{CrateDisplayName, CrateId, Edition, FileId};
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir_def::find_path::PrefixKind;
|
use hir_def::find_path::PrefixKind;
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
@ -103,7 +103,7 @@ impl Crate {
|
|||||||
db.crate_graph()[self.id].edition
|
db.crate_graph()[self.id].edition
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn display_name(self, db: &dyn HirDatabase) -> Option<CrateName> {
|
pub fn display_name(self, db: &dyn HirDatabase) -> Option<CrateDisplayName> {
|
||||||
db.crate_graph()[self.id].display_name.clone()
|
db.crate_graph()[self.id].display_name.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,15 +1,14 @@
|
|||||||
use assists::utils::FamousDefs;
|
use assists::utils::FamousDefs;
|
||||||
|
use either::Either;
|
||||||
use hir::{known, HirDisplay, Semantics};
|
use hir::{known, HirDisplay, Semantics};
|
||||||
use ide_db::RootDatabase;
|
use ide_db::RootDatabase;
|
||||||
use stdx::to_lower_snake_case;
|
use stdx::to_lower_snake_case;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, ArgListOwner, AstNode},
|
ast::{self, ArgListOwner, AstNode, NameOwner},
|
||||||
match_ast, Direction, NodeOrToken, SmolStr, SyntaxKind, TextRange, T,
|
match_ast, Direction, NodeOrToken, SmolStr, SyntaxKind, TextRange, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::FileId;
|
use crate::FileId;
|
||||||
use ast::NameOwner;
|
|
||||||
use either::Either;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub struct InlayHintsConfig {
|
pub struct InlayHintsConfig {
|
||||||
|
@ -13,7 +13,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use base_db::{CrateGraph, CrateId, CrateName, Edition, Env, FileId};
|
use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId};
|
||||||
use cfg::CfgOptions;
|
use cfg::CfgOptions;
|
||||||
use paths::{AbsPath, AbsPathBuf};
|
use paths::{AbsPath, AbsPathBuf};
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
@ -408,10 +408,12 @@ impl ProjectWorkspace {
|
|||||||
.map(|it| proc_macro_client.by_dylib_path(&it))
|
.map(|it| proc_macro_client.by_dylib_path(&it))
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let display_name =
|
||||||
|
CrateDisplayName::from_canonical_name(cargo[pkg].name.clone());
|
||||||
let crate_id = crate_graph.add_crate_root(
|
let crate_id = crate_graph.add_crate_root(
|
||||||
file_id,
|
file_id,
|
||||||
edition,
|
edition,
|
||||||
Some(CrateName::normalize_dashes(&cargo[pkg].name)),
|
Some(display_name),
|
||||||
cfg_options,
|
cfg_options,
|
||||||
env,
|
env,
|
||||||
proc_macro.clone(),
|
proc_macro.clone(),
|
||||||
@ -556,7 +558,7 @@ fn sysroot_to_crate_graph(
|
|||||||
let crate_id = crate_graph.add_crate_root(
|
let crate_id = crate_graph.add_crate_root(
|
||||||
file_id,
|
file_id,
|
||||||
Edition::Edition2018,
|
Edition::Edition2018,
|
||||||
Some(name),
|
Some(name.into()),
|
||||||
cfg_options.clone(),
|
cfg_options.clone(),
|
||||||
env,
|
env,
|
||||||
proc_macro,
|
proc_macro,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user