Skip to content

Commit

Permalink
Merge pull request #11486 from helix-editor/lsp-location-refactor
Browse files Browse the repository at this point in the history
Replace uses of `lsp::Location` with a custom Location type
  • Loading branch information
pascalkuthe authored Oct 4, 2024
2 parents 02b6f14 + da2b0a7 commit 162028d
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 155 deletions.
41 changes: 22 additions & 19 deletions helix-core/src/uri.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use std::path::{Path, PathBuf};
use std::{
fmt,
path::{Path, PathBuf},
sync::Arc,
};

/// A generic pointer to a file location.
///
/// Currently this type only supports paths to local files.
///
/// Cloning this type is cheap: the internal representation uses an Arc.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Uri {
File(PathBuf),
File(Arc<Path>),
}

impl Uri {
Expand All @@ -23,26 +29,18 @@ impl Uri {
Self::File(path) => Some(path),
}
}

pub fn as_path_buf(self) -> Option<PathBuf> {
match self {
Self::File(path) => Some(path),
}
}
}

impl From<PathBuf> for Uri {
fn from(path: PathBuf) -> Self {
Self::File(path)
Self::File(path.into())
}
}

impl TryFrom<Uri> for PathBuf {
type Error = ();

fn try_from(uri: Uri) -> Result<Self, Self::Error> {
match uri {
Uri::File(path) => Ok(path),
impl fmt::Display for Uri {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::File(path) => write!(f, "{}", path.display()),
}
}
}
Expand All @@ -59,11 +57,16 @@ pub enum UrlConversionErrorKind {
UnableToConvert,
}

impl std::fmt::Display for UrlConversionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for UrlConversionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.kind {
UrlConversionErrorKind::UnsupportedScheme => {
write!(f, "unsupported scheme in URL: {}", self.source.scheme())
write!(
f,
"unsupported scheme '{}' in URL {}",
self.source.scheme(),
self.source
)
}
UrlConversionErrorKind::UnableToConvert => {
write!(f, "unable to convert URL to file path: {}", self.source)
Expand All @@ -77,7 +80,7 @@ impl std::error::Error for UrlConversionError {}
fn convert_url_to_uri(url: &url::Url) -> Result<Uri, UrlConversionErrorKind> {
if url.scheme() == "file" {
url.to_file_path()
.map(|path| Uri::File(helix_stdx::path::normalize(path)))
.map(|path| Uri::File(helix_stdx::path::normalize(path).into()))
.map_err(|_| UrlConversionErrorKind::UnableToConvert)
} else {
Err(UrlConversionErrorKind::UnsupportedScheme)
Expand Down
Loading

0 comments on commit 162028d

Please sign in to comment.