Skip to content

Instantly share code, notes, and snippets.

@polyvertex
Created March 7, 2020 07:35
Show Gist options
  • Save polyvertex/1dd583b189bd1a46b43ebec2ee8717c4 to your computer and use it in GitHub Desktop.
Save polyvertex/1dd583b189bd1a46b43ebec2ee8717c4 to your computer and use it in GitHub Desktop.
import os.path
import sublime
import sublime_plugin
# build 3176: this does not work properly. self.view.set_name() works but it
# seems to sort-of "unlinks" the view from its physical file path, so that if
# the "Save" command is invoked upon this view, a "Save As" dialog will pop up
# even though the view was loaded from an existing file.
# def _dbg(*obj):
# import datetime
# print(str(datetime.datetime.now()) + ':', *obj)
class ViewAutoName(sublime_plugin.ViewEventListener):
@classmethod
def is_applicable(cls, settings):
return True
@classmethod
def applies_to_primary_view_only(cls):
return False
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._display_name = None
def on_load(self):
# _dbg('ON_LOAD')
self._auto_name()
def _auto_name(self):
if self._display_name:
if not self.view.name():
self.view.set_name(self._display_name)
else:
self._display_name = None
else:
path = self.view.file_name()
if not path:
return
view_name = self.view.name()
if view_name is not None and view_name[-4:] == '/.py':
return
file_name = os.path.basename(path)
if file_name != '__init__.py':
return
parent_dir_name = os.path.basename(os.path.dirname(path))
self._display_name = parent_dir_name + '/.py'
self.view.set_name(self._display_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment