def gather_candidates_universal(self, context: UserContext) -> Candidates: args: typing.List[str] = [] args += self.vars['command'] args += self.vars['options'] if self.vars['force_filetype'] and '__language' in context: args.append('--language-force={}'.format(context['__language'])) args += ['--output-format=json', '-f', '-'] if context.get('__nofile', False): tempfile = self.vim.funcs.tempname() with open(tempfile, "w") as f: f.writelines( map(lambda x: str(x + '\n'), self.vim.buffers[context['__bufnr']][:])) args += [tempfile] else: args += [context['__path']] self.print_message(context, ' '.join(args)) try: p = run(args, check=True, stdout=PIPE, stderr=PIPE) outputs = p.stdout.decode(self.vars['encoding']).splitlines() except CalledProcessError as e: err_msg = e.stderr.decode(self.vars['encoding']).splitlines() self.error_message(context, err_msg) return [] candidates = [] for entry in outputs: info = loads(entry) candidate = { 'word': info['name'], 'action__path': info['path'] if not context.get('__nofile') else context['__path'], } candidates.append(candidate) fmt = '{name:<35.35} ' if 'line' in info: candidate['action__line'] = info['line'] fmt += '{line:<6}' if 'scope' in info: fmt += '@{scope:<25.25}' else: info['file'] = Path(info['path']).name fmt += '@{file:<25}' if 'kind' in info: info['kind'] = info['kind'][0] fmt += ' [{kind}]' if 'pattern' in info: if 'line' not in info: candidate['action__pattern'] = info['pattern'] info['pattern'] = info['pattern'][2:-2].lstrip(' \t\v') info['pattern'] = '<-> ' + info['pattern'] fmt += ' {pattern}' candidate['abbr'] = fmt.format(**info) return candidates
def do_action(self, context: UserContext, action_name: str, targets: Candidates) -> bool: action = self._get_action_targets(context, action_name, targets) if not action: return True for target in [x for x in targets if 'source_index' in x]: source = self._current_sources[int(target['source_index'])] target['source_context'] = { k: v for k, v in source.context.items() if k.startswith('__') } if source.is_public_context else {} context['targets'] = targets index = action['kind'] + ',source/' + action['source'] new_context = (action['func'](context) if action['func'] else self._vim.call( 'denite#custom#_call_action', index, action['name'], context)) if new_context: context.update(new_context) return False
def on_init(self, context: UserContext) -> None: context['__path'] = (context['args'][0] if len(context['args']) > 0 else self.vim.current.buffer.name) if self.vars['force_filetype']: filetype = self.vim.current.buffer.options['filetype'] context.pop('__langauge', None) ctags_filetype = self.vars['language_map'].get(filetype, None) if ctags_filetype: context['__langauge'] = ctags_filetype
def filter(self, context: UserContext) -> Candidates: project = path2project(self.vim, context.get('path', ''), context.get('root_markers', '')) if project == '': project = self.vim.call('getcwd') project += '/' max_width = context['max_candidate_width'] return [x for x in context['candidates'] if 'action__path' not in x or x['action__path'][:max_width].startswith(project)]
def on_close(self, context: UserContext) -> None: if context.get('__proc'): context['__proc'].kill() context['__proc'] = None if not context['__temp']: return path = Path(context['__temp']) if path.exists() and not path.is_dir(): path.unlink()
def __init__(self, host: str, port: int, commands: typing.List[str], context: UserContext, timeout: int) -> None: self._enc = context.get('encoding', 'utf-8') self._eof = False self._outs: typing.List[str] = [] self._timeout = timeout self._context = context self._sock = self.connect(host, port, self._timeout) self._welcome = self.receive() self.sendall(commands) self._queue_out: Queue[str] = Queue() self._thread: typing.Optional[Thread] = Thread( target=self.enqueue_output) self._thread.start()
def gather_candidates(self, context: UserContext) -> Candidates: if not context['input']: return [] args = self.init_grep_args(context) if args == context['__args'] and context['__proc']: return self._async_gather_candidates(context, 0.5) if context.get('__proc'): context['__proc'].kill() context['__proc'] = None context['__args'] = args self.print_message(context, str(args)) context['__proc'] = process.Process(args, context, context['path']) return self._async_gather_candidates(context, 0.5)