fix(els): use NormalizedPathBuf for AbsLocation

This commit is contained in:
Shunsuke Shibayama 2023-09-13 23:26:05 +09:00
parent 138c6ff3d8
commit 38f44e8d31
10 changed files with 40 additions and 14 deletions

View file

@ -605,7 +605,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
}
let label = label.trim_end_matches('\0').to_string();
// don't show future defined items
if vi.def_loc.module.as_ref() == Some(&path)
if vi.def_loc.module.as_deref() == Some(&path)
&& name.ln_begin().unwrap_or(0) > pos.line + 1
{
continue;

View file

@ -41,7 +41,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
// Else if the target variable is an alias, jump to the definition of it.
// `foo = import "foo"` => jump to `foo.er`
// `{x;} = import "foo"` => jump to `x` of `foo.er`
if vi.def_loc.module == Some(util::uri_to_path(uri))
if vi.def_loc.module == Some(util::uri_to_path(uri).into())
&& vi.def_loc.loc == token.loc()
{
if let Some(def) = self.get_min::<Def>(uri, pos) {
@ -91,9 +91,9 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
.parse::<PylyzerStatus>()
.ok()
.map(|stat| stat.file);
py_file.unwrap_or(path.clone())
py_file.unwrap_or(path.to_path_buf())
} else {
path.clone()
path.to_path_buf()
};
let def_uri = Url::from_file_path(def_file).unwrap();
return Ok(Some(lsp_types::Location::new(def_uri, range)));

View file

@ -174,7 +174,7 @@ impl ErgConfig {
if let Some(output) = &self.dist_dir {
PathBuf::from(format!("{output}/{}", self.input.filename()))
} else {
self.input.full_path()
self.input.full_path().to_path_buf()
}
}

View file

@ -245,6 +245,7 @@ impl Input {
}
}
/// This is not normalized, so use `NormalizedPathBuf::new` to compare
pub fn path(&self) -> &Path {
self.kind.path()
}

View file

@ -1,5 +1,6 @@
use std::borrow::Borrow;
use std::ffi::OsStr;
use std::fmt;
use std::ops::Deref;
use std::path::{Component, Path, PathBuf};
@ -13,12 +14,24 @@ use crate::normalize_path;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct NormalizedPathBuf(PathBuf);
impl fmt::Display for NormalizedPathBuf {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.display())
}
}
impl<P: Into<PathBuf>> From<P> for NormalizedPathBuf {
fn from(path: P) -> Self {
NormalizedPathBuf::new(path.into())
}
}
impl AsRef<Path> for NormalizedPathBuf {
fn as_ref(&self) -> &Path {
self.0.as_path()
}
}
impl Borrow<PathBuf> for NormalizedPathBuf {
fn borrow(&self) -> &PathBuf {
&self.0
@ -43,6 +56,14 @@ impl NormalizedPathBuf {
pub fn new(path: PathBuf) -> Self {
NormalizedPathBuf(normalize_path(path.canonicalize().unwrap_or(path)))
}
pub fn as_path(&self) -> &Path {
self.0.as_path()
}
pub fn to_path_buf(&self) -> PathBuf {
self.0.clone()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

View file

@ -708,7 +708,7 @@ impl Context {
} else {
erg_std_decl_path().join(format!("{}.d.er", self.name))
};
let abs_loc = AbsLocation::new(Some(module), loc);
let abs_loc = AbsLocation::new(Some(module.into()), loc);
self.register_builtin_impl(name, t, muty, vis, py_name, abs_loc);
}

View file

@ -17,7 +17,7 @@ pub mod unify;
use std::fmt;
use std::mem;
use std::option::Option; // conflicting to Type::Option
use std::path::{Path, PathBuf};
use std::path::Path;
use erg_common::config::ErgConfig;
use erg_common::consts::DEBUG_MODE;
@ -25,6 +25,7 @@ use erg_common::consts::PYTHON_MODE;
use erg_common::dict::Dict;
use erg_common::error::Location;
use erg_common::impl_display_from_debug;
use erg_common::pathutil::NormalizedPathBuf;
use erg_common::traits::{Locational, Stream};
use erg_common::Str;
use erg_common::{fmt_option, fn_name, get_hash, log};
@ -1010,7 +1011,7 @@ impl Context {
}
pub(crate) fn absolutize(&self, loc: Location) -> AbsLocation {
AbsLocation::new(Some(PathBuf::from(self.module_path())), loc)
AbsLocation::new(Some(NormalizedPathBuf::from(self.module_path())), loc)
}
#[inline]

View file

@ -4,6 +4,7 @@
#[allow(unused_imports)]
use erg_common::log;
use erg_common::pathutil::NormalizedPathBuf;
use erg_common::traits::{Locational, Runnable, Stream};
use erg_common::Str;
use erg_parser::ast::AST;
@ -171,9 +172,9 @@ impl ASTLowerer {
if mode == "eval" {
return;
}
let self_path = self.module.context.module_path();
let self_path = NormalizedPathBuf::from(self.module.context.module_path());
for (referee, value) in self.module.context.index().members().iter() {
if referee.module.as_deref() != Some(self_path) {
if referee.module.as_ref() != Some(&self_path) {
continue;
}
let name_is_auto = &value.name[..] == "_"

View file

@ -3,6 +3,7 @@ use std::fmt;
use std::path::{Path, PathBuf};
use erg_common::dict::Dict;
use erg_common::pathutil::NormalizedPathBuf;
use erg_common::set;
use erg_common::set::Set;
use erg_common::shared::{MappedRwLockReadGuard, RwLockReadGuard, Shared};
@ -112,6 +113,7 @@ impl ModuleIndex {
}
pub fn rename_path(&mut self, old: &Path, new: PathBuf) {
let new = NormalizedPathBuf::new(new);
let mut new_members = Dict::new();
for (loc, mut value) in std::mem::take(&mut self.members) {
if value.vi.def_loc.module.as_deref() == Some(old) {

View file

@ -1,7 +1,7 @@
use std::fmt;
use std::path::PathBuf;
use erg_common::error::Location;
use erg_common::pathutil::NormalizedPathBuf;
use erg_common::set::Set;
use erg_common::{switch_lang, Str};
@ -125,7 +125,7 @@ impl VarKind {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AbsLocation {
pub module: Option<PathBuf>,
pub module: Option<NormalizedPathBuf>,
pub loc: Location,
}
@ -144,14 +144,14 @@ impl std::str::FromStr for AbsLocation {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut split = s.split('@');
let module = split.next().map(PathBuf::from);
let module = split.next().map(NormalizedPathBuf::from);
let loc = split.next().ok_or(())?.parse().map_err(|_| ())?;
Ok(Self { module, loc })
}
}
impl AbsLocation {
pub const fn new(module: Option<PathBuf>, loc: Location) -> Self {
pub const fn new(module: Option<NormalizedPathBuf>, loc: Location) -> Self {
Self { module, loc }
}