Skip to content

Commit 7ca96f6

Browse files
committed
fix(editor): consistent YAML auto-indent on newline
The indentNodeProp from @codemirror/lang-yaml computes column-based indents that produce inconsistent and odd-numbered indentation when pressing Enter (e.g. 9 spaces after a nested 'web:' line, 6 after a 4-space 'image:' line). Override it with a line-based indentService: keep the current line's indent, align list-entry keys after the dash marker, and indent one unit deeper after lines opening a block (':', '|', '>'). Fixes Dokploy#4650
1 parent 7ba9818 commit 7ca96f6

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

‎apps/dokploy/components/shared/code-editor.tsx‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import {
77
import { css } from "@codemirror/lang-css";
88
import { json } from "@codemirror/lang-json";
99
import { yaml } from "@codemirror/lang-yaml";
10-
import { StreamLanguage } from "@codemirror/language";
10+
import {
11+
getIndentUnit,
12+
indentService,
13+
StreamLanguage,
14+
} from "@codemirror/language";
1115
import { properties } from "@codemirror/legacy-modes/mode/properties";
1216
import { shell } from "@codemirror/legacy-modes/mode/shell";
1317
import { search, searchKeymap } from "@codemirror/search";
@@ -98,6 +102,27 @@ const dockerComposeServiceOptions = [
98102
},
99103
}));
100104

105+
// The indentNodeProp shipped with @codemirror/lang-yaml computes wrong
106+
// column-based indents on Enter (odd/inconsistent amounts, see #4650), so
107+
// indentation is resolved line-based here: keep the current indent, align
108+
// list-entry keys after the dash marker, and go one unit deeper after a
109+
// line that opens a block (ending in ":", "|" or ">").
110+
const yamlIndent = indentService.of((context, pos) => {
111+
const line = context.state.doc.lineAt(pos);
112+
const before = context.state.doc.sliceString(line.from, pos);
113+
const indent = /^ */.exec(before)?.[0].length ?? 0;
114+
const trimmed = before.trim();
115+
if (!trimmed || trimmed.startsWith("#")) {
116+
return indent;
117+
}
118+
const base =
119+
trimmed.startsWith("- ") && /:\s/.test(trimmed) ? indent + 2 : indent;
120+
if (/[:|>]$/.test(trimmed)) {
121+
return base + getIndentUnit(context.state);
122+
}
123+
return base;
124+
});
125+
101126
function dockerComposeComplete(
102127
context: CompletionContext,
103128
): CompletionResult | null {
@@ -160,7 +185,7 @@ export const CodeEditor = ({
160185
search(),
161186
keymap.of(searchKeymap),
162187
language === "yaml"
163-
? yaml()
188+
? [yamlIndent, yaml()]
164189
: language === "json"
165190
? json()
166191
: language === "css"

0 commit comments

Comments
 (0)