Skip to content

Commit 6fc07b3

Browse files
committed
support config: codeAction.insertSpace
Close EmmyLuaLs#475
1 parent 801b75e commit 6fc07b3

5 files changed

Lines changed: 67 additions & 5 deletions

File tree

crates/emmylua_code_analysis/resources/schema.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
"null"
1010
]
1111
},
12+
"codeAction": {
13+
"default": {
14+
"insertSpace": false
15+
},
16+
"allOf": [
17+
{
18+
"$ref": "#/definitions/EmmyrcCodeAction"
19+
}
20+
]
21+
},
1222
"codeLens": {
1323
"default": {
1424
"enable": true
@@ -524,6 +534,16 @@
524534
}
525535
]
526536
},
537+
"EmmyrcCodeAction": {
538+
"type": "object",
539+
"properties": {
540+
"insertSpace": {
541+
"description": "Whether to insert space after '---'",
542+
"default": false,
543+
"type": "boolean"
544+
}
545+
}
546+
},
527547
"EmmyrcCodeLen": {
528548
"type": "object",
529549
"properties": {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use schemars::JsonSchema;
2+
use serde::{Deserialize, Serialize};
3+
4+
#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone)]
5+
#[serde(rename_all = "camelCase")]
6+
pub struct EmmyrcCodeAction {
7+
/// Whether to insert space after '---'
8+
#[serde(default = "default_false")]
9+
pub insert_space: bool,
10+
}
11+
12+
impl Default for EmmyrcCodeAction {
13+
fn default() -> Self {
14+
Self {
15+
insert_space: default_false(),
16+
}
17+
}
18+
}
19+
20+
fn default_false() -> bool {
21+
false
22+
}

crates/emmylua_code_analysis/src/config/configs/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod code_action;
12
mod codelen;
23
mod completion;
34
mod diagnostics;
@@ -12,6 +13,7 @@ mod signature;
1213
mod strict;
1314
mod workspace;
1415

16+
pub use code_action::EmmyrcCodeAction;
1517
pub use codelen::EmmyrcCodeLen;
1618
pub use completion::{EmmyrcCompletion, EmmyrcFilenameConvention};
1719
pub use diagnostics::EmmyrcDiagnostic;

crates/emmylua_code_analysis/src/config/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use std::{
88
};
99

1010
pub use config_loader::load_configs;
11-
use configs::EmmyrcDocumentColor;
1211
pub use configs::EmmyrcFilenameConvention;
1312
pub use configs::EmmyrcLuaVersion;
13+
use configs::{EmmyrcCodeAction, EmmyrcDocumentColor};
1414
use configs::{
1515
EmmyrcCodeLen, EmmyrcCompletion, EmmyrcDiagnostic, EmmyrcHover, EmmyrcInlayHint,
1616
EmmyrcReference, EmmyrcResource, EmmyrcRuntime, EmmyrcSemanticToken, EmmyrcSignature,
@@ -54,6 +54,8 @@ pub struct Emmyrc {
5454
pub hover: EmmyrcHover,
5555
#[serde(default)]
5656
pub document_color: EmmyrcDocumentColor,
57+
#[serde(default)]
58+
pub code_action: EmmyrcCodeAction,
5759
}
5860

5961
impl Emmyrc {

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22

3-
use emmylua_code_analysis::{DiagnosticCode, LuaDocument, SemanticModel};
3+
use emmylua_code_analysis::{DiagnosticCode, Emmyrc, LuaDocument, SemanticModel};
44
use emmylua_parser::{
55
LuaAst, LuaAstNode, LuaComment, LuaCommentOwner, LuaDocTag, LuaDocTagDiagnostic, LuaExpr,
66
LuaKind, LuaStat, LuaSyntaxNode, LuaTokenKind,
@@ -71,6 +71,7 @@ pub fn build_disable_next_line_changes(
7171
start: Position,
7272
code: DiagnosticCode,
7373
) -> Option<HashMap<Uri, Vec<TextEdit>>> {
74+
let emmyrc = semantic_model.get_emmyrc();
7475
let document = semantic_model.get_document();
7576
let offset = document.get_offset(start.line as usize, start.character as usize)?;
7677
let root = semantic_model.get_root();
@@ -127,6 +128,7 @@ pub fn build_disable_next_line_changes(
127128
} else {
128129
text_edit = get_disable_next_line_text_edit(
129130
&document,
131+
&emmyrc,
130132
comment.syntax().clone(),
131133
comment.get_position(),
132134
code,
@@ -137,6 +139,7 @@ pub fn build_disable_next_line_changes(
137139
if text_edit.is_none() {
138140
text_edit = get_disable_next_line_text_edit(
139141
&document,
142+
&emmyrc,
140143
ast.syntax().clone(),
141144
ast.get_position(),
142145
code,
@@ -152,6 +155,7 @@ pub fn build_disable_next_line_changes(
152155

153156
fn get_disable_next_line_text_edit(
154157
document: &LuaDocument,
158+
emmyrc: &Emmyrc,
155159
node: LuaSyntaxNode,
156160
offset: TextSize,
157161
code: DiagnosticCode,
@@ -167,6 +171,11 @@ fn get_disable_next_line_text_edit(
167171
};
168172

169173
let line = document.get_line(offset)?;
174+
let space = if emmyrc.code_action.insert_space {
175+
" "
176+
} else {
177+
""
178+
};
170179
Some(TextEdit {
171180
range: Range {
172181
start: Position {
@@ -179,8 +188,9 @@ fn get_disable_next_line_text_edit(
179188
},
180189
},
181190
new_text: format!(
182-
"{}---@diagnostic disable-next-line: {}\n",
191+
"{}---{}@diagnostic disable-next-line: {}\n",
183192
indent_text,
193+
space,
184194
code.get_name()
185195
),
186196
})
@@ -194,6 +204,12 @@ pub fn build_disable_file_changes(
194204
let first_block = root.get_block()?;
195205
let first_child = first_block.children::<LuaAst>().next()?;
196206
let document = semantic_model.get_document();
207+
let emmyrc = semantic_model.get_emmyrc();
208+
let space = if emmyrc.code_action.insert_space {
209+
" "
210+
} else {
211+
""
212+
};
197213
let text_edit = if let LuaAst::LuaComment(comment) = first_child {
198214
if let Some(diagnostic_tag) =
199215
find_diagnostic_disable_tag(comment.clone(), DisableAction::DisableFile)
@@ -230,7 +246,7 @@ pub fn build_disable_file_changes(
230246
character: 0,
231247
},
232248
},
233-
new_text: format!("---@diagnostic disable: {}\n", code.get_name()),
249+
new_text: format!("---{}@diagnostic disable: {}\n", space, code.get_name()),
234250
}
235251
}
236252
} else {
@@ -245,7 +261,7 @@ pub fn build_disable_file_changes(
245261
character: 0,
246262
},
247263
},
248-
new_text: format!("---@diagnostic disable: {}\n", code.get_name()),
264+
new_text: format!("---{}@diagnostic disable: {}\n", space, code.get_name()),
249265
}
250266
};
251267

0 commit comments

Comments
 (0)