Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix :show targets / :show paths parsing logic #294

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 20 additions & 26 deletions src/ghci/parse/show_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ impl ShowPaths {
}

/// Convert a target (from `:show targets` output) to a module source path.
pub fn target_to_path(&self, target: &str) -> miette::Result<(Utf8PathBuf, TargetKind)> {
pub fn target_to_path(&self, target: &str) -> miette::Result<(NormalPath, TargetKind)> {
let target_path = Utf8Path::new(target);
if is_haskell_source_file(target_path) {
// The target is already a path.
if let Some(path) = self.target_path_to_path(target_path) {
tracing::trace!(%path, %target, "Target is path");
return Ok((path, TargetKind::Path));
}
let path = self.cwd.join(target_path);
tracing::trace!(%path, %target, "Target is path");
return Ok((NormalPath::new(path, &self.cwd)?, TargetKind::Path));
} else {
// Else, split by `.` to get path components.
let mut path = target.split('.').collect::<Utf8PathBuf>();
Expand All @@ -53,39 +52,34 @@ impl ShowPaths {
for haskell_source_extension in HASKELL_SOURCE_EXTENSIONS {
path.set_extension(haskell_source_extension);

if let Some(path) = self.target_path_to_path(&path) {
tracing::trace!(%path, %target, "Found path for target");
return Ok((path, TargetKind::Module));
for search_path in self.absolute_search_paths() {
let path = search_path.join(&path);
if path.exists() {
tracing::trace!(%path, %target, "Found path for target");
return Ok((NormalPath::new(path, &self.cwd)?, TargetKind::Module));
}
}
}
}
Err(miette!("Couldn't find source path for {target}"))
}

/// Convert a target path like `src/MyLib.hs` to a module source path starting with one of the
/// `search_paths`.
fn target_path_to_path(&self, target: &Utf8Path) -> Option<Utf8PathBuf> {
for search_path in self.paths() {
let path = search_path.join(target);
if path.exists() {
// Found it!
return Some(path);
fn absolute_search_paths(&self) -> impl Iterator<Item = Utf8PathBuf> + '_ {
self.search_paths.iter().map(|path| {
if path.is_absolute() {
path.clone()
} else {
self.cwd.join(path)
}
}

None
}

fn paths(&self) -> impl Iterator<Item = &Utf8PathBuf> {
self.search_paths.iter().chain(std::iter::once(&self.cwd))
})
}

/// Convert a Haskell source path to a module name.
pub fn path_to_module(&self, path: &Utf8Path) -> miette::Result<String> {
let path = path.with_extension("");
let path_str = path.as_str();

for search_path in self.paths() {
for search_path in self.absolute_search_paths() {
if let Some(suffix) = path_str.strip_prefix(search_path.as_str()) {
let module_name = Utf8Path::new(suffix)
.components()
Expand Down Expand Up @@ -238,12 +232,12 @@ mod tests {
fn test_path_to_module() {
let paths = ShowPaths {
cwd: Utf8PathBuf::from("/Users/wiggles/ghciwatch/"),
search_paths: vec![],
search_paths: vec![Utf8PathBuf::from("src")],
};

assert_eq!(
paths
.path_to_module(Utf8Path::new("/Users/wiggles/ghciwatch/Foo/Bar/Baz.hs"))
.path_to_module(Utf8Path::new("/Users/wiggles/ghciwatch/src/Foo/Bar/Baz.hs"))
.unwrap(),
"Foo.Bar.Baz"
);
Expand Down
44 changes: 16 additions & 28 deletions src/ghci/parse/show_targets.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use camino::Utf8PathBuf;
use miette::miette;
use winnow::combinator::repeat;
use winnow::Parser;

use super::lines::until_newline;
use super::show_paths::ShowPaths;
use super::TargetKind;
use crate::normal_path::NormalPath;

/// Parse `:show targets` output into a set of module source paths.
pub fn parse_show_targets(
search_paths: &ShowPaths,
input: &str,
) -> miette::Result<Vec<(Utf8PathBuf, TargetKind)>> {
) -> miette::Result<Vec<(NormalPath, TargetKind)>> {
let targets: Vec<_> = repeat(0.., until_newline)
.parse(input)
.map_err(|err| miette!("{err}"))?;
Expand All @@ -24,27 +24,31 @@ pub fn parse_show_targets(

#[cfg(test)]
mod tests {
use crate::normal_path::NormalPath;

use super::*;
use camino::Utf8PathBuf;
use indoc::indoc;
use pretty_assertions::assert_eq;

#[test]
fn test_parse_show_targets() {
let show_paths = ShowPaths {
cwd: Utf8PathBuf::from("tests/data/simple"),
search_paths: vec![
Utf8PathBuf::from("tests/data/simple/test"),
Utf8PathBuf::from("tests/data/simple/src"),
],
cwd: NormalPath::from_cwd("tests/data/simple")
.unwrap()
.absolute()
.to_owned(),
search_paths: vec![Utf8PathBuf::from("test"), Utf8PathBuf::from("src")],
};

let normal_path = |p: &str| NormalPath::new(p, &show_paths.cwd).unwrap();

assert_eq!(
parse_show_targets(
&show_paths,
indoc!(
"
src/MyLib.hs
MyLib.hs
TestMain
MyLib
MyModule
Expand All @@ -53,26 +57,10 @@ mod tests {
)
.unwrap(),
vec![
(
Utf8PathBuf::from("tests/data/simple/src/MyLib.hs"),
TargetKind::Path
),
(
Utf8PathBuf::from("tests/data/simple/src/MyLib.hs"),
TargetKind::Path
),
(
Utf8PathBuf::from("tests/data/simple/test/TestMain.hs"),
TargetKind::Module
),
(
Utf8PathBuf::from("tests/data/simple/src/MyLib.hs"),
TargetKind::Module
),
(
Utf8PathBuf::from("tests/data/simple/src/MyModule.hs"),
TargetKind::Module
),
(normal_path("src/MyLib.hs"), TargetKind::Path),
(normal_path("test/TestMain.hs"), TargetKind::Module),
(normal_path("src/MyLib.hs"), TargetKind::Module),
(normal_path("src/MyModule.hs"), TargetKind::Module),
]
);
}
Expand Down
45 changes: 45 additions & 0 deletions src/normal_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,49 @@ mod tests {

assert_eq!(test_path.into_absolute().as_os_str(), dir.as_os_str());
}

#[test]
fn test_normalpath_new() {
let base = Utf8Path::new("/Users/wiggles/ghciwatch/tests/data/simple");
let relative = Utf8Path::new("src/MyLib.hs");
let path = NormalPath::new(relative, base).unwrap();

assert_eq!(
path.absolute(),
Utf8Path::new("/Users/wiggles/ghciwatch/tests/data/simple/src/MyLib.hs")
);
assert_eq!(path.relative(), Utf8Path::new("src/MyLib.hs"));
}

#[test]
fn test_normalpath_new_parent() {
let base = Utf8Path::new("/a/b/c");
let relative = Utf8Path::new("../puppy");
let path = NormalPath::new(relative, base).unwrap();

assert_eq!(path.absolute(), Utf8Path::new("/a/b/puppy"));
assert_eq!(path.relative(), Utf8Path::new("../puppy"));
}

#[test]
fn test_normalpath_new_unrelated() {
let base = Utf8Path::new("/a/b/c");
let relative = Utf8Path::new("/d/e/f");
let path = NormalPath::new(relative, base).unwrap();

assert_eq!(path.absolute(), Utf8Path::new("/d/e/f"));
// This is kinda silly; the paths share no components in common, they're both absolute, but
// we don't get an absolute path out of it.
assert_eq!(path.relative(), Utf8Path::new("../../../d/e/f"));
}

#[test]
fn test_normalpath_new_both_relative() {
let base = Utf8Path::new("a/b/c");
let relative = Utf8Path::new("d/e/f");
let path = NormalPath::new(relative, base).unwrap();

assert_eq!(path.absolute(), Utf8Path::new("a/b/c/d/e/f"));
assert_eq!(path.relative(), Utf8Path::new("d/e/f"));
}
}
Loading