示例#1
0
    def browsing_on_done(self, index=None):
        if index == -1:
            return set_status(self.view, self.STATUS_KEY, '')

        if index == 0:
            # create from the position in the browser
            self.create_from = self.browser.path
            self.path_to_create_choosed_from_browsing = True
            return self.create_input()
        elif index == 1:
            self.browser.path = os.path.normpath(
                os.path.join(self.browser.path, '..'))
        elif index is not None:
            self.browser.path = os.path.join(self.browser.path,
                                             self.browser.items[index])

        if os.path.isfile(self.browser.path):
            self.window.open_file(self.browser.path)

        folders, files = [], []
        for item in os.listdir(self.browser.path):
            if os.path.isdir(os.path.join(self.browser.path, item)):
                folders.append(item + '/')
            else:
                files.append(item)

        self.browser.items = ['[cmd] Create from here', '[cmd] ..'
                              ] + folders + files

        set_status(
            self.view, self.STATUS_KEY,
            'Browsing at: {0}'.format(ph.user_friendly(self.browser.path)))

        self.window.show_quick_panel(self.browser.items, self.browsing_on_done,
                                     0, 2)
示例#2
0
    def run(self, paths=None):
        self.settings = get_settings()

        self.window = get_window()

        if paths is None:
            self.origin = self.window.active_view().file_name()
        else:
            self.origin = paths[0]

        initial_path = ph.user_friendly(self.origin)

        self.input = InputForPath(
            caption='Duplicate to: ',
            initial_text=initial_path,
            on_done=self.duplicate,
            on_change=None,
            on_cancel=None,
            create_from='',
            with_files=False,
            pick_first=self.settings.get('pick_first'),
            case_sensitive=self.settings.get('case_sensitive'),
            log_in_status_bar=self.settings.get('log_in_status_bar'),
            log_template='Duplicating at {0}',
            enable_browser=True)

        head = len(os.path.dirname(initial_path)) + 1
        filename = len(os.path.splitext(os.path.basename(initial_path))[0])
        self.input.input.view.selection.clear()
        self.input.input.view.selection.add(
            sublime.Region(head, head + filename))
示例#3
0
    def run(self, paths=None):
        self.settings = get_settings()
        self.window = get_window()
        self.view = self.window.active_view()

        if paths is not None:
            self.origins = paths
        else:
            self.origins = [self.view.file_name()]

        if len(self.origins) > 1:
            initial_text = ph.commonpath(self.origins)
        else:
            initial_text = os.path.dirname(self.origins[0])
        initial_text = ph.user_friendly(initial_text) + '/'

        ipt = InputForPath(
            caption='Move to',
            initial_text=initial_text,
            on_done=self.move,
            on_change=None,
            on_cancel=None,
            create_from='',
            with_files=self.settings.get('complete_with_files_too'),
            pick_first=self.settings.get('pick_first'),
            case_sensitive=self.settings.get('case_sensitive'),
            log_in_status_bar=self.settings.get('log_in_status_bar'),
            log_template='Moving at {0}',
            enable_browser=False)
示例#4
0
    def open_terminal(self, cmd, cwd, name):
        if os.path.isfile(cwd):
            cwd = os.path.dirname(cwd)

        for j, bit in enumerate(cmd):
            cmd[j] = bit.replace('$cwd', cwd)
        sm('Opening "{0}" at {1}'.format(name, ph.user_friendly(cwd)))
        return subprocess.Popen(cmd, cwd=cwd)
示例#5
0
    def duplicate(self, dst, input_path):
        user_friendly_path = ph.user_friendly(dst)

        if os.path.isdir(self.origin):
            if not os.path.exists(dst):
                shutil.copytree(self.origin, dst)
            else:
                sublime.error_message('This path already exists!')
                raise ValueError(
                    'Cannot move the directory {0!r} because it already exists {1!r}'
                    .format(self.origin, dst))
        else:
            if not os.path.exists(dst):
                with open(dst, 'w') as fp:
                    with open(self.origin, 'r') as fpread:
                        fp.write(fpread.read())
                self.window.open_file(dst)
            else:

                def overwrite():
                    try:
                        send2trash(dst)
                    except OSError as e:
                        sublime.error_message('Unable to send to trash: ', e)
                        raise OSError(
                            'Unable to send to the trash the item {0}'.format(
                                e))

                    with open(dst, 'w') as fp:
                        with open(self.origin, 'r') as fpread:
                            fp.write(fpread.read())
                    self.window.open_file(dst)

                def open_file():
                    return self.window.open_file(dst)

                yes_no_cancel_panel(
                    message=[
                        'This file already exists. Overwrite?',
                        user_friendly_path
                    ],
                    yes=overwrite,
                    no=open_file,
                    cancel=None,
                    yes_text=[
                        'Yes. Overwrite', user_friendly_path,
                        'will be sent to the trash, and then written'
                    ],
                    no_text=['Just open the target file', user_friendly_path],
                    cancel_text=["No, don't do anything"])

        refresh_sidebar(self.settings, self.window)
        return
示例#6
0
 def on_done(self, abspath, input_path):
     input_path = ph.user_friendly(input_path)
     if input_path[-1] == '/':
         return makedirs(abspath, exist_ok=True)
     if not os.path.isfile(abspath):
         makedirs(os.path.dirname(abspath), exist_ok=True)
         with open(abspath, 'w') as fp:
             pass
         template = get_template(abspath)
     else:
         template = None
     view = self.window.open_file(abspath)
     if template:
         view.settings().set('fm_insert_snippet_on_load', template)
     refresh_sidebar(self.settings, self.window)
示例#7
0
    def rename(self, dst, input_dst):
        def rename():
            makedirs(os.path.dirname(dst), exist_ok=True)
            os.rename(self.origin, dst)
            view = self.window.find_open_file(self.origin)
            if view:
                close_view(view)
            if os.path.isfile(dst):
                self.window.open_file(dst)

        if os.path.exists(dst):

            def open_file(self):
                return self.window.open_file(dst)

            def overwrite():
                try:
                    send2trash(dst)
                except OSError as e:
                    sublime.error_message('Unable to send to trash: ', e)
                    raise OSError(
                        'Unable to send the item {0!r} to the trash! Error {1!r}'
                        .format(dst, e))

                rename()

            user_friendly_path = ph.user_friendly(dst)
            return yes_no_cancel_panel(
                message=[
                    'This file already exists. Overwrite?', user_friendly_path
                ],
                yes=overwrite,
                no=open_file,
                cancel=None,
                yes_text=[
                    'Yes. Overwrite', user_friendly_path,
                    'will be sent to the trash, and then written'
                ],
                no_text=['Just open the target file', user_friendly_path],
                cancel_text=["No, don't do anything"])

        rename()
        refresh_sidebar(self.settings, self.window)
示例#8
0
    def input_on_change(self, input_path):
        self.input_path = ph.user_friendly(input_path)
        # get changed inputs and create_from from the on_change user function
        if self.user_on_change:
            new_values = self.user_on_change(
                self.input_path, self.path_to_create_choosed_from_browsing)
            if new_values is not None:
                create_from, self.input_path = new_values
                self.create_from = ph.computer_friendly(create_from)

        def reset_settings():
            self.input.settings.erase('completions')
            self.input.settings.erase('completions_index')

        def replace_with_completion(completions, index, prefix=None):
            # replace the previous completion
            # with the new one (completions[index+1])
            region = [self.input.view.sel()[0].begin()]
            # -1 because of the \t
            region.append(
                region[0] -
                len(prefix if prefix is not None else completions[index]) - 1)
            index += 1
            self.input.settings.set('completions_index', index)
            # Running fm_edit_replace will trigger this function
            # and because it is not going to find any \t
            # it's going to erase the settings
            # Adding this will prevent this behaviour
            self.input.settings.set('just_completed', True)
            self.input.view.run_command('fm_edit_replace', {
                'region': region,
                'text': completions[index]
            })
            self.prev_input_path = self.input.view.substr(
                sublime.Region(0, self.input.view.size()))

        # log in the status bar
        if self.log_in_status_bar:
            path = os.path.normpath(
                os.path.join(self.create_from,
                             ph.computer_friendly(self.input_path)))
            if self.input_path != '' and self.input_path[-1] == '/':
                path += os.path.sep
            if self.log_in_status_bar == 'user':
                path = ph.user_friendly(path)
            set_status(self.view, self.STATUS_KEY,
                       self.log_template.format(path))

        if not hasattr(self.input, 'settings'):
            return

        completions = self.input.settings.get('completions', None)
        index = self.input.settings.get('completions_index', None)

        if completions is not None and index is not None:
            # check if the user typed something after the completion
            text = input_path
            if text[-1] == '\t':
                text = text[:-1]
            if not text.endswith(tuple(completions)):
                return reset_settings()
            if '\t' in input_path:

                # there is still some completions available
                if len(completions) - 1 > index:
                    return replace_with_completion(completions, index)
        if '\t' in input_path:
            before, after = self.input_path.split('\t')
            prefix, completions = self.__get_completion_for(
                abspath=ph.computer_friendly(
                    os.path.join(self.create_from, before)),
                with_files=self.with_files,
                pick_first=self.pick_first,
                case_sensitive=self.case_sensitive,
                can_add_slash=after == '' or after[0] != '/')
            if not completions:
                return

            self.input.settings.set('completions', completions)
            self.input.settings.set('completions_index', -1)

            replace_with_completion(completions, -1, prefix)