In command type plugin self.view.file_name() returns None if view is not focused. How I can get current file name without focusing?
Strange self.view.file_name()
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.
[quote=“bizoo”]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.[/quote]
No, ST2
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
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().
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.
Using your example code above, I will try something like (totally untested):
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.
[quote=“bizoo”]Using your example code above, I will try something like (totally untested):
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.[/quote]
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
[quote=“LONGMAN”]
[quote=“bizoo”]Using your example code above, I will try something like (totally untested):
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.[/quote]
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[/quote]
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).