Файли
openidea/crates/ide-core/src/ipc_path.rs
T
rohitkumardandaтаrohitdanda b1348ee03a fix(lsp): open network-share files and upper-case drive letters from language servers (#2794)
On Windows `uri_to_path` rejected any `file:` URI host other than `localhost`, so network-share results (`file://server/share/a.ts`) from language servers were dropped from diagnostics and renames and opened as read-only virtual documents; such hosts now map to `\\server\share\...`, and `path_to_uri` writes UNC paths in the same RFC 8089 form. Lower-case or escaped drive letters (`file:///c%3A/x`) are upper-cased in `uri_to_path` and `to_ipc`, so the UI no longer treats `c:/x` and `C:/x` as different files. Non-Windows behaviour is unchanged.

Co-authored-by: rohitdanda <[email protected]>
2026-11-12 02:17:35 +08:00

157 рядки
3.8 KiB
Rust

//! Paths on the IPC boundary are `/`-separated on every OS, so the UI can split,
//! join and compare them without caring about Windows. Path fields of IPC types
//! are [`IpcPath`]: a `PathBuf` (derefs to `Path`) that serializes `C:\a\b` (and `\\?\C:\a\b`) as
//! `C:/a/b` and is a plain `string` in the generated bindings.
//!
//! Incoming paths need nothing: Windows accepts `C:/a/b`, and `Path` equality
//! and hashing are component-wise.
use std::borrow::Borrow;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// `p` as the UI sees it: `C:\a\b`, `c:\a\b` and `\\?\C:\a\b` become `C:/a/b`.
pub fn to_ipc(p: &Path) -> String {
#[cfg(windows)]
{
let mut s = dunce::simplified(p).to_string_lossy().replace('\\', "/");
// The UI compares path strings; a drive letter is case-insensitive.
if s.as_bytes().get(1) == Some(&b':') {
s[..1].make_ascii_uppercase();
}
s
}
#[cfg(not(windows))]
{
p.to_string_lossy().into_owned()
}
}
/// A path field of an IPC type.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, specta::Type)]
#[specta(transparent)]
pub struct IpcPath(#[specta(type = String)] pub PathBuf);
impl IpcPath {
pub fn into_path_buf(self) -> PathBuf {
self.0
}
}
impl Serialize for IpcPath {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&to_ipc(&self.0))
}
}
impl<'de> Deserialize<'de> for IpcPath {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
PathBuf::deserialize(d).map(Self)
}
}
impl Deref for IpcPath {
type Target = Path;
fn deref(&self) -> &Path {
&self.0
}
}
impl AsRef<Path> for IpcPath {
fn as_ref(&self) -> &Path {
&self.0
}
}
impl Borrow<Path> for IpcPath {
fn borrow(&self) -> &Path {
&self.0
}
}
impl From<PathBuf> for IpcPath {
fn from(p: PathBuf) -> Self {
Self(p)
}
}
impl From<&Path> for IpcPath {
fn from(p: &Path) -> Self {
Self(p.to_path_buf())
}
}
impl From<&PathBuf> for IpcPath {
fn from(p: &PathBuf) -> Self {
Self(p.clone())
}
}
impl From<String> for IpcPath {
fn from(p: String) -> Self {
Self(p.into())
}
}
impl From<&str> for IpcPath {
fn from(p: &str) -> Self {
Self(p.into())
}
}
impl From<IpcPath> for PathBuf {
fn from(p: IpcPath) -> Self {
p.0
}
}
impl PartialEq<PathBuf> for IpcPath {
fn eq(&self, other: &PathBuf) -> bool {
self.0 == *other
}
}
impl PartialEq<Path> for IpcPath {
fn eq(&self, other: &Path) -> bool {
self.0 == other
}
}
impl PartialEq<&Path> for IpcPath {
fn eq(&self, other: &&Path) -> bool {
self.0 == *other
}
}
impl PartialEq<IpcPath> for PathBuf {
fn eq(&self, other: &IpcPath) -> bool {
*self == other.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trips_through_json() {
let p = IpcPath::from("a/b");
assert_eq!(serde_json::to_string(&p).unwrap(), r#""a/b""#);
assert_eq!(serde_json::from_str::<IpcPath>(r#""a/b""#).unwrap(), p);
}
#[cfg(windows)]
#[test]
fn windows_paths_use_forward_slashes() {
assert_eq!(to_ipc(Path::new(r"C:\a\b")), "C:/a/b");
assert_eq!(to_ipc(Path::new(r"\\?\C:\a\b")), "C:/a/b");
assert_eq!(to_ipc(&Path::new("C:/a").join("b")), "C:/a/b");
assert_eq!(serde_json::to_string(&IpcPath::from(r"C:\a\b")).unwrap(), r#""C:/a/b""#);
assert_eq!(to_ipc(Path::new(r"c:\a\b")), "C:/a/b");
assert_eq!(to_ipc(Path::new(r"\\?\d:\a")), "D:/a");
assert_eq!(to_ipc(Path::new(r"\\server\share\a")), "//server/share/a");
}
}