Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Improve code block outdent behavior with space indentation https://github.com/siyuan-note/siyuan/issues/20061

Daniel0707cae1adaf
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
index fef4490d08..c0c27d9e1b 100644
--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20200924100950-9op5xi1.sy
@@ -7,7 +7,7 @@
"id": "20200924100950-9op5xi1",
"title": "Shortcuts",
"type": "doc",
- "updated": "20261002202250"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -4274,7 +4274,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20210106154056-koo4wdj",
- "updated": "20261001015635"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -14261,7 +14261,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20250423105026-c1t0rqo",
- "updated": "20260811122156"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -14334,7 +14334,7 @@
"Properties": {
"colgroup": "min-width: 60px;|min-width: 60px;",
"id": "20250423105026-6g9hmgk",
- "updated": "20260811122156"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -14421,6 +14421,69 @@
]
}
]
+ },
+ {
+ "Type": "NodeTableRow",
+ "Data": "tr",
+ "Children": [
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "Insert indentation at the caret, or indent selected lines"
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Tab"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableRow",
+ "Data": "tr",
+ "Children": [
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "Outdent the current line or selected lines: remove one leading tab or up to the "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Number of Tab Spaces"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " setting in leading spaces (up to 4 when set to 0)"
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Shift+Tab"
+ }
+ ]
+ }
+ ]
}
]
},
diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
index 1a3b8e0f72..418edbc24e 100644
--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20200813004551-gm0pbn1.sy
@@ -7,7 +7,7 @@
"id": "20200813004551-gm0pbn1",
"title": "快捷键",
"type": "doc",
- "updated": "20261002202250"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -4272,7 +4272,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20201227133317-rkh4bji",
- "updated": "20261001015635"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -14343,7 +14343,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20250423104310-6605t5b",
- "updated": "20260811122156"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -14416,7 +14416,7 @@
"Properties": {
"colgroup": "min-width: 60px;|min-width: 60px;",
"id": "20250423104401-pdo2ma3",
- "updated": "20260811122156"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -14503,6 +14503,69 @@
]
}
]
+ },
+ {
+ "Type": "NodeTableRow",
+ "Data": "tr",
+ "Children": [
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "在光标处插入缩进,或增加选中行的缩进"
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Tab"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableRow",
+ "Data": "tr",
+ "Children": [
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "减少当前行或选中行的缩进:删除一个行首制表符,或最多按 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Tab 空格数"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " 设置删除行首空格;设置为 0 时最多删除 4 个空格"
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Shift+Tab"
+ }
+ ]
+ }
+ ]
}
]
},
diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
index d5f1492e82..c15f4f9eaf 100644
--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226122549-jktxego.sy
@@ -7,7 +7,7 @@
"id": "20211226122549-jktxego",
"title": "快捷鍵",
"type": "doc",
- "updated": "20261002202250"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -4266,7 +4266,7 @@
"HeadingLevel": 2,
"Properties": {
"id": "20211226122652-htptsmh",
- "updated": "20261001015635"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -14261,12 +14261,12 @@
"HeadingLevel": 3,
"Properties": {
"id": "20250423105322-jc65ji5",
- "updated": "20260811122156"
+ "updated": "20261002224733"
},
"Children": [
{
"Type": "NodeText",
- "Data": "代碼區塊 "
+ "Data": "程式碼區塊 "
},
{
"Type": "NodeTextMark",
@@ -14334,7 +14334,7 @@
"Properties": {
"colgroup": "min-width: 60px;|min-width: 60px;",
"id": "20250423105322-ou0dbnf",
- "updated": "20260811122156"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -14421,6 +14421,69 @@
]
}
]
+ },
+ {
+ "Type": "NodeTableRow",
+ "Data": "tr",
+ "Children": [
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "在游標處插入縮排,或增加選取行的縮排"
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Tab"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableRow",
+ "Data": "tr",
+ "Children": [
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "減少目前行或選取行的縮排:刪除一個行首定位字元,或最多依 "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Tab 空格數"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " 設定刪除行首空格;設定為 0 時最多刪除 4 個空格"
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Shift+Tab"
+ }
+ ]
+ }
+ ]
}
]
},
diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
index a273936e02..383c0e4ee3 100644
--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-xsbxokr.sy
@@ -9,7 +9,7 @@
"id": "20240530101000-xsbxokr",
"title": "ショートカット",
"type": "doc",
- "updated": "20261002202250"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -4228,7 +4228,7 @@
"Properties": {
"ID": "20240530101000-twmuxke",
"id": "20240530101000-675gu9g",
- "updated": "20261001015635"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -14162,7 +14162,7 @@
"HeadingLevel": 3,
"Properties": {
"id": "20250423105545-q3l2fg0",
- "updated": "20260811122156"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -14235,7 +14235,7 @@
"Properties": {
"colgroup": "min-width: 60px;|min-width: 60px;",
"id": "20250423105545-be49765",
- "updated": "20260811122156"
+ "updated": "20261002224733"
},
"Children": [
{
@@ -14322,6 +14322,69 @@
]
}
]
+ },
+ {
+ "Type": "NodeTableRow",
+ "Data": "tr",
+ "Children": [
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "カーソル位置にインデントを挿入する、または選択行のインデントを増やす"
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Tab"
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableRow",
+ "Data": "tr",
+ "Children": [
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeText",
+ "Data": "現在の行または選択行のインデントを減らす:行頭のタブを1つ、または "
+ },
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Tab スペースの数"
+ },
+ {
+ "Type": "NodeText",
+ "Data": " の設定数を上限として行頭のスペースを削除する(0の場合は最大4個)"
+ }
+ ]
+ },
+ {
+ "Type": "NodeTableCell",
+ "Data": "td",
+ "Children": [
+ {
+ "Type": "NodeTextMark",
+ "TextMarkType": "kbd",
+ "TextMarkTextContent": "Shift+Tab"
+ }
+ ]
+ }
+ ]
}
]
},
diff --git a/app/src/protyle/wysiwyg/codeBlockUtil.test.ts b/app/src/protyle/wysiwyg/codeBlockUtil.test.ts
index 36e17e8a8a..3473931ddc 100644
--- a/app/src/protyle/wysiwyg/codeBlockUtil.test.ts
+++ b/app/src/protyle/wysiwyg/codeBlockUtil.test.ts
@@ -33,7 +33,14 @@ test("code block line indentation handles spaces, tabs, partial indentation and
assert.equal(updateCodeBlockLines("one\n\ntwo", "\t"), "\tone\n\t\n\ttwo");
assert.equal(updateCodeBlockLines(" one\n two\n\tthree\nfour", " ", true),
"one\ntwo\nthree\nfour");
- assert.equal(updateCodeBlockLines(" one\n\ttwo", "\t", true), " one\ntwo");
+ assert.equal(updateCodeBlockLines(" one\n\ttwo", "\t", true), "one\ntwo");
+});
+
+test("tab mode outdents pasted spaces by up to four columns without changing code whitespace", () => {
+ const text = " if ready:\n run()\n partial\n\t\tnested\n \t mixed\n \n\nvalue tail\t";
+ assert.equal(updateCodeBlockLines(text, "\t", true),
+ "if ready:\n run()\npartial\n\tnested\n\t mixed\n\n\nvalue tail\t");
+ assert.equal(updateCodeBlockLines(updateCodeBlockLines(text, "\t"), "\t", true), text);
});
const outdentAtCaret = (text: string, caret: number, tabSpace = " ") => {
@@ -42,10 +49,12 @@ const outdentAtCaret = (text: string, caret: number, tabSpace = " ") => {
};
test("collapsed code block outdent removes leading indentation from any caret position", () => {
- for (const [caret, expected] of [[0, 0], [2, 0], [4, 0], [6, 2], [9, 5]]) {
- assert.deepEqual(outdentAtCaret(" value", caret), {text: "value", caret: expected});
+ for (const tabSpace of [" ", "\t"]) {
+ for (const [caret, expected] of [[0, 0], [2, 0], [4, 0], [6, 2], [9, 5]]) {
+ assert.deepEqual(outdentAtCaret(" value", caret, tabSpace), {text: "value", caret: expected});
+ }
+ assert.deepEqual(outdentAtCaret(" value", 5, tabSpace), {text: " value", caret: 1});
}
- assert.deepEqual(outdentAtCaret(" value", 5), {text: " value", caret: 1});
assert.deepEqual(outdentAtCaret(" value", 7), {text: "value", caret: 5});
assert.deepEqual(outdentAtCaret(" value", 9, " "), {text: " value", caret: 7});
});
@@ -53,7 +62,7 @@ test("collapsed code block outdent removes leading indentation from any caret po
test("collapsed code block outdent uses the configured unit and preserves other whitespace", () => {
assert.deepEqual(outdentAtCaret("\tvalue", 6), {text: "value", caret: 5});
assert.deepEqual(outdentAtCaret("\t\tvalue", 7, "\t"), {text: "\tvalue", caret: 6});
- assert.deepEqual(outdentAtCaret(" value", 7, "\t"), {text: " value", caret: 7});
+ assert.deepEqual(outdentAtCaret(" value", 7, "\t"), {text: "value", caret: 5});
assert.deepEqual(outdentAtCaret(" \tvalue", 8), {text: "\tvalue", caret: 6});
assert.deepEqual(outdentAtCaret("value ", 9), {text: "value ", caret: 9});
assert.deepEqual(outdentAtCaret(" value tail\t", 16), {text: "value tail\t", caret: 12});
diff --git a/app/src/protyle/wysiwyg/codeBlockUtil.ts b/app/src/protyle/wysiwyg/codeBlockUtil.ts
index aa22a7e106..ee02932229 100644
--- a/app/src/protyle/wysiwyg/codeBlockUtil.ts
+++ b/app/src/protyle/wysiwyg/codeBlockUtil.ts
@@ -27,11 +27,10 @@ export const updateCodeBlockLines = (text: string, tabSpace: string, outdent = f
if (line.startsWith("\t")) {
return line.substring(1);
}
- if (tabSpace === "\t") {
- return line;
- }
+ // 制表符模式按代码块默认显示宽度处理行首空格。
+ const spaceLimit = tabSpace === "\t" ? 4 : tabSpace.length;
let spaceCount = 0;
- while (spaceCount < tabSpace.length && line[spaceCount] === " ") {
+ while (spaceCount < spaceLimit && line[spaceCount] === " ") {
spaceCount++;
}
return line.substring(spaceCount);