Sublime Forum

[SOLVED] API Request: view.set_data() and view.get_data()

#1

Hi,

I understand sublime text already persists some information tied to a single view (Bookmarks, caret location, sublime. PERSISTENT regions, etc)

I have a few plugin ideas but they require saving view-specific data. Here are the following challenges I am facing:

  • Load data by using an event listener whenever a view is opened
  • Figure out a robust way to see if the view has had any data saved before (I use a key-value dictionary with the MD5 hash of the filename as the key)
  • Whenever set_data is called, save in the background (making sure to keep a backup)
  • If the same file is open in two views, am not sure how to handle it - though they are two separate views, they are the same file.

I am requesting three functions to add to the view API:

view.get_data(key)
view.set_data(key, value)
view.get_all_data()

Edit:
Turns out I can just use view.settings() to do exactly this! I wasn’t aware of it

Thanks!

0 Likes

#2

sublime_plugin.EventListener
on_load(view)

Why not using directly the filename.lower() ?

[quote=“skyronic”]* Whenever set_data is called, save in the background (making sure to keep a backup)

  • If the same file is open in two views, am not sure how to handle it - though they are two separate views, they are the same file.[/quote]

Why not using view.settings() ?

  • They are persistent across session, but are discarded when the view is closed.
  • Cloned view share the same settings() (not sure it is what you want).
0 Likes

#3

[quote]I understand sublime text already persists some information tied to a single view (Bookmarks, caret location, sublime. PERSISTENT regions, etc)
[/quote]

That is only true, when closing and opening the application for bookmark, caret location and regions. When you close and open a file you lost everything.

0 Likes

#4

[quote=“bizoo”]
Why not using view.settings() ?

  • They are persistent across session, but are discarded when the view is closed.
  • Cloned view share the same settings() (not sure it is what you want).[/quote]

Oh my! I didn’t see view.settings(). This is exactly what I was looking for. I guess it didn’t hit me that I can just use this.

Thanks!

0 Likes

#5

Also, don’t forget there’s view.id() and view.buffer_id(), which returns a unique integer that identifies the view (or the buffer, which will be shared between cloned views)

1 Like

#6

Hi Jon,

Unfortunately, Turns out the real thing which I wanted is to store per-file metadata, rather than a per-view metadata. Even view.id() doesn’t help me here, I will need to manually look at each of the view.file_name() to build my plugin.

0 Likes