Skip to content

Modern Docker Management, Designed for Everyone

31.4 KB
Raw Blame
<script lang="ts">
	import { browser } from '$app/env';
	import { autocompletion, type Completion, type CompletionContext } from '@codemirror/autocomplete';
	import { javascript } from '@codemirror/lang-javascript';
	import { json } from '@codemirror/lang-json';
	import { markdown } from '@codemirror/lang-markdown';
	import { yaml } from '@codemirror/lang-yaml';
	import { StreamLanguage, foldAll, unfoldAll, foldKeymap } from '@codemirror/language';
	import { dockerFile } from '@codemirror/legacy-modes/mode/dockerfile';
	import { properties } from '@codemirror/legacy-modes/mode/properties';
	import { shell } from '@codemirror/legacy-modes/mode/shell';
	import { toml } from '@codemirror/legacy-modes/mode/toml';
	import {
		linter,
		lintGutter,
		forEachDiagnostic,
		nextDiagnostic,
		previousDiagnostic,
		type Diagnostic,
		type LintSource
	} from '@codemirror/lint';
	import { type Extension } from '@codemirror/state';
	import { keymap, hoverTooltip, EditorView, ViewPlugin, closeHoverTooltips, hasHoverTooltips } from '@codemirror/view';
	import { mode } from 'mode-watcher';
	import CodeMirror from 'svelte-codemirror-editor';

	import * as Command from '#lib/components/ui/command/index.js';
	import { m } from '#lib/paraglide/messages.js';
	import userStore from '#lib/stores/user-store.svelte.js';

	import type { YamlPositionContext } from './analysis/compose-analysis';
	import type { ComposeSchemaContext } from './analysis/compose-schema';
	import { analyzeEnvContent } from './analysis/env-analysis';
	import type { CodeLanguage, CodeValidationMode, DiagnosticSummary, EditorContext, OutlineItem } from './analysis/types';
	import { createDefaultSummary, ENV_SNIPPETS, YAML_SNIPPETS } from './editor-constants';
	import { createEnterIndentKeymap } from './enter-indentation';
	import { createMergeHostAttachment, type MergeAttachmentParams } from './merge-editor.svelte.js';
	import { createArcaneTheme } from './theme';

	const languageSupport = new Map(
		Object.entries({
			yaml: { validation: 'compose', extensions: () => [yaml()] },
			env: { validation: 'env', extensions: () => [StreamLanguage.define(properties)] },
			json: { validation: 'none', extensions: () => [json()] },
			toml: { validation: 'none', extensions: () => [StreamLanguage.define(toml)] },
			dockerfile: { validation: 'none', extensions: () => [StreamLanguage.define(dockerFile)] },
			shell: { validation: 'none', extensions: () => [StreamLanguage.define(shell)] },
			javascript: { validation: 'none', extensions: () => [javascript({ jsx: true })] },
			typescript: { validation: 'none', extensions: () => [javascript({ typescript: true, jsx: true })] },
			markdown: { validation: 'none', extensions: () => [markdown()] },
			systemd: { validation: 'none', extensions: () => [StreamLanguage.define(properties)] },
			plaintext: { validation: 'none', extensions: () => [] }
		} satisfies Record<CodeLanguage, { validation: CodeValidationMode; extensions: () => Extension[] }>)
	);

	type ComposeAnalysisModule = typeof import('./analysis/compose-analysis');
	type ComposeSchemaModule = typeof import('./analysis/compose-schema');

	let composeAnalysisModulePromise: Promise<ComposeAnalysisModule> | null = null;
	let composeSchemaModulePromise: Promise<ComposeSchemaModule> | null = null;

	function loadComposeAnalysisModule(): Promise<ComposeAnalysisModule> {
		composeAnalysisModulePromise ??= import('./analysis/compose-analysis');
		return composeAnalysisModulePromise;
	}

	function loadComposeSchemaModule(): Promise<ComposeSchemaModule> {
		composeSchemaModulePromise ??= import('./analysis/compose-schema');
		return composeSchemaModulePromise;
	}

	let {
		value = $bindable(''),
		language = 'yaml' as CodeLanguage,
		validationMode,
		placeholder = '',
		readOnly = false,
		fontSize = '12px',
		autoHeight = false,
		hasErrors = $bindable(false),
		fileId,
		originalValue,
		enableDiff = false,
		editorContext = {} as EditorContext,
		validationReady = $bindable(false),
		diagnosticSummary = $bindable(createDefaultSummary()),
		outlineOpen = $bindable(false),
		diffOpen = $bindable(false),
		commandPaletteOpen = $bindable(false),
		statusBar = true
	}: {
		value?: string;
		language?: CodeLanguage;
		validationMode?: CodeValidationMode;
		placeholder?: string;
		readOnly?: boolean;
		fontSize?: string;
		autoHeight?: boolean;
		hasErrors?: boolean;
		fileId?: string;
		originalValue?: string;
		enableDiff?: boolean;
		editorContext?: EditorContext;
		validationReady?: boolean;
		diagnosticSummary?: DiagnosticSummary;
		outlineOpen?: boolean;
		diffOpen?: boolean;
		commandPaletteOpen?: boolean;
		statusBar?: boolean;
	} = $props();
	void hasErrors;

	let activeOutlineItems = $state<OutlineItem[]>([]);
	let activeView = $state<EditorView | null>(null);
	let schemaState = $state<ComposeSchemaContext | null>(null);
	let shortcutsEnabled = $derived(userStore.current?.preferences?.keyboardShortcutsEnabled !== false);
	let schemaHoverSuppressedUntil = 0;

	const schemaHoverSelectionSuppressionMs = 2500;

	const storageKey = $derived(fileId ? `arcane.editor.state:${fileId}` : null);
	const isDiffActive = $derived(Boolean(enableDiff && diffOpen && originalValue !== undefined));
	const effectiveValidationMode = $derived(validationMode ?? languageSupport.get(language)?.validation);
	const effectiveEditorContext = $derived({
		envContent: editorContext?.envContent ?? '',
		composeContents: editorContext?.composeContents ?? [],
		globalVariables: editorContext?.globalVariables ?? {}
	});

	const mergeAttachmentParams = $derived({
		diffActive: isDiffActive,
		language,
		value,
		baseline: originalValue ?? ''
	} satisfies MergeAttachmentParams);

	function updateSummary(patch: Partial<DiagnosticSummary>) {
		let changed = false;
		for (const key in patch) {
			if (diagnosticSummary[key as keyof DiagnosticSummary] !== patch[key as keyof DiagnosticSummary]) {
				changed = true;
				break;
			}
		}
		if (!changed) return;

		diagnosticSummary = {
			...createDefaultSummary(),
			...diagnosticSummary,
			...patch
		};
	}

	function updateSummaryFromDiagnostics(diagnostics: Diagnostic[], patch: Partial<DiagnosticSummary> = {}) {
		let errors = 0;
		let warnings = 0;
		let infos = 0;
		let hints = 0;

		for (const diagnostic of diagnostics) {
			switch (diagnostic.severity) {
				case 'error':
					errors += 1;
					break;
				case 'warning':
					warnings += 1;
					break;
				case 'info':
					infos += 1;
					break;
				case 'hint':
					hints += 1;
					break;
			}
		}

		hasErrors = errors > 0;
		validationReady = true;
		updateSummary({
			errors,
			warnings,
			infos,
			hints,
			validationReady: true,
			...patch
		});
	}

	function markReadOnlyReady() {
		if (!readOnly) return;
		hasErrors = false;
		validationReady = true;
		updateSummary({
			errors: 0,
			warnings: 0,
			infos: 0,
			hints: 0,
			validationReady: true
		});
	}

	function markValidationNotRequiredReady() {
		if (!readOnly && effectiveValidationMode !== 'none') return;
		hasErrors = false;
		validationReady = true;
		activeOutlineItems = [];
		updateSummary({
			errors: 0,
			warnings: 0,
			infos: 0,
			hints: 0,
			validationReady: true
		});
	}

	function updateCursorSummary(view: EditorView) {
		const position = view.state.selection.main.head;
		const line = view.state.doc.lineAt(position);
		updateSummary({
			cursorLine: line.number,
			cursorCol: position - line.from + 1
		});
	}

	function hasNonEmptySelection(view: EditorView): boolean {
		return view.state.selection.ranges.some((range) => !range.empty);
	}

	function isNodeInEditor(view: EditorView, node: Node | null): boolean {
		return !!node && (node === view.dom || view.dom.contains(node));
	}

	function nativeSelectionTouchesEditor(view: EditorView): boolean {
		const selection = view.dom.ownerDocument.getSelection();
		if (!selection || selection.isCollapsed) return false;
		return isNodeInEditor(view, selection.anchorNode) || isNodeInEditor(view, selection.focusNode);
	}

	function suppressSchemaHoverForSelection() {
		schemaHoverSuppressedUntil = performance.now() + schemaHoverSelectionSuppressionMs;
	}

	function isSchemaHoverSuppressed(view: EditorView): boolean {
		return hasNonEmptySelection(view) || nativeSelectionTouchesEditor(view) || performance.now() < schemaHoverSuppressedUntil;
	}

	function closeActiveSchemaHoverForSelection(view: EditorView) {
		suppressSchemaHoverForSelection();
		if (hasHoverTooltips(view.state)) {
			view.dispatch({ effects: closeHoverTooltips });
		}
	}

	function persistEditorState(view: EditorView) {
		if (!browser || !storageKey) return;
		try {
			const payload = {
				head: view.state.selection.main.head,
				scrollTop: view.scrollDOM.scrollTop
			};
			sessionStorage.setItem(storageKey, JSON.stringify(payload));
		} catch {
			// ignore persistence errors
		}
	}

	function restoreEditorState(view: EditorView) {
		if (!browser || !storageKey) return;
		try {
			const raw = sessionStorage.getItem(storageKey);
			if (!raw) return;
			const payload = JSON.parse(raw) as { head?: number; scrollTop?: number };
			if (typeof payload.head === 'number') {
				const bounded = Math.max(0, Math.min(payload.head, view.state.doc.length));
				view.dispatch({ selection: { anchor: bounded } });
			}
			if (typeof payload.scrollTop === 'number') {
				requestAnimationFrame(() => {
					view.scrollDOM.scrollTop = payload.scrollTop ?? 0;
				});
			}
		} catch {
			// ignore bad state payload
		}
	}

	function jumpToOutlineItem(item: OutlineItem) {
		if (!activeView) return;
		activeView.dispatch({
			selection: { anchor: item.from },
			scrollIntoView: true
		});
		activeView.focus();
	}

	function formatEnvContent(content: string): string {
		const lines = content.split(/\r?\n/);
		const formatted: string[] = [];
		for (const line of lines) {
			const trimmed = line.trim();
			if (!trimmed || trimmed.startsWith('#')) {
				formatted.push(trimmed);
				continue;
			}
			const valueLine = trimmed.startsWith('export ') ? trimmed.slice(7).trim() : trimmed;
			const separator = valueLine.indexOf('=');
			if (separator < 0) {
				formatted.push(trimmed);
				continue;
			}
			const key = valueLine.slice(0, separator).trim().toUpperCase().replace(/\s+/g, '_');
			const valuePart = valueLine.slice(separator + 1).trim();
			formatted.push(`${key}=${valuePart}`);
		}
		return formatted.join('\n').replace(/\n{3,}/g, '\n\n');
	}

	async function formatDocument() {
		if (!activeView || readOnly) return;
		const current = activeView.state.doc.toString();
		let formatted = current;

		if (language === 'yaml') {
			const { parseDocument } = await import('yaml');
			const parsed = parseDocument(current, { strict: false, uniqueKeys: false });
			if (parsed.errors.length === 0) {
				formatted = parsed.toString({ indent: 2, lineWidth: 0 });
			}
		} else if (language === 'env') {
			formatted = formatEnvContent(current);
		}

		if (formatted === current) return;

		activeView.dispatch({
			changes: { from: 0, to: activeView.state.doc.length, insert: formatted }
		});
	}

	function goToLine() {
		if (!activeView) return;
		const raw = window.prompt('Go to line', String(diagnosticSummary.cursorLine));
		if (!raw) return;
		const lineNumber = Number.parseInt(raw, 10);
		if (Number.isNaN(lineNumber)) return;
		const line = activeView.state.doc.line(Math.max(1, Math.min(lineNumber, activeView.state.doc.lines)));
		activeView.dispatch({
			selection: { anchor: line.from },
			scrollIntoView: true
		});
		activeView.focus();
	}

	function executeCommand(id: string) {
		commandPaletteOpen = false;
		switch (id) {
			case 'format':
				void formatDocument();
				break;
			case 'next-diagnostic':
				if (activeView) nextDiagnostic(activeView);
				break;
			case 'prev-diagnostic':
				if (activeView) previousDiagnostic(activeView);
				break;
			case 'toggle-outline':
				outlineOpen = !outlineOpen;
				break;
			case 'toggle-diff':
				if (enableDiff && originalValue !== undefined) {
					diffOpen = !diffOpen;
				}
				break;
			case 'fold-all':
				if (activeView) foldAll(activeView);
				break;
			case 'unfold-all':
				if (activeView) unfoldAll(activeView);
				break;
			case 'jump-line':
				goToLine();
				break;
		}
	}

	const commandItems = $derived.by(() => {
		const items = [
			{ id: 'format', label: 'Format document', shortcut: 'Shift+Alt+F' },
			{ id: 'next-diagnostic', label: 'Next diagnostic', shortcut: 'F8' },
			{ id: 'prev-diagnostic', label: 'Previous diagnostic', shortcut: 'Shift+F8' },
			{ id: 'toggle-outline', label: outlineOpen ? 'Hide outline' : 'Show outline' },
			{ id: 'fold-all', label: 'Fold all' },
			{ id: 'unfold-all', label: 'Unfold all' },
			{ id: 'jump-line', label: 'Jump to line' }
		];

		if (enableDiff && originalValue !== undefined) {
			items.splice(4, 0, { id: 'toggle-diff', label: diffOpen ? 'Hide diff' : 'Show diff' });
		}

		return items;
	});

	const schemaCompletions = async (context: CompletionContext, yamlContext: YamlPositionContext): Promise<Completion[]> => {
		const schemaModule = await loadComposeSchemaModule();
		const schema = await schemaModule.getComposeSchemaContext();
		schemaState = schema;
		updateSummary({
			schemaStatus: schema.status,
			schemaMessage: schema.message
		});

		if (!schema.schema) return [];
		const before = context.matchBefore(/[\w.-]*/);
		const prefix = before?.text ?? '';

		if (yamlContext.atKey) {
			return [...YAML_SNIPPETS, ...schemaModule.getCompletionOptionsForPath(schema.schema, yamlContext.parentPath, prefix)];
		}

		return schemaModule.getEnumValueCompletions(schema.schema, yamlContext.path);
	};

	const composeCompletionSource = async (context: CompletionContext) => {
		if (language !== 'yaml' || effectiveValidationMode !== 'compose' || readOnly) return null;
		const source = context.state.doc.toString();
		const analysisModule = await loadComposeAnalysisModule();
		const yamlContext = analysisModule.findYamlPositionContext(source, context.pos);
		if (!yamlContext) return null;

		const before = context.matchBefore(/[\w.-]*/);
		if (!context.explicit && (!before || before.from === before.to)) return null;

		const options = await schemaCompletions(context, yamlContext);
		if (options.length === 0) return null;

		return {
			from: before ? before.from : context.pos,
			options,
			validFor: /[\w.-]*/
		};
	};

	const envCompletionSource = async (context: CompletionContext) => {
		if (language !== 'env' || effectiveValidationMode !== 'env' || readOnly) return null;
		const before = context.matchBefore(/[A-Za-z0-9_.-]*/);
		if (!context.explicit && (!before || before.from === before.to)) return null;

		const variableOptions = Array.from(
			new Set<string>([
				...Object.keys(effectiveEditorContext.globalVariables),
				...(effectiveEditorContext.envContent ?? '')
					.split(/\r?\n/)
					.map((line) => line.trim())
					.filter(Boolean)
					.map((line) => line.split('=')[0]?.trim() ?? '')
					.filter(Boolean)
			])
		)
			.sort((a, b) => a.localeCompare(b))
			.map(
				(key) =>
					({
						label: key,
						type: 'variable',
						apply: `${key}=`
					}) satisfies Completion
			);

		return {
			from: before ? before.from : context.pos,
			options: [...ENV_SNIPPETS, ...variableOptions],
			validFor: /[A-Za-z0-9_.-]*/
		};
	};

	const yamlHover = hoverTooltip(
		async (view, position) => {
			if (language !== 'yaml' || effectiveValidationMode !== 'compose' || isSchemaHoverSuppressed(view)) return null;
			const source = view.state.doc.toString();
			const analysisModule = await loadComposeAnalysisModule();
			const variableRef = analysisModule.resolveVariableSourceAtPosition(source, position, effectiveEditorContext);
			if (variableRef) {
				return {
					pos: position,
					create() {
						const dom = document.createElement('div');
						dom.className = 'arcane-hover';
						dom.innerHTML = `<strong>${variableRef.name}</strong><div>Source: ${variableRef.source}</div>`;
						return { dom };
					}
				};
			}

			const yamlContext = analysisModule.findYamlPositionContext(source, position);
			if (!yamlContext || !yamlContext.currentKey) return null;

			const schemaModule = await loadComposeSchemaModule();
			const schema = schemaState ?? (await schemaModule.getComposeSchemaContext());
			if (isSchemaHoverSuppressed(view)) return null;
			schemaState = schema;
			if (!schema.schema) return null;

			const doc = schemaModule.getSchemaDocForPath(schema.schema, yamlContext.path);
			if (!doc) return null;

			return {
				pos: yamlContext.keyFrom ?? position,
				end: yamlContext.keyTo ?? position,
				create() {
					const dom = document.createElement('div');
					dom.className = 'arcane-hover';
					const examples =
						doc.examples && doc.examples.length > 0 ? `<div><strong>Examples:</strong> ${doc.examples.join(', ')}</div>` : '';
					dom.innerHTML = `
						<div><strong>${doc.title ?? yamlContext.currentKey}</strong></div>
						${doc.description ? `<div>${doc.description}</div>` : ''}
						${doc.defaultValue ? `<div><strong>Default:</strong> ${doc.defaultValue}</div>` : ''}
						${examples}
					`;
					return { dom };
				}
			};
		},
		{
			hideOn(tr) {
				return tr.selection ? tr.newSelection.ranges.some((range) => !range.empty) : false;
			}
		}
	);

	const selectionHoverGuardExtension: Extension = [
		ViewPlugin.fromClass(
			class {
				private readonly view: EditorView;
				private readonly ownerDocument: Document;

				constructor(view: EditorView) {
					this.view = view;
					this.ownerDocument = view.dom.ownerDocument;
					this.ownerDocument.addEventListener('selectionchange', this.handleSelectionChange);
				}

				update() {
					if (hasNonEmptySelection(this.view)) {
						suppressSchemaHoverForSelection();
					}
				}

				destroy() {
					this.ownerDocument.removeEventListener('selectionchange', this.handleSelectionChange);
				}

				private handleSelectionChange = () => {
					if (nativeSelectionTouchesEditor(this.view)) {
						closeActiveSchemaHoverForSelection(this.view);
					}
				};
			}
		),
		EditorView.domEventHandlers({
			pointerdown(event, view) {
				if (event.pointerType === 'touch' || event.pointerType === 'pen') {
					closeActiveSchemaHoverForSelection(view);
				}
				return false;
			},
			touchstart(_event, view) {
				closeActiveSchemaHoverForSelection(view);
				return false;
			},
			touchmove(_event, view) {
				closeActiveSchemaHoverForSelection(view);
				return false;
			}
		})
	];

	const editorLifecycleExtension = EditorView.updateListener.of((update) => {
		if (!update.view) return;
		if (activeView !== update.view) {
			activeView = update.view;
		}
		if (update.docChanged || update.selectionSet) {
			updateCursorSummary(update.view);
			persistEditorState(update.view);
		}
	});

	const shortcutKeymapExtension = keymap.of([
		...foldKeymap,
		{
			key: 'F8',
			run(view) {
				return nextDiagnostic(view);
			}
		},
		{
			key: 'Shift-F8',
			run(view) {
				return previousDiagnostic(view);
			}
		},
		{
			key: 'Shift-Alt-f',
			run() {
				void formatDocument();
				return true;
			}
		},
		{
			key: 'Mod-Shift-p',
			run() {
				if (!shortcutsEnabled) return false;
				commandPaletteOpen = true;
				return true;
			}
		}
	]);

	const yamlLinter: LintSource = async (view): Promise<Diagnostic[]> => {
		if (readOnly || effectiveValidationMode !== 'compose') {
			validationReady = true;
			hasErrors = false;
			return [];
		}

		const [schemaModule, analysisModule] = await Promise.all([loadComposeSchemaModule(), loadComposeAnalysisModule()]);
		const schema = await schemaModule.getComposeSchemaContext();
		schemaState = schema;
		const analysis = await analysisModule.analyzeComposeContent(view, schema, effectiveEditorContext);
		activeOutlineItems = analysis.outlineItems;

		updateSummaryFromDiagnostics(analysis.diagnostics, {
			schemaStatus: schema.status,
			schemaMessage: schema.message,
			...analysis.summaryPatch
		});

		return analysis.diagnostics;
	};

	const envLinter: LintSource = async (view): Promise<Diagnostic[]> => {
		if (readOnly || effectiveValidationMode !== 'env') {
			validationReady = true;
			hasErrors = false;
			return [];
		}

		const analysis = analyzeEnvContent(view.state.doc.toString(), effectiveEditorContext);
		activeOutlineItems = analysis.outlineItems;

		updateSummaryFromDiagnostics(analysis.diagnostics, {
			schemaStatus: schemaState?.status ?? 'unavailable',
			schemaMessage: schemaState?.message,
			...analysis.summaryPatch
		});

		return analysis.diagnostics;
	};

	const enterIndentKeymaps: Record<CodeLanguage, Extension> = {
		yaml: createEnterIndentKeymap('yaml'),
		env: createEnterIndentKeymap('env'),
		json: createEnterIndentKeymap('json'),
		toml: createEnterIndentKeymap('toml'),
		dockerfile: createEnterIndentKeymap('dockerfile'),
		shell: createEnterIndentKeymap('shell'),
		javascript: createEnterIndentKeymap('javascript'),
		typescript: createEnterIndentKeymap('typescript'),
		markdown: createEnterIndentKeymap('markdown'),
		systemd: createEnterIndentKeymap('systemd'),
		plaintext: createEnterIndentKeymap('plaintext')
	};

	function getBasicLanguageExtension(lang: CodeLanguage): Extension[] {
		return languageSupport.get(lang)?.extensions() as Extension[];
	}

	function getLanguageExtension(lang: CodeLanguage, options: { lightweight?: boolean } = {}): Extension[] {
		const lightweight = options.lightweight === true;
		const extensions: Extension[] = [editorLifecycleExtension, selectionHoverGuardExtension, shortcutKeymapExtension];

		if (!readOnly) {
			extensions.push(enterIndentKeymaps[lang]);
		}

		extensions.push(...(getBasicLanguageExtension(lang) ?? []));
		switch (lang) {
			case 'yaml':
				if (!readOnly && !lightweight && effectiveValidationMode === 'compose') {
					extensions.push(
						lintGutter(),
						linter(yamlLinter, { delay: 140 }),
						yamlHover,
						autocompletion({
							activateOnTyping: true,
							override: [composeCompletionSource]
						})
					);
				}
				break;
			case 'env':
				if (!readOnly && !lightweight && effectiveValidationMode === 'env') {
					extensions.push(
						lintGutter(),
						linter(envLinter, { delay: 140 }),
						autocompletion({
							activateOnTyping: true,
							override: [envCompletionSource]
						})
					);
				}
				break;
		}

		return extensions;
	}

	const theme = $derived(createArcaneTheme(mode.current === 'dark'));

	const mergeHostAttachment = createMergeHostAttachment(
		{
			getTheme: () => theme,
			getLanguageExtension,
			getReadonlyLanguageExtension: getBasicLanguageExtension,
			onValueChange: (nextValue) => {
				value = nextValue;
			},
			onPrimaryViewReady: (view) => {
				activeView = view;
				restoreEditorState(view);
				updateCursorSummary(view);
				markReadOnlyReady();
				markValidationNotRequiredReady();
			}
		},
		() => mergeAttachmentParams
	);

	const extensions = $derived([...getLanguageExtension(language), theme]);

	const styles = $derived({
		'&': {
			fontSize,
			height: autoHeight ? 'auto' : '100%'
		},
		'.cm-scroller': {
			overflow: autoHeight ? 'visible' : 'auto',
			maxHeight: autoHeight ? 'none' : '100%'
		},
		'&.cm-editor[contenteditable=false]': {
			cursor: 'not-allowed'
		},
		'.cm-content[contenteditable=false]': {
			cursor: 'not-allowed'
		}
	});

	function wireNormalView(view: EditorView) {
		if (!isDiffActive) {
			activeView = view;
			restoreEditorState(view);
			updateCursorSummary(view);
			markReadOnlyReady();
			markValidationNotRequiredReady();
		}
	}

	function countCurrentDiagnostics(): number {
		if (!activeView) return 0;
		let count = 0;
		forEachDiagnostic(activeView.state, () => {
			count += 1;
		});
		return count;
	}
</script>

<div class="arcane-code-editor {autoHeight ? 'auto-height' : 'full-height'}">
	<div class="editor-main">
		{#if isDiffActive}
			<div class="merge-shell">
				<div class="merge-pane-header">
					<div class="merge-pane-label merge-pane-label-new">New (editable)</div>
					<div class="merge-pane-label merge-pane-label-old">Original (read-only)</div>
				</div>
				<div class="merge-legend">
					<span class="merge-badge merge-badge-add">+ Added</span>
					<span class="merge-badge merge-badge-del">- Removed</span>
				</div>
				<div class="merge-host" {@attach mergeHostAttachment}></div>
			</div>
		{:else}
			<CodeMirror bind:value {extensions} {styles} {placeholder} readonly={readOnly} nodebounce={true} onready={wireNormalView} />
		{/if}

		{#if outlineOpen && activeOutlineItems.length > 0}
			<div
				class="absolute top-2 right-2 z-(--arcane-z-sticky) max-h-full-inset w-64 overflow-hidden rounded-sm border bg-popover text-popover-foreground shadow-lg"
			>
				<div class="border-b px-3 py-2 text-xs font-bold">Outline</div>
				<div class="max-h-80 overflow-auto p-1">
					{#each activeOutlineItems as item (item.id)}
						<button
							type="button"
							class="w-full rounded-sm px-2 py-1 text-left text-xs hover:bg-primary/18"
							class:pl-4={item.level === 1}
							onclick={() => jumpToOutlineItem(item)}
						>
							{item.label}
						</button>
					{/each}
				</div>
			</div>
		{/if}
	</div>

	{#if statusBar}
		<div class="editor-status">
			<span>{diagnosticSummary.errors} {m.editor_errors()}</span>
			<span>{diagnosticSummary.warnings} {m.editor_warnings()}</span>
			<span>{m.editor_schema()}: {diagnosticSummary.schemaStatus}</span>
			<span>{m.editor_line()} {diagnosticSummary.cursorLine}, {m.editor_column()} {diagnosticSummary.cursorCol}</span>
			<span>{m.diagnostics()}: {countCurrentDiagnostics()}</span>
			{#if !validationReady}
				<span class="status-muted">{m.editor_validating()}</span>
			{/if}
		</div>
	{/if}

	<Command.Dialog bind:open={commandPaletteOpen} title={m.editor_commands()} description={m.editor_commands_desc()}>
		<Command.Input placeholder={m.editor_search_commands()} />
		<Command.List>
			<Command.Empty>{m.common_no_results_found()}</Command.Empty>
			<Command.Group>
				{#each commandItems as item (item.id)}
					<Command.Item value={item.label} onSelect={() => executeCommand(item.id)}>
						<span class="flex-1">{item.label}</span>
						{#if item.shortcut}
							<Command.Shortcut>{item.shortcut}</Command.Shortcut>
						{/if}
					</Command.Item>
				{/each}
			</Command.Group>
		</Command.List>
	</Command.Dialog>
</div>

<style>
	:global(.arcane-code-editor.full-height) {
		height: 100%;
		display: flex;
		flex-direction: column;
		min-height: 0;
	}
	:global(.arcane-code-editor.auto-height) {
		height: auto;
		display: flex;
		flex-direction: column;
	}
	.editor-main {
		position: relative;
		flex: 1;
		min-height: 0;
	}
	:global(.arcane-code-editor.full-height .codemirror-wrapper) {
		height: 100%;
	}
	:global(.arcane-code-editor.full-height .cm-editor) {
		height: 100%;
	}
	:global(.arcane-code-editor.auto-height .codemirror-wrapper) {
		height: auto;
	}
	:global(.arcane-code-editor.auto-height .cm-editor) {
		height: auto;
		min-height: 120px;
	}
	:global(.arcane-code-editor.auto-height .cm-editor .cm-scroller) {
		flex: 1 1 auto;
		overflow-y: visible;
	}
	:global(.arcane-code-editor.auto-height .cm-editor .cm-gutters) {
		align-self: stretch;
		height: auto;
	}
	:global(.arcane-code-editor .cm-editor .cm-scroller) {
		overflow-x: auto;
	}
	:global(.arcane-code-editor .cm-editor .cm-gutters) {
		border-right: none;
	}
	:global(.arcane-code-editor .cm-editor:has(.cm-placeholder) .cm-activeLine),
	:global(.arcane-code-editor .cm-editor:has(.cm-placeholder) .cm-activeLineGutter) {
		background-color: transparent;
	}
	:global(.arcane-code-editor .cm-mergeView) {
		height: 100%;
	}
	.merge-shell {
		height: 100%;
		min-height: 0;
		display: flex;
		flex-direction: column;
		background: color-mix(in oklab, var(--background) 96%, black 4%);
	}
	.merge-pane-header {
		display: grid;
		grid-template-columns: 1fr 1fr;
		gap: 0.5rem;
		padding: 0.35rem 0.5rem;
		border-bottom: 1px solid var(--border);
	}
	.merge-pane-label {
		font-size: 0.72rem;
		font-weight: 700;
		padding: 0.25rem 0.5rem;
		border-radius: 0.4rem;
		text-align: center;
	}
	.merge-pane-label-new {
		color: var(--success);
		background: color-mix(in oklab, var(--success) 18%, transparent);
		border: 1px solid color-mix(in oklab, var(--success) 45%, transparent);
	}
	.merge-pane-label-old {
		color: var(--destructive);
		background: color-mix(in oklab, var(--destructive) 18%, transparent);
		border: 1px solid color-mix(in oklab, var(--destructive) 45%, transparent);
	}
	.merge-legend {
		display: flex;
		gap: 0.5rem;
		align-items: center;
		padding: 0.3rem 0.5rem;
		border-bottom: 1px solid var(--border);
	}
	.merge-badge {
		font-size: 0.68rem;
		font-weight: 600;
		line-height: 1;
		padding: 0.22rem 0.45rem;
		border-radius: 999px;
	}
	.merge-badge-add {
		color: var(--success);
		background: color-mix(in oklab, var(--success) 16%, transparent);
		border: 1px solid color-mix(in oklab, var(--success) 40%, transparent);
	}
	.merge-badge-del {
		color: var(--destructive);
		background: color-mix(in oklab, var(--destructive) 16%, transparent);
		border: 1px solid color-mix(in oklab, var(--destructive) 40%, transparent);
	}
	.merge-host {
		flex: 1;
		min-height: 0;
	}
	:global(.arcane-code-editor .merge-host .cm-mergeView) {
		height: 100%;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}
	:global(.arcane-code-editor .merge-host .cm-mergeViewEditors) {
		height: 100%;
		min-height: 0;
	}
	:global(.arcane-code-editor .merge-host .cm-mergeViewEditor) {
		height: 100%;
		min-height: 0;
		display: flex;
		flex-direction: column;
	}
	:global(.arcane-code-editor .merge-host .cm-mergeViewEditor:first-child) {
		border-right: 1px solid var(--border);
	}
	:global(.arcane-code-editor .merge-host .cm-mergeViewEditor .cm-editor) {
		height: 100% !important;
		min-height: 0;
	}
	:global(.arcane-code-editor .merge-host .cm-mergeViewEditor .cm-scroller) {
		height: 100% !important;
		overflow: auto !important;
	}
	:global(.arcane-code-editor .merge-host .cm-merge-a .cm-changedLine),
	:global(.arcane-code-editor .merge-host .cm-merge-a .cm-inlineChangedLine) {
		background-color: color-mix(in oklab, var(--success) 14%, transparent) !important;
	}
	:global(.arcane-code-editor .merge-host .cm-merge-b .cm-changedLine),
	:global(.arcane-code-editor .merge-host .cm-merge-b .cm-inlineChangedLine),
	:global(.arcane-code-editor .merge-host .cm-deletedChunk) {
		background-color: color-mix(in oklab, var(--destructive) 14%, transparent) !important;
	}
	:global(.arcane-code-editor .merge-host .cm-merge-a .cm-changedText) {
		background: linear-gradient(
				color-mix(in oklab, var(--success) 70%, transparent),
				color-mix(in oklab, var(--success) 70%, transparent)
			)
			bottom/100% 2px no-repeat !important;
	}
	:global(.arcane-code-editor .merge-host .cm-merge-b .cm-changedText),
	:global(.arcane-code-editor .merge-host .cm-merge-b .cm-deletedText),
	:global(.arcane-code-editor .merge-host .cm-deletedChunk .cm-deletedText) {
		background: linear-gradient(
				color-mix(in oklab, var(--destructive) 75%, transparent),
				color-mix(in oklab, var(--destructive) 75%, transparent)
			)
			bottom/100% 2px no-repeat !important;
	}
	:global(.arcane-code-editor .merge-host .cm-merge-a .cm-changedLineGutter) {
		background-color: var(--success) !important;
	}
	:global(.arcane-code-editor .merge-host .cm-merge-b .cm-changedLineGutter),
	:global(.arcane-code-editor .merge-host .cm-merge-b .cm-deletedLineGutter) {
		background-color: var(--destructive) !important;
	}
	.editor-status {
		display: flex;
		gap: 0.75rem;
		align-items: center;
		padding: 0.25rem 0.5rem;
		font-size: 0.7rem;
		border-top: 1px solid var(--border);
		background: color-mix(in oklab, var(--background) 92%, black 8%);
		overflow-x: auto;
		white-space: nowrap;
	}
	.status-muted {
		opacity: 0.7;
	}
	:global(.arcane-hover) {
		max-width: 26rem;
		padding: 0.5rem 0.625rem;
		font-size: 0.75rem;
		line-height: 1.4;
		pointer-events: none;
		border-radius: 0.5rem;
		border: 1px solid var(--border);
		background: color-mix(in oklab, var(--background) 96%, black 4%);
		box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
	}
</style>