Skip to content

Commit afa6309

Browse files
d-akaraHuachao
authored andcommitted
Set language when opening new editor (#82)
* set language when opening new editor add request and timing info into response document make response info into comments * Added user preferences for new features * implemented review feedback changes
1 parent 9caf343 commit afa6309

File tree

4 files changed

+97
-9
lines changed

4 files changed

+97
-9
lines changed

package.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,17 @@
338338
"rest-client.previewResponseInUntitledDocument": {
339339
"type": "boolean",
340340
"default": false,
341-
"description": "Preview response in untitled document if set to true, otherwise displayed in html view"
341+
"description": "Preview response in untitle document if set to true, otherwise displayed in html view"
342+
},
343+
"rest-client.previewResponseSetUntitledDocumentLanguageByContentType": {
344+
"type": "boolean",
345+
"default": false,
346+
"description": "Attempt to automatically set document language based on Content-Type header when showing each response in new tab"
347+
},
348+
"rest-client.includeAdditionalInfoInResponse": {
349+
"type": "boolean",
350+
"default": false,
351+
"description": "Include additional information such as request URL and response time when preview is set to use untitled document"
342352
}
343353
}
344354
}

src/controllers/requestController.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ export class RequestController {
138138
if (this._restClientSettings.previewResponseInUntitledDocument) {
139139
UntitledFileContentProvider.createHttpResponseUntitledFile(
140140
response,
141-
this._restClientSettings.showResponseInDifferentTab
141+
this._restClientSettings.showResponseInDifferentTab,
142+
this._restClientSettings.previewResponseSetUntitledDocumentLanguageByContentType,
143+
this._restClientSettings.includeAdditionalInfoInResponse
142144
);
143145
} else {
144146
await commands.executeCommand('vscode.previewHtml', previewUri, ViewColumn.Two, `Response(${response.elapsedMillionSeconds}ms)`);

src/models/configurationSettings.ts

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export interface IRestClientSettings {
1616
environmentVariables: Map<string, Map<string, string>>;
1717
mimeAndFileExtensionMapping: Map<string, string>;
1818
previewResponseInUntitledDocument: boolean;
19+
previewResponseSetUntitledDocumentLanguageByContentType: boolean;
20+
includeAdditionalInfoInResponse: boolean;
21+
1922
}
2023

2124
export class RestClientSettings implements IRestClientSettings {
@@ -34,6 +37,8 @@ export class RestClientSettings implements IRestClientSettings {
3437
public environmentVariables: Map<string, Map<string, string>>;
3538
public mimeAndFileExtensionMapping: Map<string, string>;
3639
public previewResponseInUntitledDocument: boolean;
40+
public previewResponseSetUntitledDocumentLanguageByContentType: boolean;
41+
public includeAdditionalInfoInResponse: boolean;
3742

3843
public constructor() {
3944
workspace.onDidChangeConfiguration(() => {
@@ -62,6 +67,8 @@ export class RestClientSettings implements IRestClientSettings {
6267
this.mimeAndFileExtensionMapping = restClientSettings.get<Map<string, string>>("mimeAndFileExtensionMapping", new Map<string, string>());
6368

6469
this.previewResponseInUntitledDocument = restClientSettings.get<boolean>("previewResponseInUntitledDocument", false);
70+
this.previewResponseSetUntitledDocumentLanguageByContentType = restClientSettings.get<boolean>("previewResponseSetUntitledDocumentLanguageByContentType", false);
71+
this.includeAdditionalInfoInResponse = restClientSettings.get<boolean>("includeAdditionalInfoInResponse", false);
6572

6673
let httpSettings = workspace.getConfiguration('http');
6774
this.proxy = httpSettings.get<string>('proxy', undefined);

src/views/responseUntitledFileContentProvider.ts

+76-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import { EOL } from 'os';
77

88
export class UntitledFileContentProvider {
99
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';
1117
if (!createNewFile && UntitledFileContentProvider.createdFiles.length > 0) {
1218
let updateDocument = UntitledFileContentProvider.createdFiles.slice(-1)[0];
1319

@@ -17,24 +23,66 @@ export class UntitledFileContentProvider {
1723
textEditor.edit(edit => {
1824
// get previous response file Range
1925
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));
2228
});
2329
});
2430
return;
2531
}
2632
}
27-
workspace.openTextDocument({ 'language': 'http' }).then(document => {
33+
34+
workspace.openTextDocument({ 'language': language }).then(document => {
2835
UntitledFileContentProvider.createdFiles.push(document);
2936
window.showTextDocument(document, ViewColumn.Two, false).then(textEditor => {
3037
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));
3239
});
3340
});
3441
});
3542
}
3643

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) {
3886
let responseStatusLine = `HTTP/${response.httpVersion} ${response.statusCode} ${response.statusMessage}${EOL}`;
3987
let headers = '';
4088
for (var header in response.headers) {
@@ -47,6 +95,27 @@ export class UntitledFileContentProvider {
4795
}
4896
}
4997
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] : '';
51120
}
52121
}

0 commit comments

Comments
 (0)