core-uri-handlers.js
1 const fs = require('fs-plus'); 2 3 // Converts a query string parameter for a line or column number 4 // to a zero-based line or column number for the Atom API. 5 function getLineColNumber(numStr) { 6 const num = parseInt(numStr || 0, 10); 7 return Math.max(num - 1, 0); 8 } 9 10 function openFile(atom, { query }) { 11 const { filename, line, column } = query; 12 13 atom.workspace.open(filename, { 14 initialLine: getLineColNumber(line), 15 initialColumn: getLineColNumber(column), 16 searchAllPanes: true 17 }); 18 } 19 20 function windowShouldOpenFile({ query }) { 21 const { filename } = query; 22 const stat = fs.statSyncNoException(filename); 23 24 return win => 25 win.containsLocation({ 26 pathToOpen: filename, 27 exists: Boolean(stat), 28 isFile: stat.isFile(), 29 isDirectory: stat.isDirectory() 30 }); 31 } 32 33 const ROUTER = { 34 '/open/file': { handler: openFile, getWindowPredicate: windowShouldOpenFile } 35 }; 36 37 module.exports = { 38 create(atomEnv) { 39 return function coreURIHandler(parsed) { 40 const config = ROUTER[parsed.pathname]; 41 if (config) { 42 config.handler(atomEnv, parsed); 43 } 44 }; 45 }, 46 47 windowPredicate(parsed) { 48 const config = ROUTER[parsed.pathname]; 49 if (config && config.getWindowPredicate) { 50 return config.getWindowPredicate(parsed); 51 } else { 52 return () => true; 53 } 54 } 55 };