11import * as vscode from 'vscode' ;
2+ import { eolString } from '../utils/eol' ;
23import { DocInfo , linetype , Asmline } from "./scanDoc" ;
4+
35//TODO: offer different operation for different vscode.FormattingOptions
46export class AsmDocFormat implements vscode . DocumentFormattingEditProvider {
5- provideDocumentFormattingEdits ( document : vscode . TextDocument ) : vscode . TextEdit [ ] {
7+ provideDocumentFormattingEdits ( document : vscode . TextDocument , options : vscode . FormattingOptions , token : vscode . CancellationToken ) : vscode . TextEdit [ ] {
68 const textedits : vscode . TextEdit [ ] = [ ] ;
9+ const tabString = options . insertSpaces ? new Array ( options . tabSize ) . fill ( " " ) . join ( "" ) : "\t" ;
710 const docinfo = DocInfo . getDocInfo ( document ) ;
811 if ( docinfo . tree ) {
9- docinfo . tree . forEach (
10- ( item ) => {
11- formateline ( item . range . start . line , item . range . end . line , docinfo . lines , document , textedits ) ;
12+ for ( const item of docinfo . tree ) {
13+ if ( token . isCancellationRequested ) {
14+ return textedits ;
1215 }
13- ) ;
16+ const newText = formateline ( item . range , docinfo . lines , tabString ) ;
17+ const range = document . validateRange ( item . range ) ;
18+ textedits . push ( new vscode . TextEdit ( range , newText . join ( eolString ( document . eol ) ) ) ) ;
19+ }
1420 }
1521 return textedits ;
1622 }
1723}
18- function formateline ( beg : number , end : number , asmline : Asmline [ ] , document : vscode . TextDocument , formator : vscode . TextEdit [ ] ) : vscode . TextEdit [ ] {
24+
25+ /**
26+ * format a segment of assembly code
27+ * @param range the range of the code
28+ * @param asmline the array of lines information
29+ * @param tabString the string to used as tab
30+ * @returns
31+ */
32+ function formateline ( range : vscode . Range , asmline : Asmline [ ] , tabString = "\t" ) : string [ ] {
1933 let namesize = 0 , optsize = 0 , oprsize = 0 ,
20- str : string | undefined = undefined , r : vscode . Range , i : number ;
34+ str : string | undefined = undefined ;
35+ const output : string [ ] = [ ] ;
36+
2137 //scan the asmlines for information
22- for ( i = beg ; i < end ; i ++ ) {
38+ for ( let i = range . start . line ; i <= range . end . line ; i ++ ) {
2339 const item = asmline [ i ] ;
2440 if ( item . name ) { namesize = item . name . length > namesize ? item . name . length : namesize ; } //find the maxlength of label name or variabel name
2541 if ( item . operator ) { optsize = item . operator . length > optsize ? item . operator . length : optsize ; } //find the maxlength of operator
2642 if ( item . operand ) { oprsize = item . operand . length > oprsize ? item . operand . length : oprsize ; } //find the maxlength of operand
2743 }
28- for ( i = beg ; i < end ; i ++ ) {
44+
45+ for ( let i = range . start . line ; i <= range . end . line ; i ++ ) {
2946 str = undefined ;
3047 const item = asmline [ i ] ;
3148 if ( item . type === linetype . label || item . type === linetype . variable ) {
32- str = "\t" ;
49+ str = tabString ;
3350 let length = 0 ;
3451 if ( item . name ?. length ) { length = item . name . length ; }
3552 if ( item . type === linetype . label && item . name ) { str += item . name + ":" ; }
@@ -47,25 +64,22 @@ function formateline(beg: number, end: number, asmline: Asmline[], document: vsc
4764 if ( item . operand ?. length ) { length = item . operand . length ; }
4865 else { length = 0 ; }
4966 for ( let i = 0 ; i < oprsize - length ; i ++ ) { str += " " ; } //操作数后补充空格
50- str += "\t" + item . comment ;
67+ str += tabString + item . comment ;
5168 }
5269 }
5370 else if ( item . type === linetype . onlycomment ) {
54- str = "\t" + item . comment ;
71+ str = tabString + item . comment ;
5572 }
5673 else if ( item . main ) {
5774 str = item . main . replace ( / \s + / , " " ) ;
5875 const length : number = namesize + 1 + optsize + 1 + oprsize - str . length ;
5976 if ( item . comment ) {
6077 for ( let i = 0 ; i < length ; i ++ ) { str += " " ; } //后补充空格
61- str += "\t\t" + item . comment ;
78+ str += tabString + tabString + item . comment ;
6279 }
6380 }
64- if ( str && str !== item . str ) {
65- r = new vscode . Range ( item . line , 0 , item . line , item . str . length ) ;
66- formator . push ( vscode . TextEdit . replace ( document . validateRange ( r ) , str ) ) ;
67- }
81+ output . push ( str && str !== item . str ? str : item . str ) ;
6882 }
69- return formator ;
83+ return output ;
7084}
7185
0 commit comments