Use URL query parameter syntax to pass in options when opening a file


#1

Right now when you open a file path from the command line you can include add :<line>:<column> to the end of the path and the editor will be opened with the cursor on that line/column position.

This is useful, but are also other options that could be useful to set on the editor when opening from the command line. And others still could be added such as encoding, grammar, etc. And beyond that for people like me who are building custom editors there might be more options still.

Because of these reasons I think it would be useful to have a generic syntax for adding options to a file path when opening it. And in particular I think URL query parameters would work well. There’s an issue and some discussion here:

At the same time making this change to Atom’s core would add some additional constraints/expectations on how path loading should work, and it’s unclear if people want these changes in the first place.

For that reason I’ve closed that issue, I’m posting about it here as a reference point in case anyone wants something similar in the future. Also in the meantime here’s a workaround solution that’s working pretty well for me:

Opener that parses URL query parameters off into options

It’s an opener that works by parsing the incoming filePath as a URL. If the filePath isn’t normalized (i.e. the incoming filePath doesn’t match the parsedURL.pathName) then it moves the URL query variables into the options and passes the now normalized URL back through atom.workspace.open.

This solves almost all of my needs. I still think it would be a bit nicer and more consistent if Atom were doing this for me as it already handles :line:column path metadata. There is for example some browser process logic about which window paths should be opened in that my paths miss out on. But generally this solution is good enough for me.

atom.workspace.addOpener (filePath, options) ->
  parsedURL = url.parse(filePath, true)

  if parsedURL.protocol is 'file:' or not parsedURL.protocol
    decodedPath = decodeURI(parsedURL.pathname)

    unless path.extname(decodedPath) is '.toml'
      return
    if filePath isnt decodedPath
      options.hash ?= parsedURL.hash
      options[key] ?= value for key, value of parsedURL.query
      atom.workspace.open(decodedPath, options)
    else
      new TomlEditor(filePath, options)

Edited to decode pathname

With that in place you could then on command line:

atom README.toml
atom README.toml?key=value
atom file:///home/README.toml?key2=value2

And they will all open to the same editor and the key values will be passed in as options.


#2

Opps that isn’t quite right. I was testing with workspace.open and it works there. But if you include file: or any URL protocol in a command line path it gets mangled. For example:

atom http://test.com

Is passed to the opener as the path:

/Users/jesse/Desktop/http:/test.com

@leedohm does that seem right? Maybe the browser side shouldn’t manipulate the path unless it looks like a filesystem path?

Jesse


#3

I don’t know that Atom currently supports accepting URLs from the command line. So, for seeming “right” … yes, that doesn’t necessarily surprise me. Though, yes, that should be added too if someone wants to make all this work.


#4

This lack of url means you can’t open a page in the web-browser package. I posted an issue on this a long time ago.