@@ -2,7 +2,8 @@ import * as vscode from 'vscode';
22import * as nls from 'vscode-nls'
33const localize = nls . loadMessageBundle ( )
44let symbols : TasmSymbol [ ] = [ ] //record symbols in the doc in a simple way
5- let _document : vscode . TextDocument //record the processing document
5+ let _documentText : string | undefined //record the processing document
6+ let _docUri : vscode . Uri | undefined
67let asmline : Asmline [ ] = [ ] //split and analyse the document
78let docsymbol : vscode . DocumentSymbol [ ] = [ ] //record symbols in the doc in a VSCode way
89
@@ -18,7 +19,6 @@ export function getrefer(word: string, doc: vscode.TextDocument): vscode.Locatio
1819 case linetype . macro : skip = true ; break
1920 case linetype . endm : skip = false ; break
2021 case linetype . label :
21- case linetype . labelB :
2222 if ( skip === false ) {
2323 //TODO:优化匹配方式,对于变量应该考虑多种复杂的表达式如:查找var不能找到nvar
2424 if ( def ?. type === symboltype . variable && item . operand ?. includes ( word ) ) {
@@ -33,7 +33,7 @@ export function getrefer(word: string, doc: vscode.TextDocument): vscode.Locatio
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 ( doc . uri , r ) )
3737 }
3838 else {
3939
@@ -45,45 +45,45 @@ 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'
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'
5252 if ( document . eol === vscode . EndOfLine . LF ) Endline = '\n'
5353 //scan the asmlines for information
5454 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 )
55+ ( item ) => {
56+ if ( item . name ) namesize = item . name . length > namesize ? item . name . length : namesize
57+ if ( item . operator ) optsize = item . operator . length > optsize ? item . operator . length : optsize
58+ if ( item . operand ) oprsize = item . operand . length > oprsize ? item . operand . length : oprsize
59+ // console.log(item.operator, optsize)
6060 }
6161 )
6262 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
63+ ( item ) => {
64+ if ( item . type === linetype . label || item . type === linetype . variable ) {
65+ str = "\t"
66+ let length : number = 0
67+ if ( item . name ?. length ) length = item . name . length
68+ if ( item . type === linetype . label && item . name ) str += item . name + ":"
69+ else if ( item . type === linetype . variable && item . name ) str += item . name + " "
70+ else str += " "
71+ for ( let i = 0 ; i < namesize - length ; i ++ ) str += " " //标签变量名前补充空格
7572
76- for ( let i = 0 ; i < optsize - length ; i ++ ) str += " " //操作码后补充空格
77- str += " " + item . operand
78- if ( item . comment ) str += item . comment
73+ str += item . operator
74+ // if(item.name?.length) length=item.operator.length
75+ // else length=0
76+ //for(let i=0;i<optsize-length;i++) str+=" "//操作码后补充空格
77+ str += "\t" + item . operand
78+ if ( item . comment ) str += "\t" + item . comment
79+ }
80+ else {
81+ str = item . str . replace ( / \s + / , " " )
7982 }
80- else {
81- str = item . str . replace ( / \s + / , " " )
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 ) )
8286 }
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- }
8787 }
8888 )
8989
@@ -120,7 +120,7 @@ class TasmSymbol {
120120 constructor ( type : number , name : string , RangeorPosition : vscode . Range | vscode . Position ) {
121121 this . type = type
122122 this . name = name
123- if ( _document ) this . location = new vscode . Location ( _document . uri , RangeorPosition )
123+ if ( _docUri ) this . location = new vscode . Location ( _docUri , RangeorPosition )
124124 }
125125 public markdown ( ) : vscode . MarkdownString {
126126 let md = new vscode . MarkdownString ( )
@@ -231,34 +231,32 @@ class Asmline {
231231 return flag
232232 }
233233 private getvarlabel ( item : string ) {
234- let r = item . match ( / ^ \s * ( \w + \s * : | ) \s * ( \w + ) \s + ( . * ) $ / )
234+ let r = item . match ( / ^ \s * ( \w + \s * : | ) \s * ( \w + | ) ( \s + . * | ) $ / )
235235 let name : string | undefined
236236 if ( r ) {
237237 name = r [ 1 ]
238- if ( name . length === 0 ) this . type = linetype . labelB
239- else {
238+ this . type = linetype . label
239+ if ( name . length !== 0 ) {
240240 this . name = name . slice ( 0 , name . length - 1 ) . trim ( )
241- this . type = linetype . label
242241 let start = item . indexOf ( r [ 1 ] )
243242 let one : TasmSymbol = new TasmSymbol ( symboltype . label , this . name , new vscode . Position ( this . line , start ) )
244243 symbols . push ( one )
245244 }
246245 this . operator = r [ 2 ]
247- this . operand = r [ 3 ]
246+ this . operand = r [ 3 ] . trim ( )
248247 }
249- r = item . match ( / ^ \s * ( \w + \s + | ) ( [ d D ] [ b B w W d D f F q Q t T ] | = | E Q U | e q u ) \s + ( .* ) $ / )
248+ r = item . match ( / ^ \s * ( \w + \s + | ) ( [ d D ] [ b B w W d D f F q Q t T ] | = | E Q U | e q u ) ( \s + .* ) $ / )
250249 if ( r ) {
251250 name = r [ 1 ] . trim ( )
252- if ( name . length === 0 ) this . type = linetype . variableB
253- else {
254- this . type = linetype . variable
251+ this . type = linetype . variable
252+ if ( name . length !== 0 ) {
255253 this . name = name
256254 let start = item . indexOf ( r [ 1 ] )
257255 let one : TasmSymbol = new TasmSymbol ( symboltype . variable , name , new vscode . Position ( this . line , start ) )
258256 symbols . push ( one )
259257 }
260258 this . operator = r [ 2 ]
261- this . operand = r [ 3 ]
259+ this . operand = r [ 3 ] . trim ( )
262260 }
263261 }
264262 public varlabelsymbol ( ) : vscode . DocumentSymbol | undefined {
@@ -287,10 +285,11 @@ class Asmline {
287285}
288286
289287function sacnDoc ( document : vscode . TextDocument ) {
290- _document = document ; symbols = [ ] ; asmline = [ ]
288+ _documentText = document . getText ( ) ; _docUri = document . uri
289+ symbols = [ ] ; asmline = [ ]
291290 let splitor : string = '\r\n'
292291 if ( document . eol === vscode . EndOfLine . LF ) splitor = '\n'
293- let doc = document . getText ( ) . split ( splitor )
292+ let doc = _documentText . split ( splitor )
294293 // scan the document for necessary information
295294 let docsymbol : vscode . DocumentSymbol [ ] = [ ]
296295 doc . forEach (
@@ -300,10 +299,9 @@ function sacnDoc(document: vscode.TextDocument) {
300299 )
301300 console . log ( asmline )
302301}
303- export function getVscSymbols ( doc ?: vscode . TextDocument ) : vscode . DocumentSymbol [ ] {
304- docsymbol = [ ]
305- if ( doc && doc !== _document ) sacnDoc ( doc )
302+ function symboltree ( ) {
306303 let i : number
304+ //寻找段,宏指令信息
307305 asmline . forEach (
308306 ( line , index , array ) => {
309307 //是否为宏指令
@@ -395,6 +393,14 @@ export function getVscSymbols(doc?: vscode.TextDocument): vscode.DocumentSymbol[
395393 }
396394 }
397395 )
396+
397+ }
398+ export function scanDocumnt ( doc ?: vscode . TextDocument ) : vscode . DocumentSymbol [ ] {
399+ docsymbol = [ ]
400+ if ( doc && ( doc . getText ( ) !== _documentText || doc . uri !== _docUri ) ) {
401+ sacnDoc ( doc )
402+ symboltree ( )
403+ }
398404 return docsymbol
399405}
400406
0 commit comments