Skip to content

Commit abdbeb2

Browse files
committed
LSP: Fix VSCode doesn't recognise canonized file paths
1 parent 488c818 commit abdbeb2

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

src/compiler_top.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ impl Linker {
9898
if file_path.is_dir() {
9999
self.add_all_files_in_directory_recurse(&file_path);
100100
} else if file_path.is_file() && file_path.extension() == Some(OsStr::new("sus")) {
101-
let file_identifier = UniqueFileID::from_path(&file_path).unwrap();
101+
let file_identifier =
102+
UniqueFileID::from_path(&file_path, file_path.to_string_lossy().to_string())
103+
.unwrap();
102104
self.add_or_update_file_from_disk(file_identifier);
103105
}
104106
}
@@ -108,7 +110,7 @@ impl Linker {
108110
if path.is_dir() {
109111
self.add_all_files_in_directory_recurse(path);
110112
} else {
111-
match UniqueFileID::from_path(path) {
113+
match UniqueFileID::from_path(path, path.to_string_lossy().to_string()) {
112114
Ok(file_identifier) => {
113115
self.add_or_update_file_from_disk(file_identifier);
114116
}

src/dev_aid/lsp/mod.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,13 @@ fn cvt_location_list_of_lists(
8686
impl UniqueFileID {
8787
fn from_uri(uri: &Url) -> Result<UniqueFileID, String> {
8888
if uri.scheme() == "file" {
89-
UniqueFileID::from_path(&uri.to_file_path().unwrap())
89+
UniqueFileID::from_path(&uri.to_file_path().unwrap(), uri.to_string())
9090
} else {
9191
Ok(UniqueFileID::from_non_path_str(uri.to_string()))
9292
}
9393
}
9494
fn to_uri(&self) -> Url {
95-
if self.inode.is_some() {
96-
Url::from_file_path(&self.name).expect(&self.name)
97-
} else {
98-
Url::parse(&self.name).expect(&self.name)
99-
}
95+
Url::parse(&self.name).expect(&self.name)
10096
}
10197
}
10298

@@ -218,6 +214,14 @@ fn initialize_all_files(linker: &mut Linker, init_params: &InitializeParams) {
218214
linker.add_file_or_directory(f);
219215
}
220216
}
217+
218+
// Convert all the Path IDs to URIs
219+
for (_, f) in &mut linker.files {
220+
f.file_identifier.name =
221+
Url::from_file_path(std::fs::canonicalize(&f.file_identifier.name).unwrap())
222+
.expect(&f.file_identifier.name)
223+
.to_string();
224+
}
221225
}
222226

223227
fn gather_completions(linker: &Linker, position: usize) -> Vec<CompletionItem> {
@@ -562,11 +566,11 @@ fn handle_notification(
562566
linker.add_or_update_file_from_disk(file_identifier);
563567
} else if event.typ == FileChangeType::DELETED {
564568
let uri_as_string = event.uri.to_string();
565-
// Delete URIs that don't have a file backing
566-
if let Some(existing_file_id) = linker.files.find(|_, data| {
567-
data.file_identifier.inode.is_none()
568-
&& data.file_identifier.name == uri_as_string
569-
}) {
569+
// Try to delete URIs (including those that that don't have a file backing)
570+
if let Some(existing_file_id) = linker
571+
.files
572+
.find(|_, data| data.file_identifier.name == uri_as_string)
573+
{
570574
linker.remove_file(existing_file_id);
571575
}
572576
} else {
@@ -577,7 +581,7 @@ fn handle_notification(
577581
let mut to_delete: Vec<FileUUID> = Vec::new();
578582
for (id, f) in &linker.files {
579583
if f.file_identifier.inode.is_some() {
580-
match std::fs::exists(&f.file_identifier.name) {
584+
match std::fs::exists(f.file_identifier.to_uri().path()) {
581585
Ok(true) => {}
582586
Ok(false) | Err(_) => to_delete.push(id),
583587
}

src/linker/mod.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,20 @@ pub struct UniqueFileID {
158158
pub name: String,
159159
}
160160
impl UniqueFileID {
161-
pub fn from_path(path: &Path) -> Result<UniqueFileID, String> {
161+
pub fn from_path(path: &Path, name: String) -> Result<UniqueFileID, String> {
162162
if !path.is_file() || path.extension() != Some(OsStr::new("sus")) {
163-
return Err(format!("{} is not a .sus file!", path.to_string_lossy()));
163+
return Err(format!("{name} is not a .sus file!"));
164164
}
165-
match path.canonicalize() {
166-
Ok(absolute_path) => Ok(UniqueFileID {
167-
inode: Some(same_file::Handle::from_path(path).unwrap()),
168-
name: absolute_path.to_string_lossy().to_string(),
165+
match same_file::Handle::from_path(path) {
166+
Ok(inode) => Ok(UniqueFileID {
167+
inode: Some(inode),
168+
name,
169169
}),
170-
Err(err) => Err(format!(
171-
"'{}' is not an existing file? {err}",
172-
path.to_string_lossy()
173-
)),
170+
Err(err) => Err(format!("'{name}' is not an existing file? {err}",)),
174171
}
175172
}
176-
pub fn from_non_path_str(identifier: String) -> UniqueFileID {
177-
UniqueFileID {
178-
inode: None,
179-
name: identifier,
180-
}
173+
pub fn from_non_path_str(name: String) -> UniqueFileID {
174+
UniqueFileID { inode: None, name }
181175
}
182176
}
183177
impl Display for UniqueFileID {

0 commit comments

Comments
 (0)