Skip to content

Commit e422ea7

Browse files
committed
update rename
1 parent c315491 commit e422ea7

File tree

3 files changed

+88
-40
lines changed

3 files changed

+88
-40
lines changed

crates/emmylua_ls/src/handlers/rename/rename_type.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ fn determine_type_name_for_file(
8888

8989
// 检查引用文件是否声明了相同的命名空间
9090
if let Some(file_namespace) = type_index.get_file_namespace(&reference_file_id) {
91+
if file_namespace == reserved_namespace {
92+
return new_simple_name.to_string();
93+
}
9194
if reserved_namespace.starts_with(&format!("{}.", file_namespace)) {
9295
return new_simple_name.to_string();
9396
}
@@ -96,6 +99,9 @@ fn determine_type_name_for_file(
9699
// 检查引用文件是否使用了相应的命名空间
97100
if let Some(using_namespaces) = type_index.get_file_using_namespace(&reference_file_id) {
98101
for using_namespace in using_namespaces {
102+
if using_namespace == reserved_namespace {
103+
return new_simple_name.to_string();
104+
}
99105
if reserved_namespace.starts_with(&format!("{}.", using_namespace)) {
100106
return new_simple_name.to_string();
101107
}

crates/emmylua_ls/src/handlers/test/rename_test.rs

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,74 @@
11
#[cfg(test)]
22
mod tests {
3+
use lsp_types::WorkspaceEdit;
4+
35
use crate::handlers::test_lib::ProviderVirtualWorkspace;
46

7+
fn check_len(result: &Option<WorkspaceEdit>, len: usize) -> bool {
8+
let Some(result) = result else {
9+
return false;
10+
};
11+
if let Some(changes) = &result.changes {
12+
let mut count = 0;
13+
for (_, edits) in changes {
14+
count += edits.len();
15+
}
16+
if count != len {
17+
return false;
18+
}
19+
}
20+
true
21+
}
22+
23+
/// 检查指定名称的文件的`new_text`是否为预期值
24+
fn check_new_text(result: &Option<WorkspaceEdit>, file_name: &str, new_text: &str) -> bool {
25+
let Some(result) = result else {
26+
return false;
27+
};
28+
if let Some(changes) = &result.changes {
29+
for (uri, edits) in changes {
30+
let path = uri.path().as_str();
31+
if path.ends_with(format!("/{}", file_name).as_str()) {
32+
if let Some(edit) = edits.first() {
33+
if edit.new_text == new_text {
34+
return true;
35+
}
36+
}
37+
}
38+
}
39+
}
40+
false
41+
}
42+
543
#[test]
644
fn test_int_key() {
745
let mut ws = ProviderVirtualWorkspace::new();
8-
assert!(ws.check_rename(
9-
r#"
46+
assert!(check_len(
47+
&ws.check_rename(
48+
r#"
1049
local export = {
1150
[<??>1] = 1,
1251
}
1352
1453
export[1] = 2
1554
"#,
16-
"2".to_string(),
17-
2,
55+
"2".to_string()
56+
),
57+
2
1858
));
1959

20-
assert!(ws.check_rename(
21-
r#"
60+
assert!(check_len(
61+
&ws.check_rename(
62+
r#"
2263
local export = {
2364
[1] = 1,
2465
}
2566
2667
export[<??>1] = 2
2768
"#,
28-
"2".to_string(),
29-
2,
69+
"2".to_string()
70+
),
71+
2
3072
));
3173
}
3274

@@ -42,9 +84,8 @@ mod tests {
4284
Test[1] = 2
4385
"#,
4486
"2".to_string(),
45-
2,
4687
);
47-
assert!(result);
88+
assert!(check_len(&result, 2));
4889
}
4990

5091
#[test]
@@ -64,9 +105,8 @@ mod tests {
64105
end
65106
"#,
66107
"_next".to_string(),
67-
2,
68108
);
69-
assert!(result);
109+
assert!(check_len(&result, 2));
70110
}
71111

72112
#[test]
@@ -79,9 +119,8 @@ mod tests {
79119
---@type Para<??>ms<number>
80120
"#,
81121
"Params1".to_string(),
82-
2,
83122
);
84-
assert!(result);
123+
assert!(check_len(&result, 2));
85124
}
86125

87126
#[test]
@@ -99,9 +138,8 @@ mod tests {
99138
ABC.test()
100139
"#,
101140
"test1".to_string(),
102-
2,
103141
);
104-
assert!(result);
142+
assert!(check_len(&result, 2));
105143
}
106144

107145
#[test]
@@ -116,9 +154,8 @@ mod tests {
116154
end
117155
"#,
118156
"aaa1".to_string(),
119-
3,
120157
);
121-
assert!(result);
158+
assert!(check_len(&result, 3));
122159
}
123160
{
124161
let result = ws.check_rename(
@@ -129,9 +166,8 @@ mod tests {
129166
end
130167
"#,
131168
"aaa1".to_string(),
132-
3,
133169
);
134-
assert!(result);
170+
assert!(check_len(&result, 3));
135171
}
136172
}
137173

@@ -147,14 +183,33 @@ mod tests {
147183
148184
"#,
149185
);
150-
ws.check_rename(
186+
let result = ws.check_rename(
151187
r#"
152188
---@namespace Luakit
153189
---@class Test.Abc<??>
154190
local Test = {}
155191
"#,
156192
"Abc".to_string(),
157-
2,
158193
);
194+
assert!(check_len(&result, 2));
195+
assert!(check_new_text(&result, "a.lua", "Luakit.Abc"));
196+
}
197+
198+
#[test]
199+
fn test_namespace_class_1() {
200+
let mut ws = ProviderVirtualWorkspace::new();
201+
let result = ws.check_rename(
202+
r#"
203+
---@namespace Luakit
204+
---@class Abc
205+
local Test = {}
206+
207+
---@type Abc<??>
208+
local a = Test
209+
"#,
210+
"AAA".to_string(),
211+
);
212+
assert!(check_len(&result, 2));
213+
assert!(check_new_text(&result, "virtual_0.lua", "AAA"));
159214
}
160215
}

crates/emmylua_ls/src/handlers/test_lib/mod.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use emmylua_code_analysis::{EmmyLuaAnalysis, Emmyrc, FileId, VirtualUrlGenerator
44
use lsp_types::{
55
CodeActionResponse, CompletionItemKind, CompletionResponse, CompletionTriggerKind,
66
GotoDefinitionResponse, Hover, HoverContents, InlayHint, MarkupContent, Position,
7-
SemanticTokensResult, SignatureHelpContext, SignatureHelpTriggerKind,
7+
SemanticTokensResult, SignatureHelpContext, SignatureHelpTriggerKind, WorkspaceEdit,
88
};
99
use tokio_util::sync::CancellationToken;
1010

@@ -352,32 +352,19 @@ impl ProviderVirtualWorkspace {
352352
};
353353

354354
let data = serde_json::to_string(&result).unwrap();
355-
dbg!(&data);
355+
// dbg!(&data);
356356
Some(result)
357357
}
358358

359-
pub fn check_rename(&mut self, block_str: &str, new_name: String, len: usize) -> bool {
359+
pub fn check_rename(&mut self, block_str: &str, new_name: String) -> Option<WorkspaceEdit> {
360360
let content = Self::handle_file_content(block_str);
361361
let Some((content, position)) = content else {
362-
return false;
362+
return None;
363363
};
364364
let file_id = self.def(&content);
365365
let result = rename(&self.analysis, file_id, position, new_name.clone());
366-
let Some(result) = result else {
367-
return false;
368-
};
369366
// dbg!(&result);
370-
if let Some(changes) = result.changes {
371-
let mut count = 0;
372-
for (_, edits) in changes {
373-
count += edits.len();
374-
}
375-
if count != len {
376-
return false;
377-
}
378-
}
379-
380-
true
367+
return result;
381368
}
382369

383370
pub fn check_references(&mut self, block_str: &str) -> Option<Vec<lsp_types::Location>> {

0 commit comments

Comments
 (0)