Sublime Forum

View.set_name in 2.x alpha

#1

when I call view.set_name(‘name’), it marks the buffer as dirty and will no longer save to the original filename.
is there any way currently around this, or could there be in the future? I’m working on a plugin to distinguish between identically-named files in the file list - I want to be able to change their name but still save them as the original file.

0 Likes

#2

The view name is the same as the file name, so when you change the name of the view you’re changing the name of the file.
A possible workaround:
Check if the buffer is clean. If it is, then after view.set_name, save the buffer so it still seems clean. This will have the side-effect of creating a new file.
Listen for on_pre_save, and in the callback set the view name back to its original, save the view, and change the name back to the new name (which will then be saved).
Listen for on_close, and in the callback delete the new file. This will leave you with just the original-named file, but still allow it to have have a different name while it’s being edited.

I haven’t tried it, but I think that should work. Good luck!

0 Likes

#3

I tried on_pre_save, doesn’t seem to trigger in time.
do we have any plugins changing the view name? if it doesn’t break anything in existence, I’d be for a separation of view name and file name - give us a way to set the title and the file it will save to.

0 Likes

#4

It doesn’t trigger in time? Could you just listen to on_post_save and do things in a different order?
I agree it would be cool if view names and file names could be separate. It would also be cool if identically-named files automatically had part of their path names added to their view names so that you could tell them apart.

0 Likes

#5

that’s exactly what I was working on. it’s working but going to save the file will prompt you to choose where to save… making it much less seamless, even if I were to swap it back on post_save. Jon? :smile:

for anyone curious, my renaming method is as follows:

  1. get the name of all views (I use a hack for this, but it should be in the api in the future :stuck_out_tongue:)
  2. group any views with the same filename
  3. find the common suffix of each group (by reversing and using os.path.commonprefix)
  4. chop the common suffix, jump to the closest directory separator, hack the pathname based on conditions:
    a) if there are one or more folders in the common suffix, prefix /…/ to the filename, otherwise prefix /
    b) add the topmost folder name before this

this logic produces results as follows:
input:

  1. foo/bar/common.py
  2. foo/baz/common.py
  3. bar/foo/common.py

output (instead of just showing common.py):

  1. bar/common.py
  2. baz/common.py
  3. foo/common.py

input:

  1. foo/common/init.py
  2. bar/common/init.py
  3. baz/common/init.py

output (instead of just showing init.py):

  1. foo/…/init.py
  2. bar/…/init.py
  3. baz/…/init.py
0 Likes

#6

How you change view display name?
in ST3 3083 it seems flash file_name too:

view.file_name()
‘/home/kes/.config/sublime-text-3/Packages/rsub/zzz’

view.set_name( ‘hello’ )
view.file_name()

0 Likes