1

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?

5
  • What is wrong with using my plugin which you referenced in your question? If you need help with how to install it, post in that thead and I'll give instructions. BTW when specifying args it is not necessary to specify the col it can be omitted (same goes for the row but that would be self-defeating. Commented Jun 16, 2019 at 12:30
  • @mattst Just need to know how to do without plugin and where commands are stated for that. If there is no way , then plugin is ok Commented Jun 16, 2019 at 12:48
  • There is no way to do it with native Sublime Text commands. In the thread you can see all the combinations that I tried and none worked. If there was an alternative way of doing it someone would have replied in the thread saying so. I think you'll need to use my plugin which solves the problem. Commented Jun 16, 2019 at 17:17
  • @mattst I will use your plugin. If there was no alternative way of doing it someone should reply that too in the thread . Thats why I posted here. Is there any way to find the available args for a command using api? Commented Jun 17, 2019 at 6:58
  • No there is no way to find available args using the API. As OdatNurd stated the Sublime Text NATIVE open_file command and the API open_file command are NOT the same thing despite having the same name. In all probability the native open_file command takes only 1 arg, the file path. One of the great things about Sublime Text is when you need extra or different functionality you can usually write a plugin to provide it; in this case the plugin took me only a few minutes to write and is just 4 lines of code. Commented Jun 17, 2019 at 10:21

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.