Skip to content

fix: does not works in no workspace mode #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "protols"
description = "Language server for proto3 files"
version = "0.11.1"
version = "0.11.2"
edition = "2024"
license = "MIT"
homepage = "https://github.com/coder3101/protols"
Expand Down
31 changes: 30 additions & 1 deletion src/config/workspace.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::{HashMap, HashSet},
env,
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -62,7 +63,7 @@ impl WorkspaceProtoConfigs {
let wr: ProtolsConfig = basic_toml::from_str(&content).unwrap_or_default();
let fmt = ClangFormatter::new(
&wr.config.path.clang_format,
wpath.to_str().expect("non-utf8 path"),
Some(wpath.to_str().expect("non-utf8 path")),
);

self.workspaces.insert(w.uri.clone());
Expand Down Expand Up @@ -103,6 +104,34 @@ impl WorkspaceProtoConfigs {
ipath.extend_from_slice(&self.protoc_include_prefix);
Some(ipath)
}

pub fn no_workspace_mode(&mut self) {
let wr = ProtolsConfig::default();
let rp = if cfg!(target_os = "windows") {
let mut d = String::from("C");
if let Ok(cdir) = env::current_dir() {
if let Some(drive) = cdir.components().next() {
d = drive.as_os_str().to_string_lossy().to_string()
}
}
format!("{d}://")
} else {
String::from("/")
};
let uri = match Url::from_file_path(&rp) {
Err(err) => {
tracing::error!(?err, "failed to convert path: {rp} to Url");
return;
}
Ok(uri) => uri,
};

let fmt = ClangFormatter::new(&wr.config.path.clang_format, None);

self.workspaces.insert(uri.clone());
self.configs.insert(uri.clone(), wr);
self.formatters.insert(uri.clone(), fmt);
}
}

#[cfg(test)]
Expand Down
10 changes: 6 additions & 4 deletions src/formatter/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::ProtoFormatter;

pub struct ClangFormatter {
pub path: String,
working_dir: String,
working_dir: Option<String>,
temp_dir: TempDir,
}

Expand Down Expand Up @@ -66,11 +66,11 @@ impl Replacement<'_> {
}

impl ClangFormatter {
pub fn new(cmd: &str, wdir: &str) -> Self {
pub fn new(cmd: &str, wdir: Option<&str>) -> Self {
Self {
temp_dir: tempdir().expect("faile to creat temp dir"),
path: cmd.to_owned(),
working_dir: wdir.to_owned(),
working_dir: wdir.map(ToOwned::to_owned),
}
}

Expand All @@ -83,7 +83,9 @@ impl ClangFormatter {

fn get_command(&self, f: &str, u: &Path) -> Option<Command> {
let mut c = Command::new(self.path.as_str());
c.current_dir(self.working_dir.as_str());
if let Some(wd) = &self.working_dir {
c.current_dir(wd.as_str());
}
c.stdin(File::open(u).ok()?);
c.args([
"--output-replacements-xml",
Expand Down
3 changes: 3 additions & 0 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ impl LanguageServer for ProtoLanguageServer {
..Default::default()
}),
})
} else {
tracing::info!("running in no workspace mode");
self.configs.no_workspace_mode()
}

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