Skip to content

Commit 8547f82

Browse files
authored
Merge pull request EmmyLuaLs#1089 from lewis6991/fix/format-insert-space
fix: make code actions follow formatter tag spacing
2 parents 5fee6b0 + 9c1da2a commit 8547f82

5 files changed

Lines changed: 28 additions & 25 deletions

File tree

crates/emmylua_code_analysis/resources/schema.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"codeAction": {
1313
"$ref": "#/$defs/EmmyrcCodeAction",
1414
"default": {
15-
"insertSpace": false
15+
"insertSpace": null
1616
}
1717
},
1818
"codeLens": {
@@ -491,9 +491,12 @@
491491
"type": "object",
492492
"properties": {
493493
"insertSpace": {
494-
"description": "Add space after `---` comments when inserting `@diagnostic disable-next-line`.",
495-
"type": "boolean",
496-
"default": false,
494+
"description": "Add space after `---` comments when inserting `@diagnostic disable-next-line`.\n\nWhen omitted, this follows the formatter's resolved\n`emmy_doc.space_between_tag_columns` setting.",
495+
"type": [
496+
"boolean",
497+
"null"
498+
],
499+
"default": null,
497500
"x-vscode-setting": true
498501
}
499502
}
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
use schemars::JsonSchema;
22
use serde::{Deserialize, Serialize};
33

4-
#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone)]
4+
#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone, Default)]
55
#[serde(rename_all = "camelCase")]
66
pub struct EmmyrcCodeAction {
77
/// Add space after `---` comments when inserting `@diagnostic disable-next-line`.
8-
#[serde(default = "default_false")]
8+
///
9+
/// When omitted, this follows the formatter's resolved
10+
/// `emmy_doc.space_between_tag_columns` setting.
11+
#[serde(default)]
912
#[schemars(extend("x-vscode-setting" = true))]
10-
pub insert_space: bool,
11-
}
12-
13-
impl Default for EmmyrcCodeAction {
14-
fn default() -> Self {
15-
Self {
16-
insert_space: default_false(),
17-
}
18-
}
19-
}
20-
21-
fn default_false() -> bool {
22-
false
13+
pub insert_space: Option<bool>,
2314
}

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

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

33
use emmylua_code_analysis::{DiagnosticCode, Emmyrc, LuaDocument, SemanticModel};
4+
use emmylua_formatter::resolve_config_for_path;
45
use emmylua_parser::{
56
LuaAst, LuaAstNode, LuaComment, LuaCommentOwner, LuaDocTag, LuaDocTagDiagnostic, LuaExpr,
67
LuaKind, LuaStat, LuaSyntaxNode, LuaTokenKind,
@@ -176,7 +177,7 @@ fn get_disable_next_line_text_edit(
176177
};
177178

178179
let line = document.get_line(offset)?;
179-
let space = if emmyrc.code_action.insert_space {
180+
let space = if code_action_insert_space(emmyrc, document) {
180181
" "
181182
} else {
182183
""
@@ -210,7 +211,7 @@ pub fn build_disable_file_changes(
210211
let first_child = first_block.children::<LuaAst>().next()?;
211212
let document = semantic_model.get_document();
212213
let emmyrc = semantic_model.get_emmyrc();
213-
let space = if emmyrc.code_action.insert_space {
214+
let space = if code_action_insert_space(emmyrc, &document) {
214215
" "
215216
} else {
216217
""
@@ -278,6 +279,14 @@ pub fn build_disable_file_changes(
278279
Some(changes)
279280
}
280281

282+
fn code_action_insert_space(emmyrc: &Emmyrc, document: &LuaDocument) -> bool {
283+
emmyrc.code_action.insert_space.unwrap_or_else(|| {
284+
resolve_config_for_path(Some(document.get_file_path().as_path()), None)
285+
.map(|resolved| resolved.config.emmy_doc.space_between_tag_columns)
286+
.unwrap_or(false)
287+
})
288+
}
289+
281290
fn find_diagnostic_disable_tag(
282291
comment: LuaComment,
283292
action: DisableAction,

docs/config/emmyrc_json_CN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ EmmyLua Analyzer Rust 推荐把配置写在项目根目录的 `.emmyrc.json` 中
110110
{
111111
"$schema": "https://raw.githubusercontent.com/EmmyLuaLs/emmylua-analyzer-rust/refs/heads/main/crates/emmylua_code_analysis/resources/schema.json",
112112
"codeAction": {
113-
"insertSpace": false
113+
"insertSpace": null
114114
},
115115
"codeLens": {
116116
"enable": true
@@ -469,7 +469,7 @@ EmmyLua Analyzer Rust 推荐把配置写在项目根目录的 `.emmyrc.json` 中
469469

470470
| 分组 | 字段 | 默认值 | 说明 |
471471
| --- | --- | --- | --- |
472-
| `codeAction` | `insertSpace` | `false` | 插入 `@diagnostic disable-next-line` 时在 `---` 后补空格 |
472+
| `codeAction` | `insertSpace` | `null` | 插入 `@diagnostic disable-next-line` 时覆盖格式化器的 `emmy_doc.space_between_tag_columns` 设置 |
473473
| `codeLens` | `enable` | `true` | 启用 CodeLens |
474474
| `documentColor` | `enable` | `true` | 识别颜色字符串并显示颜色预览 |
475475
| `hint` | `enable` | `true` | 总开关 |

docs/config/emmyrc_json_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ This template is a good starting point for most Lua projects:
110110
{
111111
"$schema": "https://raw.githubusercontent.com/EmmyLuaLs/emmylua-analyzer-rust/refs/heads/main/crates/emmylua_code_analysis/resources/schema.json",
112112
"codeAction": {
113-
"insertSpace": false
113+
"insertSpace": null
114114
},
115115
"codeLens": {
116116
"enable": true
@@ -469,7 +469,7 @@ External tool object:
469469

470470
| Section | Field | Default | Description |
471471
| --- | --- | --- | --- |
472-
| `codeAction` | `insertSpace` | `false` | Add a space after `---` when inserting `@diagnostic disable-next-line` |
472+
| `codeAction` | `insertSpace` | `null` | Override formatter `emmy_doc.space_between_tag_columns` when inserting `@diagnostic disable-next-line` |
473473
| `codeLens` | `enable` | `true` | Enable CodeLens |
474474
| `documentColor` | `enable` | `true` | Detect color-like strings and show color previews |
475475
| `hint` | `enable` | `true` | Master switch |

0 commit comments

Comments
 (0)