Skip to content

Commit

Permalink
lsp_types: Make Url::from_file/directory_path infallible
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
the-mikedavis committed Oct 14, 2024
1 parent 5e6cb2c commit fd24a19
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
12 changes: 5 additions & 7 deletions helix-lsp-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use serde_json::Value;
pub struct Url(String);

impl Url {
#[allow(clippy::result_unit_err)]
pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Self, ()> {
pub fn from_file_path<P: AsRef<Path>>(path: P) -> Self {
use percent_encoding::{percent_encode, AsciiSet, CONTROLS};
#[cfg(any(unix, target_os = "redox"))]
use std::os::unix::prelude::OsStrExt;
Expand Down Expand Up @@ -60,16 +59,15 @@ impl Url {
// An URL's path must not be empty.
serialization.push('/');
}
Ok(Self(serialization))
Self(serialization)
}

#[allow(clippy::result_unit_err)]
pub fn from_directory_path<P: AsRef<Path>>(path: P) -> Result<Self, ()> {
let Self(mut serialization) = Self::from_file_path(path)?;
pub fn from_directory_path<P: AsRef<Path>>(path: P) -> Self {
let Self(mut serialization) = Self::from_file_path(path);
if !serialization.ends_with('/') {
serialization.push('/');
}
Ok(Self(serialization))
Self(serialization)
}

/// Returns the serialized representation of the URL as a `&str`
Expand Down
19 changes: 8 additions & 11 deletions helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ fn workspace_for_path(path: &Path) -> WorkspaceFolder {

lsp::WorkspaceFolder {
name,
uri: lsp::Url::from_directory_path(path)
.expect("absolute paths can be converted to `Url`s"),
uri: lsp::Url::from_directory_path(path),
}
}

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

let workspace_folders = root.clone().into_iter().collect();
let root_uri = root.clone().map(|root| {
lsp::Url::from_file_path(root).expect("absolute paths can be converted to `Url`s")
});
let root_uri = root.clone().map(lsp::Url::from_file_path);
// `root_uri` and `workspace_folder` can be empty in case there is no workspace
// `root_url` can not, use `workspace` as a fallback
let root_path = root.unwrap_or(workspace);
Expand Down Expand Up @@ -743,11 +740,11 @@ impl Client {
} else {
Url::from_file_path(path)
};
Some(url.ok()?.into_string())
url.into_string()
};
let files = vec![lsp::FileRename {
old_uri: url_from_path(old_path)?,
new_uri: url_from_path(new_path)?,
old_uri: url_from_path(old_path),
new_uri: url_from_path(new_path),
}];
let request = self.call_with_timeout::<lsp::request::WillRenameFiles>(
&lsp::RenameFilesParams { files },
Expand Down Expand Up @@ -777,12 +774,12 @@ impl Client {
} else {
Url::from_file_path(path)
};
Some(url.ok()?.into_string())
url.into_string()
};

let files = vec![lsp::FileRename {
old_uri: url_from_path(old_path)?,
new_uri: url_from_path(new_path)?,
old_uri: url_from_path(old_path),
new_uri: url_from_path(new_path),
}];
Some(self.notify::<lsp::notification::DidRenameFiles>(lsp::RenameFilesParams { files }))
}
Expand Down
5 changes: 1 addition & 4 deletions helix-lsp/src/file_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,13 @@ impl Handler {
log::warn!("LSP client was dropped: {id}");
return false;
};
let Ok(uri) = lsp::Url::from_file_path(&path) else {
return true;
};
log::debug!(
"Sending didChangeWatchedFiles notification to client '{}'",
client.name()
);
if let Err(err) = crate::block_on(client
.did_change_watched_files(vec![lsp::FileEvent {
uri,
uri: lsp::Url::from_file_path(&path),
// We currently always send the CHANGED state
// since we don't actually have more context at
// the moment.
Expand Down
2 changes: 1 addition & 1 deletion helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1811,7 +1811,7 @@ impl Document {

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

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

0 comments on commit fd24a19

Please sign in to comment.