summaryrefslogtreecommitdiff
path: root/cli/util/path.rs
diff options
context:
space:
mode:
authorHajime-san <41257923+Hajime-san@users.noreply.github.com>2024-03-28 00:58:18 +0900
committerGitHub <noreply@github.com>2024-03-27 15:58:18 +0000
commitfeb744cebd37263026893c7e7c4852daa5df24d0 (patch)
tree99f8da702a85406acec872494a760c538025354e /cli/util/path.rs
parent3462248571fd3193106a0427b3d8f585f9716c48 (diff)
fix(lsp): decoding percent-encoding(non-ASCII) file path correctly (#22582)
Diffstat (limited to 'cli/util/path.rs')
-rw-r--r--cli/util/path.rs29
1 files changed, 27 insertions, 2 deletions
diff --git a/cli/util/path.rs b/cli/util/path.rs
index b64fde6b9..144676c01 100644
--- a/cli/util/path.rs
+++ b/cli/util/path.rs
@@ -156,11 +156,12 @@ pub fn relative_specifier(
text.push('/');
}
- Some(if text.starts_with("../") || text.starts_with("./") {
+ let text = if text.starts_with("../") || text.starts_with("./") {
text
} else {
format!("./{text}")
- })
+ };
+ Some(to_percent_decoded_str(&text))
}
/// Gets a path with the specified file stem suffix.
@@ -265,6 +266,24 @@ pub fn matches_pattern_or_exact_path(
false
}
+/// For decoding percent-encodeing string
+/// could be used for module specifier string literal of local modules,
+/// or local file path to display `non-ASCII` characters correctly
+/// # Examples
+/// ```
+/// use crate::util::path::to_percent_decoded_str;
+///
+/// let str = to_percent_decoded_str("file:///Users/path/to/%F0%9F%A6%95.ts");
+/// assert_eq!(str, "file:///Users/path/to/🦕.ts");
+/// ```
+pub fn to_percent_decoded_str(s: &str) -> String {
+ match percent_encoding::percent_decode_str(s).decode_utf8() {
+ Ok(s) => s.to_string(),
+ // when failed to decode, return the original string
+ Err(_) => s.to_string(),
+ }
+}
+
#[cfg(test)]
mod test {
use super::*;
@@ -457,4 +476,10 @@ mod test {
PathBuf::from("/test_2.d.cts")
);
}
+
+ #[test]
+ fn test_to_percent_decoded_str() {
+ let str = to_percent_decoded_str("%F0%9F%A6%95");
+ assert_eq!(str, "🦕");
+ }
}