Sublime Forum

Strange self.view.file_name()

#1

In command type plugin self.view.file_name() returns None if view is not focused. How I can get current file name without focusing?

0 Likes

#2

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.

0 Likes

#3

[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

0 Likes

#4

:smiley:
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().

0 Likes

#5

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.

0 Likes

#6

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

0 Likes

#7

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.

0 Likes

#8

[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

0 Likes

#9

sublime.active_window().active_view()

0 Likes

#10

[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).

0 Likes

#11

@bizoo, I want take active view file content and replace with my text. But if cursor is not in view, I cant’t get and replace active view text

0 Likes