windows: Improve file_finder to support match with unix style path (#12357)

Release Notes:

- Improved file_finder to support match with Unix style path.


Sometimes we may get the Unix style path string, for example the result
of `git status`:

```bash
$ git status
On branch improve-file-finder-match-unix-paths
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   crates/file_finder/src/file_finder.rs
```

For example, from GitHub page:

<img width="760" alt="image"
src="https://github.com/zed-industries/zed/assets/5518/c6fe8d8a-839e-4eef-a162-43b1dde09593">

If we copy that path to file_finder, it will not match any files on
Windows.

## Before

<img width="699" alt="屏幕截图 2024-05-28 001037"
src="https://github.com/zed-industries/zed/assets/5518/2d2d729e-7d27-421b-9a38-cfe4e53cc033">


## After

Use Unix style path:

<img width="689" alt="屏幕截图 2024-05-28 001150"
src="https://github.com/zed-industries/zed/assets/5518/e82dc8d6-bd6c-4b78-bd91-5b5210da73c4">

Use Windows style path:

<img width="629" alt="屏幕截图 2024-05-28 001302"
src="https://github.com/zed-industries/zed/assets/5518/4892019e-b2f4-41aa-bbf7-2f5f8af7aafa">
This commit is contained in:
Jason Lee 2024-06-21 02:33:49 +08:00 committed by GitHub
parent 97abf35529
commit d501a877a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 79 additions and 38 deletions

View File

@ -54,7 +54,7 @@ struct Args {
fn parse_path_with_position(
argument_str: &str,
) -> Result<PathLikeWithPosition<PathBuf>, std::convert::Infallible> {
PathLikeWithPosition::parse_str(argument_str, |path_str| {
PathLikeWithPosition::parse_str(argument_str, |_, path_str| {
Ok(Path::new(path_str).to_path_buf())
})
}

View File

@ -793,17 +793,18 @@ impl PickerDelegate for FileFinderDelegate {
cx.notify();
Task::ready(())
} else {
let query = PathLikeWithPosition::parse_str(raw_query, |path_like_str| {
Ok::<_, std::convert::Infallible>(FileSearchQuery {
raw_query: raw_query.to_owned(),
file_query_end: if path_like_str == raw_query {
None
} else {
Some(path_like_str.len())
},
let query =
PathLikeWithPosition::parse_str(&raw_query, |normalized_query, path_like_str| {
Ok::<_, std::convert::Infallible>(FileSearchQuery {
raw_query: normalized_query.to_owned(),
file_query_end: if path_like_str == raw_query {
None
} else {
Some(path_like_str.len())
},
})
})
})
.expect("infallible");
.expect("infallible");
if Path::new(query.path_like.path_query()).is_absolute() {
self.lookup_absolute_path(query, cx)

View File

@ -1855,9 +1855,9 @@ fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
}
fn test_path_like(test_str: &str) -> PathLikeWithPosition<FileSearchQuery> {
PathLikeWithPosition::parse_str(test_str, |path_like_str| {
PathLikeWithPosition::parse_str(test_str, |normalized_query, path_like_str| {
Ok::<_, std::convert::Infallible>(FileSearchQuery {
raw_query: test_str.to_owned(),
raw_query: normalized_query.to_owned(),
file_query_end: if path_like_str == test_str {
None
} else {

View File

@ -637,7 +637,7 @@ fn possible_open_targets(
maybe_path: &String,
cx: &mut ViewContext<TerminalView>,
) -> Task<Vec<(PathLikeWithPosition<PathBuf>, Metadata)>> {
let path_like = PathLikeWithPosition::parse_str(maybe_path.as_str(), |path_str| {
let path_like = PathLikeWithPosition::parse_str(maybe_path.as_str(), |_, path_str| {
Ok::<_, std::convert::Infallible>(Path::new(path_str).to_path_buf())
})
.expect("infallible");

View File

@ -107,13 +107,17 @@ impl<P> PathLikeWithPosition<P> {
/// Parses a string that possibly has `:row:column` suffix.
/// Ignores trailing `:`s, so `test.rs:22:` is parsed as `test.rs:22`.
/// If any of the row/column component parsing fails, the whole string is then parsed as a path like.
/// If on Windows, `s` will replace `/` with `\` for compatibility.
pub fn parse_str<E>(
s: &str,
parse_path_like_str: impl Fn(&str) -> Result<P, E>,
parse_path_like_str: impl Fn(&str, &str) -> Result<P, E>,
) -> Result<Self, E> {
#[cfg(target_os = "windows")]
let s = &s.replace('/', "\\");
let fallback = |fallback_str| {
Ok(Self {
path_like: parse_path_like_str(fallback_str)?,
path_like: parse_path_like_str(s, fallback_str)?,
row: None,
column: None,
})
@ -125,7 +129,7 @@ impl<P> PathLikeWithPosition<P> {
{
let is_absolute = trimmed.starts_with(r"\\?\");
if is_absolute {
return Self::parse_absolute_path(trimmed, parse_path_like_str);
return Self::parse_absolute_path(trimmed, |p| parse_path_like_str(s, p));
}
}
@ -150,7 +154,7 @@ impl<P> PathLikeWithPosition<P> {
Ok(row) => {
if maybe_col_str.is_empty() {
Ok(Self {
path_like: parse_path_like_str(path_like_str)?,
path_like: parse_path_like_str(s, path_like_str)?,
row: Some(row),
column: None,
})
@ -159,12 +163,12 @@ impl<P> PathLikeWithPosition<P> {
maybe_col_str.split_once(':').unwrap_or((maybe_col_str, ""));
match maybe_col_str.parse::<u32>() {
Ok(col) => Ok(Self {
path_like: parse_path_like_str(path_like_str)?,
path_like: parse_path_like_str(s, path_like_str)?,
row: Some(row),
column: Some(col),
}),
Err(_) => Ok(Self {
path_like: parse_path_like_str(path_like_str)?,
path_like: parse_path_like_str(s, path_like_str)?,
row: Some(row),
column: None,
}),
@ -172,7 +176,7 @@ impl<P> PathLikeWithPosition<P> {
}
}
Err(_) => Ok(Self {
path_like: parse_path_like_str(path_like_str)?,
path_like: parse_path_like_str(s, path_like_str)?,
row: None,
column: None,
}),
@ -323,11 +327,13 @@ impl PathMatcher {
mod tests {
use super::*;
type TestPath = PathLikeWithPosition<String>;
type TestPath = PathLikeWithPosition<(String, String)>;
fn parse_str(s: &str) -> TestPath {
TestPath::parse_str(s, |s| Ok::<_, std::convert::Infallible>(s.to_string()))
.expect("infallible")
TestPath::parse_str(s, |normalized, s| {
Ok::<_, std::convert::Infallible>((normalized.to_string(), s.to_string()))
})
.expect("infallible")
}
#[test]
@ -336,7 +342,7 @@ mod tests {
(
"test_file.rs",
PathLikeWithPosition {
path_like: "test_file.rs".to_string(),
path_like: ("test_file.rs".to_string(), "test_file.rs".to_string()),
row: None,
column: None,
},
@ -344,7 +350,7 @@ mod tests {
(
"test_file.rs:1",
PathLikeWithPosition {
path_like: "test_file.rs".to_string(),
path_like: ("test_file.rs:1".to_string(), "test_file.rs".to_string()),
row: Some(1),
column: None,
},
@ -352,7 +358,7 @@ mod tests {
(
"test_file.rs:1:2",
PathLikeWithPosition {
path_like: "test_file.rs".to_string(),
path_like: ("test_file.rs:1:2".to_string(), "test_file.rs".to_string()),
row: Some(1),
column: Some(2),
},
@ -384,7 +390,7 @@ mod tests {
assert_eq!(
actual,
PathLikeWithPosition {
path_like: "test_file.rs".to_string(),
path_like: (input.to_string(), "test_file.rs".to_string()),
row,
column,
},
@ -401,7 +407,7 @@ mod tests {
(
"test_file.rs:",
PathLikeWithPosition {
path_like: "test_file.rs".to_string(),
path_like: ("test_file.rs:".to_string(), "test_file.rs".to_string()),
row: None,
column: None,
},
@ -409,7 +415,7 @@ mod tests {
(
"test_file.rs:1:",
PathLikeWithPosition {
path_like: "test_file.rs".to_string(),
path_like: ("test_file.rs:1:".to_string(), "test_file.rs".to_string()),
row: Some(1),
column: None,
},
@ -417,7 +423,10 @@ mod tests {
(
"crates/file_finder/src/file_finder.rs:1902:13:",
PathLikeWithPosition {
path_like: "crates/file_finder/src/file_finder.rs".to_string(),
path_like: (
"crates/file_finder/src/file_finder.rs:1902:13:".to_string(),
"crates/file_finder/src/file_finder.rs".to_string(),
),
row: Some(1902),
column: Some(13),
},
@ -429,7 +438,7 @@ mod tests {
(
"test_file.rs:",
PathLikeWithPosition {
path_like: "test_file.rs".to_string(),
path_like: ("test_file.rs:".to_string(), "test_file.rs".to_string()),
row: None,
column: None,
},
@ -437,7 +446,7 @@ mod tests {
(
"test_file.rs:1:",
PathLikeWithPosition {
path_like: "test_file.rs".to_string(),
path_like: ("test_file.rs:1:".to_string(), "test_file.rs".to_string()),
row: Some(1),
column: None,
},
@ -445,7 +454,10 @@ mod tests {
(
"\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:",
PathLikeWithPosition {
path_like: "C:\\Users\\someone\\test_file.rs".to_string(),
path_like: (
"\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:".to_string(),
"C:\\Users\\someone\\test_file.rs".to_string(),
),
row: Some(1902),
column: Some(13),
},
@ -453,7 +465,10 @@ mod tests {
(
"\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:",
PathLikeWithPosition {
path_like: "C:\\Users\\someone\\test_file.rs".to_string(),
path_like: (
"\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:".to_string(),
"C:\\Users\\someone\\test_file.rs".to_string(),
),
row: Some(1902),
column: Some(13),
},
@ -461,11 +476,36 @@ mod tests {
(
"\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:",
PathLikeWithPosition {
path_like: "C:\\Users\\someone\\test_file.rs".to_string(),
path_like: (
"\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:".to_string(),
"C:\\Users\\someone\\test_file.rs".to_string(),
),
row: Some(1902),
column: None,
},
),
(
"crates/utils/paths.rs",
PathLikeWithPosition {
path_like: (
"crates\\utils\\paths.rs".to_string(),
"crates\\utils\\paths.rs".to_string(),
),
row: None,
column: None,
},
),
(
"crates/utils/paths.rs:101",
PathLikeWithPosition {
path_like: (
"crates\\utils\\paths.rs:101".to_string(),
"crates\\utils\\paths.rs".to_string(),
),
row: Some(101),
column: None,
},
),
];
for (input, expected) in input_and_expected {

View File

@ -55,7 +55,7 @@ impl OpenRequest {
fn parse_file_path(&mut self, file: &str) {
if let Some(decoded) = urlencoding::decode(file).log_err() {
if let Some(path_buf) =
PathLikeWithPosition::parse_str(&decoded, |s| PathBuf::try_from(s)).log_err()
PathLikeWithPosition::parse_str(&decoded, |_, s| PathBuf::try_from(s)).log_err()
{
self.open_paths.push(path_buf)
}
@ -295,7 +295,7 @@ pub async fn handle_cli_connection(
.map(|path_with_position_string| {
PathLikeWithPosition::parse_str(
&path_with_position_string,
|path_str| {
|_, path_str| {
Ok::<_, std::convert::Infallible>(
Path::new(path_str).to_path_buf(),
)