11'use strict' ;
22
33import { EOL } from 'os' ;
4- import { ViewColumn , window , workspace } from 'vscode' ;
4+ import { languages , Position , Range , TextDocument , ViewColumn , window , workspace } from 'vscode' ;
55import { Headers } from '../models/base' ;
66import { RestClientSettings } from '../models/configurationSettings' ;
77import { 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 {
0 commit comments