Skip to content

Commit 9508e97

Browse files
author
liv
committed
feat: write absolute root module paths for lsp
1 parent fd84c2d commit 9508e97

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/project.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use glob::glob;
22
use serde::{Deserialize, Serialize};
3-
use std::env;
43
use std::error::Error;
4+
use std::path::PathBuf;
55
use std::process::Command;
6+
use std::{env, fs};
67

78
/// Contains the structure of resulting rust-project.json file
89
/// and functions to build the data required to create the file
@@ -38,27 +39,29 @@ impl RustAnalyzerProject {
3839
}
3940

4041
/// If path contains .rs extension, add a crate to `rust-project.json`
41-
fn path_to_json(&mut self, path: String) {
42-
if let Some((_, ext)) = path.split_once('.') {
42+
fn path_to_json(&mut self, path: PathBuf) -> Result<(), Box<dyn Error>> {
43+
if let Some(ext) = path.extension() {
4344
if ext == "rs" {
45+
let abspath = fs::canonicalize(path)?;
4446
self.crates.push(Crate {
45-
root_module: path,
47+
root_module: abspath.display().to_string(),
4648
edition: "2021".to_string(),
4749
deps: Vec::new(),
4850
// This allows rust_analyzer to work inside #[test] blocks
4951
cfg: vec!["test".to_string()],
5052
})
5153
}
5254
}
55+
56+
Ok(())
5357
}
5458

5559
/// Parse the exercises folder for .rs files, any matches will create
5660
/// a new `crate` in rust-project.json which allows rust-analyzer to
5761
/// treat it like a normal binary
5862
pub fn exercises_to_json(&mut self) -> Result<(), Box<dyn Error>> {
59-
for e in glob("./exercises/**/*")? {
60-
let path = e?.to_string_lossy().to_string();
61-
self.path_to_json(path);
63+
for path in glob("./exercises/**/*")? {
64+
self.path_to_json(path?)?;
6265
}
6366
Ok(())
6467
}

0 commit comments

Comments
 (0)