Sublime Forum

Bug in new_file?

#1

I posted this at StackOverflow when the forum was down. Hopefully I can get some more information here.

In Sublime Text 3 I have a few packages that open new files within the window. They have functions that look like:

class BufferCommand(sublime_plugin.WindowCommand): def run(self): view = self.window.new_file() ...

I grabbed this for something I am working on, and couldn’t get a new file to generate, even though that works for the plugin I snagged it from, and is similar to how it is used in Packages/Default/new_templates.py. After some more searching, I found the following code, and it works as expected

class TestCommand(sublime_plugin.TextCommand): def run(self,edit): view = self.view.window().new_file()

Can somebody tell me why?

0 Likes

#2

I was looking through other threads and found viewtopic.php?f=6&t=17579#p65921. Would that be why the second block works and not the first?

0 Likes

#3

The first one is a Window command and the second one is a text command. A window command is applied to the Window and the text command to a view.

You can always use, sublime.active_window().active_view()

0 Likes

#4

Tito, are you referring to the first or second post?

I scrapped the code and wrote it again by memory, choosing random variable names. It seems like it didn’t like the name of my variable, which caused me say “oh duh.”

This grabbing the currently active window correct? What I am working on is trying to extend the orgmode plugin and add an agenda view. Probably should add other things first, but eh, it is a project I can work on at night.

0 Likes

#5

just,
view = sublime.active_window().new_file()

0 Likes

#6

Basically, Window Commands run in context of a window, so they get a window set in a class attributes (accessable via self.window).
Text (or View) Commands run in the context of a view, so they get a view set in a class attribute (self.view). Text commands additionally get send an edit parameter in their run method.

The command type is determined by the class you are inheriting. There is sublime_plugin.WindowCommand and sublime_plugin.TextCommand.

I generally discourage from using sublime.active_window().active_view() because it limits automization and programmatical execution of the command on a non-active window or non-focused view. For obvious reasons, the active_* variants require you to have said window/view focused in order to work on them.

0 Likes