Skip to content

Commit 0599127

Browse files
committed
add preferred local alias fix
1 parent b3d565e commit 0599127

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

crates/emmylua_code_analysis/src/diagnostic/checker/code_style/preferred_local_alias.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use emmylua_parser::{
55
LuaSyntaxKind,
66
};
77
use rowan::{NodeOrToken, TextRange};
8+
use serde_json::json;
89

910
use crate::{
1011
DiagnosticCode, LuaDeclId, LuaSemanticDeclId, SemanticDeclLevel, SemanticModel,
@@ -247,7 +248,9 @@ fn check_index_expr_preference(
247248
name = alias_info.preferred_name
248249
)
249250
.to_string(),
250-
None,
251+
Some(json!({
252+
"preferredAlias": alias_info.preferred_name.clone(),
253+
})),
251254
);
252255
}
253256

crates/emmylua_ls/locales/action/zh_CN.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ Do you want to modify the require path?: |
1919
2020
Modify: |
2121
修改
22+
23+
Replace with local alias '%{name}': |
24+
替换为本地变量别名 '%{name}'

crates/emmylua_ls/src/handlers/code_actions/actions/build_fix_code.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,30 @@ pub fn build_add_doc_tag(
7575

7676
Some(())
7777
}
78+
79+
pub fn build_preferred_local_alias_fix(
80+
semantic_model: &SemanticModel,
81+
actions: &mut Vec<CodeActionOrCommand>,
82+
range: Range,
83+
data: &Option<serde_json::Value>,
84+
) -> Option<()> {
85+
let alias_name = data.as_ref()?.get("preferredAlias")?.as_str()?.to_string();
86+
87+
let document = semantic_model.get_document();
88+
let text_edit = TextEdit {
89+
range,
90+
new_text: alias_name.clone(),
91+
};
92+
93+
actions.push(CodeActionOrCommand::CodeAction(CodeAction {
94+
title: t!("Replace with local alias '%{name}'", name = alias_name).to_string(),
95+
kind: Some(CodeActionKind::QUICKFIX),
96+
edit: Some(WorkspaceEdit {
97+
changes: Some(HashMap::from([(document.get_uri(), vec![text_edit])])),
98+
..Default::default()
99+
}),
100+
..Default::default()
101+
}));
102+
103+
Some(())
104+
}

crates/emmylua_ls/src/handlers/code_actions/build_actions.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ use lsp_types::{
88

99
use super::actions::{
1010
build_add_doc_tag, build_disable_file_changes, build_disable_next_line_changes,
11+
build_need_check_nil, build_preferred_local_alias_fix,
1112
};
12-
use crate::handlers::{
13-
code_actions::actions::build_need_check_nil,
14-
command::{DisableAction, make_disable_code_command},
15-
};
13+
use crate::handlers::command::{DisableAction, make_disable_code_command};
1614

1715
pub fn build_actions(
1816
semantic_model: &SemanticModel,
@@ -71,6 +69,9 @@ fn add_fix_code_action(
7169
match diagnostic_code {
7270
DiagnosticCode::NeedCheckNil => build_need_check_nil(semantic_model, actions, range, data),
7371
DiagnosticCode::UnknownDocTag => build_add_doc_tag(semantic_model, actions, range, data),
72+
DiagnosticCode::PreferredLocalAlias => {
73+
build_preferred_local_alias_fix(semantic_model, actions, range, data)
74+
}
7475
_ => Some(()),
7576
}
7677
}

0 commit comments

Comments
 (0)