Skip to content

Commit b2ea548

Browse files
committed
update auto_require
1 parent bf09b9b commit b2ea548

File tree

3 files changed

+78
-12
lines changed

3 files changed

+78
-12
lines changed

crates/emmylua_ls/src/handlers/command/commands/emmy_auto_require.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use emmylua_parser::{LuaAstNode, LuaExpr, LuaStat};
55
use lsp_types::{ApplyWorkspaceEditParams, Command, Position, TextEdit, WorkspaceEdit};
66
use serde_json::Value;
77

8-
use crate::{
9-
context::ServerContextSnapshot,
10-
util::{module_name_convert, time_cancel_token},
11-
};
8+
use crate::{context::ServerContextSnapshot, util::time_cancel_token};
129

1310
use super::CommandSpec;
1411

@@ -21,7 +18,8 @@ impl CommandSpec for AutoRequireCommand {
2118
let add_to: FileId = serde_json::from_value(args.first()?.clone()).ok()?;
2219
let need_require_file_id: FileId = serde_json::from_value(args.get(1)?.clone()).ok()?;
2320
let position: Position = serde_json::from_value(args.get(2)?.clone()).ok()?;
24-
let member_name: String = serde_json::from_value(args.get(3)?.clone()).ok()?;
21+
let local_name: String = serde_json::from_value(args.get(3)?.clone()).ok()?;
22+
let member_name: String = serde_json::from_value(args.get(4)?.clone()).ok()?;
2523

2624
let analysis = context.analysis().read().await;
2725
let semantic_model = analysis.compilation.get_semantic_model(add_to)?;
@@ -32,8 +30,6 @@ impl CommandSpec for AutoRequireCommand {
3230
let emmyrc = semantic_model.get_emmyrc();
3331
let require_like_func = &emmyrc.runtime.require_like_function;
3432
let auto_require_func = emmyrc.completion.auto_require_function.clone();
35-
let file_conversion = emmyrc.completion.auto_require_naming_convention;
36-
let local_name = module_name_convert(module_info, file_conversion);
3733
let require_separator = emmyrc.completion.auto_require_separator.clone();
3834
let full_module_path = match require_separator.as_str() {
3935
"." | "" => module_info.full_module_name.clone(),
@@ -196,12 +192,14 @@ pub fn make_auto_require(
196192
add_to: FileId,
197193
need_require_file_id: FileId,
198194
position: Position,
199-
member_name: Option<String>,
195+
local_name: String, // 导入时使用的名称
196+
member_name: Option<String>, // 导入的成员名, 不要包含前缀`.`号, 它将拼接到 `require` 后面. 例如 require("a").member
200197
) -> Command {
201198
let args = vec![
202199
serde_json::to_value(add_to).unwrap(),
203200
serde_json::to_value(need_require_file_id).unwrap(),
204201
serde_json::to_value(position).unwrap(),
202+
serde_json::to_value(local_name).unwrap(),
205203
serde_json::to_value(member_name.unwrap_or_default()).unwrap(),
206204
];
207205

crates/emmylua_ls/src/handlers/completion/providers/auto_require_provider.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ fn add_module_completion_item(
7777

7878
let completion_name = module_name_convert(module_info, file_conversion);
7979
if !completion_name.to_lowercase().starts_with(prefix) {
80-
try_add_member_completion_items(
80+
// 如果模块名不匹配, 则根据导出类型添加完成项
81+
add_completion_item_by_type(
8182
builder,
8283
prefix,
8384
module_info,
@@ -98,7 +99,7 @@ fn add_module_completion_item(
9899
None
99100
};
100101
let completion_item = CompletionItem {
101-
label: completion_name,
102+
label: completion_name.clone(),
102103
kind: Some(lsp_types::CompletionItemKind::MODULE),
103104
label_details: Some(lsp_types::CompletionItemLabelDetails {
104105
detail: Some(format!(" (in {})", module_info.full_module_name)),
@@ -109,6 +110,7 @@ fn add_module_completion_item(
109110
builder.semantic_model.get_file_id(),
110111
module_info.file_id,
111112
position,
113+
completion_name,
112114
None,
113115
)),
114116
data,
@@ -120,7 +122,7 @@ fn add_module_completion_item(
120122
Some(())
121123
}
122124

123-
fn try_add_member_completion_items(
125+
fn add_completion_item_by_type(
124126
builder: &CompletionBuilder,
125127
prefix: &str,
126128
module_info: &ModuleInfo,
@@ -192,7 +194,7 @@ fn try_add_member_completion_items(
192194
};
193195

194196
let completion_item = CompletionItem {
195-
label: key_name,
197+
label: key_name.clone(),
196198
kind: Some(get_completion_kind(&member_info.typ)),
197199
label_details: Some(lsp_types::CompletionItemLabelDetails {
198200
detail: Some(format!(" (in {})", module_info.full_module_name)),
@@ -203,6 +205,7 @@ fn try_add_member_completion_items(
203205
builder.semantic_model.get_file_id(),
204206
module_info.file_id,
205207
position,
208+
key_name,
206209
Some(member_info.key.to_path().to_string()),
207210
)),
208211
data,
@@ -213,6 +216,42 @@ fn try_add_member_completion_items(
213216
}
214217
}
215218
}
219+
LuaType::Signature(_) => {
220+
let semantic_id = module_info.semantic_id.as_ref()?;
221+
if let LuaSemanticDeclId::LuaDecl(decl_id) = semantic_id {
222+
let decl = builder
223+
.semantic_model
224+
.get_db()
225+
.get_decl_index()
226+
.get_decl(&decl_id)?;
227+
let name = decl.get_name();
228+
if name.to_lowercase().starts_with(prefix) {
229+
if builder.env_duplicate_name.contains(name) {
230+
return None;
231+
}
232+
233+
let completion_item = CompletionItem {
234+
label: name.to_string(),
235+
kind: Some(get_completion_kind(&export_type)),
236+
label_details: Some(lsp_types::CompletionItemLabelDetails {
237+
detail: Some(format!(" (in {})", module_info.full_module_name)),
238+
..Default::default()
239+
}),
240+
command: Some(make_auto_require(
241+
"",
242+
builder.semantic_model.get_file_id(),
243+
module_info.file_id,
244+
position,
245+
name.to_string(),
246+
None,
247+
)),
248+
..Default::default()
249+
};
250+
251+
completions.push(completion_item);
252+
}
253+
}
254+
}
216255
_ => {}
217256
}
218257
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,4 +2373,33 @@ mod tests {
23732373

23742374
Ok(())
23752375
}
2376+
2377+
#[gtest]
2378+
fn test_modle_return_signature() -> Result<()> {
2379+
let mut ws = ProviderVirtualWorkspace::new();
2380+
ws.def_file(
2381+
"test.lua",
2382+
r#"
2383+
---@export global
2384+
local function processError()
2385+
return 1
2386+
end
2387+
return processError
2388+
"#,
2389+
);
2390+
2391+
check!(ws.check_completion_with_kind(
2392+
r#"
2393+
processError<??>
2394+
"#,
2395+
vec![VirtualCompletionItem {
2396+
label: "processError".to_string(),
2397+
kind: CompletionItemKind::FUNCTION,
2398+
label_detail: Some(" (in test)".to_string()),
2399+
}],
2400+
CompletionTriggerKind::INVOKED
2401+
));
2402+
2403+
Ok(())
2404+
}
23762405
}

0 commit comments

Comments
 (0)