@@ -7,7 +7,13 @@ import { EOL } from 'os';
7
7
8
8
export class UntitledFileContentProvider {
9
9
private static createdFiles : TextDocument [ ] = [ ] ;
10
- public static createHttpResponseUntitledFile ( response : HttpResponse , createNewFile : boolean ) {
10
+ public static createHttpResponseUntitledFile ( response : HttpResponse , createNewFile : boolean , autoSetLanguage : boolean , additionalInfo : boolean ) {
11
+ // Currently unable to find a way to change language through API for already opened file.
12
+ // So when reusing same editor, default to 'http'
13
+ // This can be fixed when vscode issue #1800 is resolved
14
+ if ( ! createNewFile ) autoSetLanguage = false ; // override for now until vscode issue is resolved
15
+
16
+ const language = ( createNewFile && autoSetLanguage ) ? UntitledFileContentProvider . languageFromContentType ( response ) : 'http' ;
11
17
if ( ! createNewFile && UntitledFileContentProvider . createdFiles . length > 0 ) {
12
18
let updateDocument = UntitledFileContentProvider . createdFiles . slice ( - 1 ) [ 0 ] ;
13
19
@@ -17,24 +23,66 @@ export class UntitledFileContentProvider {
17
23
textEditor . edit ( edit => {
18
24
// get previous response file Range
19
25
var startPosition = new Position ( 0 , 0 ) ;
20
- var endPoistion = updateDocument . lineAt ( updateDocument . lineCount - 1 ) . range . end ;
21
- edit . replace ( new Range ( startPosition , endPoistion ) , UntitledFileContentProvider . formatResponse ( response ) ) ;
26
+ var endPosition = updateDocument . lineAt ( updateDocument . lineCount - 1 ) . range . end ;
27
+ edit . replace ( new Range ( startPosition , endPosition ) , UntitledFileContentProvider . formatResponse ( response , language , additionalInfo , autoSetLanguage ) ) ;
22
28
} ) ;
23
29
} ) ;
24
30
return ;
25
31
}
26
32
}
27
- workspace . openTextDocument ( { 'language' : 'http' } ) . then ( document => {
33
+
34
+ workspace . openTextDocument ( { 'language' : language } ) . then ( document => {
28
35
UntitledFileContentProvider . createdFiles . push ( document ) ;
29
36
window . showTextDocument ( document , ViewColumn . Two , false ) . then ( textEditor => {
30
37
textEditor . edit ( edit => {
31
- edit . insert ( new Position ( 0 , 0 ) , UntitledFileContentProvider . formatResponse ( response ) ) ;
38
+ edit . insert ( new Position ( 0 , 0 ) , UntitledFileContentProvider . formatResponse ( response , language , additionalInfo , autoSetLanguage ) ) ;
32
39
} ) ;
33
40
} ) ;
34
41
} ) ;
35
42
}
36
43
37
- private static formatResponse ( response : HttpResponse ) : string {
44
+ private static languageFromContentType ( response : HttpResponse ) : string {
45
+ let contentType = response . getResponseHeaderValue ( "Content-Type" ) ;
46
+ if ( ! contentType ) return 'http' ;
47
+
48
+ let types = [ 'xml' , 'json' , 'html' , 'css' ] ;
49
+ for ( let type of types ) {
50
+ if ( contentType . includes ( type ) ) return type ;
51
+ } ;
52
+
53
+ return 'http' ;
54
+ }
55
+
56
+ private static formatResponse ( response : HttpResponse , language : string , additionalInfo : boolean , autoSetLanguage : boolean ) {
57
+ const { responseStatusLine, headers, body} = UntitledFileContentProvider . extractStandardResponseInformation ( response ) ;
58
+ let responseInformation ;
59
+ if ( additionalInfo ) {
60
+ const formattedAdditionalInfo = UntitledFileContentProvider . formatAdditionalResponseInformation ( response ) ;
61
+ responseInformation = `${ responseStatusLine } ${ formattedAdditionalInfo } ${ headers } ` ;
62
+ } else {
63
+ responseInformation = `${ responseStatusLine } ${ headers } ` ;
64
+ }
65
+
66
+ return autoSetLanguage ? UntitledFileContentProvider . formatResponseForLanguage ( responseInformation , body , language ) :
67
+ UntitledFileContentProvider . formatResponseDefault ( responseInformation , body ) ;
68
+ }
69
+
70
+ private static formatResponseDefault ( responseInformation :string , responseBody : string ) {
71
+ return `${ responseInformation } ${ EOL } ${ responseBody } ` ;
72
+ }
73
+
74
+ private static formatResponseForLanguage ( responseInformation :string , responseBody : string , language : string ) {
75
+ let commentBegin = UntitledFileContentProvider . commentBegin ( language ) ;
76
+ let commentEnd = UntitledFileContentProvider . commentEnd ( language ) ;
77
+ return `${ commentBegin } ${ EOL } ${ responseInformation } ${ commentEnd } ${ EOL } ${ responseBody } ` ;
78
+ }
79
+ private static formatAdditionalResponseInformation ( response : HttpResponse ) {
80
+ let requestURL = `Request: ${ response . requestUrl } ${ EOL } ` ;
81
+ let elapsedTime = `Elapsed time: ${ response . elapsedMillionSeconds } ms${ EOL } ` ;
82
+ return `${ requestURL } ${ elapsedTime } ` ;
83
+ }
84
+
85
+ private static extractStandardResponseInformation ( response : HttpResponse ) {
38
86
let responseStatusLine = `HTTP/${ response . httpVersion } ${ response . statusCode } ${ response . statusMessage } ${ EOL } ` ;
39
87
let headers = '' ;
40
88
for ( var header in response . headers ) {
@@ -47,6 +95,27 @@ export class UntitledFileContentProvider {
47
95
}
48
96
}
49
97
let body = ResponseFormatUtility . FormatBody ( response . body , response . getResponseHeaderValue ( "content-type" ) ) ;
50
- return `${ responseStatusLine } ${ headers } ${ EOL } ${ body } ` ;
98
+ return { responseStatusLine, headers, body} ;
99
+ }
100
+
101
+ private static commentBegin ( language : string ) {
102
+ const REST_RESPONSE = 'REST Response Information:' ;
103
+ let commentStyle = {
104
+ xml : '<!-- ' ,
105
+ json : '/* ' ,
106
+ html : '<!-- ' ,
107
+ css : '/* '
108
+ }
109
+ return commentStyle [ language ] ? commentStyle [ language ] + REST_RESPONSE : '' ;
110
+ }
111
+
112
+ private static commentEnd ( language : string ) {
113
+ let commentStyle = {
114
+ xml : '-->' ,
115
+ json : '*/' ,
116
+ html : '-->' ,
117
+ css : '*/'
118
+ }
119
+ return commentStyle [ language ] ? commentStyle [ language ] : '' ;
51
120
}
52
121
}
0 commit comments