def test_parse_tag_line(): assert util.parse_tagline( 'name file /pattern/;"', '') == { 'name': 'name', 'file': 'file', 'pattern': 'pattern', 'line': '', 'type': '', 'ref': '', } assert util.parse_tagline( 'name file 1100;"', '') == { 'name': 'name', 'file': 'file', 'pattern': '', 'line': '1100', 'type': '', 'ref': '', } assert util.parse_tagline( 'name file 1;" f', '') == { 'name': 'name', 'file': 'file', 'pattern': '', 'line': '1', 'type': 'f', 'ref': '', } assert util.parse_tagline( 'name file 1;" f foo bar', '') == { 'name': 'name', 'file': 'file', 'pattern': '', 'line': '1', 'type': 'f', 'ref': 'foo bar', } assert util.parse_tagline( 'name file / pattern/;"', '') == { 'name': 'name', 'file': 'file', 'pattern': ' pattern', 'line': '', 'type': '', 'ref': '', }
def gather_candidates(self, context): candidates = [] for f in self._tags: with open(f, 'r', encoding=context['encoding'], errors='replace') as ins: for line in ins: if re.match('!', line) or not line: continue info = parse_tagline(line.rstrip(), f) candidate = { 'word': info['name'], 'action__path': info['file'], } if info['type']: fmt = '{name} [{type}] {file} {ref}' else: fmt = '{name} {file} {ref}' candidate['abbr'] = fmt.format(**info) if info['line']: candidate['action__line'] = info['line'] else: candidate['action__pattern'] = info['pattern'] candidates.append(candidate) return sorted(candidates, key=lambda value: value['word'])
def gather_candidates(self, context): with tempfile.NamedTemporaryFile(mode='w') as tf: args = [] args += self.vars['command'] args += self.vars['options'] args += ['-o', tf.name] args += [context['__path']] self.print_message(context, args) tf.close() try: check_output(args).decode(self.vars['encoding'], 'replace') except CalledProcessError: return [] candidates = [] with open(tf.name, encoding=self.vars['encoding']) as f: for line in f: if re.match('!', line) or not line: continue info = parse_tagline(line.rstrip(), tf.name) if info['type'] in self.vars['ignore_types']: continue candidates.append({ 'word': '{name} [{type}] {ref}'.format(**info), 'action__path': context['__path'], 'action__pattern': info['pattern'] }) return candidates
def gather_candidates(self, context): with tempfile.NamedTemporaryFile( mode='w', encoding=self.vars['encoding']) as tf: args = [] args += self.vars['command'] args += self.vars['options'] args += [self.vars['file_opt'], tf.name] args += [context['__path']] self.print_message(context, args) tf.close() try: check_output(args).decode(self.vars['encoding'], 'replace') except CalledProcessError: return [] candidates = [] with open(tf.name, encoding=self.vars['encoding'], errors='replace') as f: for line in f: if re.match('!', line) or not line: continue info = parse_tagline(line.rstrip(), tf.name) if info['type'] in self.vars['ignore_types']: continue candidates.append({ 'word': '{name} [{type}] {ref}'.format(**info), 'action__path': context['__path'], 'action__pattern': info['pattern'] }) return candidates
def _get_candidate(self, filename: str, line: str) -> Candidate: if re.match('!', line) or not line: return {} info = parse_tagline(line.rstrip(), filename) candidate = { 'word': info['name'], 'action__path': info['file'] } info['name'] = ( (info['name'][:33] + '..') if len(info['name']) >= 33 else info['name'] ) info['file'] = Path(info['file']).name fmt = '{name:<35} @{file:<25}' if info['line']: candidate['action__line'] = info['line'] fmt += ':{line} [{type}] {ref}' else: candidate['action__pattern'] = info['pattern'] m = re.search(r'\^\S*(.*)\$', info['pattern']) if m: info['pattern'] = '<-> ' + m.group(1).lstrip() fmt += ' [{type}] {pattern}' candidate['abbr'] = fmt.format(**info) return candidate
def gather_candidates(self, context): with tempfile.NamedTemporaryFile(mode='w') as tf: command = [] command += self.vars['command'] command += self.vars['options'] command += [tf.name, context['__cwd']] try: check_output(command).decode(self.vars['encoding']) except CalledProcessError: return [] candidates = [] with open(tf.name) as f: for line in f: if re.match('!', line) or not line: continue info = parse_tagline(line.rstrip()) if info['type'] in self.vars['ignore_types']: continue info['file'] = self.vim.call( 'fnamemodify', info['file'], ':~:.') candidates.append({ 'word': '{file} {name} [{type}] {ref}'.format(**info), 'action__path': info['file'], 'action__pattern': info['pattern'], '__type': info['type'] }) return sorted( candidates, key=lambda item: (item['action__path'], item['__type']) )
def gather_candidates_legacy(self, context: UserContext) -> Candidates: with tempfile.NamedTemporaryFile( mode='w', encoding=self.vars['encoding'] ) as tf: args: typing.List[str] = [] args += self.vars['command'] args += self.vars['options'] args += [self.vars['file_opt'], tf.name] args += [context['__path']] self.print_message(context, ' '.join(args)) # Close this file before giving to ctags # Otherwise this will error on Windows tf.close() try: check_output(args).decode(self.vars['encoding'], 'replace') except CalledProcessError: return [] ignore_types = self.vars['ignore_types'] candidates = [] with open(tf.name, encoding=self.vars['encoding'], errors='replace') as f: for line in f: if re.match('!', line) or not line: continue info = parse_tagline(line.rstrip(), tf.name) if info['type'] in ignore_types: continue candidate = { 'word': info['name'], 'action__path': info['file'] } info['name'] = ( (info['name'][:33] + '..') if len(info['name']) >= 33 else info['name'] ) info['file'] = Path(info['file']).name fmt = '{name:<35} @{file:<25}' if info['line']: candidate['action__line'] = info['line'] fmt += ':{line} [{type}] {ref}' else: candidate['action__pattern'] = info['pattern'] m = re.search(r'\^\S*(.*)\$', info['pattern']) if m: info['pattern'] = '<-> ' + m.group(1).lstrip() fmt += ' [{type}] {pattern}' candidate['abbr'] = fmt.format(**info) candidates.append(candidate) return candidates
def gather_candidates(self, context): candidates = [] for f in self.vim.call('tagfiles'): with open(f, 'r') as ins: for line in ins: if re.match('!', line) or not line: continue info = parse_tagline(line.rstrip(), f) candidates.append({ 'word': '{name} [{type}] {ref}'.format(**info), 'action__path': info['file'], 'action__pattern': info['pattern'] }) return sorted(candidates, key=lambda value: value['word'])
def test_parse_tag_line(): assert util.parse_tagline('name file /pattern/;"', '') == { 'name': 'name', 'file': str(Path('file').resolve()), 'pattern': 'pattern', 'line': '', 'type': '', 'ref': '', } assert util.parse_tagline('name file 1100;"', '') == { 'name': 'name', 'file': str(Path('file').resolve()), 'pattern': '', 'line': '1100', 'type': '', 'ref': '', } assert util.parse_tagline('name file /*foo*', '') == { 'name': 'name', 'file': str(Path('file').resolve()), 'pattern': r'\*foo\*', 'line': '', 'type': '', 'ref': '', } assert util.parse_tagline('name file 1;" f', '') == { 'name': 'name', 'file': str(Path('file').resolve()), 'pattern': '', 'line': '1', 'type': 'f', 'ref': '', } assert util.parse_tagline('name file 1;" f foo bar', '') == { 'name': 'name', 'file': str(Path('file').resolve()), 'pattern': '', 'line': '1', 'type': 'f', 'ref': 'foo bar', } assert util.parse_tagline('name file / pattern/;"', '') == { 'name': 'name', 'file': str(Path('file').resolve()), 'pattern': ' pattern', 'line': '', 'type': '', 'ref': '', }
def gather_candidates(self, context): candidates = [] for f in self.__tags: with open(f, 'r') as ins: for line in ins: if re.match('!', line) or not line: continue info = parse_tagline(line.rstrip(), f) candidate = { 'word': '{name} [{type}] {file} {ref}'.format(**info), 'action__path': info['file'], } if info['line']: candidate['action__line'] = info['line'] else: candidate['action__pattern'] = info['pattern'] candidates.append(candidate) return sorted(candidates, key=lambda value: value['word'])
def gather_candidates(self, context): with tempfile.NamedTemporaryFile(mode='w', encoding=self.vars['encoding']) as tf: args = [] args += self.vars['command'] args += self.vars['options'] args += [self.vars['file_opt'], tf.name] args += [context['__path']] self.print_message(context, args) tf.close() try: check_output(args).decode(self.vars['encoding'], 'replace') except CalledProcessError: return [] candidates = [] with open(tf.name, encoding=self.vars['encoding'], errors='replace') as f: for line in f: if re.match('!', line) or not line: continue info = parse_tagline(line.rstrip(), tf.name) candidate = { 'word': info['name'], 'action__path': info['file'], } fmt = '{name} [{type}] {file} {ref}' candidate['abbr'] = fmt.format(**info) if info['line']: candidate['action__line'] = info['line'] else: candidate['action__pattern'] = info['pattern'] candidates.append(candidate) return candidates