Skip to content

Commit 421d0a6

Browse files
committed
Preseve whitespace for non-opcode lines
Except for the intial indent to the opcode tab stop, maintain existing whitespace for non-opcode lines.
1 parent 6007e61 commit 421d0a6

4 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/common/format-asm.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function buildFormattedLine(indented: boolean, label: string, opcode: string, op
5454
return result;
5555
}
5656

57-
export function formatAsmLine(raw: string, lineNum: number, indentUnit: string, tabSize: number, asmTabStops: AsmTabStops): string {
57+
export function formatAsmLine(raw: string, lineNum: number, indentUnit: string, tabSize: number, asmTabStops: AsmTabStops, mnemonics?: Set<string>): string {
5858
const text = raw.trimEnd();
5959
if (text === '') return '';
6060

@@ -77,6 +77,21 @@ export function formatAsmLine(raw: string, lineNum: number, indentUnit: string,
7777
[opcode, operand] = splitFirst(content);
7878
}
7979

80+
// If line does not contain a known opcode, preserve whitespace after iniital indent.
81+
if (mnemonics && opcode && !mnemonics.has(opcode.toLowerCase())) {
82+
const rest = text.substring(firstNonSpace);
83+
if (indented) {
84+
return padToColumn('', asmTabStops.opcodes, indentUnit, tabSize) + rest;
85+
}
86+
if (opcode) {
87+
// Non-indented with label: fix indent between label and rest.
88+
const afterLabel = label.length;
89+
const opcodePos = afterLabel + text.substring(afterLabel).search(/\S/);
90+
return label + padToColumn(label, asmTabStops.opcodes, indentUnit, tabSize).substring(label.length) + text.substring(opcodePos);
91+
}
92+
return text;
93+
}
94+
8095
const newText = buildFormattedLine(indented, label, opcode, operand, comment, indentUnit, tabSize, asmTabStops);
8196
if (newText.replace(/\s/g, '') !== text.replace(/\s/g, '')) {
8297
console.warn(`format: skipping line ${lineNum}, mangles non-whitespace characters\n- before: ${JSON.stringify(text)}\n- after: ${JSON.stringify(newText)}`);
@@ -85,10 +100,10 @@ export function formatAsmLine(raw: string, lineNum: number, indentUnit: string,
85100
return newText;
86101
}
87102

88-
export function formatText(text: string, tabSize: number, asmTabStops: AsmTabStops): string {
103+
export function formatText(text: string, tabSize: number, asmTabStops: AsmTabStops, mnemonics?: Set<string>): string {
89104
if (!asmTabStops) return text;
90105
const indent = ' '.repeat(tabSize);
91106
const lines = text.split('\n');
92-
const formatted = lines.map((line, i) => formatAsmLine(line, i + 1, indent, tabSize, asmTabStops));
107+
const formatted = lines.map((line, i) => formatAsmLine(line, i + 1, indent, tabSize, asmTabStops, mnemonics));
93108
return formatted.join('\n');
94109
}

src/common/tabdetect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const MNEMONICS_6502 = new Set([...opcodes6502].map(s => s.toLowerCase()));
4343
const MNEMONICS_6809 = new Set([...opcodes6809].map(s => s.toLowerCase()));
4444
const MNEMONICS_Z80 = new Set([...opcodesZ80].map(s => s.toLowerCase()));
4545

46-
const mnemonicsByMode: Record<string, Set<string>> = {
46+
export const mnemonicsByMode: Record<string, Set<string>> = {
4747
'6502': MNEMONICS_6502,
4848
'6809': MNEMONICS_6809,
4949
'z80': MNEMONICS_Z80,

src/ide/format.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { EditorView } from "@codemirror/view";
44
import { formatAsmLine } from "../common/format-asm";
55
import { tabStopsFacet } from "./settings";
66

7-
export function formatDocument(view: EditorView, isAsm: boolean) {
7+
export function formatDocument(view: EditorView, isAsm: boolean, mnemonics?: Set<string>) {
88
let state = view.state;
99
const changeSets: ChangeSet[] = [];
1010

@@ -72,7 +72,7 @@ export function formatDocument(view: EditorView, isAsm: boolean) {
7272
for (let i = 1; i <= state.doc.lines; i++) {
7373
if (!isTargetLine(i)) continue;
7474
const line = state.doc.line(i);
75-
const formatted = formatAsmLine(line.text, i, indent, tabSize, stops);
75+
const formatted = formatAsmLine(line.text, i, indent, tabSize, stops, mnemonics);
7676
if (formatted !== line.text) {
7777
specs.push({ from: line.from, to: line.to, insert: formatted });
7878
}

src/ide/views/editors.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { mbo } from "../../themes/mbo";
2323
import { formatDocument } from "../format";
2424
import { loadSettings, registerEditor, settingsExtensions } from "../settings";
2525
import { clearBreakpoint, current_project, isAsmMode, lastDebugState, platform, runToPC } from "../ui";
26+
import { mnemonicsByMode } from "../../common/tabdetect";
2627
import { createAssetHeaderPlugin } from "./assetdecorations";
2728
import { ProjectView } from "./baseviews";
2829
import { createTextTransformFilterEffect, textTransformFilterCompartment } from "./filters";
@@ -92,6 +93,7 @@ export class SourceEditor implements ProjectView {
9293
newEditor(parent: HTMLElement, text: string, includesAsm?: boolean) {
9394
var modedef = MODEDEFS[this.mode] || MODEDEFS.default;
9495
var isAsm = isAsmMode(this.mode);
96+
var mnemonics = mnemonicsByMode[this.mode];
9597
var lineWrap = !!modedef.lineWrap;
9698
var theme = modedef.theme || MODEDEFS.default.theme;
9799

@@ -142,7 +144,7 @@ export class SourceEditor implements ProjectView {
142144
keydown(event, view) {
143145
if (event.shiftKey && event.altKey && event.code === 'KeyF') {
144146
event.preventDefault();
145-
formatDocument(view, isAsm);
147+
formatDocument(view, isAsm, mnemonics);
146148
}
147149
}
148150
}),

0 commit comments

Comments
 (0)