Skip to content

Commit e0ed10b

Browse files
committed
fix: reference and rename in file missing pkg
1 parent 6850af1 commit e0ed10b

File tree

5 files changed

+70
-23
lines changed

5 files changed

+70
-23
lines changed

sample/test.proto

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ message SomeMessage {
1010
CustomType another = 2;
1111
}
1212

13-
message A {
13+
message CapitalA {
1414
// B is a b
15-
message B {
15+
message CapitalB {
1616

1717
}
1818

19-
B b = 1;
19+
a.b.c.CapitalA.CapitalB b = 1;
2020
}
2121

2222
message C {
23-
A.B ab = 1;
24-
A a = 2;
23+
CapitalA.CapitalB ab = 1;
24+
.a.b.c.CapitalA a = 2;
2525
}

src/config/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ pub struct ProtolsConfig {
1717
}
1818

1919
#[derive(Serialize, Deserialize, Debug, Clone)]
20-
pub struct FormatterConfig {
21-
}
20+
pub struct FormatterConfig {}
2221

2322
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
2423
#[serde(default)]
@@ -38,7 +37,7 @@ impl Default for PathConfig {
3837
fn default() -> Self {
3938
Self {
4039
clang_format: default_clang_format_path(),
41-
protoc: default_protoc_path()
40+
protoc: default_protoc_path(),
4241
}
4342
}
4443
}

src/config/workspace.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ impl WorkspaceProtoConfigs {
149149
mod test {
150150
use async_lsp::lsp_types::{Url, WorkspaceFolder};
151151
use insta::assert_yaml_snapshot;
152-
use tempfile::tempdir;
153152
use std::path::PathBuf;
153+
use tempfile::tempdir;
154154

155155
use super::{CONFIG_FILE_NAMES, WorkspaceProtoConfigs};
156156

@@ -263,13 +263,16 @@ mod test {
263263
let include_paths = ws.get_include_paths(&inworkspace).unwrap();
264264

265265
// Check that CLI paths are included in the result
266-
assert!(include_paths.iter().any(|p| p.ends_with("relative/path") ||
267-
p == &PathBuf::from("/path/to/protos")));
268-
266+
assert!(
267+
include_paths
268+
.iter()
269+
.any(|p| p.ends_with("relative/path") || p == &PathBuf::from("/path/to/protos"))
270+
);
271+
269272
// The relative path should be resolved relative to the workspace
270273
let resolved_relative_path = tmpdir.path().join("relative/path");
271274
assert!(include_paths.contains(&resolved_relative_path));
272-
275+
273276
// The absolute path should be included as is
274277
assert!(include_paths.contains(&PathBuf::from("/path/to/protos")));
275278
}

src/state.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ impl ProtoLanguageState {
6767
.values()
6868
.filter(|tree| {
6969
let content = self.get_content(&tree.uri);
70-
tree.get_package_name(content.as_bytes())
71-
.unwrap_or(".")
72-
== package
70+
tree.get_package_name(content.as_bytes()).unwrap_or(".") == package
7371
})
7472
.map(ToOwned::to_owned)
7573
.collect()

src/workspace/rename.rs

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,39 @@ impl ProtoLanguageState {
2626
let package = tree.get_package_name(content.as_ref()).unwrap_or(".");
2727
let mut old = identifier.to_string();
2828
let mut new = new_text.to_string();
29-
if current_package != package {
30-
old = format!("{current_package}.{old}");
31-
new = format!("{current_package}.{new}");
29+
let mut v = vec![];
30+
31+
// Global scope: Reference by only . or within global directly
32+
if current_package == "." {
33+
if package == "." {
34+
v.extend(tree.rename_field(&old, &new, content.as_str()));
35+
}
36+
37+
old = format!(".{old}");
38+
new = format!(".{new}");
39+
40+
v.extend(tree.rename_field(&old, &new, content.as_str()));
41+
42+
if !v.is_empty() {
43+
h.insert(tree.uri.clone(), v);
44+
}
45+
return h;
3246
}
33-
let v = tree.rename_field(&old, &new, content.as_str());
47+
48+
let full_old = format!("{current_package}.{old}");
49+
let full_new = format!("{current_package}.{new}");
50+
let global_full_old = format!(".{current_package}.{old}");
51+
let global_full_new = format!(".{current_package}.{new}");
52+
53+
// Current package: Reference by full or relative name or directly
54+
if current_package == package {
55+
v.extend(tree.rename_field(&old, &new, content.as_str()));
56+
}
57+
58+
// Otherwise, full reference
59+
v.extend(tree.rename_field(&full_old, &full_new, content.as_str()));
60+
v.extend(tree.rename_field(&global_full_old, &global_full_new, content.as_str()));
61+
3462
if !v.is_empty() {
3563
h.insert(tree.uri.clone(), v);
3664
}
@@ -54,10 +82,29 @@ impl ProtoLanguageState {
5482
let content = self.get_content(&tree.uri);
5583
let package = tree.get_package_name(content.as_ref()).unwrap_or(".");
5684
let mut old = identifier.to_owned();
57-
if current_package != package {
58-
old = format!("{current_package}.{old}");
85+
// Global scope: Reference by only . or within global directly
86+
if current_package == "." {
87+
if package == "." {
88+
v.extend(tree.reference_field(&old, content.as_str()));
89+
}
90+
91+
old = format!(".{old}");
92+
v.extend(tree.reference_field(&old, content.as_str()));
93+
94+
return v;
5995
}
60-
v.extend(tree.reference_field(&old, content.as_str()));
96+
97+
let full_old = format!("{current_package}.{old}");
98+
let global_full_old = format!(".{current_package}.{old}");
99+
100+
// Current package: Reference by full or relative name or directly
101+
if current_package == package {
102+
v.extend(tree.reference_field(&old, content.as_str()));
103+
}
104+
105+
// Otherwise, full reference
106+
v.extend(tree.reference_field(&full_old, content.as_str()));
107+
v.extend(tree.reference_field(&global_full_old, content.as_str()));
61108
v
62109
});
63110
if r.is_empty() { None } else { Some(r) }

0 commit comments

Comments
 (0)