Smart Manoj
Smart Manoj

Reputation: 5824

Open file at specific line number using “open_file” command

My question is similar to the thread in SublimeHQ forum.

I have added some entries in my Default.sublime-commands file to open files, e.g.

{ "caption": "File Open: File Name", 
  "command": "open_file",
  "args": {"file": "/path/to/file.ext"} },

They work fine but I’d like one of them to open at a specific line number and can’t find the right notation.

This does NOT open the file at line 123, instead it opens the file path: /path/to/file.ext:123.

{ "caption": "File Open: File Name", 
  "command": "open_file",
  "args": {"file": "/path/to/file.ext:123"} },

They suggested:

Try adding “flags” : 1 to the argument list. The API open_file need to have the flag ENCODED_POSITION to extract the line/column from the file name, maybe it is the same.

{ "caption": "File Open: File Name", 
  "command": "open_file",
  "args": {"file": "/path/to/file.ext:123", "flags":1} },

But this also failed.

In sublime.py

def open_file(self, fname, flags=0, group=-1):

In api_reference

open_file(file_name, <flags>)

But in the args key we use file and not file_name & fname .

Where it is stated?

How to do this without creating a new plugin?

Upvotes: 1

Views: 363

Answers (1)

OdatNurd
OdatNurd

Reputation: 22791

The open_file command does indeed not handle file names with an encoded position at the end of them. That particular command is implemented in the core and not in a plugin, which means that it's not possible to introspect it's arguments and it also doesn't generate any sort of error if you attempt to invoke it with incorrect arguments. Thus it's not possible to determine if there is some way to invoke it in a way that would use the encoded position information short of manually trying arguments and values to see what they do or asking the developers to provide such information (or add it if it's not there).

The API reference is for actual plugins; it doesn't associate in any direct way with any commands that may happen to exist with the same name. The arguments that you see in the actual API definition generally differ from what is documented.

One reason for that is that API methods are generally class methods, which means they take an implied self argument when you declare them that's not needed when you call them. Additionally often arguments have a different documented name than actual name due to clarity, historical reasons, and so on. An example of that is the above difference between file_name and fname; unless you are invoking the method with keyword arguments, this difference doesn't matter.

There are also sometimes arguments to API methods that aren't documented, which could be for any number of reason. An example of that would be the group argument above, which determines in what file group the file opens; the API allows it, but unless you look at the appropriate file, you don't know it's possible to use it.

This is all a long winded way of saying that as far as I'm aware there is no way to get around having to use a plugin for this. The one in the forum post you linked is one example of that; a vaguely simpler example would be:

import sublime
import sublime_plugin


class OpenFileEncodedCommand(sublime_plugin.WindowCommand):
    def run(self, file):
        self.window.open_file(file, sublime.ENCODED_POSITION)

This implements an open_file_encoded command that allow you to add :row or :row:col to the filename you provide. This is slightly different than the plugin linked above in that it takes explicit row and col arguments to specify the position.

In practice one over the other is all down to how you want to specify the position information.

Upvotes: 3

Related Questions