Skip to content

Commit 7594ff7

Browse files
fix: does not works in no workspace mode (#69)
closes #68
1 parent 1a4e4d1 commit 7594ff7

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "protols"
33
description = "Language server for proto3 files"
4-
version = "0.11.1"
4+
version = "0.11.2"
55
edition = "2024"
66
license = "MIT"
77
homepage = "https://github.com/coder3101/protols"

src/config/workspace.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{
22
collections::{HashMap, HashSet},
3+
env,
34
path::{Path, PathBuf},
45
};
56

@@ -62,7 +63,7 @@ impl WorkspaceProtoConfigs {
6263
let wr: ProtolsConfig = basic_toml::from_str(&content).unwrap_or_default();
6364
let fmt = ClangFormatter::new(
6465
&wr.config.path.clang_format,
65-
wpath.to_str().expect("non-utf8 path"),
66+
Some(wpath.to_str().expect("non-utf8 path")),
6667
);
6768

6869
self.workspaces.insert(w.uri.clone());
@@ -103,6 +104,34 @@ impl WorkspaceProtoConfigs {
103104
ipath.extend_from_slice(&self.protoc_include_prefix);
104105
Some(ipath)
105106
}
107+
108+
pub fn no_workspace_mode(&mut self) {
109+
let wr = ProtolsConfig::default();
110+
let rp = if cfg!(target_os = "windows") {
111+
let mut d = String::from("C");
112+
if let Ok(cdir) = env::current_dir() {
113+
if let Some(drive) = cdir.components().next() {
114+
d = drive.as_os_str().to_string_lossy().to_string()
115+
}
116+
}
117+
format!("{d}://")
118+
} else {
119+
String::from("/")
120+
};
121+
let uri = match Url::from_file_path(&rp) {
122+
Err(err) => {
123+
tracing::error!(?err, "failed to convert path: {rp} to Url");
124+
return;
125+
}
126+
Ok(uri) => uri,
127+
};
128+
129+
let fmt = ClangFormatter::new(&wr.config.path.clang_format, None);
130+
131+
self.workspaces.insert(uri.clone());
132+
self.configs.insert(uri.clone(), wr);
133+
self.formatters.insert(uri.clone(), fmt);
134+
}
106135
}
107136

108137
#[cfg(test)]

src/formatter/clang.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use super::ProtoFormatter;
1616

1717
pub struct ClangFormatter {
1818
pub path: String,
19-
working_dir: String,
19+
working_dir: Option<String>,
2020
temp_dir: TempDir,
2121
}
2222

@@ -66,11 +66,11 @@ impl Replacement<'_> {
6666
}
6767

6868
impl ClangFormatter {
69-
pub fn new(cmd: &str, wdir: &str) -> Self {
69+
pub fn new(cmd: &str, wdir: Option<&str>) -> Self {
7070
Self {
7171
temp_dir: tempdir().expect("faile to creat temp dir"),
7272
path: cmd.to_owned(),
73-
working_dir: wdir.to_owned(),
73+
working_dir: wdir.map(ToOwned::to_owned),
7474
}
7575
}
7676

@@ -83,7 +83,9 @@ impl ClangFormatter {
8383

8484
fn get_command(&self, f: &str, u: &Path) -> Option<Command> {
8585
let mut c = Command::new(self.path.as_str());
86-
c.current_dir(self.working_dir.as_str());
86+
if let Some(wd) = &self.working_dir {
87+
c.current_dir(wd.as_str());
88+
}
8789
c.stdin(File::open(u).ok()?);
8890
c.args([
8991
"--output-replacements-xml",

src/lsp.rs

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ impl LanguageServer for ProtoLanguageServer {
7474
..Default::default()
7575
}),
7676
})
77+
} else {
78+
tracing::info!("running in no workspace mode");
79+
self.configs.no_workspace_mode()
7780
}
7881

7982
let mut rename_provider: OneOf<bool, RenameOptions> = OneOf::Left(true);

0 commit comments

Comments
 (0)