<commit_before>"use strict";

import { TextDocumentContentProvider, EventEmitter, Event, Uri, window, extensions } from 'vscode';
import { HttpResponse } from '../models/httpResponse'
import { MimeUtility } from '../mimeUtility'
import * as Constants from '../constants'
import * as path from 'path'

const hljs = require('highlight.js');
const codeHighlightLinenums = require('code-highlight-linenums');

var pd = require('pretty-data').pd;

export class HttpResponseTextDocumentContentProvider implements TextDocumentContentProvider {
    private static cssFilePath: string = path.join(extensions.getExtension(Constants.ExtensionId).extensionPath, Constants.CSSFolderName, Constants.CSSFileName);

    private _onDidChange = new EventEmitter<Uri>();
    response: HttpResponse;

    constructor(response: HttpResponse) {
        this.response = response;
    }

    public provideTextDocumentContent(uri: Uri): string {
        if (this.response) {
            let code = `HTTP/${this.response.httpVersion} ${this.response.statusCode} ${this.response.statusMessage}
${HttpResponseTextDocumentContentProvider.formatHeaders(this.response.headers)}
${HttpResponseTextDocumentContentProvider.formatBody(this.response.body, this.response.getResponseHeaderValue("content-type"))}`;
            return `
            <head>
                <link rel="stylesheet" href="${HttpResponseTextDocumentContentProvider.cssFilePath}">
            </head>
            <body>
                <div>
                    <pre><code class="http">${codeHighlightLinenums(code, { hljs: hljs, lang: 'http', start: 1 })}</code></pre>
                </div>
            </body>`;
        }
    }

    get onDidChange(): Event<Uri> {
        return this._onDidChange.event;
    }

    public update(uri: Uri) {
        this._onDidChange.fire(uri);
    }

    private static formatHeaders(headers: { [key: string]: string }): string {
        let headerString = '';
        for (var header in headers) {
            if (headers.hasOwnProperty(header)) {
                let value = headers[header];
                if (typeof headers[header] === 'string') {
                    value = <string>headers[header];
                }
                headerString += `${header}: ${value}\n`;
            }
        }
        return headerString;
    }

    private static formatBody(body: string, contentType: string): string {
        if (contentType) {
            let type = MimeUtility.parse(contentType).type;
            if (type === 'application/json') {
                if (HttpResponseTextDocumentContentProvider.isJsonString(body)) {
                    body = JSON.stringify(JSON.parse(body), null, 4);<commit_msg>Set json indentation to 2<commit_after>"use strict";

import { TextDocumentContentProvider, EventEmitter, Event, Uri, window, extensions } from 'vscode';
import { HttpResponse } from '../models/httpResponse'
import { MimeUtility } from '../mimeUtility'
import * as Constants from '../constants'
import * as path from 'path'

const hljs = require('highlight.js');
const codeHighlightLinenums = require('code-highlight-linenums');

var pd = require('pretty-data').pd;

export class HttpResponseTextDocumentContentProvider implements TextDocumentContentProvider {
    private static cssFilePath: string = path.join(extensions.getExtension(Constants.ExtensionId).extensionPath, Constants.CSSFolderName, Constants.CSSFileName);

    private _onDidChange = new EventEmitter<Uri>();
    response: HttpResponse;

    constructor(response: HttpResponse) {
        this.response = response;
    }

    public provideTextDocumentContent(uri: Uri): string {
        if (this.response) {
            let code = `HTTP/${this.response.httpVersion} ${this.response.statusCode} ${this.response.statusMessage}
${HttpResponseTextDocumentContentProvider.formatHeaders(this.response.headers)}
${HttpResponseTextDocumentContentProvider.formatBody(this.response.body, this.response.getResponseHeaderValue("content-type"))}`;
            return `
            <head>
                <link rel="stylesheet" href="${HttpResponseTextDocumentContentProvider.cssFilePath}">
            </head>
            <body>
                <div>
                    <pre><code class="http">${codeHighlightLinenums(code, { hljs: hljs, lang: 'http', start: 1 })}</code></pre>
                </div>
            </body>`;
        }
    }

    get onDidChange(): Event<Uri> {
        return this._onDidChange.event;
    }

    public update(uri: Uri) {
        this._onDidChange.fire(uri);
    }

    private static formatHeaders(headers: { [key: string]: string }): string {
        let headerString = '';
        for (var header in headers) {
            if (headers.hasOwnProperty(header)) {
                let value = headers[header];
                if (typeof headers[header] === 'string') {
                    value = <string>headers[header];
                }
                headerString += `${header}: ${value}\n`;
            }
        }
        return headerString;
    }

    private static formatBody(body: string, contentType: string): string {
        if (contentType) {
            let type = MimeUtility.parse(contentType).type;
            if (type === 'application/json') {
                if (HttpResponseTextDocumentContentProvider.isJsonString(body)) {
                    body = JSON.stringify(JSON.parse(body), null, 2);
{'fraction_non_alphanumeric': 0.12277448071216618, 'fraction_numerical': 0.000927299703264095, 'mean_word_length': 2.7581881533101047, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2270176', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1401, 'n_words': 309}