@@ -15,7 +15,8 @@ const autoLinker = require('autolinker');
1515const hljs = require ( 'highlight.js' ) ;
1616
1717export class HttpResponseTextDocumentContentProvider extends BaseTextDocumentContentProvider {
18- private static cssFilePath : string = path . join ( extensions . getExtension ( Constants . ExtensionId ) . extensionPath , Constants . CSSFolderName , Constants . CSSFileName ) ;
18+ private static cssFilePath : string = Uri . file ( path . join ( extensions . getExtension ( Constants . ExtensionId ) . extensionPath , Constants . CSSFolderName , Constants . CSSFileName ) ) . toString ( ) ;
19+ private static scriptFilePath : string = Uri . file ( path . join ( extensions . getExtension ( Constants . ExtensionId ) . extensionPath , Constants . ScriptsFolderName , Constants . ScriptFileName ) ) . toString ( ) ;
1920
2021 public constructor ( public settings : RestClientSettings ) {
2122 super ( ) ;
@@ -40,7 +41,7 @@ export class HttpResponseTextDocumentContentProvider extends BaseTextDocumentCon
4041 }
4142 return `
4243 <head>
43- <link rel="stylesheet" href="${ HttpResponseTextDocumentContentProvider . cssFilePath } ">
44+ <link rel="stylesheet" type="text/css" href="${ HttpResponseTextDocumentContentProvider . cssFilePath } ">
4445 ${ this . getSettingsOverrideStyles ( width ) }
4546 </head>
4647 <body>
@@ -50,6 +51,7 @@ export class HttpResponseTextDocumentContentProvider extends BaseTextDocumentCon
5051 : this . addUrlLinks ( innerHtml ) }
5152 <a id="scroll-to-top" role="button" aria-label="scroll to top" onclick="scroll(0,0)"><span class="icon"></span></a>
5253 </div>
54+ <script type="text/javascript" src="${ HttpResponseTextDocumentContentProvider . scriptFilePath } " charset="UTF-8"></script>
5355 </body>` ;
5456 }
5557 }
@@ -116,11 +118,11 @@ ${HttpResponseTextDocumentContentProvider.formatHeaders(response.headers)}`;
116118 this . settings . fontWeight ? `font-weight: ${ this . settings . fontWeight } ;` : '' ,
117119 '}' ,
118120 'code .line {' ,
119- `padding-left: calc(${ width } ch + 18px );` ,
121+ `padding-left: calc(${ width } ch + 20px );` ,
120122 '}' ,
121123 'code .line:before {' ,
122124 `width: ${ width } ch;` ,
123- `margin-left: calc(-${ width } ch + -27px );` ,
125+ `margin-left: calc(-${ width } ch + -30px );` ,
124126 '}' ,
125127 '</style>' ] . join ( '\n' ) ;
126128 }
@@ -133,9 +135,16 @@ ${HttpResponseTextDocumentContentProvider.formatHeaders(response.headers)}`;
133135 code = code . split ( / \r \n | \r | \n / ) ;
134136 let max = ( 1 + code . length ) . toString ( ) . length ;
135137
138+ const foldingRanges = this . getFoldingRange ( code ) ;
139+
136140 code = code
137141 . map ( function ( line , i ) {
138- return '<span class="line width-' + max + '" start="' + ( 1 + i ) + '">' + line + '</span>' ;
142+ const lineNum = i + 1 ;
143+ const range = foldingRanges . has ( lineNum )
144+ ? ` range-start="${ foldingRanges . get ( lineNum ) . start } " range-end="${ foldingRanges . get ( lineNum ) . end } "`
145+ : '' ;
146+ const folding = foldingRanges . has ( lineNum ) ? '<span class="icon"></span>' : '' ;
147+ return `<span class="line width-${ max } " start="${ lineNum } "${ range } >${ line } ${ folding } </span>` ;
139148 } )
140149 . join ( '\n' ) ;
141150 return code ;
@@ -178,6 +187,32 @@ ${HttpResponseTextDocumentContentProvider.formatHeaders(response.headers)}`;
178187 } ) ;
179188 }
180189
190+ private getFoldingRange ( lines : string [ ] ) : Map < number , FoldingRange > {
191+ const result = new Map < number , FoldingRange > ( ) ;
192+ const stack = [ ] ;
193+
194+ const leadingSpaceCount = lines
195+ . map ( ( line , index ) => [ index , line . search ( / \S / ) ] )
196+ . filter ( ( [ , num ] ) => num !== - 1 ) ;
197+ for ( const [ index , [ lineIndex , count ] ] of leadingSpaceCount . entries ( ) ) {
198+ if ( index === 0 ) {
199+ continue ;
200+ }
201+
202+ const [ prevLineIndex , prevCount ] = leadingSpaceCount [ index - 1 ] ;
203+ if ( prevCount < count ) {
204+ stack . push ( [ prevLineIndex , prevCount ] ) ;
205+ } else if ( prevCount > count ) {
206+ let prev ;
207+ while ( ( prev = stack . slice ( - 1 ) [ 0 ] ) && ( prev [ 1 ] >= count ) ) {
208+ stack . pop ( ) ;
209+ result . set ( prev [ 0 ] + 1 , new FoldingRange ( prev [ 0 ] + 1 , lineIndex ) ) ;
210+ }
211+ }
212+ }
213+ return result ;
214+ }
215+
181216 private static formatHeaders ( headers : { [ key : string ] : string } ) : string {
182217 let headerString = '' ;
183218 for ( let header in headers ) {
@@ -212,4 +247,9 @@ ${HttpResponseTextDocumentContentProvider.formatHeaders(response.headers)}`;
212247 return null ;
213248 }
214249 }
250+ }
251+
252+ class FoldingRange {
253+ public constructor ( public start : number , public end : number ) {
254+ }
215255}
0 commit comments