Skip to content

Modern Docker Management, Designed for Everyone

fix: newline indentations are incorrect in project editor

Kyle Mendellfc2f2330662e
diff --git a/frontend/src/lib/components/code-editor/editor.svelte b/frontend/src/lib/components/code-editor/editor.svelte
index 94fd7df33..692fa49a6 100644
--- a/frontend/src/lib/components/code-editor/editor.svelte
+++ b/frontend/src/lib/components/code-editor/editor.svelte
@@ -22,7 +22,8 @@
import configStore from '$lib/stores/config-store';
import { arcaneDarkInit } from './theme';
import { createDefaultSummary, ENV_SNIPPETS, YAML_SNIPPETS } from './editor-constants';
- import { createMergeEnterIndentKeymap, createMergeHostAction, type MergeActionParams } from './merge-editor';
+ import { createEnterIndentKeymap } from './enter-indentation';
+ import { createMergeHostAction, type MergeActionParams } from './merge-editor';
import {
analyzeComposeContent,
findYamlPositionContext,
@@ -521,10 +522,19 @@
return analysis.diagnostics;
};
+ const enterIndentKeymaps: Record<CodeLanguage, Extension> = {
+ yaml: createEnterIndentKeymap('yaml'),
+ env: createEnterIndentKeymap('env')
+ };
+
function getLanguageExtension(lang: CodeLanguage, options: { lightweight?: boolean } = {}): Extension[] {
const lightweight = options.lightweight === true;
const extensions: Extension[] = [editorLifecycleExtension, shortcutKeymapExtension];
+ if (!readOnly) {
+ extensions.push(enterIndentKeymaps[lang]);
+ }
+
switch (lang) {
case 'yaml':
extensions.push(yaml());
@@ -563,10 +573,8 @@
return arcaneDarkInit();
});
- const mergeEnterIndentKeymap = createMergeEnterIndentKeymap();
const mergeHostAction = createMergeHostAction({
getTheme: () => theme,
- mergeEnterIndentKeymap,
getLanguageExtension,
onValueChange: (nextValue) => {
value = nextValue;
diff --git a/frontend/src/lib/components/code-editor/enter-indentation.ts b/frontend/src/lib/components/code-editor/enter-indentation.ts
new file mode 100644
index 000000000..97268adbd
--- /dev/null
+++ b/frontend/src/lib/components/code-editor/enter-indentation.ts
@@ -0,0 +1,92 @@
+import { getIndentUnit, getIndentation, indentString } from '@codemirror/language';
+import { EditorState, Prec, type Extension } from '@codemirror/state';
+import { EditorView } from '@codemirror/view';
+import type { CodeLanguage } from './analysis/types';
+
+function getYamlHeuristicIndent(
+ lineBreakState: EditorState,
+ textBeforeCursor: string,
+ currentIndent: string,
+ isCursorAtLineEnd: boolean
+): string | null {
+ if (!isCursorAtLineEnd) return null;
+
+ const trimmedBeforeCursor = textBeforeCursor.trimEnd();
+ const startsYamlBlock =
+ /:\s*(?:#.*)?$/.test(trimmedBeforeCursor) ||
+ /:\s*[|>][-+0-9]*\s*(?:#.*)?$/.test(trimmedBeforeCursor) ||
+ /^-\s*(?:#.*)?$/.test(trimmedBeforeCursor);
+
+ if (!startsYamlBlock) return null;
+
+ return indentString(lineBreakState, currentIndent.length + getIndentUnit(lineBreakState));
+}
+
+export function createEnterIndentKeymap(language: CodeLanguage): Extension {
+ return Prec.highest(
+ EditorView.domEventHandlers({
+ keydown(event, view) {
+ if (
+ event.key !== 'Enter' ||
+ event.defaultPrevented ||
+ event.isComposing ||
+ event.altKey ||
+ event.ctrlKey ||
+ event.metaKey ||
+ view.state.facet(EditorState.readOnly)
+ ) {
+ return false;
+ }
+
+ const selection = view.state.selection.main;
+ if (!selection) return false;
+
+ const from = selection.from;
+ const to = selection.to;
+ const currentLine = view.state.doc.lineAt(from);
+ const isCursorAtLineEnd = from === to && from === currentLine.to;
+ const currentIndent = currentLine.text.match(/^\s*/)?.[0] ?? '';
+ const nextLine = currentLine.number < view.state.doc.lines ? view.state.doc.line(currentLine.number + 1) : null;
+ const hasNextNonEmptyLine = Boolean(nextLine && nextLine.text.trim().length > 0);
+ const lineBreakPos = from + 1;
+
+ const lineBreakTransaction = view.state.update({
+ changes: { from, to, insert: '\n' }
+ });
+ const lineBreakState = lineBreakTransaction.state;
+
+ let indentation = '';
+
+ if (isCursorAtLineEnd && hasNextNonEmptyLine) {
+ indentation = nextLine?.text.match(/^\s*/)?.[0] ?? '';
+ } else {
+ const computedIndent = getIndentation(lineBreakState, lineBreakPos);
+ const textBeforeCursor = currentLine.text.slice(0, Math.max(0, from - currentLine.from));
+ const yamlHeuristicIndent =
+ language === 'yaml'
+ ? getYamlHeuristicIndent(lineBreakState, textBeforeCursor, currentIndent, isCursorAtLineEnd)
+ : null;
+
+ if (computedIndent !== null) {
+ indentation = indentString(lineBreakState, computedIndent);
+ }
+
+ if (yamlHeuristicIndent && yamlHeuristicIndent.length > indentation.length) {
+ indentation = yamlHeuristicIndent;
+ } else if (computedIndent === null) {
+ indentation = textBeforeCursor.match(/^\s*/)?.[0] ?? '';
+ }
+ }
+
+ event.preventDefault();
+ view.dispatch({
+ changes: { from, to, insert: `\n${indentation}` },
+ selection: { anchor: lineBreakPos + indentation.length },
+ userEvent: 'input'
+ });
+
+ return true;
+ }
+ })
+ );
+}
diff --git a/frontend/src/lib/components/code-editor/merge-editor.ts b/frontend/src/lib/components/code-editor/merge-editor.ts
index 99af3784f..0f21a84d2 100644
--- a/frontend/src/lib/components/code-editor/merge-editor.ts
+++ b/frontend/src/lib/components/code-editor/merge-editor.ts
@@ -1,9 +1,9 @@
import { yaml } from '@codemirror/lang-yaml';
-import { StreamLanguage, getIndentation, indentString } from '@codemirror/language';
+import { StreamLanguage } from '@codemirror/language';
import { properties } from '@codemirror/legacy-modes/mode/properties';
import { EditorState, type Extension } from '@codemirror/state';
import { MergeView } from '@codemirror/merge';
-import { keymap, EditorView } from '@codemirror/view';
+import { EditorView } from '@codemirror/view';
import type { Action } from 'svelte/action';
import type { CodeLanguage } from './analysis/types';
@@ -16,61 +16,13 @@ export type MergeActionParams = {
type CreateMergeHostActionOptions = {
getTheme: () => Extension;
- mergeEnterIndentKeymap: Extension;
getLanguageExtension: (lang: CodeLanguage, options?: { lightweight?: boolean }) => Extension[];
onValueChange: (nextValue: string) => void;
onPrimaryViewReady: (view: EditorView) => void;
};
-export function createMergeEnterIndentKeymap(): Extension {
- return keymap.of([
- {
- key: 'Enter',
- run(view) {
- const selection = view.state.selection.main;
- if (!selection) return false;
-
- const from = selection.from;
- const to = selection.to;
- const currentLine = view.state.doc.lineAt(from);
- const isCursorAtLineEnd = from === to && from === currentLine.to;
- const nextLine = currentLine.number < view.state.doc.lines ? view.state.doc.line(currentLine.number + 1) : null;
- const hasNextNonEmptyLine = Boolean(nextLine && nextLine.text.trim().length > 0);
- const lineBreakPos = from + 1;
-
- const lineBreakTransaction = view.state.update({
- changes: { from, to, insert: '\n' }
- });
- const lineBreakState = lineBreakTransaction.state;
-
- let indentation = '';
-
- if (isCursorAtLineEnd && hasNextNonEmptyLine) {
- indentation = nextLine?.text.match(/^\s*/)?.[0] ?? '';
- } else {
- const computedIndent = getIndentation(lineBreakState, lineBreakPos);
- if (computedIndent !== null) {
- indentation = indentString(lineBreakState, computedIndent);
- } else {
- const textBeforeCursor = currentLine.text.slice(0, Math.max(0, from - currentLine.from));
- indentation = textBeforeCursor.match(/^\s*/)?.[0] ?? '';
- }
- }
-
- view.dispatch({
- changes: { from, to, insert: `\n${indentation}` },
- selection: { anchor: lineBreakPos + indentation.length },
- userEvent: 'input'
- });
-
- return true;
- }
- }
- ]);
-}
-
export function createMergeHostAction(options: CreateMergeHostActionOptions): Action<HTMLDivElement, MergeActionParams> {
- const { getTheme, mergeEnterIndentKeymap, getLanguageExtension, onValueChange, onPrimaryViewReady } = options;
+ const { getTheme, getLanguageExtension, onValueChange, onPrimaryViewReady } = options;
return (node, params) => {
let currentParams = params;
@@ -94,7 +46,6 @@ export function createMergeHostAction(options: CreateMergeHostActionOptions): Ac
doc: currentParams.value,
extensions: [
...getLanguageExtension(currentParams.language, { lightweight: true }),
- mergeEnterIndentKeymap,
theme,
EditorView.updateListener.of((update) => {
if (update.docChanged) {
diff --git a/tests/spec/project.spec.ts b/tests/spec/project.spec.ts
index 30fffa843..1262e8d98 100644
--- a/tests/spec/project.spec.ts
+++ b/tests/spec/project.spec.ts
@@ -25,6 +25,12 @@ async function setCodeMirrorValue(page: Page, editor: Locator, text: string) {
await page.keyboard.type(text, { delay: 0 });
}
+async function getCodeMirrorValue(editor: Locator) {
+ const content = editor.locator('.cm-content').first();
+ await expect(content).toBeVisible();
+ return content.evaluate((node) => (node as HTMLElement).innerText ?? '');
+}
+
async function createProjectViaUI(page: Page, projectName: string) {
const containerName = `test-redis-container-${Date.now()}`;
const envFile = TEST_ENV_FILE.replace(/CONTAINER_NAME=.*/m, `CONTAINER_NAME=${containerName}`);
@@ -240,6 +246,24 @@ test.describe('New Compose Project Page', () => {
await expect(page.getByRole('heading', { name: 'Environment (.env)' })).toBeVisible();
});
+ test('should preserve YAML indentation when pressing Enter in compose editor', async ({
+ page
+ }) => {
+ const composeEditor = page.locator('.cm-editor:visible').first();
+ const composeContent = composeEditor.locator('.cm-content').first();
+
+ await expect(composeContent).toBeVisible();
+ await composeContent.click({ position: { x: 10, y: 10 } });
+ await composeContent.press('ControlOrMeta+A');
+ await page.keyboard.type('services:', { delay: 0 });
+ await page.keyboard.press('Enter');
+ await page.keyboard.type('web:', { delay: 0 });
+
+ await expect
+ .poll(async () => (await getCodeMirrorValue(composeEditor)).replace(/\r/g, ''))
+ .toContain('services:\n web:');
+ });
+
test('should validate required fields', async ({ page }) => {
const createButton = page
.getByRole('button', { name: 'Create Project' })