@@ -14,28 +14,28 @@ export function getrefer(word: string, doc: vscode.TextDocument): vscode.Locatio
1414 output . push ( def . location )
1515 asmline . forEach (
1616 ( item , index ) => {
17- switch ( item . type ) {
18- case linetype . macro :skip = true ; break
19- case linetype . endm :skip = false ; break
17+ switch ( item . type ) {
18+ case linetype . macro : skip = true ; break
19+ case linetype . endm : skip = false ; break
2020 case linetype . label :
2121 case linetype . labelB :
22- if ( skip === false ) {
22+ if ( skip === false ) {
2323 //TODO:优化匹配方式,对于变量应该考虑多种复杂的表达式如:查找var不能找到nvar
24- if ( def ?. type === symboltype . variable && item . operand ?. includes ( word ) ) {
24+ if ( def ?. type === symboltype . variable && item . operand ?. includes ( word ) ) {
2525 let start = item . str . indexOf ( word )
2626 r = new vscode . Range ( index , start , index , start + word . length )
2727 }
28- if ( def ?. type === symboltype . macro && item . operator === word ) {
28+ if ( def ?. type === symboltype . macro && item . operator === word ) {
2929 let start = item . str . indexOf ( word )
3030 r = new vscode . Range ( index , start , index , start + word . length )
3131 }
32- if ( def ?. type === symboltype . procedure || def ?. type === symboltype . label && item . operand === word ) {
32+ if ( def ?. type === symboltype . procedure || def ?. type === symboltype . label && item . operand === word ) {
3333 let start = item . str . indexOf ( word )
3434 r = new vscode . Range ( index , start , index , start + word . length )
3535 }
36- if ( r ) output . push ( new vscode . Location ( _document . uri , r ) )
36+ if ( r ) output . push ( new vscode . Location ( _document . uri , r ) )
3737 }
38- else {
38+ else {
3939
4040 }
4141
@@ -45,6 +45,50 @@ export function getrefer(word: string, doc: vscode.TextDocument): vscode.Locatio
4545 }
4646 return output
4747}
48+ export function codeformatting ( document : vscode . TextDocument , options : vscode . FormattingOptions ) :vscode . TextEdit [ ] {
49+ let formator :vscode . TextEdit [ ] = [ ] ,
50+ namesize :number = 0 , optsize :number = 0 , oprsize :number = 0 , str :string | undefined = undefined ,
51+ r :vscode . Range , Endline : string = '\r\n'
52+ if ( document . eol === vscode . EndOfLine . LF ) Endline = '\n'
53+ //scan the asmlines for information
54+ asmline . forEach (
55+ ( item ) => {
56+ if ( item . name ) namesize = item . name . length > optsize ?item . name . length :namesize
57+ if ( item . operator ) optsize = item . operator . length > optsize ?item . operator . length :optsize
58+ if ( item . operand ) optsize = item . operand . length > optsize ?item . operand . length :optsize
59+ console . log ( optsize )
60+ }
61+ )
62+ asmline . forEach (
63+ ( item ) => {
64+ if ( item . operator ) {
65+ str = "\t"
66+ let length :number = 0
67+ if ( item . name ?. length ) length = item . name . length
68+ for ( let i = 0 ; i < namesize - length ; i ++ ) str += " " //标签变量名前补充空格
69+ if ( item . type === linetype . label && item . name ) str += item . name + ":"
70+ else if ( item . type === linetype . variable && item . name ) str += item . name + " "
71+ else str += " "
72+ if ( item . name ?. length ) length = item . operator . length
73+ else length = 0
74+ str += item . operator
75+
76+ for ( let i = 0 ; i < optsize - length ; i ++ ) str += " " //操作码后补充空格
77+ str += " " + item . operand
78+ if ( item . comment ) str += item . comment
79+ }
80+ else {
81+ str = item . str . replace ( / \s + / , " " )
82+ }
83+ if ( str && str !== item . str ) {
84+ r = new vscode . Range ( item . line , 0 , item . line , item . str . length )
85+ formator . push ( vscode . TextEdit . replace ( document . validateRange ( r ) , str ) )
86+ }
87+ }
88+ )
89+
90+ return formator
91+ }
4892
4993//part I scan the document for information
5094enum symboltype {
@@ -243,9 +287,9 @@ class Asmline {
243287}
244288
245289function sacnDoc ( document : vscode . TextDocument ) {
246- _document = document ; symbols = [ ] ; asmline = [ ]
247- let splitor :string = '\r\n'
248- if ( document . eol === vscode . EndOfLine . LF ) splitor = '\n'
290+ _document = document ; symbols = [ ] ; asmline = [ ]
291+ let splitor : string = '\r\n'
292+ if ( document . eol === vscode . EndOfLine . LF ) splitor = '\n'
249293 let doc = document . getText ( ) . split ( splitor )
250294 // scan the document for necessary information
251295 let docsymbol : vscode . DocumentSymbol [ ] = [ ]
@@ -257,7 +301,7 @@ function sacnDoc(document: vscode.TextDocument) {
257301 console . log ( asmline )
258302}
259303export function getVscSymbols ( doc ?: vscode . TextDocument ) : vscode . DocumentSymbol [ ] {
260- docsymbol = [ ]
304+ docsymbol = [ ]
261305 if ( doc && doc !== _document ) sacnDoc ( doc )
262306 let i : number
263307 asmline . forEach (
0 commit comments