Cleanup linenum matching and add some tests
This commit is contained in:
parent
7800c598f6
commit
cb4a9d89cf
@ -702,7 +702,7 @@ def main(args: List[str]) -> Optional[Dict[str, Any]]:
|
||||
return None
|
||||
|
||||
|
||||
def linenum_handle_result(args: List[str], data: Dict[str, Any], target_window_id: int, boss: BossType, extra_cli_args: Sequence[str], *a: Any) -> None:
|
||||
def linenum_process_result(data: Dict[str, Any]) -> Tuple[str, int]:
|
||||
pat = re.compile(r':(\d+)$')
|
||||
for m, g in zip(data['match'], data['groupdicts']):
|
||||
if m:
|
||||
@ -715,10 +715,13 @@ def linenum_handle_result(args: List[str], data: Dict[str, Any], target_window_i
|
||||
line = m.group(1)
|
||||
path = path[:-len(m.group())]
|
||||
|
||||
path = os.path.expanduser(path.split(':')[-1])
|
||||
line = int(line)
|
||||
break
|
||||
else:
|
||||
return os.path.expanduser(path), int(line)
|
||||
return '', -1
|
||||
|
||||
|
||||
def linenum_handle_result(args: List[str], data: Dict[str, Any], target_window_id: int, boss: BossType, extra_cli_args: Sequence[str], *a: Any) -> None:
|
||||
path, line = linenum_process_result(data)
|
||||
if not path:
|
||||
return
|
||||
|
||||
cmd = [x.format(path=path, line=line) for x in extra_cli_args or ('vim', '+{line}', '{path}')]
|
||||
|
||||
@ -1,18 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
|
||||
import os
|
||||
from . import BaseTest
|
||||
|
||||
|
||||
class TestHints(BaseTest):
|
||||
|
||||
def test_url_hints(self):
|
||||
from kittens.hints.main import parse_hints_args, functions_for, mark, convert_text
|
||||
from kittens.hints.main import (
|
||||
Mark, convert_text, functions_for, linenum_marks,
|
||||
linenum_process_result, mark, parse_hints_args
|
||||
)
|
||||
args = parse_hints_args([])[0]
|
||||
pattern, post_processors = functions_for(args)
|
||||
|
||||
def create_marks(text, cols=20):
|
||||
def create_marks(text, cols=20, mark=mark):
|
||||
text = convert_text(text, cols)
|
||||
return tuple(mark(pattern, post_processors, text, args))
|
||||
|
||||
@ -30,8 +33,27 @@ class TestHints(BaseTest):
|
||||
t(f'`xyz <{u}>`_.', u)
|
||||
t(f'<a href="{u}">moo', u)
|
||||
|
||||
def m(text, path, line, cols=20):
|
||||
|
||||
def adapt(pattern, postprocessors, text, *a):
|
||||
return linenum_marks(text, args, Mark, ())
|
||||
|
||||
marks = create_marks(text, cols, mark=adapt)
|
||||
data = {'groupdicts': [m.groupdict for m in marks], 'match': [m.text for m in marks]}
|
||||
self.ae(linenum_process_result(data), (path, line))
|
||||
|
||||
args = parse_hints_args('--type=linenum'.split())[0]
|
||||
m('file.c:23', 'file.c', 23)
|
||||
m('file.c:23:32', 'file.c', 23)
|
||||
m('file.cpp:23:1', 'file.cpp', 23)
|
||||
m('a/file.c:23', 'a/file.c', 23)
|
||||
m('a/file.c:23:32', 'a/file.c', 23)
|
||||
m('~/file.c:23:32', os.path.expanduser('~/file.c'), 23)
|
||||
|
||||
def test_ip_hints(self):
|
||||
from kittens.hints.main import parse_hints_args, functions_for, mark, convert_text
|
||||
from kittens.hints.main import (
|
||||
convert_text, functions_for, mark, parse_hints_args
|
||||
)
|
||||
args = parse_hints_args(['--type', 'ip'])[0]
|
||||
pattern, post_processors = functions_for(args)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user