<repo_name>vthiery/vscode-prettify-selected-json<file_sep>/src/JsonPrettifier.ts
'use strict';
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';

// Prettify function
function prettify(textEditor, startLine, endLine) {
    var range = new vscode.Range(startLine, endLine);
    // Return if empty
    if (range.isEmpty) {
        return;
    }
    // Get text from range
    var ugly = textEditor.document.getText(range);

    // Prettify the selection
    let json = null;
    try {
        json = JSON.parse(ugly);
    } catch (e) {
        vscode.window.showInformationMessage(e.message);
        return;
    }
    // Use the tabSize defined in user settings
    const indentSpacing = vscode.window.activeTextEditor.options.tabSize;
    // Stringify the result
    let pretty = JSON.stringify(json, null, indentSpacing);

    textEditor.edit(builder => {
        builder.replace(range, pretty);
    });
}

// Prettify the active selection following the input method
function prettifyActive() {
    var textEditor = vscode.window.activeTextEditor;
    var selection = textEditor.selection;
    // Apply sort on our selection with a given method
    prettify(textEditor, selection.start, selection.end);
}

// Prettify
exports.prettify = prettifyActive;
<file_sep>/vsc-extension-quickstart.md
# Welcome to VS Code Prettify Selected JSON Extension

## What's in the folder
* This folder contains all of the files necessary for developing and packaging the extension
* `package.json` - describe the extension, compilation scripts and lists the dependencies,
* `src/extension.ts` - expose the commands to be called in VS Code,
* `src/JsonPrettifier.ts` - implements the actual JSON prettifier.

## Get up and running straight away
* press `F5` to open a new window with your extension loaded
* run your command from the command palette by pressing (`Ctrl+Shift+P` or `Cmd+Shift+P` on Mac) and typing `Prettify selected JSON`
* set breakpoints in your code inside the sources to debug the extension

## Make changes
* changes can be made via pull requests but should always stick to the original purpose of the extension.

<file_sep>/src/extension.ts
'use strict';
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';

var prettifier = require('./JsonPrettifier');

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
    var commands = [
            vscode.commands.registerCommand('prettifyJSON', prettifier.prettify)
        ];

    commands.forEach(
        function (command) {
            context.subscriptions.push(command);
        }
    );
}

// this method is called when your extension is deactivated
export function deactivate() {
}<file_sep>/README.md
# Visual Studio Code Prettify Selected JSON Extension

The purpose of this extension is to offer a JSON prettifier that will act on a given selection. Hence, one can only prettify
locally and keep one-line JSON and pretty JSON in the same file.

It is available on Visual Studio Marketplace

**[Prettify Selected JSON in Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=vthiery.prettify-selected-json)**

Or can be forked directly on github and packaged from the sources.

## Installation
* Install via package manager: hit F1 or "Ctrl + Shift + P", type install and then type Prettify Selected JSON and hit enter.
* Clone the git repository and package it up via [vsce](https://code.visualstudio.com/docs/tools/vscecli) for example.

## License
This extension is under MIT license.
