UNPKG

alm

Version:

The best IDE for TypeScript

93 lines (92 loc) 3.5 kB
"use strict"; /** * Maintains a ring of stuff that has been copy / cut so you can paste any of them 🌹 */ Object.defineProperty(exports, "__esModule", { value: true }); /** Imports */ var monacoUtils_1 = require("./monaco/monacoUtils"); var commands = require("./commands/commands"); var utils = require("../common/utils"); var ui = require("./ui"); var uix = require("./uix"); var clipboardRing = []; // New items at the head of the ring var maxItems = 10; var index = 0; function addToClipboardRing() { var codeEditor = uix.API.getFocusedCodeEditorIfAny(); if (!codeEditor) return; index = 0; // Reset seek index var selected = monacoUtils_1.getSelectionOrCurrentLine(codeEditor.editor); addSelected(selected); } exports.addToClipboardRing = addToClipboardRing; function addSelected(selected) { // Just prevents the item being added right next to each other var before = utils.rangeLimited({ num: index - 1, min: 0, max: clipboardRing.length - 1, loopAround: true }); var after = utils.rangeLimited({ num: index + 1, min: 0, max: clipboardRing.length - 1, loopAround: true }); if (clipboardRing[before] === selected || clipboardRing[after] === selected // Cause we will remove last if we get to max items, check second last too || clipboardRing[maxItems - 2] === selected) { return false; } clipboardRing.unshift(selected); if (clipboardRing.length > maxItems) { clipboardRing.pop(); } // console.log(clipboardRing,index); // DEBUG return true; } function pasteFromClipboardRing() { var codeEditor = uix.API.getFocusedCodeEditorIfAny(); if (!codeEditor) return; var selection = codeEditor.editor.getSelection(); var hasSelection = !selection.isEmpty(); if (!clipboardRing.length) { ui.notifyInfoQuickDisappear('Clipboard Ring Empty'); // TODO: hand over to os command + select if anything gets pasted return; } var item = clipboardRing[index]; var lines = item.split('\n'); var lastLineLength = lines[lines.length - 1].length; var doc = codeEditor.editor.getModel(); /** Find the start */ var from = { line: selection.startLineNumber - 1, ch: selection.startColumn - 1 }; /** Also add any current selection to the clipboard ring */ if (hasSelection) { var added = addSelected(codeEditor.editor.getModel().getValueInRange(selection)); // Add current selection to the ring if (added) { index++; } } // replace selection (if any) with a new one // have the new item selected var line = lines.length > 1 ? from.line + (lines.length - 1) : from.line; var ch = lines.length > 1 ? lastLineLength : from.ch + item.length; var to = { line: line, ch: ch }; monacoUtils_1.replaceSelection({ editor: codeEditor.editor, newText: item }); codeEditor.editor.setSelection({ startLineNumber: from.line + 1, startColumn: from.ch + 1, endLineNumber: to.line + 1, endColumn: to.ch + 1 }); // update the index (and loop around) index = utils.rangeLimited({ num: index + 1, min: 0, max: clipboardRing.length - 1, loopAround: true }); } exports.pasteFromClipboardRing = pasteFromClipboardRing; commands.copy.on(function () { addToClipboardRing(); }); commands.cut.on(function () { addToClipboardRing(); }); commands.pasteFromRing.on(function () { pasteFromClipboardRing(); });