Skip to content

Commit 9529fe6

Browse files
committed
Use setTextDocumentLanguage API to rewrite text document view rending logic Huachao#82
1 parent 37783ba commit 9529fe6

4 files changed

Lines changed: 28 additions & 8 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"url": "https://github.com/Huachao/vscode-restclient.git"
2525
},
2626
"engines": {
27-
"vscode": "^1.26.0"
27+
"vscode": "^1.28.0"
2828
},
2929
"categories": [
3030
"Other",

‎src/views/codeSnippetWebview.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class CodeSnippetWebview extends BaseWebview {
1818
super();
1919
}
2020

21-
public render(convertResult: string, title: string, lang: string) {
21+
public async render(convertResult: string, title: string, lang: string) {
2222
let panel: WebviewPanel;
2323
if (this.panels.length === 0) {
2424
panel = window.createWebviewPanel(

‎src/views/httpResponseTextDocumentView.ts‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
import { EOL } from 'os';
4-
import { ViewColumn, window, workspace } from 'vscode';
4+
import { languages, Position, Range, TextDocument, ViewColumn, window, workspace } from 'vscode';
55
import { Headers } from '../models/base';
66
import { RestClientSettings } from '../models/configurationSettings';
77
import { HttpResponse } from '../models/httpResponse';
@@ -13,15 +13,35 @@ export class HttpResponseTextDocumentView {
1313

1414
private readonly settings: RestClientSettings = RestClientSettings.Instance;
1515

16+
protected readonly documents: TextDocument[] = [];
17+
1618
public constructor() {
19+
workspace.onDidCloseTextDocument(e => {
20+
const index = this.documents.indexOf(e);
21+
if (index !== -1) {
22+
this.documents.splice(index, 1);
23+
}
24+
});
1725
}
1826

19-
public render(response: HttpResponse, column: ViewColumn) {
27+
public async render(response: HttpResponse, column: ViewColumn) {
2028
const content = this.getTextDocumentContent(response);
2129
const language = this.getVSCodeDocumentLanguageId(response);
22-
workspace.openTextDocument({ language, content }).then(document => {
23-
window.showTextDocument(document, { viewColumn: column, preserveFocus: !this.settings.previewResponsePanelTakeFocus, preview: !this.settings.showResponseInDifferentTab });
24-
});
30+
let document: TextDocument;
31+
if (this.settings.showResponseInDifferentTab || this.documents.length === 0) {
32+
document = await workspace.openTextDocument({ language, content });
33+
this.documents.push(document);
34+
await window.showTextDocument(document, { viewColumn: column, preserveFocus: !this.settings.previewResponsePanelTakeFocus, preview: false });
35+
} else {
36+
document = this.documents[this.documents.length - 1];
37+
languages.setTextDocumentLanguage(document, language);
38+
const editor = await window.showTextDocument(document, { viewColumn: column, preserveFocus: !this.settings.previewResponsePanelTakeFocus, preview: false });
39+
editor.edit(edit => {
40+
const startPosition = new Position(0, 0);
41+
const endPosition = document.lineAt(document.lineCount - 1).range.end;
42+
edit.replace(new Range(startPosition, endPosition), content);
43+
});
44+
}
2545
}
2646

2747
private getTextDocumentContent(response: HttpResponse): string {

‎src/views/httpResponseWebview.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class HttpResponseWebview extends BaseWebview {
3737
this.iconFilePath = Uri.file(path.join(this.extensionPath, Constants.ImagesFolderName, Constants.IconFileName));
3838
}
3939

40-
public render(response: HttpResponse, column: ViewColumn) {
40+
public async render(response: HttpResponse, column: ViewColumn) {
4141
let panel: WebviewPanel;
4242
if (this.settings.showResponseInDifferentTab || this.panels.length === 0) {
4343
panel = window.createWebviewPanel(

0 commit comments

Comments
 (0)