Merge pull request #8661 from atom/mb-fix-opening-files-with-line-numbers

Fix handling of filename:line:column command-line arguments
This commit is contained in:
Max Brunsfeld
2015-09-03 12:19:46 -07:00
2 changed files with 43 additions and 59 deletions

View File

@@ -17,6 +17,8 @@ url = require 'url'
{EventEmitter} = require 'events'
_ = require 'underscore-plus'
LocationSuffixRegExp = /(:\d+)(:\d+)?$/
DefaultSocketPath =
if process.platform is 'win32'
'\\\\.\\pipe\\atom-sock'
@@ -63,12 +65,11 @@ class AtomApplication
exit: (status) -> app.exit(status)
constructor: (options) ->
{@resourcePath, @version, @devMode, @safeMode, @socketPath} = options
{@resourcePath, @devResourcePath, @executedFrom, @version, @devMode, @safeMode, @socketPath} = options
global.atomApplication = this
@pidsToOpenWindows = {}
@pathsToOpen ?= []
@windows = []
@autoUpdateManager = new AutoUpdateManager(@version, options.test)
@@ -160,7 +161,7 @@ class AtomApplication
devMode: @focusedWindow()?.devMode
safeMode: @focusedWindow()?.safeMode
@on 'application:run-all-specs', -> @runSpecs(exitWhenDone: false, resourcePath: global.devResourcePath, safeMode: @focusedWindow()?.safeMode)
@on 'application:run-all-specs', -> @runSpecs(exitWhenDone: false, resourcePath: @devResourcePath, safeMode: @focusedWindow()?.safeMode)
@on 'application:run-benchmarks', -> @runBenchmarks()
@on 'application:quit', -> app.quit()
@on 'application:new-window', -> @openPath(_.extend(windowDimensions: @focusedWindow()?.getDimensions(), getLoadSettings()))
@@ -252,7 +253,7 @@ class AtomApplication
@applicationMenu.update(win, template, keystrokesByCommand)
ipc.on 'run-package-specs', (event, specDirectory) =>
@runSpecs({resourcePath: global.devResourcePath, specDirectory: specDirectory, exitWhenDone: false})
@runSpecs({resourcePath: @devResourcePath, specDirectory: specDirectory, exitWhenDone: false})
ipc.on 'command', (event, command) =>
@emit(command)
@@ -369,13 +370,8 @@ class AtomApplication
# :windowDimensions - Object with height and width keys.
# :window - {AtomWindow} to open file paths in.
openPaths: ({pathsToOpen, pidToKillWhenClosed, newWindow, devMode, safeMode, windowDimensions, profileStartup, window}={}) ->
pathsToOpen = pathsToOpen.map (pathToOpen) ->
if fs.existsSync(pathToOpen)
fs.normalize(pathToOpen)
else
pathToOpen
locationsToOpen = (@locationForPathToOpen(pathToOpen) for pathToOpen in pathsToOpen)
pathsToOpen = (locationToOpen.pathToOpen for locationToOpen in locationsToOpen)
unless pidToKillWhenClosed or newWindow
existingWindow = @windowForPaths(pathsToOpen, devMode)
@@ -398,8 +394,8 @@ class AtomApplication
else
if devMode
try
bootstrapScript = require.resolve(path.join(global.devResourcePath, 'src', 'window-bootstrap'))
resourcePath = global.devResourcePath
bootstrapScript = require.resolve(path.join(@devResourcePath, 'src', 'window-bootstrap'))
resourcePath = @devResourcePath
bootstrapScript ?= require.resolve('../window-bootstrap')
resourcePath ?= @resourcePath
@@ -500,7 +496,7 @@ class AtomApplication
resourcePath = @resourcePath
try
bootstrapScript = require.resolve(path.resolve(global.devResourcePath, 'spec', 'spec-bootstrap'))
bootstrapScript = require.resolve(path.resolve(@devResourcePath, 'spec', 'spec-bootstrap'))
catch error
bootstrapScript = require.resolve(path.resolve(__dirname, '..', '..', 'spec', 'spec-bootstrap'))
@@ -511,7 +507,7 @@ class AtomApplication
runBenchmarks: ({exitWhenDone, specDirectory}={}) ->
try
bootstrapScript = require.resolve(path.resolve(global.devResourcePath, 'benchmark', 'benchmark-bootstrap'))
bootstrapScript = require.resolve(path.resolve(@devResourcePath, 'benchmark', 'benchmark-bootstrap'))
catch error
bootstrapScript = require.resolve(path.resolve(__dirname, '..', '..', 'benchmark', 'benchmark-bootstrap'))
@@ -523,19 +519,20 @@ class AtomApplication
locationForPathToOpen: (pathToOpen) ->
return {pathToOpen} unless pathToOpen
return {pathToOpen} if url.parse(pathToOpen).protocol?
return {pathToOpen} if fs.existsSync(pathToOpen)
pathToOpen = pathToOpen.replace(/[:\s]+$/, '')
match = pathToOpen.match(LocationSuffixRegExp)
[fileToOpen, initialLine, initialColumn] = path.basename(pathToOpen).split(':')
return {pathToOpen} unless initialLine
return {pathToOpen} unless parseInt(initialLine) >= 0
if match?
pathToOpen = pathToOpen.slice(0, -match[0].length)
initialLine = Math.max(0, parseInt(match[1].slice(1)) - 1) if match[1]
initialColumn = Math.max(0, parseInt(match[2].slice(1)) - 1) if match[2]
else
initialLine = initialColumn = null
unless url.parse(pathToOpen).protocol?
pathToOpen = path.resolve(@executedFrom, fs.normalize(pathToOpen))
# Convert line numbers to a base of 0
initialLine = Math.max(0, initialLine - 1) if initialLine
initialColumn = Math.max(0, initialColumn - 1) if initialColumn
pathToOpen = path.join(path.dirname(pathToOpen), fileToOpen)
{pathToOpen, initialLine, initialColumn}
# Opens a native dialog to prompt the user for a path.

View File

@@ -5,23 +5,13 @@ app = require 'app'
fs = require 'fs-plus'
path = require 'path'
yargs = require 'yargs'
url = require 'url'
nslog = require 'nslog'
console.log = nslog
process.on 'uncaughtException', (error={}) ->
nslog(error.message) if error.message?
nslog(error.stack) if error.stack?
console.log = require 'nslog'
start = ->
setupUncaughtExceptionHandler()
setupAtomHome()
setupCompileCache()
if process.platform is 'win32'
SquirrelUpdate = require './squirrel-update'
squirrelCommand = process.argv[1]
return if SquirrelUpdate.handleStartupEvent(app, squirrelCommand)
return if handleStartupEventWithSquirrel()
args = parseCommandLine()
@@ -29,34 +19,21 @@ start = ->
event.preventDefault()
args.pathsToOpen.push(pathToOpen)
args.urlsToOpen = []
addUrlToOpen = (event, urlToOpen) ->
event.preventDefault()
args.urlsToOpen.push(urlToOpen)
app.on 'open-file', addPathToOpen
app.on 'open-url', addUrlToOpen
app.on 'will-finish-launching', ->
setupCrashReporter()
app.on 'will-finish-launching', setupCrashReporter
app.on 'ready', ->
app.removeListener 'open-file', addPathToOpen
app.removeListener 'open-url', addUrlToOpen
cwd = args.executedFrom?.toString() or process.cwd()
args.pathsToOpen = args.pathsToOpen.map (pathToOpen) ->
normalizedPath = fs.normalize(pathToOpen)
if url.parse(pathToOpen).protocol?
pathToOpen
else if cwd
path.resolve(cwd, normalizedPath)
else
path.resolve(pathToOpen)
AtomApplication = require path.join(args.resourcePath, 'src', 'browser', 'atom-application')
AtomApplication.open(args)
console.log("App load time: #{Date.now() - global.shellStartTime}ms") unless args.test
normalizeDriveLetterName = (filePath) ->
@@ -65,16 +42,22 @@ normalizeDriveLetterName = (filePath) ->
else
filePath
global.devResourcePath = normalizeDriveLetterName(
process.env.ATOM_DEV_RESOURCE_PATH ? path.join(app.getHomeDir(), 'github', 'atom')
)
setupUncaughtExceptionHandler = ->
process.on 'uncaughtException', (error={}) ->
console.log(error.message) if error.message?
console.log(error.stack) if error.stack?
handleStartupEventWithSquirrel = ->
return false unless process.platform is 'win32'
SquirrelUpdate = require './squirrel-update'
squirrelCommand = process.argv[1]
SquirrelUpdate.handleStartupEvent(app, squirrelCommand)
setupCrashReporter = ->
crashReporter.start(productName: 'Atom', companyName: 'GitHub')
setupAtomHome = ->
return if process.env.ATOM_HOME
atomHome = path.join(app.getHomeDir(), '.atom')
try
atomHome = fs.realpathSync(atomHome)
@@ -132,7 +115,7 @@ parseCommandLine = ->
process.stdout.write("#{version}\n")
process.exit(0)
executedFrom = args['executed-from']
executedFrom = args['executed-from']?.toString() ? process.cwd()
devMode = args['dev']
safeMode = args['safe']
pathsToOpen = args._
@@ -143,6 +126,8 @@ parseCommandLine = ->
logFile = args['log-file']
socketPath = args['socket-path']
profileStartup = args['profile-startup']
urlsToOpen = []
devResourcePath = process.env.ATOM_DEV_RESOURCE_PATH ? path.join(app.getHomeDir(), 'github', 'atom')
if args['resource-path']
devMode = true
@@ -158,7 +143,7 @@ parseCommandLine = ->
resourcePath = packageDirectoryPath if packageManifest.name is 'atom'
if devMode
resourcePath ?= global.devResourcePath
resourcePath ?= devResourcePath
unless fs.statSyncNoException(resourcePath)
resourcePath = path.dirname(path.dirname(__dirname))
@@ -168,8 +153,10 @@ parseCommandLine = ->
process.env.PATH = args['path-environment'] if args['path-environment']
resourcePath = normalizeDriveLetterName(resourcePath)
devResourcePath = normalizeDriveLetterName(devResourcePath)
{resourcePath, pathsToOpen, executedFrom, test, version, pidToKillWhenClosed,
devMode, safeMode, newWindow, specDirectory, logFile, socketPath, profileStartup}
{resourcePath, devResourcePath, pathsToOpen, urlsToOpen, executedFrom, test,
version, pidToKillWhenClosed, devMode, safeMode, newWindow, specDirectory,
logFile, socketPath, profileStartup}
start()