mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
The URI query string should specify line and column numbers as a user would, starting at 1, while the Atom API starts at 0.
46 lines
1.1 KiB
JavaScript
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
|
|
}
|
|
}
|
|
}
|