Exemplo n.º 1
0
    def gather_candidates(self, context):
        if not self.vars['command']:
            return []

        if context['__proc']:
            return self.__async_gather_candidates(context,
                                                  context['async_timeout'])

        if context['is_redraw']:
            self.__cache = {}

        directory = context['__directory']
        if not isdir(directory):
            return []

        if directory in self.__cache:
            return self.__cache[directory]

        if ':directory' in self.vars['command']:
            args = parse_command(self.vars['command'], directory=directory)
        else:
            args = self.vars['command'] + [directory]
        self.print_message(context, args)
        context['__proc'] = Process(args, context, directory)
        context['__current_candidates'] = []
        return self.__async_gather_candidates(context, 0.5)
Exemplo n.º 2
0
    def gather_candidates(self, context):
        if not self.vars['command']:
            return []

        directory = context['__directory']
        if not Path(directory).is_dir():
            return []

        if context['is_redraw'] and directory in self._cache:
            self._cache.pop(directory)
        if directory in self._cache:
            return self._cache[directory]

        if context['__proc']:
            return self._async_gather_candidates(context,
                                                 context['async_timeout'])

        if ':directory' in self.vars['command']:
            args = parse_command(self.vars['command'], directory=directory)
        else:
            args = self.vars['command'] + [directory]
        if shutil.which(args[0]) is None:
            self.error_message(context, args[0] + ' is not executable.')
            return []
        self.print_message(context, args)
        context['__proc'] = Process(args, context, directory)
        context['__current_candidates'] = []
        return self._async_gather_candidates(context, 0.5)
Exemplo n.º 3
0
    def gather_candidates(self, context):
        if self.__proc:
            return self.__async_gather_candidates(context, 0.5)

        if context['__input'] == '':
            return []

        commands = []
        commands += self.vars['command']
        commands += self.vars['default_opts']
        commands += self.vars['recursive_opts']
        commands += context['__arguments']
        commands += self.vars['separator']
        commands += shlex.split(context['__input'])
        commands += self.vars['final_opts']

        self.__proc = Process(commands, context, context['__directory'])
        return self.__async_gather_candidates(context, 2.0)
Exemplo n.º 4
0
    def gather_candidates(self, context):
        if self.__proc:
            return self.__async_gather_candidates(context, 0.5)

        opts = copy.copy(self.vars['default_opts'])
        if len(context['args']) > 0:
            args = context['args']
            if 'all' in args:
                opts += ['--all']

        commands = []
        commands += self.vars['command']
        commands += self.vars['action']
        commands += opts
        commands += self.vars['separator']

        self.__proc = Process(commands, context,
                              self.vim.call('expand', context['path']))
        return self.__async_gather_candidates(context, 2.0)
Exemplo n.º 5
0
class Source(Base):
    def __init__(self, vim):
        super().__init__(vim)

        self.name = 'directory_rec'
        self.kind = 'directory'
        self.vars = {'command': []}

    def on_init(self, context):
        self.__proc = None
        directory = context['args'][0] if len(
            context['args']) > 0 else self.vim.call('getcwd')
        context['__directory'] = self.vim.call('expand', directory)

    def on_close(self, context):
        if self.__proc:
            self.__proc.kill()
            self.__proc = None

    def gather_candidates(self, context):
        if self.__proc:
            return self.__async_gather_candidates(context)

        if not self.vars['command']:
            self.vars['command'] = [
                'find', '-L', context['__directory'], '-path', '*/.git/*',
                '-prune', '-o', '-type', 'l', '-print', '-o', '-type', 'd',
                '-print'
            ]
        else:
            self.vars['command'].append(context['__directory'])
        self.__proc = Process(self.vars['command'], context,
                              context['__directory'])
        return self.__async_gather_candidates(context)

    def __async_gather_candidates(self, context):
        outs, errs = self.__proc.communicate(timeout=2.0)
        context['is_async'] = not self.__proc.eof()
        return [{'word': x + '/', 'action__path': x} for x in outs if x != '']
Exemplo n.º 6
0
    def gather_candidates(self, context):
        if context['__proc']:
            return self.__async_gather_candidates(context,
                                                  context['async_timeout'])

        if context['is_redraw']:
            self.__cache = {}

        directory = context['__directory']

        if directory in self.__cache:
            return self.__cache[directory]

        if ':directory' in self.vars['command']:
            command = parse_command(self.vars['command'], directory=directory)
        else:
            command = self.vars['command'] + [directory]
        context['__proc'] = Process(command, context, directory)
        context['__current_candidates'] = []
        return self.__async_gather_candidates(context, 0.5)
Exemplo n.º 7
0
    def gather_candidates(self, context):
        if not context['__proc']:
            context['__proc'] = Process(self.build_command(context), context, context['path'])

        outs, errs = context['__proc'].communicate(0.5)

        if errs:
            self.error_message(context, errs)

        context['is_async'] = not context['__proc'].eof()

        if context['__proc'].eof():
            context['__proc'] = None

        if not outs:
            return []

        outs = outs if getattr(self, 'duplicatable', None) else list(dict.fromkeys(outs))
        candidates = [self.build_candidate(x, context) for x in outs]
        return [x for x in candidates if x]
Exemplo n.º 8
0
class Source(Base):
    def __init__(self, vim):
        super().__init__(vim)

        self.name = 'gitn_status'
        self.kind = 'gitn_status'
        self.vars = {
            'command': ['git'],
            'action': ['status'],
            'default_opts': ['-s'],
            'separator': ['--'],
        }

    def on_init(self, context):
        self.__proc = None
        context['__directory'] = self.vim.call('expand', context['path'])

    def on_close(self, context):
        if self.__proc:
            self.__proc.kill()
            self.__proc = None

    def highlight(self):
        Gitn.highlight(self.vim, HIGHLIGHT)

    def define_syntax(self):
        self.vim.command('syntax region ' + self.syntax_name +
                         ' start=// end=/$/ '
                         'contains=gitn_status_line,deniteMatched contained')

    def gather_candidates(self, context):
        if self.__proc:
            return self.__async_gather_candidates(context, 0.5)

        commands = []
        commands += self.vars['command']
        commands += self.vars['action']
        commands += self.vars['default_opts']
        commands += self.vars['separator']

        self.__proc = Process(commands, context, context['__directory'])
        return self.__async_gather_candidates(context, 2.0)

    def __async_gather_candidates(self, context, timeout):
        outs, errs = self.__proc.communicate(timeout=timeout)
        context['is_async'] = not self.__proc.eof()
        if self.__proc.eof():
            self.__proc = None

        candidates = []

        for line in outs:
            result = self.__parse_short_status(line, context)
            if result:
                [paths, word, index, work] = result
                candidates.append({
                    'word':
                    '{1:<9} {3:1}{2:<9}:{0}'.format(
                        os.path.relpath(word, start=context['__directory']),
                        TO_DISPLAY[index], TO_DISPLAY[work],
                        '!' if work != Status.unmodified else ' '),
                    'action__path':
                    paths[0],
                    'action__paths':
                    paths,
                    'action__line':
                    0,
                    'action__col':
                    0,
                })

        return candidates

    def __parse_short_status(self, line, context):
        m = re.search(r'^(.)(.)\s*(.+)$', line)
        if not m or not m.group(3):
            return []

        [index, work, path] = m.groups()

        index_status = Status.by_short(index)
        work_status = Status.by_short(work)

        # TODO: refactor to more simple logic
        if index_status == Status.renamed:
            m = re.search(r'^(.+)\s+->\s+(.+)$', path)
            [path_from, path_to] = m.groups()
            paths = [
                p if not os.path.isabs(p) else context['__directory'] + '/' + p
                for p in [path_from, path_to]
            ]
        else:
            paths = [
                p if not os.path.isabs(p) else context['__directory'] + '/' + p
                for p in [path]
            ]

        return [paths, path, index_status, work_status]
Exemplo n.º 9
0
class Source(Base):
    def __init__(self, vim):
        super().__init__(vim)

        self.name = 'grep'
        self.kind = 'file'
        self.vars = {
            'command': ['grep'],
            'default_opts': ['-inH'],
            'recursive_opts': ['-r'],
            'separator': ['--'],
            'final_opts': ['.'],
        }

    def on_init(self, context):
        self.__proc = None
        directory = context['args'][0] if len(
            context['args']) > 0 else context['directory']
        context['__arguments'] = context['args'][1:]
        context['__directory'] = self.vim.call('expand', directory)
        context['__input'] = self.vim.call('input', 'Pattern: ',
                                           context['input'])

    def on_close(self, context):
        if self.__proc:
            self.__proc.kill()
            self.__proc = None

    def gather_candidates(self, context):
        if self.__proc:
            return self.__async_gather_candidates(context, 0.5)

        if context['__input'] == '':
            return []

        commands = []
        commands += self.vars['command']
        commands += self.vars['default_opts']
        commands += self.vars['recursive_opts']
        commands += context['__arguments']
        commands += self.vars['separator']
        commands += shlex.split(context['__input'])
        commands += self.vars['final_opts']

        self.__proc = Process(commands, context, context['__directory'])
        return self.__async_gather_candidates(context, 2.0)

    def __async_gather_candidates(self, context, timeout):
        outs, errs = self.__proc.communicate(timeout=timeout)
        context['is_async'] = not self.__proc.eof()

        candidates = []

        for line in outs:
            result = parse_jump_line(context['__directory'], line)
            if result:
                candidates.append({
                    'word':
                    '{0}:{1}{2}: {3}'.format(
                        os.path.relpath(result[0],
                                        start=context['__directory']),
                        result[1],
                        (':' + result[2] if result[2] != '0' else ''),
                        result[3]),
                    'action__path':
                    result[0],
                    'action__line':
                    result[1],
                    'action__col':
                    result[2],
                })
        return candidates
Exemplo n.º 10
0
class Source(Base):
    def __init__(self, vim):
        super().__init__(vim)

        self.name = 'file_rec'
        self.kind = 'file'
        self.vars = {
            'command': [],
            'min_cache_files': 10000,
        }
        self.__cache = {}

    def on_init(self, context):
        self.__proc = None
        directory = context['args'][0] if len(
            context['args']) > 0 else context['path']
        context['__directory'] = self.vim.call('expand', directory)

    def on_close(self, context):
        if self.__proc:
            self.__proc.kill()
            self.__proc = None

    def gather_candidates(self, context):
        if self.__proc:
            candidates = self.__async_gather_candidates(context, 0.5)
            return candidates

        if context['is_redraw']:
            self.__cache = {}

        directory = context['__directory']

        if directory in self.__cache:
            return self.__cache[directory]

        command = copy(self.vars['command'])
        if not command:
            if context['is_windows']:
                return []

            command = [
                'find', '-L', directory, '-path', '*/.git/*', '-prune', '-o',
                '-type', 'l', '-print', '-o', '-type', 'f', '-print'
            ]
        else:
            command.append(directory)
        self.__proc = Process(command, context, directory)
        self.__current_candidates = []
        return self.__async_gather_candidates(context, 2.0)

    def __async_gather_candidates(self, context, timeout):
        outs, errs = self.__proc.communicate(timeout=timeout)
        context['is_async'] = not self.__proc.eof()
        if self.__proc.eof():
            self.__proc = None
        if not outs:
            return []
        if isabs(outs[0]):
            candidates = [{
                'word': relpath(x, start=context['__directory']),
                'action__path': x
            } for x in outs if x != '']
        else:
            candidates = [{
                'word': x,
                'action__path': context['__directory'] + '/' + x
            } for x in outs if x != '']
        self.__current_candidates += candidates
        if len(self.__current_candidates) >= self.vars['min_cache_files']:
            self.__cache[context['__directory']] = self.__current_candidates
        return candidates
Exemplo n.º 11
0
class Source(Base):
    def __init__(self, vim):
        super().__init__(vim)

        self.name = 'grep'
        self.syntax_name = 'deniteSource_grep'
        self.kind = 'file'
        self.vars = {
            'command': ['grep'],
            'default_opts': ['-inH'],
            'recursive_opts': ['-r'],
            'separator': ['--'],
            'final_opts': ['.'],
        }
        self.matchers = ['matcher_ignore_globs', 'matcher_regexp']

    def on_init(self, context):
        self.__proc = None
        directory = ''
        if context['args']:
            directory = context['args'][0]
        if not directory:
            directory = context['path']
        context['__arguments'] = context['args'][1:]
        context['__directory'] = self.vim.call('expand', directory)
        context['__input'] = self.vim.call('input', 'Pattern: ',
                                           context['input'])

    def on_close(self, context):
        if self.__proc:
            self.__proc.kill()
            self.__proc = None

    def highlight_syntax(self):
        input_str = self.context['__input']
        self.vim.command(GREP_HEADER_SYNTAX)
        self.vim.command(GREP_FILE_SYNTAX)
        self.vim.command(GREP_FILE_HIGHLIGHT)
        self.vim.command(GREP_LINE_SYNTAX)
        self.vim.command(GREP_LINE_HIGHLIGHT)
        self.vim.command(
            'syntax region ' + self.syntax_name + ' start=// end=/$/ '
            'contains=deniteSource_grepHeader,deniteMatched contained')
        self.vim.command('syntax match deniteGrepInput /%s/ ' %
                         escape_syntax(input_str) + 'contained containedin=' +
                         self.syntax_name)
        self.vim.command('highlight default link deniteGrepInput Function')

    def gather_candidates(self, context):
        if self.__proc:
            return self.__async_gather_candidates(context, 0.5)

        if context['__input'] == '':
            return []

        commands = []
        commands += self.vars['command']
        commands += self.vars['default_opts']
        commands += self.vars['recursive_opts']
        commands += context['__arguments']
        commands += self.vars['separator']
        commands += shlex.split(context['__input'])
        commands += self.vars['final_opts']

        self.__proc = Process(commands, context, context['__directory'])
        return self.__async_gather_candidates(context, 2.0)

    def __async_gather_candidates(self, context, timeout):
        outs, errs = self.__proc.communicate(timeout=timeout)
        context['is_async'] = not self.__proc.eof()
        if self.__proc.eof():
            self.__proc = None

        candidates = []

        for line in outs:
            result = parse_jump_line(context['__directory'], line)
            if result:
                candidates.append({
                    'word':
                    '{0}:{1}{2} {3}'.format(
                        os.path.relpath(result[0],
                                        start=context['__directory']),
                        result[1],
                        (':' + result[2] if result[2] != '0' else ''),
                        result[3]),
                    'action__path':
                    result[0],
                    'action__line':
                    result[1],
                    'action__col':
                    result[2],
                })
        return candidates
Exemplo n.º 12
0
class Source(Base):
    def __init__(self, vim):
        super().__init__(vim)

        self.name = 'gitn_branch'
        self.kind = 'gitn_branch'
        self.vars = {
            'command': ['git'],
            'action': ['branch'],
            'default_opts': ['--list'],
            'separator': ['--'],
        }

    def on_init(self, context):
        self.__proc = None

    def on_close(self, context):
        if self.__proc:
            self.__proc.kill()
            self.__proc = None

    def highlight(self):
        Gitn.highlight(self.vim, HIGHLIGHT)

    def define_syntax(self):
        self.vim.command('syntax region ' + self.syntax_name +
                         ' start=// end=/$/ '
                         'contains=gitn_branch_line,deniteMatched contained')

    def gather_candidates(self, context):
        if self.__proc:
            return self.__async_gather_candidates(context, 0.5)

        opts = copy.copy(self.vars['default_opts'])
        if len(context['args']) > 0:
            args = context['args']
            if 'all' in args:
                opts += ['--all']

        commands = []
        commands += self.vars['command']
        commands += self.vars['action']
        commands += opts
        commands += self.vars['separator']

        self.__proc = Process(commands, context,
                              self.vim.call('expand', context['path']))
        return self.__async_gather_candidates(context, 2.0)

    def __async_gather_candidates(self, context, timeout):
        outs, errs = self.__proc.communicate(timeout=timeout)
        context['is_async'] = not self.__proc.eof()
        if self.__proc.eof():
            self.__proc = None

        candidates = []

        for line in outs:
            result = self.__parse_branch(line, context)
            if result:
                [name, ref_name, is_current, is_remote, is_tracked] = result
                candidates.append({
                    'word':
                    '{0} {1}{2}'.format(
                        '*' if is_current else ' ',
                        ref_name + ' -> ' if ref_name != '' else '', name),
                    'action__name':
                    name,
                })

        return candidates

    def __parse_branch(self, line, context):
        name = ''
        ref_name = ''
        current = ''
        is_current = False
        is_remote = False
        is_tracked = True

        m = False

        if not m:
            m = re.search(r'^([* ]) ([^ ]+)$', line)
            if m: [current, name] = m.groups()

        if not m:
            m = re.search(r'^([* ]) ([^ ]+) -> ([^ ]+)$', line)
            if m: [current, ref_name, name] = m.groups()

        if not m:
            m = re.search(r'^([* ]) ([(][^)]+[)])$', line)
            if m:
                [current, name] = m.groups()
                is_tracked = False

        is_current = current == '*'
        #is_remote =

        return [name, ref_name, is_current, is_remote, is_tracked]
Exemplo n.º 13
0
class Source(Base):
    def __init__(self, vim):
        super().__init__(vim)

        self.name = 'gitn_log'
        self.kind = 'gitn_log'
        self.vars = {
            'command': ['git'],
            'action': ['log'],
            'default_opts': [
                '--date=default',
                '--graph',
                '--pretty=format:":::%H:::%P:::%an:::%ae:::%ad:::%at:::%cn:::%ce:::%cd:::%ct:::%s:::"',
            ],
            'separator': ['--'],
            'file': [''],
        }

    def on_init(self, context):
        self.__proc = None

        if len(context['args']) < 1:
            self.vars['file'] = ['']
        else:
            self.vars['file'] = [context['args'][0]]

    def on_close(self, context):
        if self.__proc:
            self.__proc.kill()
            self.__proc = None

    def highlight(self):
        Gitn.highlight(self.vim, DATE_GRAPH_HIGHLIGHT)
        Gitn.highlight(self.vim, AUTHOR_NAME_HIGHLIGHT)

    def define_syntax(self):
        self.vim.command(
            'syntax region ' + self.syntax_name + ' start=// end=/$/ '
            'contains=gitnLog_dateGraphHeader,gitnLog_authorNameHeader,deniteMathced contained'
        )

    def gather_candidates(self, context):
        if self.__proc:
            return self.__async_gather_candidates(context, 0.5)

        commands = []
        commands += self.vars['command']
        commands += self.vars['action']
        commands += self.vars['default_opts']
        commands += self.vars['separator']
        commands += self.vars['file']

        self.__proc = Process(commands, context,
                              self.vim.call('expand', context['path']))
        return self.__async_gather_candidates(context, 2.0)

    def __async_gather_candidates(self, context, timeout):
        outs, errs = self.__proc.communicate(timeout=timeout)
        context['is_async'] = not self.__proc.eof()
        if self.__proc.eof():
            self.__proc = None

        candidates = []

        for line in outs:
            result = self.__parse(line)
            if result:
                if 'subject' in result and result['subject'] != '':
                    candidates.append({
                        'word':
                        '{0} {1}: {2} : {3}'.format(
                            time.strftime(
                                '%Y/%m/%d %H:%M',
                                time.gmtime(result['author']['time'])),
                            result['graph'],
                            result['author']['name'],
                            result['subject'],
                        ),
                        'action__log':
                        result,
                        'action__path':
                        context['args'][1]
                        if len(context['args']) >= 2 else '',
                    })
                elif 'graph' in result:
                    candidates.append({
                        'word':
                        '                 {0}'.format(result['graph'].strip()),
                    })

        return candidates

    def __parse(self, line):

        m = re.search(r'^([*|/\\ ]+)\s?(.+)?$', line)

        [graph, value] = m.groups()

        if not m or not m.group(2): return {'graph': graph}

        splited = value.split(':::')

        if len(splited) <= 1: return {'graph': graph}

        [
            own_hash, parent_hash, author_name, author_email, author_date,
            author_time, committer_name, committer_email, committer_date,
            committer_time, subject
        ] = splited[1:-1]

        return {
            'graph': graph,
            'subject': subject,
            'hash': {
                'own': own_hash,
                'parent': parent_hash,
            },
            'author': {
                'name': author_name,
                'email': author_email,
                'date': author_date,
                'time': int(author_time, 10),
            },
            'committer': {
                'name': committer_name,
                'email': committer_email,
                'date': committer_date,
                'time': int(committer_time, 10),
            },
        }
Exemplo n.º 14
0
class Source(Base):

    def __init__(self, vim):
        super().__init__(vim)

        self.name = 'gitn_changed_files'
        self.kind = 'file'
        self.vars = {
            'command': ['git'],
            'action': ['diff-tree'],
            'default_opts': ['-r', '--no-commit-id'],
            'ish': [],
        }

    def on_init(self, context):
        self.__proc = None

        if len(context['args']) < 2: return

        self.vars['ish'] = ['{0}^..{1}'.format(context['args'][0], context['args'][1])]

        context['__directory'] = self.vim.call('expand', context['path'])

    def on_close(self, context):
        if self.__proc:
            self.__proc.kill()
            self.__proc = None

    def highlight(self):
        Gitn.highlight(self.vim, HIGHLIGHT)

    def define_syntax(self):
        self.vim.command(
            'syntax region ' + self.syntax_name + ' start=// end=/$/ '
            'contains=gitn_change_files_line,deniteMatched contained')

    def gather_candidates(self, context):
        if self.__proc:
            return self.__async_gather_candidates(context, 0.5)

        commands = []
        commands += self.vars['command']
        commands += self.vars['action']
        commands += self.vars['default_opts']
        commands += self.vars['ish']

        self.__proc = Process(commands, context, context['__directory'])

        return self.__async_gather_candidates(context, 2.0)

    def __async_gather_candidates(self, context, timeout):
        outs, errs = self.__proc.communicate(timeout=timeout)
        context['is_async'] = not self.__proc.eof()
        if self.__proc.eof():
            self.__proc = None

        candidates = []

        for line in outs:
            result = self.__parse(line, context)
            if result:
                [path, status] = result
                candidates.append({
                    'word': '{1:<9}:{0}'.format(
                        os.path.relpath(path, start=context['__directory']),
                        TO_DISPLAY[status]),
                    'action__path': path,
                    'action__line': 0,
                    'action__col': 0,
                })

        return candidates

    def __parse(self, line, context):
        m = re.search(r':([^ ]+) ([^ ]+) ([^ ]+) ([^ ]+) (.)\t([^	]+)\t?(.+)?$', line)
        if not m or not m.group(6): return []

        [src_mode, dst_mode, src_commit, dst_commit, status, src_path, dst_path] = m.groups()

        if not os.path.isabs(src_path):
            path = context['__directory'] + '/' + src_path

        return [path, Status.by_short(status)]