From 9508e9791495f6e6f4c64b330a6cbba37241fafc Mon Sep 17 00:00:00 2001 From: liv Date: Wed, 17 May 2023 16:14:10 +0200 Subject: [PATCH] feat: write absolute root module paths for lsp --- src/project.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/project.rs b/src/project.rs index 6e48350..7865d92 100644 --- a/src/project.rs +++ b/src/project.rs @@ -1,8 +1,9 @@ use glob::glob; use serde::{Deserialize, Serialize}; -use std::env; use std::error::Error; +use std::path::PathBuf; use std::process::Command; +use std::{env, fs}; /// Contains the structure of resulting rust-project.json file /// and functions to build the data required to create the file @@ -38,11 +39,12 @@ impl RustAnalyzerProject { } /// If path contains .rs extension, add a crate to `rust-project.json` - fn path_to_json(&mut self, path: String) { - if let Some((_, ext)) = path.split_once('.') { + fn path_to_json(&mut self, path: PathBuf) -> Result<(), Box> { + if let Some(ext) = path.extension() { if ext == "rs" { + let abspath = fs::canonicalize(path)?; self.crates.push(Crate { - root_module: path, + root_module: abspath.display().to_string(), edition: "2021".to_string(), deps: Vec::new(), // This allows rust_analyzer to work inside #[test] blocks @@ -50,15 +52,16 @@ impl RustAnalyzerProject { }) } } + + Ok(()) } /// Parse the exercises folder for .rs files, any matches will create /// a new `crate` in rust-project.json which allows rust-analyzer to /// treat it like a normal binary pub fn exercises_to_json(&mut self) -> Result<(), Box> { - for e in glob("./exercises/**/*")? { - let path = e?.to_string_lossy().to_string(); - self.path_to_json(path); + for path in glob("./exercises/**/*")? { + self.path_to_json(path?)?; } Ok(()) }