Sublime Forum

Uniquely & Persistently Identifying a View

#1

Howdy,

I’m trying to identify a view in a way that works (i.e. such that the result is unique and doesn’t change) after Sublime Text (or the OS, for that matter) is restarted.

At first I thought some combination of view.window().id() and view.id() or view.buffer_id() could serve that purpose, but it looks like they aren’t persistent.

On the other hand, I’d prefer not to rely on view.file_name() or view.name() because they are both potentially ambiguous and/or not present.

What is the right way to do this, then?

Thank you,
Alvaro

0 Likes

#2

view.id() is newly created on each restart.

A way to reliably and uniquely reference views across restarts is to look at their index and their group, using window.get_view_index(view). Of course, this only holds true if the latest indices you saved are still correct and the user did not re-arrange tabs.

Another idea is to store a custom setting, preferrably time-dependent like a uuid4, in each view’s settings storage. Example: view.settings().set("custom_id", uuid.uuid4())

0 Likes

#3

Yes, this looks like the most reliable way, thanks. Why is it being time-dependent important—to minimize collisions?

Alvaro

0 Likes

#4

[quote=“ajg”]
Yes, this looks like the most reliable way, thanks. Why is it being time-dependent important—to minimize collisions?

Alvaro[/quote]

Yes, it’s probably the easiest way to create a truly unique identifier.

>>> [uuid.uuid4() for _ in range(10)] [UUID('cc7803ac-c587-4211-8e15-1c9b636f45e2'), UUID('e57fbfe3-10a7-4712-85b9-16dd563f3083'), UUID('dbc1daa0-85b1-4ed6-af40-fc7dced02280'), UUID('233e4cb0-9497-403b-a119-d7fa5eb79bdb'), UUID('b181aaa7-ea56-4953-9da2-b59be4915232'), UUID('b4f6e242-7d67-42bf-ae75-05762fd631fc'), UUID('da609bb4-d365-4841-9c04-29f3b03d9ade'), UUID('87088d2f-e773-4f5d-bc81-d02bed428b8a'), UUID('cd59b65c-4876-4665-9411-828eafae25c4'), UUID('d09d294d-512d-4190-bb6e-9c76dff4ab24')]

0 Likes