Skip to content

Commit

Permalink
better deal with uri conversion to file path
Browse files Browse the repository at this point in the history
  • Loading branch information
glehmann committed May 4, 2024
1 parent 3192050 commit 153cdb0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ jobs:
run: ${{ env.CARGO }} build --workspace ${{ env.TARGET_FLAGS }} --release

- name: Run tests
run: ${{ env.CARGO }} test --workspace ${{ env.TARGET_FLAGS }} --release
run: ${{ env.CARGO }} test --workspace --no-fail-fast ${{ env.TARGET_FLAGS }} --release

- uses: actions/upload-artifact@v4
if: contains(fromJSON('["aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-unknown-linux-musl", "x86_64-pc-windows-msvc"]'), matrix.target)
Expand Down
37 changes: 27 additions & 10 deletions src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::time::Instant;

use clean_path::Clean;
Expand Down Expand Up @@ -46,8 +45,8 @@ impl Backend {
}

pub fn load_workspace_docs(&self, dir: &Path) -> error::Result<()> {
let glob_expr = &format!("{}/**/Earthfile", dir.to_string_lossy());
for f in glob::glob(glob_expr).glob_ctx(glob_expr)? {
let glob_expr = dir.join("**").join("Earthfile").to_string_lossy().to_string();
for f in glob::glob(&glob_expr).glob_ctx(&glob_expr)? {
let path = f?;
self.docs.insert(
Url::from_file_path(&path)
Expand All @@ -59,7 +58,8 @@ impl Backend {
}

pub fn match_earthfile_ref(&self, origin: &Url, earthfile_ref: &str) -> Result<Vec<Url>> {
let path = PathBuf::from_str(origin.path())
let path = origin
.to_file_path()
.map_err(|_| request_failed("can't compute the earthfile path"))?;
let path = path
.parent()
Expand All @@ -69,7 +69,13 @@ impl Backend {
.docs
.iter()
.map(|i| i.key().clone())
.flat_map(|uri| if glob_match(&path, uri.path()) { Some(uri) } else { None })
.flat_map(|uri| {
if glob_match(&path, &uri.to_file_path().unwrap().to_string_lossy()) {
Some(uri)
} else {
None
}
})
.collect())
}
}
Expand All @@ -81,17 +87,28 @@ impl LanguageServer for Backend {
// store the workspaces locations
if let Some(workspaces) = params.workspace_folders {
for workspace in workspaces {
if workspace.uri.scheme() == "file" {
self.workspaces
.insert(workspace.name, PathBuf::from_str(workspace.uri.path()).unwrap());
} else {
if workspace.uri.scheme() != "file" {
self.client
.log_message(MessageType::ERROR, "Unsupported workspace scheme")
.await;
}
if let Ok(path) = workspace.uri.to_file_path() {
self.workspaces.insert(workspace.name, path);
} else {
self.client
.log_message(
MessageType::ERROR,
"Can't convert the workspace URI to file path",
)
.await;
}
}
} else if let Some(root) = params.root_uri {
self.workspaces.insert("default".into(), PathBuf::from_str(root.path()).unwrap());
self.workspaces.insert(
"default".into(),
root.to_file_path()
.map_err(|_| request_failed("can't compute the earthfile path"))?,
);
// } else if let Some(root) = params.root_path {
// self.workspaces.insert("default".into(), PathBuf::from_str(&root).unwrap());
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/references.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, str::FromStr, sync::OnceLock};
use std::sync::OnceLock;

use clean_path::Clean;
use tower_lsp::{jsonrpc::Result, lsp_types::*};
Expand Down Expand Up @@ -100,8 +100,8 @@ fn get_target(backend: &Backend, params: &ReferenceParams) -> Result<Option<(Url
let earthfile_capture = m.captures.iter().find(|c| c.index == target_earthfile_idx);
let target_uri = if let Some(earthfile_capture) = earthfile_capture {
let earthfile = doc.node_content(earthfile_capture.node);
let path = PathBuf::from_str(uri.path())
.map_err(|_| request_failed("can't compute the earthfile path"))?;
let path =
uri.to_file_path().map_err(|_| request_failed("can't compute the earthfile path"))?;
let path = path
.parent()
.ok_or_else(|| request_failed("can't compute the current Earthfile parent"))?;
Expand Down
13 changes: 8 additions & 5 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{path::PathBuf, str::FromStr};

use clean_path::Clean;
use glob_match::glob_match;
use ropey::RopeSlice;
Expand Down Expand Up @@ -45,11 +43,16 @@ pub fn request_failed(msg: &str) -> Error {
}

pub fn is_earthfile_ref_match(origin: &Url, earthfile_ref: &str, target_uri: &Url) -> Result<bool> {
let path = PathBuf::from_str(origin.path())
.map_err(|_| request_failed("can't compute the earthfile path"))?;
let path =
origin.to_file_path().map_err(|_| request_failed("can't compute the earthfile path"))?;
let path = path
.parent()
.ok_or_else(|| request_failed("can't compute the current Earthfile parent"))?;
let path = path.join(earthfile_ref).join("Earthfile").clean().to_string_lossy().to_string();
Ok(glob_match(&path, target_uri.path()))
let target_path = target_uri
.to_file_path()
.map_err(|_| request_failed("can't compute the earthfile path"))?
.to_string_lossy()
.to_string();
Ok(glob_match(&path, &target_path))
}

0 comments on commit 153cdb0

Please sign in to comment.