Files
atom/src/core-uri-handlers.js
Michelle Tilley b3e9989cd2 Translate line and column numbers from URI handlers
The URI query string should specify line and column numbers as a user
would, starting at 1, while the Atom API starts at 0.
2017-11-16 14:26:21 -08:00

46 lines
1.1 KiB
JavaScript

// 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
return (win) => win.containsPath(filename)
}
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 (win) => true
}
}
}