Sublime Forum

Find_open_file with multiple views

#1

Is there a way to get all the views associated with an open file? The find_open_file method only returns a single view, and I don’t see a way to retrieve all the views for the underlying buffer.

In particular, I want add_regions to update all views for a file. It appears that regions are unique per view.

0 Likes

#2

As far as I’m aware the only way to find all of the views that represent the same file is to compare the buffer_id() value of all views (either in a particular window or among the results of sublime.windows()); that will be unique among all views that represent the same file.

For example, this command will print to the console the list of view objects in the current window that represent the same file as the view that the command is executed in:

import sublime
import sublime_plugin


def views_for_file(window, file_name):
    view = window.find_open_file(file_name)
    if view is None:
        return None

    return [v for v in window.views() if v.buffer_id() == view.buffer_id()]


class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        print(views_for_file(self.view.window(), self.view.file_name()))

    def is_enabled(self):
        return self.view.file_name() is not None

Similarly if you already have a view (as the command here does) you could just compare the buffer_id() directly without having to do a lookup.

1 Like