示例#1
0
    def load_favorites(cls, obj, clean=False):
        """Load favorites list."""

        errors = False
        try:
            with open(obj.file_name, "r") as f:
                # Allow C style comments and be forgiving of trailing commas
                content = sanitize_json(f.read(), True)
            file_list = json.loads(content)

            # Clean out dead links
            if clean:
                cls.clean_orphaned_favorites(file_list)
                cls.create_favorite_list(obj, file_list, force=True)

            # Update internal list and access times
            obj.last_access = getmtime(obj.file_name)
            obj.files = file_list
        except Exception:
            errors = True
            if cls.is_global_file(obj):
                error('Failed to load %s!' % basename(obj.file_name))
            else:
                error(
                    'Failed to load %s!\nDid you rename your project?\n'
                    'Try toggling "Per Projects" off and on and try again.' % basename(obj.file_name)
                )
        return errors
示例#2
0
    def load_favorites(cls, obj, clean=False):
        """Load favorites list."""

        errors = False
        try:
            file_list = cls.read_favs_file(obj.file_name)

            # Clean out dead links
            if clean:
                cls.clean_orphaned_favorites(file_list)
                cls.create_favorite_list(obj, file_list, force=True)

            # Update internal list and access times
            obj.last_access = os.path.getmtime(obj.file_name)
            obj.files = file_list
        except Exception:
            errors = True
            if cls.is_global_file(obj):
                error('Failed to load %s!' % os.path.basename(obj.file_name))
            else:
                error(
                    'Failed to load %s!\nDid you rename your project?\n'
                    'Try toggling "Per Projects" off and on and try again.' % os.path.basename(obj.file_name)
                )
        return errors
示例#3
0
    def create_group(self, value):
        """Create the specified group."""

        repeat = False
        if value == "":
            # Require an actual name
            error("Please provide a valid group name.")
            repeat = True
        elif Favs.exists(value, group=True):
            # Do not allow duplicates
            error("Group \"%s\" already exists." % value)
            repeat = True
        else:
            # Add group
            Favs.add_group(value)
            self.add(self.name, value)
        if repeat:
            # Ask again if name was not sufficient
            v = self.window.show_input_panel(
                "Create Group: ",
                "New Group",
                self.create_group,
                None,
                None
            )
            v.run_command("select_all")
示例#4
0
    def load_favorites(cls, obj, clean=False):
        errors = False
        try:
            with open(obj.file_name, "r") as f:
                # Allow C style comments and be forgiving of trailing commas
                content = sanitize_json(f.read(), True)
            file_list = json.loads(content)

            # TODO: remove this when enough time passes
            # Update version format
            if "version" not in file_list or file_list["version"] < FAVORITE_LIST_VERSION:
                cls.update_list_format(file_list)
                cls.create_favorite_list(obj, file_list, force=True)

            # Clean out dead links
            if clean:
                cls.clean_orphaned_favorites(file_list)
                cls.create_favorite_list(obj, file_list, force=True)

            # Update internal list and access times
            obj.last_access = getmtime(obj.file_name)
            obj.files = file_list
        except:
            errors = True
            if cls.is_global_file():
                error('Failed to load %s!' % basename(obj.file_name))
            else:
                error(
                    'Failed to load %s!\nDid you rename your project?\nTry toggling "Per Projects" off and on and try again.' % basename(obj.file_name)
                )
        return errors
示例#5
0
    def load_favorites(cls, obj, clean=False):
        """Load favorites list."""

        errors = False
        try:
            with open(obj.file_name, "r") as f:
                # Allow C style comments and be forgiving of trailing commas
                content = sanitize_json(f.read(), True)
            file_list = json.loads(content)

            # Clean out dead links
            if clean:
                cls.clean_orphaned_favorites(file_list)
                cls.create_favorite_list(obj, file_list, force=True)

            # Update internal list and access times
            obj.last_access = getmtime(obj.file_name)
            obj.files = file_list
        except Exception:
            errors = True
            if cls.is_global_file(obj):
                error('Failed to load %s!' % basename(obj.file_name))
            else:
                error('Failed to load %s!\nDid you rename your project?\n'
                      'Try toggling "Per Projects" off and on and try again.' %
                      basename(obj.file_name))
        return errors
    def open_file(self, value, group=False):
        """Open the file(s)."""

        if value == -1:
            return

        if value >= 0:
            active_group = self.window.active_group()
            if value < self.num_files or (group and value < self.num_files + 1):
                # Open global file, file in group, or all files in group
                names = []
                if group:
                    if value == 0:
                        # Open all files in group
                        names = [self.files[x][1] for x in range(0, self.num_files)]
                    else:
                        # Open file in group
                        names.append(self.files[value - 1][1])
                else:
                    # Open global file
                    names.append(self.files[value][1])

                # Iterate through file list ensure they load in proper view index order
                count = 0
                focus_view = None

                for n in names:
                    if os.path.exists(n):
                        view = self.window.open_file(n)
                        if view is not None:
                            focus_view = view
                            if active_group >= 0:
                                self.window.set_view_index(view, active_group, count)
                            count += 1
                    else:
                        error("The following file does not exist:\n%s" % n)
                if focus_view is not None:
                    # Horrible ugly hack to ensure opened file gets focus
                    def fn(focus_view):
                        """Ensure focus of view."""
                        self.window.focus_view(focus_view)
                        self.window.show_quick_panel(["None"], None)
                        self.window.run_command("hide_overlay")
                    sublime.set_timeout(lambda: fn(focus_view), 500)
            else:
                # Decend into group
                value -= self.num_files
                self.files = Favs.all_files(group_name=self.groups[value][0].replace("Group: ", "", 1))
                self.num_files = len(self.files)
                self.groups = []
                self.num_groups = 0

                # Show files in group
                if self.num_files:
                    self.window.show_quick_panel(
                        [["Open Group", ""]] + self.files,
                        lambda x: self.open_file(x, group=True)
                    )
                else:
                    error("No favorites found! Try adding some.")
    def edit_alias(self, value, group=False):
        """Edit alias."""

        if value == -1:
            return

        if value >= 0:
            if value < self.num_files or (group and value < self.num_files + 1):
                # Open global file, file in group, or all files in group
                name = self.files[value][0]

                self.current_index = value
                self.window.show_input_panel("Alias:", name, self.apply_alias, None, None)
            else:
                # Decend into group
                value -= self.num_files
                self.group_name = self.groups[value][0].replace("Group: ", "", 1)
                self.files = Favs.all_files(group_name=self.group_name)
                self.num_files = len(self.files)
                self.groups = []
                self.num_groups = 0

                # Show files in group
                if self.num_files:
                    self.window.show_quick_panel(
                        self.files,
                        lambda x: self.edit_alias(x, group=True)
                    )
                else:
                    error("No favorites found! Try adding some.")
    def add(self, names, group_name=None):
        """Add favorites."""

        disk_omit_count = 0
        added = 0
        # Iterate names and add them to group/global if not already added
        for n in names:
            if Favs.file_index(n, group_name=group_name) is None:
                if os.path.exists(n):
                    Favs.set(n, group_name=group_name)
                    added += 1
                else:
                    # File does not exist on disk; cannot add
                    disk_omit_count += 1
        if added:
            # Save if files were added
            Favs.save(True)
            if len(names) == 1 and settings().get('always_ask_alias', False):
                self.prompt_for_alias(os.path.basename(names[0]), group_name)

        if disk_omit_count:
            # Alert that files could be added
            if disk_omit_count == 1:
                message = "1 file does not exist on disk!"
            else:
                message = "%d file(s) do not exist on disk!" % disk_omit_count
            error(message)
示例#9
0
    def open_file(self, value, group=False):
        if value >= 0:
            active_group = self.window.active_group()
            if value < self.num_files or (group
                                          and value < self.num_files + 1):
                # Open global file, file in group, or all fiels in group
                names = []
                if group:
                    if value == 0:
                        # Open all files in group
                        names = [
                            self.files[x][1] for x in range(0, self.num_files)
                        ]
                    else:
                        # Open file in group
                        names.append(self.files[value - 1][1])
                else:
                    # Open global file
                    names.append(self.files[value][1])

                # Iterate through file list ensure they load in proper view index order
                count = 0
                focus_view = None
                for n in names:
                    if exists(n):
                        view = self.window.open_file(n)
                        if view is not None:
                            focus_view = view
                            if active_group >= 0:
                                self.window.set_view_index(
                                    view, active_group, count)
                            count += 1
                    else:
                        error("The following file does not exist:\n%s" % n)
                if focus_view is not None:
                    # Horrible ugly hack to ensure opened file gets focus
                    def fn(v):
                        self.window.focus_view(focus_view)
                        self.window.show_quick_panel(["None"], None)
                        self.window.run_command("hide_overlay")

                    sublime.set_timeout(lambda: fn(focus_view), 500)
            else:
                # Decend into group
                value -= self.num_files
                self.files = Favs.all_files(
                    group_name=self.groups[value][0].replace("Group: ", "", 1))
                self.num_files = len(self.files)
                self.groups = []
                self.num_groups = 0

                # Show files in group
                if self.num_files:
                    self.window.show_quick_panel(
                        [["Open Group", ""]] + self.files,
                        lambda x: self.open_file(x, group=True))
                else:
                    error("No favorites found! Try adding some.")
示例#10
0
 def run(self):
     if not Favs.load(win_id=self.window.id()):
         self.files = Favs.all_files()
         self.num_files = len(self.files)
         self.groups = Favs.all_groups()
         self.num_groups = len(self.groups)
         if self.num_files + self.num_groups > 0:
             self.window.show_quick_panel(self.files + self.groups,
                                          self.open_file)
         else:
             error("No favorites found! Try adding some.")
示例#11
0
    def run(self):
        win_id = self.window.id()

        # Try and toggle back to global first
        if not Favs.toggle_global(win_id):
            return

        # Try and toggle per project
        if Favs.toggle_per_projects(win_id):
            error('Could not find a project file!')
        else:
            Favs.open(win_id=self.window.id())
示例#12
0
    def run(self):
        win_id = self.window.id()

        # Try and toggle back to global first
        if not Favs.toggle_global(win_id):
            return

        # Try and toggle per project
        if Favs.toggle_per_projects(win_id):
            error('Could not find a project file!')
        else:
            Favs.open(win_id=self.window.id())
示例#13
0
 def run(self):
     if not Favs.load(win_id=self.window.id()):
         self.files = Favs.all_files()
         self.num_files = len(self.files)
         self.groups = Favs.all_groups()
         self.num_groups = len(self.groups)
         if self.num_files + self.num_groups > 0:
             self.window.show_quick_panel(
                 self.files + self.groups,
                 self.open_file
             )
         else:
             error("No favorites found! Try adding some.")
示例#14
0
    def run(self):
        if not Favs.load(win_id=self.window.id()):
            # Present both files and groups for removal
            self.files = Favs.all_files()
            self.num_files = len(self.files)
            self.groups = Favs.all_groups()
            self.num_groups = len(self.groups)

            # Show panel
            if self.num_files + self.num_groups > 0:
                self.window.show_quick_panel(self.files + self.groups,
                                             self.remove)
            else:
                error("No favorites to remove!")
示例#15
0
    def create_favorite_list(cls, obj, file_list, force=False):
        """Create the favorites list."""

        errors = False

        if not os.path.exists(obj.file_name) or force:
            try:
                # Save as a JSON file
                cls.write_favs_file(obj.file_name, file_list)
                obj.last_access = os.path.getmtime(obj.file_name)
            except Exception:
                error('Failed to write %s!' % os.path.basename(obj.file_name))
                errors = True
        return errors
示例#16
0
    def create_favorite_list(cls, obj, file_list, force=False):
        errors = False

        if not exists(obj.file_name) or force:
            try:
                # Save as a JSON file
                j = json.dumps(file_list, sort_keys=True, indent=4, separators=(',', ': '))
                with open(obj.file_name, 'w') as f:
                    f.write(j + "\n")
                obj.last_access = getmtime(obj.file_name)
            except:
                error('Failed to write %s!' % basename(obj.file_name))
                errors = True
        return errors
示例#17
0
    def create_favorite_list(cls, obj, file_list, force=False):
        """Create the favorites list."""

        errors = False

        if not os.path.exists(obj.file_name) or force:
            try:
                # Save as a JSON file
                cls.write_favs_file(obj.file_name, file_list)
                obj.last_access = os.path.getmtime(obj.file_name)
            except Exception:
                error('Failed to write %s!' % os.path.basename(obj.file_name))
                errors = True
        return errors
    def run(self):
        """Run the command."""

        if not Favs.load(win_id=self.window.id()):
            self.files = Favs.all_files()
            self.num_files = len(self.files)
            self.groups = Favs.all_groups()
            self.num_groups = len(self.groups)
            # initialize group to None, will be set if descending into a group
            self.group_name = None
            if self.num_files + self.num_groups > 0:
                self.window.show_quick_panel(self.files + self.groups,
                                             self.edit_alias)
            else:
                error("No favorites found! Try adding some.")
示例#19
0
    def run(self):
        if not Favs.load(win_id=self.window.id()):
            # Present both files and groups for removal
            self.files = Favs.all_files()
            self.num_files = len(self.files)
            self.groups = Favs.all_groups()
            self.num_groups = len(self.groups)

            # Show panel
            if self.num_files + self.num_groups > 0:
                self.window.show_quick_panel(
                    self.files + self.groups,
                    self.remove
                )
            else:
                error("No favorites to remove!")
    def create_favorite_list(cls, obj, file_list, force=False):
        """Create the favorites list."""

        errors = False

        if not exists(obj.file_name) or force:
            try:
                # Save as a JSON file
                j = json.dumps(file_list, sort_keys=True, indent=4, separators=(",", ": "))
                with open(obj.file_name, "w") as f:
                    f.write(j + "\n")
                obj.last_access = getmtime(obj.file_name)
            except Exception:
                error("Failed to write %s!" % basename(obj.file_name))
                errors = True
        return errors
    def run(self):
        """Run the command."""

        if not Favs.load(win_id=self.window.id()):
            self.files = Favs.all_files()
            self.num_files = len(self.files)
            self.groups = Favs.all_groups()
            self.num_groups = len(self.groups)
            # initialize group to None, will be set if descending into a group
            self.group_name = None
            if self.num_files + self.num_groups > 0:
                self.window.show_quick_panel(
                    self.files + self.groups,
                    self.edit_alias
                )
            else:
                error("No favorites found! Try adding some.")
示例#22
0
    def create_favorite_list(cls, obj, file_list, force=False):
        errors = False

        if not exists(obj.file_name) or force:
            try:
                # Save as a JSON file
                j = json.dumps(file_list,
                               sort_keys=True,
                               indent=4,
                               separators=(',', ': '))
                with open(obj.file_name, 'w') as f:
                    f.write(j + "\n")
                obj.last_access = getmtime(obj.file_name)
            except:
                error('Failed to write %s!' % basename(obj.file_name))
                errors = True
        return errors
示例#23
0
 def project_adjust(cls, obj, win_id, force=False):
     enabled = cls.is_project_tracked(obj, win_id)
     if enabled:
         project = cls.get_project(win_id)
         if project is not None:
             project_favs = splitext(project)[0] + "-favs.json"
         if not exists(project_favs) and not force:
             error('Cannot find favorite list!\nProject name probably changed.\nSwitching to global list.')
             obj.projects.remove(win_id)
             obj.file_name = obj.global_file
             obj.last_access = 0
         # Make sure project is the new target
         if project_favs != obj.file_name:
             obj.file_name = project_favs
             obj.last_access = 0
     elif not FavFileMgr.is_global_file(obj):
         obj.file_name = obj.global_file
         obj.last_access = 0
     return enabled
示例#24
0
 def add(self, names, group_name=None):
     disk_omit_count = 0
     added = 0
     # Iterate names and add them to group/global if not already added
     for n in names:
         if not Favs.exists(n, group_name=group_name):
             if exists(n):
                 Favs.set(n, group_name=group_name)
                 added += 1
             else:
                 # File does not exist on disk; cannot add
                 disk_omit_count += 1
     if added:
         # Save if files were added
         Favs.save(True)
     if disk_omit_count:
         # Alert that files could be added
         message = "1 file does not exist on disk!" if disk_omit_count == 1 else "%d file(s) do not exist on disk!" % disk_omit_count
         error(message)
示例#25
0
 def create_group(self, value):
     repeat = False
     if value == "":
         # Require an actual name
         error("Please provide a valid group name.")
         repeat = True
     elif Favs.exists(value, group=True):
         # Do not allow duplicates
         error("Group \"%s\" already exists.")
         repeat = True
     else:
         # Add group
         Favs.add_group(value)
         self.add(self.name, value)
     if repeat:
         # Ask again if name was not sufficient
         v = self.window.show_input_panel("Create Group: ", "New Group",
                                          self.create_group, None, None)
         v.run_command("select_all")
示例#26
0
 def add(self, names, group_name=None):
     disk_omit_count = 0
     added = 0
     # Iterate names and add them to group/global if not already added
     for n in names:
         if not Favs.exists(n, group_name=group_name):
             if exists(n):
                 Favs.set(n, group_name=group_name)
                 added += 1
             else:
                 # File does not exist on disk; cannot add
                 disk_omit_count += 1
     if added:
         # Save if files were added
         Favs.save(True)
     if disk_omit_count:
         # Alert that files could be added
         message = "1 file does not exist on disk!" if disk_omit_count == 1 else "%d file(s) do not exist on disk!" % disk_omit_count
         error(message)
    def remove(self, value, group=False, group_name=None):
        """Remove the favorite(s)."""

        if value >= 0:
            # Remove file from global, file from group list, or entire group
            if value < self.num_files or (group
                                          and value < self.num_files + 1):
                name = None
                if group:
                    if group_name is None:
                        return
                    if value == 0:
                        # Remove group
                        Favs.remove_group(group_name)
                        Favs.save(True)
                        return
                    else:
                        # Remove group file
                        name = self.files[value - 1][1]
                else:
                    # Remove global file
                    name = self.files[value][1]

                # Remove file and save
                Favs.remove(name, group_name=group_name)
                Favs.save(True)
            else:
                # Decend into group
                value -= self.num_files
                group_name = self.groups[value][0].replace("Group: ", "", 1)
                self.files = Favs.all_files(group_name=group_name)
                self.num_files = len(self.files)
                self.groups = []
                self.num_groups = 0
                # Show group files
                if self.num_files:
                    self.window.show_quick_panel(
                        [["Remove Group", ""]] + self.files, lambda x: self.
                        remove(x, group=True, group_name=group_name))
                else:
                    error("No favorites found! Try adding some.")
示例#28
0
    def remove(self, value, group=False, group_name=None):
        """Remove the favorite(s)."""

        if value >= 0:
            # Remove file from global, file from group list, or entire group
            if value < self.num_files or (group and value < self.num_files + 1):
                name = None
                if group:
                    if group_name is None:
                        return
                    if value == 0:
                        # Remove group
                        Favs.remove_group(group_name)
                        Favs.save(True)
                        return
                    else:
                        # Remove group file
                        name = self.files[value - 1][1]
                else:
                    # Remove global file
                    name = self.files[value][1]

                # Remove file and save
                Favs.remove(name, group_name=group_name)
                Favs.save(True)
            else:
                # Decend into group
                value -= self.num_files
                group_name = self.groups[value][0].replace("Group: ", "", 1)
                self.files = Favs.all_files(group_name=group_name)
                self.num_files = len(self.files)
                self.groups = []
                self.num_groups = 0
                # Show group files
                if self.num_files:
                    self.window.show_quick_panel(
                        [["Remove Group", ""]] + self.files,
                        lambda x: self.remove(x, group=True, group_name=group_name)
                    )
                else:
                    error("No favorites found! Try adding some.")
示例#29
0
    def load_favorite_files(cls, obj, force=False, clean=False, win_id=None):
        errors = False

        # Is project enabled
        FavProjects.project_adjust(obj, win_id, force)

        if not exists(obj.file_name):
            if force:
                # Create file list if it doesn't exist
                if cls.create_favorite_list(obj, {"version": 1, "files": [], "groups": {}}, force=True):
                    error('Failed to cerate %s!' % basename(obj.file_name))
                    errors = True
                else:
                    force = True
            else:
                errors = True

        # Only reload if file has been written since last access (or if forced reload)
        if not errors and (force or getmtime(obj.file_name) != obj.last_access):
            errors = cls.load_favorites(obj, clean=clean)
        return errors
示例#30
0
 def project_adjust(cls, obj, win_id, force=False):
     enabled = cls.is_project_tracked(obj, win_id)
     if enabled:
         project = cls.get_project(win_id)
         if project is not None:
             project_favs = splitext(project)[0] + "-favs.json"
         if not exists(project_favs) and not force:
             error(
                 'Cannot find favorite list!\nProject name probably changed.\nSwitching to global list.'
             )
             obj.projects.remove(win_id)
             obj.file_name = obj.global_file
             obj.last_access = 0
         # Make sure project is the new target
         if project_favs != obj.file_name:
             obj.file_name = project_favs
             obj.last_access = 0
     elif not FavFileMgr.is_global_file(obj):
         obj.file_name = obj.global_file
         obj.last_access = 0
     return enabled
    def run(self, **kwds):
        """Run the command."""
        group_name = kwds.get("group_name", None)
        open_direct = kwds.get("open_direct", False)

        if not Favs.load(win_id=self.window.id()):
            if group_name is not None and not Favs.exists(group_name, group=True):
                error("group_name: %s not exist yet!" % group_name)
                return

            self.files = Favs.all_files(group_name)
            self.num_files = len(self.files)
            self.groups = [] if group_name is not None else Favs.all_groups()
            self.num_groups = len(self.groups)
            if self.num_files + self.num_groups > 0:
                if open_direct is True:
                    if group_name is None:
                        error("must specify group_name for open_direct")
                    else:
                        self.open_file(0, group=True)
                    return

                self.window.show_quick_panel(
                    self.files + self.groups,
                    self.open_file
                )
            else:
                error("No favorites found! Try adding some.")
示例#32
0
    def load_favorites(cls, obj, clean=False):
        """Load favorites list."""

        errors = False
        try:
            file_list = cls.read_favs_file(obj.file_name)

            # Clean out dead links
            if clean:
                cls.clean_orphaned_favorites(file_list)
                cls.create_favorite_list(obj, file_list, force=True)

            # Update internal list and access times
            obj.last_access = os.path.getmtime(obj.file_name)
            obj.files = file_list
        except Exception:
            errors = True
            if cls.is_global_file(obj):
                error('Failed to load %s!' % os.path.basename(obj.file_name))
            else:
                error('Failed to load %s!\nDid you rename your project?\n'
                      'Try toggling "Per Projects" off and on and try again.' %
                      os.path.basename(obj.file_name))
        return errors