Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

fix(jsonc): stop member removal from leaving whitespace-only lines

Jason029e6f0e3713
diff --git a/src-tauri/src/jsonc_document.rs b/src-tauri/src/jsonc_document.rs
index 41e8cf5fa..6f5805f40 100644
--- a/src-tauri/src/jsonc_document.rs
+++ b/src-tauri/src/jsonc_document.rs
@@ -335,11 +335,12 @@ fn remove_object_pair_at(
if index < pairs.len() {
let after_comma = removed_context.wsc.3.unwrap_or_default();
if index == 0 {
- ensure_object_context(context).wsc.0.push_str(&after_comma);
+ let leading = &mut ensure_object_context(context).wsc.0;
+ *leading = merge_gap(leading, &after_comma, Gap::Before);
} else {
let previous = ensure_kvp_context(&mut pairs[index - 1]);
let separator = previous.wsc.3.take().unwrap_or_default();
- previous.wsc.3 = Some(format!("{separator}{after_comma}"));
+ previous.wsc.3 = Some(merge_gap(&separator, &after_comma, Gap::Before));
}
} else if index == 0 {
let object_context = ensure_object_context(context);
@@ -351,14 +352,36 @@ fn remove_object_pair_at(
let previous = ensure_kvp_context(&mut pairs[index - 1]);
let separator = previous.wsc.3.take().unwrap_or_default();
if let Some(after_comma) = removed_context.wsc.3 {
- previous.wsc.3 = Some(format!("{separator}{after_comma}"));
+ previous.wsc.3 = Some(merge_gap(&separator, &after_comma, Gap::After));
} else {
- previous.wsc.2.push_str(&separator);
- previous.wsc.2.push_str(&removed_context.wsc.2);
+ let tail = merge_gap(&separator, &removed_context.wsc.2, Gap::After);
+ previous.wsc.2.push_str(&tail);
}
}
}
+/// 删掉一个成员后,它前后的两段空白(含注释)合成一段。两段都只是空白时只留一段,
+/// 不然换行加缩进会叠两遍、留下一行空格:删第一个或中间的成员留前一段(下一个成员原样缩进),
+/// 删最后一个留后一段(收尾的括号缩进不变)。有注释的照旧拼接,不丢注释。
+#[derive(Clone, Copy)]
+enum Gap {
+ Before,
+ After,
+}
+
+fn merge_gap(before: &str, after: &str, keep: Gap) -> String {
+ let blank = |text: &str| text.chars().all(char::is_whitespace);
+ match (blank(before), blank(after)) {
+ (true, true) => match keep {
+ Gap::Before => before.to_string(),
+ Gap::After => after.to_string(),
+ },
+ (true, false) => after.to_string(),
+ (false, true) => before.to_string(),
+ (false, false) => format!("{before}{after}"),
+ }
+}
+
fn remove_array_value_at(
values: &mut Vec<RtJSONArrayValue>,
context: &mut Option<RtJSONArrayContext>,
@@ -372,11 +395,12 @@ fn remove_array_value_at(
if index < values.len() {
let after_comma = removed_context.wsc.1.unwrap_or_default();
if index == 0 {
- ensure_array_context(context).wsc.0.push_str(&after_comma);
+ let leading = &mut ensure_array_context(context).wsc.0;
+ *leading = merge_gap(leading, &after_comma, Gap::Before);
} else {
let previous = ensure_array_value_context(&mut values[index - 1]);
let separator = previous.wsc.1.take().unwrap_or_default();
- previous.wsc.1 = Some(format!("{separator}{after_comma}"));
+ previous.wsc.1 = Some(merge_gap(&separator, &after_comma, Gap::Before));
}
} else if index == 0 {
let array_context = ensure_array_context(context);
@@ -388,10 +412,10 @@ fn remove_array_value_at(
let previous = ensure_array_value_context(&mut values[index - 1]);
let separator = previous.wsc.1.take().unwrap_or_default();
if let Some(after_comma) = removed_context.wsc.1 {
- previous.wsc.1 = Some(format!("{separator}{after_comma}"));
+ previous.wsc.1 = Some(merge_gap(&separator, &after_comma, Gap::After));
} else {
- previous.wsc.0.push_str(&separator);
- previous.wsc.0.push_str(&removed_context.wsc.0);
+ let tail = merge_gap(&separator, &removed_context.wsc.0, Gap::After);
+ previous.wsc.0.push_str(&tail);
}
}
}
@@ -753,6 +777,52 @@ mod tests {
}
}
+ #[test]
+ fn removing_members_leaves_no_blank_lines() {
+ let source = "{\n \"a\": 1,\n \"b\": [\n 1,\n 2,\n 3\n ],\n \"c\": 3\n}\n";
+ for (desired, expected) in [
+ (
+ json!({"b":[1,2,3],"c":3}),
+ "{\n \"b\": [\n 1,\n 2,\n 3\n ],\n \"c\": 3\n}\n",
+ ),
+ (
+ json!({"a":1,"b":[1,2,3]}),
+ "{\n \"a\": 1,\n \"b\": [\n 1,\n 2,\n 3\n ]\n}\n",
+ ),
+ (
+ json!({"a":1,"b":[2,3],"c":3}),
+ "{\n \"a\": 1,\n \"b\": [\n 2,\n 3\n ],\n \"c\": 3\n}\n",
+ ),
+ (
+ json!({"a":1,"b":[1,3],"c":3}),
+ "{\n \"a\": 1,\n \"b\": [\n 1,\n 3\n ],\n \"c\": 3\n}\n",
+ ),
+ (
+ json!({"a":1,"b":[1,2],"c":3}),
+ "{\n \"a\": 1,\n \"b\": [\n 1,\n 2\n ],\n \"c\": 3\n}\n",
+ ),
+ ] {
+ let mut doc = JsoncDocument::parse(source).unwrap();
+ doc.apply(&desired).unwrap();
+ assert_eq!(doc.validated_source().unwrap(), expected);
+ }
+ // 中间那个成员带的注释照旧保留,下一个成员不会被行注释吞掉
+ let mut doc =
+ JsoncDocument::parse("{\n \"a\": 1,\n // about b\n \"b\": 2,\n \"c\": 3\n}")
+ .unwrap();
+ doc.apply(&json!({"a":1,"c":3})).unwrap();
+ let output = doc.validated_source().unwrap();
+ assert!(output.contains("// about b"));
+ assert_eq!(
+ json5::from_str::<Value>(&output).unwrap(),
+ json!({"a":1,"c":3})
+ );
+ // 带尾逗号时删最后一个,收尾括号缩进不变
+ let mut doc = JsoncDocument::parse("{\n \"a\": 1,\n \"b\": 2,\n}").unwrap();
+ doc.apply(&json!({"a":1})).unwrap();
+ assert_eq!(doc.validated_source().unwrap(), "{\n \"a\": 1,\n}");
+ }
+
#[test]
fn appending_keeps_trailing_commas_and_does_not_copy_comments_as_indentation() {
let mut doc = JsoncDocument::parse("{\n\t/*keep*/ \"items\": [1,],\n}\n").unwrap();