11import * as vscode from 'vscode' ;
22import * as nls from 'vscode-nls'
33const localize = nls . loadMessageBundle ( )
4- const symbols :TasmSymbol [ ] = [ ]
4+ let symbols :TasmSymbol [ ] = [ ]
55let _document :vscode . TextDocument
66let docsymbol :vscode . DocumentSymbol [ ] = [ ]
7- enum symboltype {
7+ enum symboltype {
88 other ,
99 macro ,
1010 procedure ,
1111 struct ,
1212 label ,
13- asmvar ,
13+ variable ,
1414 segment
1515}
1616function SymbolVSCfy ( a :symboltype ) {
@@ -21,7 +21,7 @@ function SymbolVSCfy(a:symboltype){
2121 case symboltype . procedure :output = vscode . SymbolKind . Function ; break ;
2222 case symboltype . struct :output = vscode . SymbolKind . Struct ; break ;
2323 case symboltype . label :output = vscode . SymbolKind . Key ; break ;
24- case symboltype . asmvar :output = vscode . SymbolKind . Variable ; break ;
24+ case symboltype . variable :output = vscode . SymbolKind . Variable ; break ;
2525 }
2626 return output
2727}
@@ -40,11 +40,11 @@ class TasmSymbol{
4040 let typestr :string = " "
4141 switch ( this . type ) {
4242 case symboltype . label :typestr = localize ( "keykind.Label" , "label" ) ; break ;
43- case symboltype . asmvar :typestr = localize ( "keykind.Variable" , "variable" ) ; break ;
43+ case symboltype . variable :typestr = localize ( "keykind.Variable" , "variable" ) ; break ;
4444 case symboltype . procedure :typestr = localize ( "keykind.Procedure" , "procedure" ) ; break ;
4545 case symboltype . struct :typestr = localize ( "keykind.Structure" , "Structure" ) ; break ;
4646 case symboltype . macro :typestr = localize ( "keykind.Macro" , "macro" ) ; break ;
47- case symboltype . macro :typestr = localize ( "keykind.Segment" , "segment" ) ; break ;
47+ case symboltype . segment :typestr = localize ( "keykind.Segment" , "segment" ) ; break ;
4848 }
4949 md . appendMarkdown ( '**' + typestr + "** " + this . name )
5050 return md
@@ -96,37 +96,39 @@ function scanline(item:string,line:number):Asmline|null{
9696 return asmline
9797}
9898function getvarlabel ( item :string , index :number , belong ?:string ) :vscode . DocumentSymbol | undefined {
99- let r = item . match ( / \s * ( \w + ) \s * : / )
100- let vscsymbol :vscode . DocumentSymbol
99+ let vscsymbol :vscode . DocumentSymbol | undefined
101100 let name :string
101+ let kind :vscode . SymbolKind
102102 let range :vscode . Range
103103 let srange :vscode . Range
104+ let r = item . match ( / ( \w + ) \s * : ( [ ^ ; : ] * ) / )
104105 if ( r ) {
105106 name = r [ 1 ]
106107 let start = item . indexOf ( name )
107108 let one :TasmSymbol = new TasmSymbol ( symboltype . label , r [ 1 ] , new vscode . Position ( index , start ) , belong )
108109 symbols . push ( one )
109- range = new vscode . Range ( index , 0 , start , item . length )
110- srange = new vscode . Range ( index , start , index , index + r [ 1 ] . length )
111- let kind = SymbolVSCfy ( symboltype . label )
112- vscsymbol = new vscode . DocumentSymbol ( name , item , kind , range , srange )
113- console . log ( vscsymbol )
110+ range = new vscode . Range ( index , 0 , index , item . length )
111+ srange = new vscode . Range ( index , start , index , start + name . length )
112+ kind = SymbolVSCfy ( symboltype . label )
113+ vscsymbol = new vscode . DocumentSymbol ( name , r [ 2 ] , kind , range , srange )
114+ // console.log(vscsymbol)
114115 }
115- r = item . match ( / \s * ( \w + ) \s * [ d D ] [ b B w W d D f F q Q t T ] / )
116+ r = item . match ( / \s * ( \w + ) \s + [ d D ] [ b B w W d D f F q Q t T ] \s + / )
116117 if ( r ) {
117118 name = r [ 1 ]
118119 let start = item . indexOf ( r [ 1 ] )
119- let one :TasmSymbol = new TasmSymbol ( symboltype . asmvar , r [ 1 ] , new vscode . Position ( index , start ) , belong )
120+ let one :TasmSymbol = new TasmSymbol ( symboltype . variable , r [ 1 ] , new vscode . Position ( index , start ) , belong )
120121 symbols . push ( one )
121- range = new vscode . Range ( index , 0 , start , item . length )
122- srange = new vscode . Range ( index , start , index , index + r [ 1 ] . length )
123- return new vscode . DocumentSymbol ( r [ 1 ] , item , SymbolVSCfy ( symboltype . label ) , range , srange )
122+ kind = SymbolVSCfy ( symboltype . variable )
123+ range = new vscode . Range ( index , 0 , index , item . length )
124+ srange = new vscode . Range ( index , start , index , start + r [ 1 ] . length )
125+ vscsymbol = new vscode . DocumentSymbol ( name , item , kind , range , srange )
126+ console . log ( vscsymbol )
124127 }
125- return
128+ return vscsymbol
126129}
127130export function sacnDoc ( document :vscode . TextDocument ) : vscode . DocumentSymbol [ ] {
128- _document = document
129- let doc = document . getText ( ) . split ( '\n' )
131+ _document = document ; symbols = [ ] ; let doc = document . getText ( ) . split ( '\n' )
130132 // scan the document for necessary information
131133 let docsymbol :vscode . DocumentSymbol [ ] = [ ]
132134 let asmline :Asmline [ ] = [ ]
@@ -135,7 +137,8 @@ export function sacnDoc(document:vscode.TextDocument) : vscode.DocumentSymbol[]
135137 let line = scanline ( item , index )
136138 if ( line !== null ) asmline . push ( line )
137139 }
138- )
140+ )
141+ console . log ( asmline )
139142 let skip :boolean , i :number
140143 asmline . forEach (
141144 ( line , index , array ) => {
@@ -153,16 +156,29 @@ export function sacnDoc(document:vscode.TextDocument) : vscode.DocumentSymbol[]
153156 if ( line . name && line_endm ?. line ) {
154157 let macrorange = new vscode . Range ( line . line , line . index , line_endm ?. line , line_endm ?. index )
155158 symbols . push ( new TasmSymbol ( symboltype . macro , line . name , macrorange ) )
156- let varlabel :vscode . DocumentSymbol [ ] = [ ]
157159 let symbol1 = new vscode . DocumentSymbol ( line . name + ": " + getType ( KeywordType . Macro ) , " " , SymbolVSCfy ( symboltype . macro ) , macrorange , new vscode . Range ( line . line , line . index , line . line , line . index + line . name . length ) )
158- symbol1 . children = varlabel
159160 docsymbol . push ( symbol1 )
160161 }
161162 }
162- if ( line . type === linetype . segment ) {
163+ else if ( line . type === linetype . segment ) {
163164 let line_ends :Asmline | undefined
164- //寻赵段结束的位置
165+ let proc :Asmline | undefined //正在寻找的子程序信息
166+ let procschild :vscode . DocumentSymbol [ ] = [ ]
167+ //寻找段结束的位置,并收集子程序的信息
165168 for ( i = index ; i < asmline . length ; i ++ ) {
169+ //寻找子程序
170+ if ( array [ i ] . type === linetype . proc ) {
171+ proc = array [ i ]
172+ }
173+ if ( array [ i ] . type === linetype . endp && proc ?. name === array [ i ] . name ) {
174+ let _name = array [ i ] . name
175+ if ( proc ?. name && _name ) {
176+ let range :vscode . Range = new vscode . Range ( proc ?. line , proc ?. index , array [ i ] . line , array [ i ] . index + _name . length )
177+ let srange :vscode . Range = new vscode . Range ( proc . line , proc . index , proc ?. line , proc ?. index + proc ?. name ?. length )
178+ procschild . push ( new vscode . DocumentSymbol ( proc ?. name , doc [ proc ?. line ] , SymbolVSCfy ( symboltype . procedure ) , range , srange ) )
179+ }
180+ }
181+ //寻找段结束语句
166182 if ( array [ i ] . type === linetype . ends && array [ i ] . name === line . name ) {
167183 line_ends = array [ i ]
168184 break
@@ -172,26 +188,42 @@ export function sacnDoc(document:vscode.TextDocument) : vscode.DocumentSymbol[]
172188 if ( line . name && line_ends ?. line ) {
173189 let range = new vscode . Range ( line . line , line . index , line_ends ?. line , line_ends ?. index )
174190 symbols . push ( new TasmSymbol ( symboltype . segment , line . name , range ) )
175- let varlabel :vscode . DocumentSymbol [ ] = [ ]
176191 let symbol1 = new vscode . DocumentSymbol ( line . name + ": " + getType ( KeywordType . Segment ) , " " , SymbolVSCfy ( symboltype . segment ) , range , new vscode . Range ( line . line , line . index , line . line , line . index + line . name . length ) )
177- symbol1 . children = varlabel
192+ symbol1 . children = procschild
178193 docsymbol . push ( symbol1 )
179194 }
180195 }
181196 }
182197 )
183- docsymbol . forEach (
184- ( item ) => {
185- // if(item.kind==SymbolVSCfy(symboltype.macro)){
186- // let symbol3:vscode.DocumentSymbol|undefined
187- // for (i=item.range.start.line;i<=item.range.end.line;i++){
188- // symbol3=getvarlabel(doc[i],i)
189- // if(symbol3)item.children.push(symbol3)
190- // }
191- // }
192- item . children . push ( new vscode . DocumentSymbol ( "fk" , "fake" , 1 , new vscode . Range ( 1 , 1 , 1 , 1 ) , new vscode . Range ( 1 , 1 , 1 , 1 ) ) )
198+ docsymbol . forEach (
199+ ( item ) => {
200+ //将宏指令范围内的变量和标号添加到宏
201+ if ( item . kind == SymbolVSCfy ( symboltype . macro ) ) {
202+ let symbol3 :vscode . DocumentSymbol | undefined
203+ for ( i = item . range . start . line ; i <= item . range . end . line ; i ++ ) {
204+ symbol3 = getvarlabel ( doc [ i ] , i )
205+ if ( symbol3 ) item . children . push ( symbol3 )
206+ }
207+ }
208+ //将变量,标号添加到逻辑段和子程序
209+ else if ( item . kind == SymbolVSCfy ( symboltype . segment ) ) {
210+ let symbol2 :vscode . DocumentSymbol | undefined
211+ item . children . forEach (
212+ ( item2 , index , array ) => {
213+ for ( i = item2 . range . start . line ; i <= item2 . range . end . line ; i ++ ) {
214+ let symbol3 = getvarlabel ( doc [ i ] , i )
215+ doc [ i ] = " "
216+ if ( symbol3 ) item2 . children . push ( symbol3 )
217+ }
218+ } ,
219+ )
220+ for ( i = item . range . start . line + 1 ; i < item . range . end . line ; i ++ ) {
221+ symbol2 = getvarlabel ( doc [ i ] , i )
222+ if ( symbol2 ) item . children . push ( symbol2 )
223+ }
193224 }
194- )
225+ }
226+ )
195227 return docsymbol
196228 }
197229
0 commit comments