{"size": 4054, "ext": "js", "max_stars_repo_path": "dot_config/oni2/extensions/aki77.rails-db-schema-0.2.3/out/Commands.js", "max_stars_repo_name": "filiperocha/dotfiles_old", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "dot_config/oni2/extensions/aki77.rails-db-schema-0.2.3/out/Commands.js", "max_issues_repo_name": "filiperocha/dotfiles_old", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "dot_config/oni2/extensions/aki77.rails-db-schema-0.2.3/out/Commands.js", "max_forks_repo_name": "filiperocha/dotfiles_old", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 38.9807692308, "max_line_length": 118, "alphanum_fraction": 0.5493339911}
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const vscode_1 = require("vscode");
const sortOn = require("sort-on");
class Commands {
    constructor(schema) {
        this.schema = schema;
        this.accept = () => {
            const selectedItem = this.quickPick.selectedItems[0];
            if (this.quickPick.step === 1) {
                this.showColumns(selectedItem.label);
            }
            else {
                this.insertColumn(selectedItem.label);
            }
        };
        this.quickPick = vscode_1.window.createQuickPick();
        this.quickPick.onDidAccept(this.accept);
        this.quickPick.totalSteps = 2;
    }
    open(tableName) {
        return __awaiter(this, void 0, void 0, function* () {
            if (tableName) {
                yield this.openSchema(tableName);
                return;
            }
            const selected = yield vscode_1.window.showQuickPick(this.schema.getTableNames());
            if (!selected) {
                return;
            }
            yield this.openSchema(selected);
        });
    }
    insert() {
        return __awaiter(this, void 0, void 0, function* () {
            const items = this.schema
                .getTableNames()
                .map(tableName => ({ label: tableName }));
            this.quickPick.busy = true;
            this.quickPick.value = "";
            this.quickPick.placeholder = "Select a Table";
            this.quickPick.step = 1;
            this.quickPick.show();
            this.quickPick.items = sortOn(items, "label");
            this.quickPick.busy = false;
        });
    }
    openSchema(tableName) {
        return __awaiter(this, void 0, void 0, function* () {
            const table = this.schema.getTable(tableName);
            if (!table) {
                return;
            }
            const editor = yield vscode_1.window.showTextDocument(this.schema.getUri());
            const position = new vscode_1.Position(table.line, 0);
            editor.selection = new vscode_1.Selection(position, position);
            editor.revealRange(editor.selection, vscode_1.TextEditorRevealType.InCenter);
        });
    }
    showColumns(tableName) {
        const table = this.schema.getTable(tableName);
        if (!table) {
            return;
        }
        const items = Array.from(table.columns.values()).map(column => ({
            label: column.name,
            description: column.lineText
        }));
        this.quickPick.busy = true;
        this.quickPick.value = "";
        this.quickPick.step = 2;
        this.quickPick.placeholder = "Select a Column";
        this.quickPick.items = sortOn(items, "label");
        this.quickPick.busy = false;
    }
    insertColumn(column) {
        return __awaiter(this, void 0, void 0, function* () {
            const editor = vscode_1.window.activeTextEditor;
            if (!editor) {
                return;
            }
            this.quickPick.hide();
            yield editor.edit(edit => {
                if (editor.selection.isEmpty) {
                    edit.insert(editor.selection.active, column);
                }
                else {
                    edit.replace(editor.selection, column);
                }
            });
        });
    }
}
exports.default = Commands;
//# sourceMappingURL=Commands.js.map