Skip to content

Commit 38f44e8

Browse files
committed
fix(els): use NormalizedPathBuf for AbsLocation
1 parent 138c6ff commit 38f44e8

File tree

10 files changed

+40
-14
lines changed

10 files changed

+40
-14
lines changed

crates/els/completion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
605605
}
606606
let label = label.trim_end_matches('\0').to_string();
607607
// don't show future defined items
608-
if vi.def_loc.module.as_ref() == Some(&path)
608+
if vi.def_loc.module.as_deref() == Some(&path)
609609
&& name.ln_begin().unwrap_or(0) > pos.line + 1
610610
{
611611
continue;

crates/els/definition.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
4141
// Else if the target variable is an alias, jump to the definition of it.
4242
// `foo = import "foo"` => jump to `foo.er`
4343
// `{x;} = import "foo"` => jump to `x` of `foo.er`
44-
if vi.def_loc.module == Some(util::uri_to_path(uri))
44+
if vi.def_loc.module == Some(util::uri_to_path(uri).into())
4545
&& vi.def_loc.loc == token.loc()
4646
{
4747
if let Some(def) = self.get_min::<Def>(uri, pos) {
@@ -91,9 +91,9 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
9191
.parse::<PylyzerStatus>()
9292
.ok()
9393
.map(|stat| stat.file);
94-
py_file.unwrap_or(path.clone())
94+
py_file.unwrap_or(path.to_path_buf())
9595
} else {
96-
path.clone()
96+
path.to_path_buf()
9797
};
9898
let def_uri = Url::from_file_path(def_file).unwrap();
9999
return Ok(Some(lsp_types::Location::new(def_uri, range)));

crates/erg_common/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl ErgConfig {
174174
if let Some(output) = &self.dist_dir {
175175
PathBuf::from(format!("{output}/{}", self.input.filename()))
176176
} else {
177-
self.input.full_path()
177+
self.input.full_path().to_path_buf()
178178
}
179179
}
180180

crates/erg_common/io.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ impl Input {
245245
}
246246
}
247247

248+
/// This is not normalized, so use `NormalizedPathBuf::new` to compare
248249
pub fn path(&self) -> &Path {
249250
self.kind.path()
250251
}

crates/erg_common/pathutil.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::borrow::Borrow;
22
use std::ffi::OsStr;
3+
use std::fmt;
34
use std::ops::Deref;
45
use std::path::{Component, Path, PathBuf};
56

@@ -13,12 +14,24 @@ use crate::normalize_path;
1314
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
1415
pub struct NormalizedPathBuf(PathBuf);
1516

17+
impl fmt::Display for NormalizedPathBuf {
18+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19+
write!(f, "{}", self.display())
20+
}
21+
}
22+
1623
impl<P: Into<PathBuf>> From<P> for NormalizedPathBuf {
1724
fn from(path: P) -> Self {
1825
NormalizedPathBuf::new(path.into())
1926
}
2027
}
2128

29+
impl AsRef<Path> for NormalizedPathBuf {
30+
fn as_ref(&self) -> &Path {
31+
self.0.as_path()
32+
}
33+
}
34+
2235
impl Borrow<PathBuf> for NormalizedPathBuf {
2336
fn borrow(&self) -> &PathBuf {
2437
&self.0
@@ -43,6 +56,14 @@ impl NormalizedPathBuf {
4356
pub fn new(path: PathBuf) -> Self {
4457
NormalizedPathBuf(normalize_path(path.canonicalize().unwrap_or(path)))
4558
}
59+
60+
pub fn as_path(&self) -> &Path {
61+
self.0.as_path()
62+
}
63+
64+
pub fn to_path_buf(&self) -> PathBuf {
65+
self.0.clone()
66+
}
4667
}
4768

4869
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

crates/erg_compiler/context/initialize/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ impl Context {
708708
} else {
709709
erg_std_decl_path().join(format!("{}.d.er", self.name))
710710
};
711-
let abs_loc = AbsLocation::new(Some(module), loc);
711+
let abs_loc = AbsLocation::new(Some(module.into()), loc);
712712
self.register_builtin_impl(name, t, muty, vis, py_name, abs_loc);
713713
}
714714

crates/erg_compiler/context/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ pub mod unify;
1717
use std::fmt;
1818
use std::mem;
1919
use std::option::Option; // conflicting to Type::Option
20-
use std::path::{Path, PathBuf};
20+
use std::path::Path;
2121

2222
use erg_common::config::ErgConfig;
2323
use erg_common::consts::DEBUG_MODE;
2424
use erg_common::consts::PYTHON_MODE;
2525
use erg_common::dict::Dict;
2626
use erg_common::error::Location;
2727
use erg_common::impl_display_from_debug;
28+
use erg_common::pathutil::NormalizedPathBuf;
2829
use erg_common::traits::{Locational, Stream};
2930
use erg_common::Str;
3031
use erg_common::{fmt_option, fn_name, get_hash, log};
@@ -1010,7 +1011,7 @@ impl Context {
10101011
}
10111012

10121013
pub(crate) fn absolutize(&self, loc: Location) -> AbsLocation {
1013-
AbsLocation::new(Some(PathBuf::from(self.module_path())), loc)
1014+
AbsLocation::new(Some(NormalizedPathBuf::from(self.module_path())), loc)
10141015
}
10151016

10161017
#[inline]

crates/erg_compiler/lint.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
#[allow(unused_imports)]
66
use erg_common::log;
7+
use erg_common::pathutil::NormalizedPathBuf;
78
use erg_common::traits::{Locational, Runnable, Stream};
89
use erg_common::Str;
910
use erg_parser::ast::AST;
@@ -171,9 +172,9 @@ impl ASTLowerer {
171172
if mode == "eval" {
172173
return;
173174
}
174-
let self_path = self.module.context.module_path();
175+
let self_path = NormalizedPathBuf::from(self.module.context.module_path());
175176
for (referee, value) in self.module.context.index().members().iter() {
176-
if referee.module.as_deref() != Some(self_path) {
177+
if referee.module.as_ref() != Some(&self_path) {
177178
continue;
178179
}
179180
let name_is_auto = &value.name[..] == "_"

crates/erg_compiler/module/index.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fmt;
33
use std::path::{Path, PathBuf};
44

55
use erg_common::dict::Dict;
6+
use erg_common::pathutil::NormalizedPathBuf;
67
use erg_common::set;
78
use erg_common::set::Set;
89
use erg_common::shared::{MappedRwLockReadGuard, RwLockReadGuard, Shared};
@@ -112,6 +113,7 @@ impl ModuleIndex {
112113
}
113114

114115
pub fn rename_path(&mut self, old: &Path, new: PathBuf) {
116+
let new = NormalizedPathBuf::new(new);
115117
let mut new_members = Dict::new();
116118
for (loc, mut value) in std::mem::take(&mut self.members) {
117119
if value.vi.def_loc.module.as_deref() == Some(old) {

crates/erg_compiler/varinfo.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
2-
use std::path::PathBuf;
32

43
use erg_common::error::Location;
4+
use erg_common::pathutil::NormalizedPathBuf;
55
use erg_common::set::Set;
66
use erg_common::{switch_lang, Str};
77

@@ -125,7 +125,7 @@ impl VarKind {
125125

126126
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
127127
pub struct AbsLocation {
128-
pub module: Option<PathBuf>,
128+
pub module: Option<NormalizedPathBuf>,
129129
pub loc: Location,
130130
}
131131

@@ -144,14 +144,14 @@ impl std::str::FromStr for AbsLocation {
144144

145145
fn from_str(s: &str) -> Result<Self, Self::Err> {
146146
let mut split = s.split('@');
147-
let module = split.next().map(PathBuf::from);
147+
let module = split.next().map(NormalizedPathBuf::from);
148148
let loc = split.next().ok_or(())?.parse().map_err(|_| ())?;
149149
Ok(Self { module, loc })
150150
}
151151
}
152152

153153
impl AbsLocation {
154-
pub const fn new(module: Option<PathBuf>, loc: Location) -> Self {
154+
pub const fn new(module: Option<NormalizedPathBuf>, loc: Location) -> Self {
155155
Self { module, loc }
156156
}
157157

0 commit comments

Comments
 (0)