@@ -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}
0 commit comments