-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore-uri-handlers.js
55 lines (47 loc) · 1.3 KB
/
core-uri-handlers.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
const fs = require('fs-plus');
// Converts a query string parameter for a line or column number
// to a zero-based line or column number for the Atom API.
function getLineColNumber(numStr) {
const num = parseInt(numStr || 0, 10);
return Math.max(num - 1, 0);
}
function openFile(atom, { query }) {
const { filename, line, column } = query;
atom.workspace.open(filename, {
initialLine: getLineColNumber(line),
initialColumn: getLineColNumber(column),
searchAllPanes: true
});
}
function windowShouldOpenFile({ query }) {
const { filename } = query;
const stat = fs.statSyncNoException(filename);
return win =>
win.containsLocation({
pathToOpen: filename,
exists: Boolean(stat),
isFile: stat.isFile(),
isDirectory: stat.isDirectory()
});
}
const ROUTER = {
'/open/file': { handler: openFile, getWindowPredicate: windowShouldOpenFile }
};
module.exports = {
create(atomEnv) {
return function coreURIHandler(parsed) {
const config = ROUTER[parsed.pathname];
if (config) {
config.handler(atomEnv, parsed);
}
};
},
windowPredicate(parsed) {
const config = ROUTER[parsed.pathname];
if (config && config.getWindowPredicate) {
return config.getWindowPredicate(parsed);
} else {
return () => true;
}
}
};