Home Download Buy Blog Forum Support

Strange self.view.file_name()

Strange self.view.file_name()

Postby LONGMAN on Tue Mar 19, 2013 11:20 am

In command type plugin self.view.file_name() returns None if view is not focused. How I can get current file name without focusing?
AutoBackups: ST2 / ST3
CodeFormatter: ST2 / ST3
StringUtilities: ST2 / ST3
LONGMAN
 
Posts: 68
Joined: Mon Nov 26, 2012 5:03 pm
Location: Tbilisi, Georgia

Re: Strange self.view.file_name()

Postby bizoo on Tue Mar 19, 2013 11:40 am

ST3 ?
I'm sure it works in ST2, otherwise some of my plugins couldn't work anymore.

Post your code, it will help to understand the problem.
bizoo
 
Posts: 876
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Strange self.view.file_name()

Postby LONGMAN on Tue Mar 19, 2013 12:13 pm

bizoo wrote:ST3 ?
I'm sure it works in ST2, otherwise some of my plugins couldn't work anymore.

Post your code, it will help to understand the problem.

No, ST2
Code: Select all
class TestCommand(sublime_plugin.TextCommand):
   def run(self, edit):
            file_name = self.view.file_name()
            print file_name

If view out of focus (if cursor in the console), file_name is None
AutoBackups: ST2 / ST3
CodeFormatter: ST2 / ST3
StringUtilities: ST2 / ST3
LONGMAN
 
Posts: 68
Joined: Mon Nov 26, 2012 5:03 pm
Location: Tbilisi, Georgia

Re: Strange self.view.file_name()

Postby bizoo on Tue Mar 19, 2013 12:36 pm

LONGMAN wrote:If view out of focus (if cursor in the console), file_name is None

:D
So it means that self.view is the console, which has no file_name.

What do you want to do ?
You probably want to create a sublime_plugin.WindowCommand and use self.window.views().
bizoo
 
Posts: 876
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Strange self.view.file_name()

Postby robertcollier4 on Tue Mar 19, 2013 1:00 pm

Try: sublime.active_window().active_view()

I wrote a TextCommand type plugin (MarkFromFindpanel) in which i was able to use self.view.substr(self.view.full_line(0)) to get the text in the find panel and then use sublime.active_window().active_view().add_regions to act upon the file while the focus was still in the find panel. Tested that this works in ST3.
robertcollier4
 
Posts: 154
Joined: Sun Feb 24, 2013 5:37 pm

Re: Strange self.view.file_name()

Postby LONGMAN on Tue Mar 19, 2013 1:17 pm

I want get open (active) file content, independent of cursor destination
AutoBackups: ST2 / ST3
CodeFormatter: ST2 / ST3
StringUtilities: ST2 / ST3
LONGMAN
 
Posts: 68
Joined: Mon Nov 26, 2012 5:03 pm
Location: Tbilisi, Georgia

Re: Strange self.view.file_name()

Postby bizoo on Tue Mar 19, 2013 1:28 pm

Using your example code above, I will try something like (totally untested):
Code: Select all
class TestWinCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window,active_view()
        if view:
            view.run_command('test')

And call the test_win command in place of your test command.
Or insert your code directly in the TestWinCommand, depend of what you want to do.
bizoo
 
Posts: 876
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Strange self.view.file_name()

Postby LONGMAN on Tue Mar 19, 2013 2:26 pm

bizoo wrote:Using your example code above, I will try something like (totally untested):
Code: Select all
class TestWinCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window,active_view()
        if view:
            view.run_command('test')

And call the test_win command in place of your test command.
Or insert your code directly in the TestWinCommand, depend of what you want to do.

Thanks for answer. Now I have not edit variable in run function and I can't replace text in view.
view.replace(edit, file_text, new_text) not works
AutoBackups: ST2 / ST3
CodeFormatter: ST2 / ST3
StringUtilities: ST2 / ST3
LONGMAN
 
Posts: 68
Joined: Mon Nov 26, 2012 5:03 pm
Location: Tbilisi, Georgia

Re: Strange self.view.file_name()

Postby robertcollier4 on Tue Mar 19, 2013 2:54 pm

LONGMAN wrote:I want get open (active) file content, independent of cursor destination

sublime.active_window().active_view()
robertcollier4
 
Posts: 154
Joined: Sun Feb 24, 2013 5:37 pm

Re: Strange self.view.file_name()

Postby bizoo on Tue Mar 19, 2013 3:15 pm

LONGMAN wrote:
bizoo wrote:Using your example code above, I will try something like (totally untested):
Code: Select all
class TestWinCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window,active_view()
        if view:
            view.run_command('test')

And call the test_win command in place of your test command.
Or insert your code directly in the TestWinCommand, depend of what you want to do.

Thanks for answer. Now I have not edit variable in run function and I can't replace text in view.
view.replace(edit, file_text, new_text) not works

There's no edit in WindowCommand, you can create one yourself but it's incompatible with ST3.
This is why the WindowCommand (test_win) run the TextCommand (test).
You can use the solution from robertcollier4 too.

Without knowing what you want to do, I couldn't tell you what's the best solution (IMHO).
bizoo
 
Posts: 876
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 11 guests