-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathatomUtils.js
263 lines (263 loc) · 8.79 KB
/
atomUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var path = require("path");
var fs = require("fs");
var fsu = require("../utils/fsUtil");
var _atom = require("atom");
var url = require("url");
function getEditorPosition(editor) {
var bufferPos = editor.getCursorBufferPosition();
return getEditorPositionForBufferPosition(editor, bufferPos);
}
exports.getEditorPosition = getEditorPosition;
function getEditorPositionForBufferPosition(editor, bufferPos) {
var buffer = editor.getBuffer();
return buffer.characterIndexForPosition(bufferPos);
}
exports.getEditorPositionForBufferPosition = getEditorPositionForBufferPosition;
function isAllowedExtension(ext) {
return (ext == '.ts' || ext == '.tst' || ext == '.tsx');
}
exports.isAllowedExtension = isAllowedExtension;
function isActiveEditorOnDiskAndTs() {
var editor = atom.workspace.getActiveTextEditor();
return onDiskAndTs(editor);
}
exports.isActiveEditorOnDiskAndTs = isActiveEditorOnDiskAndTs;
function onDiskAndTs(editor) {
if (editor instanceof require('atom').TextEditor) {
var filePath = editor.getPath();
if (!filePath) {
return false;
}
var ext = path.extname(filePath);
if (isAllowedExtension(ext)) {
if (fs.existsSync(filePath)) {
return true;
}
}
}
return false;
}
exports.onDiskAndTs = onDiskAndTs;
function onDiskAndTsRelated(editor) {
if (editor instanceof require('atom').TextEditor) {
var filePath = editor.getPath();
if (!filePath) {
return false;
}
var ext = path.extname(filePath);
if (isAllowedExtension(ext)) {
if (fs.existsSync(filePath)) {
return true;
}
}
if (filePath.endsWith('tsconfig.json')) {
return true;
}
}
return false;
}
exports.onDiskAndTsRelated = onDiskAndTsRelated;
function getFilePathPosition() {
var editor = atom.workspace.getActiveTextEditor();
var filePath = editor.getPath();
var position = getEditorPosition(editor);
return { filePath: filePath, position: position };
}
exports.getFilePathPosition = getFilePathPosition;
function getFilePath() {
var editor = atom.workspace.getActiveTextEditor();
var filePath = editor.getPath();
return { filePath: filePath };
}
exports.getFilePath = getFilePath;
function getEditorsForAllPaths(filePaths) {
var map = {};
var activeEditors = atom.workspace.getTextEditors().filter(function (editor) { return !!editor.getPath(); });
function addConsistentlyToMap(editor) {
map[fsu.consistentPath(editor.getPath())] = editor;
}
activeEditors.forEach(addConsistentlyToMap);
var newPaths = filePaths.filter(function (p) { return !map[p]; });
if (!newPaths.length)
return Promise.resolve(map);
var promises = newPaths.map(function (p) { return atom.workspace.open(p, {}); });
return Promise.all(promises).then(function (editors) {
editors.forEach(addConsistentlyToMap);
return map;
});
}
exports.getEditorsForAllPaths = getEditorsForAllPaths;
function getRangeForTextSpan(editor, ts) {
var buffer = editor.buffer;
var start = editor.buffer.positionForCharacterIndex(ts.start);
var end = editor.buffer.positionForCharacterIndex(ts.start + ts.length);
var range = new _atom.Range(start, end);
return range;
}
exports.getRangeForTextSpan = getRangeForTextSpan;
function getTypeScriptEditorsWithPaths() {
return atom.workspace.getTextEditors()
.filter(function (editor) { return !!editor.getPath(); })
.filter(function (editor) { return (path.extname(editor.getPath()) === '.ts'); });
}
exports.getTypeScriptEditorsWithPaths = getTypeScriptEditorsWithPaths;
function getOpenTypeScritEditorsConsistentPaths() {
return getTypeScriptEditorsWithPaths().map(function (e) { return fsu.consistentPath(e.getPath()); });
}
exports.getOpenTypeScritEditorsConsistentPaths = getOpenTypeScritEditorsConsistentPaths;
function quickNotifySuccess(htmlMessage) {
var notification = atom.notifications.addSuccess(htmlMessage, { dismissable: true });
setTimeout(function () {
notification.dismiss();
}, 800);
}
exports.quickNotifySuccess = quickNotifySuccess;
function quickNotifyWarning(htmlMessage) {
var notification = atom.notifications.addWarning(htmlMessage, { dismissable: true });
setTimeout(function () {
notification.dismiss();
}, 800);
}
exports.quickNotifyWarning = quickNotifyWarning;
function formatCode(editor, edits) {
for (var i = edits.length - 1; i >= 0; i--) {
var edit = edits[i];
editor.setTextInBufferRange([[edit.start.line, edit.start.col], [edit.end.line, edit.end.col]], edit.newText);
}
}
exports.formatCode = formatCode;
function kindToColor(kind) {
switch (kind) {
case 'interface':
return 'rgb(16, 255, 0)';
case 'keyword':
return 'rgb(0, 207, 255)';
case 'class':
return 'rgb(255, 0, 194)';
default:
return 'white';
}
}
exports.kindToColor = kindToColor;
function kindToType(kind) {
switch (kind) {
case 'interface':
return 'type';
case 'identifier':
return 'variable';
case 'local function':
return 'function';
case 'local var':
return 'variable';
case 'var':
case 'parameter':
return 'variable';
case 'alias':
return 'import';
case 'type parameter':
return 'type';
default:
return kind.split(' ')[0];
}
}
exports.kindToType = kindToType;
function commandForTypeScript(e) {
var editor = atom.workspace.getActiveTextEditor();
if (!editor)
return e.abortKeyBinding() && false;
var ext = path.extname(editor.getPath());
if (!isAllowedExtension(ext))
return e.abortKeyBinding() && false;
return true;
}
exports.commandForTypeScript = commandForTypeScript;
function getCurrentPath() {
var editor = atom.workspace.getActiveTextEditor();
return fsu.consistentPath(editor.getPath());
}
exports.getCurrentPath = getCurrentPath;
exports.knownScopes = {
reference: 'reference.path.string',
require: 'require.path.string',
es6import: 'es6import.path.string'
};
function editorInTheseScopes(matches) {
var editor = atom.workspace.getActiveTextEditor();
var scopes = editor.getLastCursor().getScopeDescriptor().scopes;
var lastScope = scopes[scopes.length - 1];
if (matches.some(function (p) { return lastScope === p; }))
return lastScope;
else
return '';
}
exports.editorInTheseScopes = editorInTheseScopes;
function getActiveEditor() {
return atom.workspace.getActiveTextEditor();
}
exports.getActiveEditor = getActiveEditor;
function uriForPath(uriProtocol, filePath) {
return uriProtocol + "//" + filePath;
}
exports.uriForPath = uriForPath;
function registerOpener(config) {
atom.commands.add(config.commandSelector, config.commandName, function (e) {
if (!commandForTypeScript(e))
return;
var uri = uriForPath(config.uriProtocol, getCurrentPath());
var old_pane = atom.workspace.paneForURI(uri);
if (old_pane) {
old_pane.destroyItem(old_pane.itemForUri(uri));
}
atom.workspace.open(uri, config.getData());
});
atom.workspace.addOpener(function (uri, data) {
try {
var protocol = url.parse(uri).protocol;
}
catch (error) {
return;
}
if (protocol !== config.uriProtocol) {
return;
}
return config.onOpen(data);
});
}
exports.registerOpener = registerOpener;
function triggerLinter() {
atom.commands.dispatch(atom.views.getView(atom.workspace.getActiveTextEditor()), 'linter:lint');
}
exports.triggerLinter = triggerLinter;
function getFilePathRelativeToAtomProject(filePath) {
filePath = fsu.consistentPath(filePath);
return '~' + atom.project.relativize(filePath);
}
exports.getFilePathRelativeToAtomProject = getFilePathRelativeToAtomProject;
function openFile(filePath, position) {
if (position === void 0) { position = {}; }
var config = {};
if (position.line) {
config.initialLine = position.line - 1;
}
if (position.col) {
config.initialColumn = position.col;
}
atom.workspace.open(filePath, config);
}
exports.openFile = openFile;
var _snippetsManager;
function _setSnippetsManager(snippetsManager) {
_snippetsManager = snippetsManager;
}
exports._setSnippetsManager = _setSnippetsManager;
function insertSnippet(snippet, editor, cursor) {
if (_snippetsManager) {
_snippetsManager.insertSnippet(snippet, editor, cursor);
}
else {
console.error('Why no snippet manager?');
}
}
exports.insertSnippet = insertSnippet;