Skip to content

Commit af82ec0

Browse files
committed
feat: relative include path uses git root if available
1 parent adda8ae commit af82ec0

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ clang_format_path = "/usr/bin/clang-format" # clang-format binary to execute in
9595

9696
The `[config]` section contains stable settings that should generally remain unchanged.
9797

98-
- `include_paths`: Directories to search for `.proto` files.
98+
- `include_paths`: Directories to search for `.proto` files. Absolute or relative to git root. If git root is unavailble, LSP's workspace is used.
9999
- `disable_parse_diagnostics`: Set to `true` to disable diagnostics during parsing.
100100

101101
#### Experimental Configuration

src/config/workspace.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{
22
collections::{HashMap, HashSet},
33
path::{Path, PathBuf},
4+
process::Command,
45
};
56

67
use async_lsp::lsp_types::{Url, WorkspaceFolder};
@@ -17,10 +18,22 @@ pub struct WorkspaceProtoConfigs {
1718
configs: HashMap<Url, ProtolsConfig>,
1819
formatters: HashMap<Url, ClangFormatter>,
1920
protoc_include_prefix: Vec<PathBuf>,
21+
git_root: Option<PathBuf>,
2022
}
2123

2224
impl WorkspaceProtoConfigs {
2325
pub fn new() -> Self {
26+
let mut git_root = None;
27+
if let Ok(output) = Command::new("git")
28+
.args(["rev-parse", "--show-toplevel"])
29+
.output()
30+
{
31+
if output.status.success() {
32+
let p = String::from_utf8_lossy(&output.stdout).to_string();
33+
git_root = Some(PathBuf::from(p))
34+
}
35+
}
36+
2437
Self {
2538
workspaces: Default::default(),
2639
formatters: Default::default(),
@@ -30,6 +43,7 @@ impl WorkspaceProtoConfigs {
3043
.map(|l| l.include_paths)
3144
.unwrap_or_default(),
3245
configs: Default::default(),
46+
git_root,
3347
}
3448
}
3549

@@ -81,9 +95,18 @@ impl WorkspaceProtoConfigs {
8195
}
8296

8397
pub fn get_include_paths(&self, uri: &Url) -> Option<Vec<PathBuf>> {
84-
let c = self.get_config_for_uri(uri)?;
85-
let w = self.get_workspace_for_uri(uri)?.to_file_path().ok()?;
86-
let mut ipath: Vec<PathBuf> = c
98+
let cfg = self.get_config_for_uri(uri)?;
99+
100+
// How to replace relative paths in include_paths?
101+
// If there's a git root use that else the root will be LSP workspace root
102+
let w = if let Some(root) = &self.git_root {
103+
tracing::info!(?root, "using git root as relative path replacer");
104+
root.clone()
105+
} else {
106+
self.get_workspace_for_uri(uri)?.to_file_path().ok()?
107+
};
108+
109+
let mut ipath: Vec<PathBuf> = cfg
87110
.config
88111
.include_paths
89112
.iter()

src/workspace/hover.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,6 @@ Included from {}"#,
663663

664664
#[cfg(test)]
665665
mod test {
666-
use std::path::PathBuf;
667-
668666
use insta::assert_yaml_snapshot;
669667

670668
use crate::context::hoverable::Hoverables;

0 commit comments

Comments
 (0)