Skip to content

Commit 9b14d74

Browse files
committed
fix: does not works in no workspace mode
1 parent 1a4e4d1 commit 9b14d74

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl WorkspaceProtoConfigs {
6262
let wr: ProtolsConfig = basic_toml::from_str(&content).unwrap_or_default();
6363
let fmt = ClangFormatter::new(
6464
&wr.config.path.clang_format,
65-
wpath.to_str().expect("non-utf8 path"),
65+
Some(wpath.to_str().expect("non-utf8 path")),
6666
);
6767

6868
self.workspaces.insert(w.uri.clone());
@@ -103,6 +103,23 @@ impl WorkspaceProtoConfigs {
103103
ipath.extend_from_slice(&self.protoc_include_prefix);
104104
Some(ipath)
105105
}
106+
107+
pub fn no_workspace_mode(&mut self) {
108+
let wr = ProtolsConfig::default();
109+
let uri = match Url::from_file_path("/") {
110+
Err(err) => {
111+
tracing::error!(?err, "failed to convert path: '/' to Url");
112+
return;
113+
}
114+
Ok(uri) => uri,
115+
};
116+
117+
let fmt = ClangFormatter::new(&wr.config.path.clang_format, None);
118+
119+
self.workspaces.insert(uri.clone());
120+
self.configs.insert(uri.clone(), wr);
121+
self.formatters.insert(uri.clone(), fmt);
122+
}
106123
}
107124

108125
#[cfg(test)]

src/formatter/clang.rs

Lines changed: 6 additions & 4 deletions
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

Lines changed: 3 additions & 0 deletions
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)