def gather_candidates(self, context): context['is_interactive'] = True candidates = [] path = (context['args'][1] if len(context['args']) > 1 else context['path']) path = abspath(self.vim, path) inp = expand(context['input']) filename = (inp if os.path.isabs(inp) else os.path.join(path, inp)) if context['args'] and context['args'][0] == 'new': candidates.append({ 'word': filename, 'abbr': '[new] ' + filename, 'action__path': abspath(self.vim, filename), }) else: glb = os.path.dirname(filename) if os.path.dirname( filename) != '/' else '' glb += '/.*' if os.path.basename( filename).startswith('.') else '/*' for f in glob.glob(glb): fullpath = abspath(self.vim, f) candidates.append({ 'word': f, 'abbr': (os.path.relpath(f, path) if fullpath != path else os.path.normpath(f)) + ( '/' if os.path.isdir(f) else ''), 'kind': ('directory' if os.path.isdir(f) else 'file'), 'action__path': fullpath, }) return candidates
def gather_candidates(self, context): context['is_interactive'] = True candidates = [] path = (context['args'][1] if len(context['args']) > 1 else context['path']) filename = (context['input'] if os.path.isabs(context['input']) else os.path.join(path, context['input'])) if context['args'] and context['args'][0] == 'new': candidates.append({ 'word': filename, 'abbr': '[new] ' + filename, 'action__path': abspath(self.vim, filename), }) else: for f in glob.glob(filename + '*'): candidates.append({ 'word': f, 'abbr': f + ('/' if os.path.isdir(f) else ''), 'kind': ('directory' if os.path.isdir(f) else 'file'), 'action__path': abspath(self.vim, f), }) return candidates
def gather_candidates(self, context: UserContext) -> Candidates: result = parse_jump_line( self.vim.call('getcwd'), context['__line']) try: if not result or not Path(result[0]).is_file(): result = parse_jump_line( self.vim.call('getcwd'), context['__cfile']) if result and Path(result[0]).is_file(): return [{ 'word': '{}: {}{}: {}'.format( result[0], result[1], (':' + result[2] if result[2] != '0' else ''), result[3]), 'action__path': result[0], 'action__line': result[1], 'action__col': result[2], }] cfile = context['__cfile'] cpath = Path(abspath(self.vim, cfile)) if match('[./]+$', cfile): return [] if cpath.exists() and cpath.is_file(): return [{'word': cfile, 'action__path': abspath(self.vim, cfile)}] if _checkhost(cfile) or match( r'https?://(127\.0\.0\.1|localhost)[:/]', cfile): return [{'word': cfile, 'action__path': cfile}] except OSError: pass return []
def gather_candidates(self, context: UserContext) -> Candidates: context['is_interactive'] = True candidates = [] arg = context['args'][0] if context['args'] else '' if arg and arg != 'new' and arg != 'hidden': self.error_message(context, f'invalid argument: "{arg}"') path = (context['args'][1] if len(context['args']) > 1 else context['path']) path = abspath(self.vim, path) inp = Path(expand(context['input'])) filename = (str(inp) if inp.is_absolute() else str( Path(path).joinpath(inp))) if arg == 'new': candidates.append({ 'word': filename, 'abbr': '[new] ' + filename, 'action__path': abspath(self.vim, filename), }) else: file_path = Path(filename) glb = str(file_path if file_path.is_dir() else file_path.parent) # Note: Path('../.').name convert to ".." hidden = arg == 'hidden' or re.match( r'\.([^.]|$)', str(self.vim.call('fnamemodify', context['input'], ':t'))) glb += '/.*' if hidden else '/*' glb = re.sub(r'//', r'/', glb) for f in glob.glob(glb): f = re.sub(r'\n', r'\\n', f) fullpath = abspath(self.vim, f) f_path = Path(f) abbr = (str(f_path.relative_to(path)) if fullpath != path and f.startswith(path + sep) else fullpath) + (sep if f_path.is_dir() else '') candidates.append({ 'word': f, 'abbr': abbr, 'kind': ('directory' if f_path.is_dir() else 'file'), 'action__path': fullpath, }) return candidates
def gather_candidates(self, context: UserContext) -> Candidates: linenr = context['__linenr'] candidates: Candidates = [] for bufnr in context['__bufnrs']: lines = [{ 'word': x, 'abbr': (context['__fmt'] % (i + 1, x)), 'action__path': abspath(self.vim, self.vim.current.buffer.name), 'action__bufnr': bufnr, 'action__col': 0, 'action__line': (i + 1), 'action__text': x, } for [i, x] in enumerate( self.vim.call('getbufline', bufnr, 1, '$'))] if context['__emptiness'] == 'noempty': lines = list(filter(lambda c: c['word'] != '', lines)) if context['__direction'] == 'all': candidates += lines elif context['__direction'] == 'backward': candidates += list(reversed(lines[:linenr])) + list( reversed(lines[linenr:])) else: candidates += lines[linenr - 1:] + lines[:linenr - 1] return candidates
def gather_candidates(self, context): candidates = [] bufnr = context['__bufnr'] result = [] pattern = self.vim.eval('@/') result = self.vim.call('searchres#getsearchresult', pattern) for res in result: word = self.vim.call('getline', res[0]) line = [{ 'word': word, 'abbr': "{0:>3}: {1}".format(res[0], word), 'action__bufnr': bufnr, 'action__path': abspath(self.vim, self.vim.current.buffer.name), 'action__line': res[0], 'action__col': res[1], 'action__text': word }] candidates += line if not candidates: self.vim.call('denite#util#print_error', 'searchres: No matches.') return candidates
def on_init(self, context: UserContext) -> None: buf = self.vim.current.buffer context['__bufnr'] = buf.number context['__fmt'] = '%' + str(len(str(self.vim.call('line', '$')))) + 'd: %s' bufpath = util.abspath(self.vim, buf.name) context['__temp'] = '' if (buf.options['modified'] or 'nofile' in buf.options['buftype'] or not self.vim.call('filereadable', bufpath)): context['__temp'] = self.vim.call('denite#helper#_get_temp_file', buf.number) context['__path'] = context['__temp'] else: context['__path'] = bufpath # Backwards compatibility for `ack` if (self.vars['command'] and self.vars['command'][0] == 'ack' and self.vars['pattern_opt'] == ['-e']): self.vars['pattern_opt'] = ['--match'] # Interactive mode context['is_interactive'] = True context['__args'] = ''
def gather_candidates(self, context): context['is_interactive'] = True candidates = [] if context['args'] and context['args'][0] == 'new': candidates.append({ 'word': context['input'], 'abbr': '[new] ' + context['input'], 'action__path': abspath(self.vim, context['input']), }) else: for f in glob.glob(context['input'] + '*'): candidates.append({ 'word': f, 'abbr': f + ('/' if os.path.isdir(f) else ''), 'action__path': abspath(self.vim, f), }) return candidates
def action_new(self, context): path = util.input(self.vim, context, 'New file: ', completion='file') if not path: return context['targets'] = [{ 'word': path, 'action__path': util.abspath(self.vim, path), }] self.action_open(context)
def action_new(self, context: UserContext) -> None: path = str(self.vim.call('denite#util#input', 'New file: ', '', 'file')) if not path: return context['targets'] = [{ 'word': path, 'action__path': util.abspath(self.vim, path), }] self.action_open(context)
def gather_candidates(self, context: UserContext) -> Candidates: context['is_interactive'] = True candidates = [] new = context['args'][0] if context['args'] else '' if new and new != 'new': self.error_message(context, f'invalid argument: "{new}"') path = (context['args'][1] if len(context['args']) > 1 else context['path']) path = abspath(self.vim, path) inp = Path(expand(context['input'])) filename = (str(inp) if inp.is_absolute() else str( Path(path).joinpath(inp))) if new == 'new': candidates.append({ 'word': filename, 'abbr': '[new] ' + filename, 'action__path': abspath(self.vim, filename), }) else: file_path = Path(filename) glb = str(file_path) glb += '/.*' if str(file_path.name).startswith('.') else '/*' for f in glob.glob(glb): fullpath = abspath(self.vim, f) f = re.sub(r'\n', r'\\n', f) f_path = Path(f) abbr = (str(f_path.relative_to(path)) if fullpath != path and f.startswith(path + '/') else str(f_path.resolve()) ) + ('/' if f_path.is_dir() else '') candidates.append({ 'word': f, 'abbr': abbr, 'kind': ('directory' if f_path.is_dir() else 'file'), 'action__path': fullpath, }) return candidates
def on_init(self, context): if not context['is_windows'] and not self.vars['command']: self.vars['command'] = [ 'find', '-L', ':directory', '-path', '*/.git/*', '-prune', '-o', '-type', 'l', '-print', '-o', '-type', 'd', '-print'] context['__proc'] = None directory = context['args'][0] if len( context['args']) > 0 else context['path'] context['__directory'] = abspath(self.vim, directory)
def _init_paths(self, context, args): paths = [] arg = args.get(0, []) if arg: if isinstance(arg, str): paths = [arg] elif not isinstance(arg, list): raise AttributeError('`args[0]` needs to be a `str` or `list`') elif context['path']: paths = [context['path']] return [util.abspath(self.vim, x) for x in paths]
def on_init(self, context): if not context['is_windows'] and not self.vars['command']: self.vars['command'] = [ 'find', '-L', ':directory', '-path', '*/.git/*', '-prune', '-o', '-type', 'l', '-print', '-o', '-type', 'd', '-print' ] context['__proc'] = None directory = context['args'][0] if len( context['args']) > 0 else context['path'] context['__directory'] = abspath(self.vim, directory)
def gather_candidates(self, context): out = subprocess.run([ 'bin/q', '--sql', 'SELECT filename FROM articles ORDER BY date desc' ], stdout=subprocess.PIPE) return [{ 'word': x, 'abbr': x, 'action__path': abspath(self.vim, x) } for x in out.stdout.decode("ascii").split("\n")]
def _init_paths(self, context, args): paths = [] arg = args.get(0, []) if arg: if isinstance(arg, str): paths = [arg] elif not isinstance(arg, list): raise AttributeError( '`args[0]` needs to be a `str` or `list`') elif context['path']: paths = [context['path']] return [util.abspath(self.vim, x) for x in paths]
def on_init(self, context): context['__proc'] = None # Backwards compatibility for `ack` if (self.vars['command'] and self.vars['command'][0] == 'ack' and self.vars['pattern_opt'] == ['-e']): self.vars['pattern_opt'] = ['--match'] args = dict(enumerate(context['args'])) # paths arg = args.get(0, []) if arg: if isinstance(arg, str): arg = [arg] elif not isinstance(arg, list): raise AttributeError('`args[0]` needs to be a `str` or `list`') elif context['path']: arg = [context['path']] context['__paths'] = [util.abspath(self.vim, x) for x in arg] # arguments arg = args.get(1, []) if arg: if isinstance(arg, str): if arg == '!': arg = util.input(self.vim, context, 'Argument: ') arg = shlex.split(arg) elif not isinstance(arg, list): raise AttributeError('`args[1]` needs to be a `str` or `list`') context['__arguments'] = arg # patterns arg = args.get(2, []) if arg: if isinstance(arg, str): if arg == '!': # Interactive mode context['is_interactive'] = True arg = '' else: arg = [arg] elif not isinstance(arg, list): raise AttributeError('`args[2]` needs to be a `str` or `list`') elif context['input']: arg = [context['input']] else: pattern = util.input(self.vim, context, 'Pattern: ') if pattern: arg = [pattern] context['__patterns'] = arg
def gather_candidates(self, context): """Gather parent directories.""" base_path = self.vim.call('getcwd') if context['args']: base_path = abspath(self.vim, context['args'][0]) path = Path(base_path) parents = [str(path)] while str(path) != '/': path = path.parent parents.append(str(path)) candidates = [{'word': d, 'action__path': d} for d in parents] return candidates
def gather_candidates(self, context: UserContext) -> Candidates: context['is_interactive'] = True candidates = [] new = context['args'][0] if context['args'] else '' if new and new != 'new': self.error_message(context, f'invalid argument: "{new}"') path = (context['args'][1] if len(context['args']) > 1 else context['path']) path = abspath(self.vim, path) inp = expand(context['input']) filename = (inp if os.path.isabs(inp) else os.path.join(path, inp)) if new == 'new': candidates.append({ 'word': filename, 'abbr': '[new] ' + filename, 'action__path': abspath(self.vim, filename), }) else: glb = os.path.dirname(filename) if os.path.dirname( filename) != '/' else '' glb += '/.*' if os.path.basename( filename).startswith('.') else '/*' for f in glob.glob(glb): fullpath = abspath(self.vim, f) f = re.sub(r'\n', r'\\n', f) isdir = os.path.isdir(f) candidates.append({ 'word': f, 'abbr': (os.path.relpath(f, path) if fullpath != path else os.path.normpath(f)) + ( '/' if isdir else ''), 'kind': ('directory' if isdir else 'file'), 'action__path': fullpath, }) return candidates
def gather_candidates(self, context): context['is_interactive'] = True candidates = [] path = (context['args'][1] if len(context['args']) > 1 else context['path']) filename = (context['input'] if os.path.isabs(context['input']) else os.path.join(path, context['input'])) if context['args'] and context['args'][0] == 'new': candidates.append({ 'word': filename, 'abbr': '[new] ' + filename, 'action__path': abspath(self.vim, filename), }) else: for f in glob.glob(os.path.dirname(filename) + '/*'): candidates.append({ 'word': f, 'abbr': f + ('/' if os.path.isdir(f) else ''), 'kind': ('directory' if os.path.isdir(f) else 'file'), 'action__path': abspath(self.vim, f), }) return candidates
def _init_paths(self, context: UserContext, args: typing.Dict[int, str]) -> typing.List[str]: paths: typing.List[str] = [] arg: typing.Union[str, typing.List[str]] = args.get(0, []) if arg: if isinstance(arg, str): paths = [arg] elif isinstance(arg, list): paths = arg[:] else: raise AttributeError('`args[0]` needs to be a `str` or `list`') elif context['path']: paths = [context['path']] return [util.abspath(self.vim, x) for x in paths]
def gather_candidates(self, context): if len(context['args']) > 0: path = context['args'][0] to_abspath = lambda f: '{}/{}'.format(path, f) else: path = context['path'] to_abspath = lambda f: abspath(self.vim, f) isdir = os.path.isdir return [ { 'word': f + ('/' if isdir(to_abspath(f)) else ''), 'kind': ('directory' if isdir(to_abspath(f)) else 'file'), 'action__path': to_abspath(f), } for f in os.listdir(path) ]
def on_init(self, context): if not context['is_windows'] and not self.vars['command']: self.vars['command'] = [ 'find', '-L', ':directory', '-path', '*/.git/*', '-prune', '-o', '-type', 'l', '-print', '-o', '-type', 'f', '-print'] if context['is_windows'] and not self.vars['command']: scantree = join(path.split(__file__)[0], pardir, 'scantree.py') self.vars['command'] = ['python', scantree, ':directory'] context['__proc'] = None directory = context['args'][0] if len( context['args']) > 0 else context['path'] context['__directory'] = abspath(self.vim, directory)
def on_init(self, context): context['__proc'] = None context['__patterns'] = None context['is_interactive'] = True context['__arguments'] = '' for arg in context['args']: if arg == 'regex': context['__arguments'] += '-r ' elif arg == 'case': context['__arguments'] += '-i ' elif arg == 'word': context['__arguments'] += '-w ' directory = context['args'][0] if len( context['args']) > 0 else context['path'] context['__directory'] = [follow_link(abspath(self.vim, directory))]
def gather_candidates(self, context: UserContext) -> None: for source in self._current_sources: ctx = source.context ctx['is_redraw'] = context['is_redraw'] ctx['messages'] = context['messages'] ctx['error_messages'] = context['error_messages'] ctx['input'] = context['input'] ctx['prev_input'] = context['input'] ctx['event'] = 'gather' ctx['async_timeout'] = 0.01 ctx['path'] = abspath(self._vim, context['path']) candidates = self._gather_source_candidates(source.context, source) ctx['all_candidates'] = candidates ctx['candidates'] = candidates context['messages'] = ctx['messages']
def gather_candidates(self, context): for source in self._current_sources: ctx = source.context ctx['is_redraw'] = context['is_redraw'] ctx['messages'] = context['messages'] ctx['mode'] = context['mode'] ctx['input'] = context['input'] ctx['prev_input'] = context['input'] ctx['event'] = 'gather' ctx['async_timeout'] = 0.01 ctx['path'] = abspath(self._vim, context['path']) candidates = self._gather_source_candidates( source.context, source) ctx['all_candidates'] = candidates ctx['candidates'] = candidates context['messages'] = ctx['messages']
def gather_candidates(self, context): context['is_interactive'] = True candidates = [] path = context['args'][0] if context['args'] else context['path'] inputs = context['input'].split() narrowing = False if inputs and os.path.isabs(inputs[0]): path = inputs[0] narrowing = True for f in (glob.glob(os.path.join(path, '*')) + glob.glob(os.path.join(path, '.*'))): candidates.append({ 'word': f if narrowing else os.path.basename(f), 'abbr': f + (os.path.sep if os.path.isdir(f) else ''), 'kind': 'directory' if os.path.isdir(f) else 'file', 'action__path': abspath(self.vim, f), }) return candidates
def _async_gather_candidates(self, context, timeout): outs, errs = context['__proc'].communicate(timeout=timeout) if errs: return [{ 'word': x, } for x in errs] context['is_async'] = not context['__proc'].eof() if context['__proc'].eof(): context['__proc'] = None candidates = [] for out in outs[:-1]: candidates.append({ 'word': out[1:], 'kind': 'file', 'action__path': util.abspath(self.vim, extract_filename(out)), }) candidates.append({ 'word': outs[-1], 'kind': 'word', }) return candidates
def on_init(self, context: UserContext) -> None: buf = self.vim.current.buffer context['__bufnr'] = buf.number context['__fmt'] = '%' + str(len( str(self.vim.call('line', '$')))) + 'd: %s' bufpath = util.abspath(self.vim, buf.name) context['__temp'] = '' if (buf.options['modified'] or 'nofile' in buf.options['buftype'] or not self.vim.call('filereadable', bufpath)): context['__temp'] = self.vim.call( 'denite#helper#_get_temp_file', buf.number) context['__path'] = context['__temp'] else: context['__path'] = bufpath # Interactive mode context['is_interactive'] = True context['__args'] = ''
def on_init(self, context): """scantree.py command has special meaning, using the internal scantree.py Implementation""" if self.vars['command']: if self.vars['command'][0] == 'scantree.py': self.vars['command'] = self.parse_command_for_scantree( self.vars['command']) else: if not context['is_windows']: self.vars['command'] = [ 'find', '-L', ':directory', '-path', '*/.git/*', '-prune', '-o', '-type', 'l', '-print', '-o', '-type', 'f', '-print'] else: self.vars['command'] = self.parse_command_for_scantree( ['scantree.py']) context['__proc'] = None directory = context['args'][0] if len( context['args']) > 0 else context['path'] context['__directory'] = abspath(self.vim, directory)
def __async_gather_candidates(self, context, timeout): outs, errs = context['__proc'].communicate(timeout=timeout) if errs: self.error_message(errs) context['is_async'] = not context['__proc'].eof() if context['__proc'].eof(): context['__proc'] = None candidates = [] for line in outs: candidates.append({ 'word': line, 'abbr': line + ('/' if os.path.isdir(line) else ''), 'kind': ('directory' if os.path.isdir(line) else 'file'), 'action__path': abspath(self.vim, line), }) return candidates
def on_init(self, context): """scantree.py command has special meaning, using the internal scantree.py Implementation""" if self.vars['command']: if self.vars['command'][0] == 'scantree.py': self.vars['command'] = self.parse_command_for_scantree( self.vars['command']) else: if not context['is_windows']: self.vars['command'] = [ 'find', '-L', ':directory', '-path', '*/.git/*', '-prune', '-o', '-type', 'l', '-print', '-o', '-type', 'f', '-print' ] else: self.vars['command'] = self.parse_command_for_scantree( ['scantree.py', '--path', ':directory']) context['__proc'] = None directory = context['args'][0] if len( context['args']) > 0 else context['path'] context['__directory'] = abspath(self.vim, directory)
def gather_candidates(self, context): result = parse_jump_line( self.vim.call('getcwd'), context['__line']) if result and os.path.isfile(result[0]): return [{ 'word': '{0}: {1}{2}: {3}'.format( result[0], result[1], (':' + result[2] if result[2] != '0' else ''), result[3]), 'action__path': result[0], 'action__line': result[1], 'action__col': result[2], }] cfile = context['__cfile'] if match('[./]+$', cfile): return [] if os.path.exists(cfile): return [{'word': cfile, 'action__path': abspath(self.vim, cfile)}] if _checkhost(cfile): return [{'word': cfile, 'action__path': cfile}] return []
def gather_candidates(self, context: UserContext) -> Candidates: if context['is_redraw']: self._buflines = {} context['is_interactive'] = True if not context['input']: return [] linenr = context['__linenr'] candidates: Candidates = [] for bufnr in context['__bufnrs']: lines = [{ 'word': x, 'abbr': context['__fmt'] % (i + 1, x), 'action__path': abspath(self.vim, self.vim.current.buffer.name), 'action__bufnr': bufnr, 'action__col': 0, 'action__line': i + 1, 'action__text': x, } for [i, x] in enumerate(self._getbufline(bufnr))] if context['__emptiness'] == 'noempty': lines = list(filter(lambda c: c['word'] != '', lines)) if context['__direction'] == 'all': candidates += lines elif context['__direction'] == 'backward': candidates += list(reversed(lines[:linenr])) + list( reversed(lines[linenr:])) else: candidates += lines[linenr - 1:] + lines[:linenr - 1] return candidates
def gather_candidates(self, context: UserContext) -> Candidates: result = parse_jump_line( self.vim.call('getcwd'), context['__line']) if result and os.path.isfile(result[0]): return [{ 'word': '{}: {}{}: {}'.format( result[0], result[1], (':' + result[2] if result[2] != '0' else ''), result[3]), 'action__path': result[0], 'action__line': result[1], 'action__col': result[2], }] cfile = context['__cfile'] if match('[./]+$', cfile): return [] if os.path.exists(cfile): return [{'word': cfile, 'action__path': abspath(self.vim, cfile)}] if _checkhost(cfile) or match( r'https?://(127\.0\.0\.1|localhost)[:/]', cfile): return [{'word': cfile, 'action__path': cfile}] return []
def gather_candidates(self, context): result = parse_jump_line(self.vim.call('getcwd'), context['__line']) if result and os.path.isfile(result[0]): return [{ 'word': '{0}: {1}{2}: {3}'.format( result[0], result[1], (':' + result[2] if result[2] != '0' else ''), result[3]), 'action__path': result[0], 'action__line': result[1], 'action__col': result[2], }] cfile = context['__cfile'] if match('[./]+$', cfile): return [] if os.path.exists(cfile): return [{'word': cfile, 'action__path': abspath(self.vim, cfile)}] if _checkhost(cfile): return [{'word': cfile, 'action__path': cfile}] return []
def gather_candidates(self, context: UserContext) -> Candidates: return [{ "word": x["word"], "action__path": abspath(self.vim, x["word"]) } for x in super().gather_candidates(context)]