Skip to content

Commit fd24a19

Browse files
committed
lsp_types: Make Url::from_file/directory_path infallible
These functions return `Result<Self, ()>` in the `url` crate but the result is unnecessary since the functions never return the error branch. We can eliminate the Result to remove some `expect`s in the calling code.
1 parent 5e6cb2c commit fd24a19

File tree

4 files changed

+15
-23
lines changed

4 files changed

+15
-23
lines changed

helix-lsp-types/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ use serde_json::Value;
1818
pub struct Url(String);
1919

2020
impl Url {
21-
#[allow(clippy::result_unit_err)]
22-
pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Self, ()> {
21+
pub fn from_file_path<P: AsRef<Path>>(path: P) -> Self {
2322
use percent_encoding::{percent_encode, AsciiSet, CONTROLS};
2423
#[cfg(any(unix, target_os = "redox"))]
2524
use std::os::unix::prelude::OsStrExt;
@@ -60,16 +59,15 @@ impl Url {
6059
// An URL's path must not be empty.
6160
serialization.push('/');
6261
}
63-
Ok(Self(serialization))
62+
Self(serialization)
6463
}
6564

66-
#[allow(clippy::result_unit_err)]
67-
pub fn from_directory_path<P: AsRef<Path>>(path: P) -> Result<Self, ()> {
68-
let Self(mut serialization) = Self::from_file_path(path)?;
65+
pub fn from_directory_path<P: AsRef<Path>>(path: P) -> Self {
66+
let Self(mut serialization) = Self::from_file_path(path);
6967
if !serialization.ends_with('/') {
7068
serialization.push('/');
7169
}
72-
Ok(Self(serialization))
70+
Self(serialization)
7371
}
7472

7573
/// Returns the serialized representation of the URL as a `&str`

helix-lsp/src/client.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ fn workspace_for_path(path: &Path) -> WorkspaceFolder {
4242

4343
lsp::WorkspaceFolder {
4444
name,
45-
uri: lsp::Url::from_directory_path(path)
46-
.expect("absolute paths can be converted to `Url`s"),
45+
uri: lsp::Url::from_directory_path(path),
4746
}
4847
}
4948

@@ -203,9 +202,7 @@ impl Client {
203202
Transport::start(reader, writer, stderr, id, name.clone());
204203

205204
let workspace_folders = root.clone().into_iter().collect();
206-
let root_uri = root.clone().map(|root| {
207-
lsp::Url::from_file_path(root).expect("absolute paths can be converted to `Url`s")
208-
});
205+
let root_uri = root.clone().map(lsp::Url::from_file_path);
209206
// `root_uri` and `workspace_folder` can be empty in case there is no workspace
210207
// `root_url` can not, use `workspace` as a fallback
211208
let root_path = root.unwrap_or(workspace);
@@ -743,11 +740,11 @@ impl Client {
743740
} else {
744741
Url::from_file_path(path)
745742
};
746-
Some(url.ok()?.into_string())
743+
url.into_string()
747744
};
748745
let files = vec![lsp::FileRename {
749-
old_uri: url_from_path(old_path)?,
750-
new_uri: url_from_path(new_path)?,
746+
old_uri: url_from_path(old_path),
747+
new_uri: url_from_path(new_path),
751748
}];
752749
let request = self.call_with_timeout::<lsp::request::WillRenameFiles>(
753750
&lsp::RenameFilesParams { files },
@@ -777,12 +774,12 @@ impl Client {
777774
} else {
778775
Url::from_file_path(path)
779776
};
780-
Some(url.ok()?.into_string())
777+
url.into_string()
781778
};
782779

783780
let files = vec![lsp::FileRename {
784-
old_uri: url_from_path(old_path)?,
785-
new_uri: url_from_path(new_path)?,
781+
old_uri: url_from_path(old_path),
782+
new_uri: url_from_path(new_path),
786783
}];
787784
Some(self.notify::<lsp::notification::DidRenameFiles>(lsp::RenameFilesParams { files }))
788785
}

helix-lsp/src/file_event.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,13 @@ impl Handler {
106106
log::warn!("LSP client was dropped: {id}");
107107
return false;
108108
};
109-
let Ok(uri) = lsp::Url::from_file_path(&path) else {
110-
return true;
111-
};
112109
log::debug!(
113110
"Sending didChangeWatchedFiles notification to client '{}'",
114111
client.name()
115112
);
116113
if let Err(err) = crate::block_on(client
117114
.did_change_watched_files(vec![lsp::FileEvent {
118-
uri,
115+
uri: lsp::Url::from_file_path(&path),
119116
// We currently always send the CHANGED state
120117
// since we don't actually have more context at
121118
// the moment.

helix-view/src/document.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ impl Document {
18111811

18121812
/// File path as a URL.
18131813
pub fn url(&self) -> Option<lsp::Url> {
1814-
lsp::Url::from_file_path(self.path()?).ok()
1814+
self.path().map(lsp::Url::from_file_path)
18151815
}
18161816

18171817
pub fn uri(&self) -> Option<helix_core::Uri> {

0 commit comments

Comments
 (0)