feat: write absolute root module paths for lsp

This commit is contained in:
liv 2023-05-17 16:14:10 +02:00
parent fd84c2d8f7
commit 9508e97914

View File

@ -1,8 +1,9 @@
use glob::glob; use glob::glob;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::env;
use std::error::Error; use std::error::Error;
use std::path::PathBuf;
use std::process::Command; use std::process::Command;
use std::{env, fs};
/// Contains the structure of resulting rust-project.json file /// Contains the structure of resulting rust-project.json file
/// and functions to build the data required to create the 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` /// If path contains .rs extension, add a crate to `rust-project.json`
fn path_to_json(&mut self, path: String) { fn path_to_json(&mut self, path: PathBuf) -> Result<(), Box<dyn Error>> {
if let Some((_, ext)) = path.split_once('.') { if let Some(ext) = path.extension() {
if ext == "rs" { if ext == "rs" {
let abspath = fs::canonicalize(path)?;
self.crates.push(Crate { self.crates.push(Crate {
root_module: path, root_module: abspath.display().to_string(),
edition: "2021".to_string(), edition: "2021".to_string(),
deps: Vec::new(), deps: Vec::new(),
// This allows rust_analyzer to work inside #[test] blocks // 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 /// Parse the exercises folder for .rs files, any matches will create
/// a new `crate` in rust-project.json which allows rust-analyzer to /// a new `crate` in rust-project.json which allows rust-analyzer to
/// treat it like a normal binary /// treat it like a normal binary
pub fn exercises_to_json(&mut self) -> Result<(), Box<dyn Error>> { pub fn exercises_to_json(&mut self) -> Result<(), Box<dyn Error>> {
for e in glob("./exercises/**/*")? { for path in glob("./exercises/**/*")? {
let path = e?.to_string_lossy().to_string(); self.path_to_json(path?)?;
self.path_to_json(path);
} }
Ok(()) Ok(())
} }