|
| 1 | +"use strict"; |
| 2 | +var fs_1 = require('fs'); |
| 3 | +var exists_1 = require('../services/exists'); |
| 4 | +var getEditorCount = 0; |
| 5 | +function save() { |
| 6 | + var editor = findEditor(); |
| 7 | + editor.save(); |
| 8 | +} |
| 9 | +exports.save = save; |
| 10 | +function findEditor() { |
| 11 | + var editor = atom.workspace.getActiveTextEditor(); |
| 12 | + var max = 1000; |
| 13 | + if (!editor) { |
| 14 | + getEditorCount += 1; |
| 15 | + setTimeout(function () { |
| 16 | + return findEditor(); |
| 17 | + }, 10); |
| 18 | + } |
| 19 | + else if (getEditorCount > max) { |
| 20 | + console.log('Failed to find active editor'); |
| 21 | + return null; |
| 22 | + } |
| 23 | + else { |
| 24 | + getEditorCount = 0; |
| 25 | + return editor; |
| 26 | + } |
| 27 | +} |
| 28 | +exports.findEditor = findEditor; |
| 29 | +function getEditor() { |
| 30 | + return new Promise(function (resolve, reject) { |
| 31 | + resolve(findEditor()); |
| 32 | + }); |
| 33 | +} |
| 34 | +exports.getEditor = getEditor; |
| 35 | +function open(filePath, options) { |
| 36 | + if (options === void 0) { options = {}; } |
| 37 | + if (exists_1.fileExists(filePath)) { |
| 38 | + fs_1.unlink(filePath); |
| 39 | + } |
| 40 | + atom.workspace.open(filePath, options); |
| 41 | +} |
| 42 | +exports.open = open; |
| 43 | +function set(text) { |
| 44 | + return getEditor().then(function (editor) { |
| 45 | + editor.setText(text); |
| 46 | + editor.insertNewline(); |
| 47 | + editor.moveToBottom(); |
| 48 | + editor.save(); |
| 49 | + setCursorPosition(editor); |
| 50 | + }); |
| 51 | +} |
| 52 | +exports.set = set; |
| 53 | +function insert(text, options) { |
| 54 | + if (options === void 0) { options = {}; } |
| 55 | + options = Object.assign(options, { |
| 56 | + autoIndent: true, |
| 57 | + }); |
| 58 | + return (getEditor().then(function insertWithEditor(editor) { |
| 59 | + editor.moveToBottom(); |
| 60 | + editor.insertText(text, options); |
| 61 | + editor.insertNewline(); |
| 62 | + editor.moveToBottom(); |
| 63 | + editor.save(); |
| 64 | + setCursorPosition(editor); |
| 65 | + })); |
| 66 | +} |
| 67 | +exports.insert = insert; |
| 68 | +var cursor = /::\s?>/g; |
| 69 | +function setCursorPosition(editor) { |
| 70 | + editor.scan(cursor, function (scanned) { |
| 71 | + editor.setCursorScreenPosition(scanned.range.start); |
| 72 | + scanned.replace(''); |
| 73 | + scanned.stop(); |
| 74 | + }); |
| 75 | +} |
0 commit comments