Support line= style links
This commit is contained in:
parent
13eacac1f8
commit
c85ac1baf6
1 changed files with 37 additions and 22 deletions
|
@ -25,7 +25,6 @@ I haven't found any existing/mature scripts for this, please let me know if you
|
||||||
PROTOCOL_NAME = 'editor'
|
PROTOCOL_NAME = 'editor'
|
||||||
|
|
||||||
|
|
||||||
# TODO FIXME add a test for : in filename
|
|
||||||
def test_parse_uri():
|
def test_parse_uri():
|
||||||
assert parse_uri('editor:///path/to/file') == (
|
assert parse_uri('editor:///path/to/file') == (
|
||||||
'/path/to/file',
|
'/path/to/file',
|
||||||
|
@ -42,12 +41,23 @@ def test_parse_uri():
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
|
|
||||||
# TODO FIXME not sure about this..
|
# TODO not sure about whether this or lien= thing is preferrable
|
||||||
assert parse_uri('editor:///path/to/file/and/line:10') == (
|
assert parse_uri('editor:///path/to/file:10') == (
|
||||||
'/path/to/file/and/line',
|
'/path/to/file',
|
||||||
10,
|
10,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# todo not sure about this. I guess it's a more 'proper' way? non ambiguous and supports columns and other stuff potentially
|
||||||
|
assert parse_uri('editor:///path/to/file?line=10') == (
|
||||||
|
'/path/to/file',
|
||||||
|
10,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert parse_uri('editor:///path/to/file:oops/and:more') == (
|
||||||
|
'/path/to/file:oops/and:more',
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
|
||||||
import pytest # type: ignore
|
import pytest # type: ignore
|
||||||
with pytest.raises(Exception):
|
with pytest.raises(Exception):
|
||||||
parse_uri('badmime://whatever')
|
parse_uri('badmime://whatever')
|
||||||
|
@ -76,7 +86,7 @@ import sys
|
||||||
import subprocess
|
import subprocess
|
||||||
from subprocess import check_call, run
|
from subprocess import check_call, run
|
||||||
import tempfile
|
import tempfile
|
||||||
from urllib.parse import unquote
|
from urllib.parse import unquote, urlparse, parse_qsl
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -119,27 +129,30 @@ from typing import Tuple, Optional, List
|
||||||
Line = int
|
Line = int
|
||||||
File = str
|
File = str
|
||||||
def parse_uri(uri: str) -> Tuple[File, Optional[Line]]:
|
def parse_uri(uri: str) -> Tuple[File, Optional[Line]]:
|
||||||
proto = PROTOCOL_NAME + '://'
|
pr = urlparse(uri)
|
||||||
if not uri.startswith(proto):
|
if pr.scheme != PROTOCOL_NAME:
|
||||||
error(f"Unexpected protocol {uri}")
|
error(f"Unexpected protocol {uri}")
|
||||||
# not sure if a good idea to keep trying?
|
# not sure if a good idea to keep trying nevertheless?
|
||||||
|
path = unquote(pr.path)
|
||||||
uri = uri[len(proto):]
|
|
||||||
spl = uri.split(':')
|
|
||||||
|
|
||||||
linenum: Optional[int] = None
|
linenum: Optional[int] = None
|
||||||
if len(spl) == 1:
|
|
||||||
pass # no lnum specified
|
line_s = dict(parse_qsl(pr.query)).get('line', None)
|
||||||
elif len(spl) == 2:
|
if line_s is not None:
|
||||||
uri = spl[0]
|
linenum = int(line_s)
|
||||||
# TODO could use that for column number? maybe an overkill though..
|
|
||||||
# https://www.gnu.org/software/emacs/manual/html_node/emacs/emacsclient-Options.html
|
|
||||||
linenum = int(spl[1])
|
|
||||||
else:
|
else:
|
||||||
# TODO what if it actually has colons?
|
spl = path.rsplit(':', maxsplit=1)
|
||||||
error(f"Extra colons in URI {uri}")
|
|
||||||
uri = unquote(uri)
|
# meh. not sure about this
|
||||||
return (uri, linenum)
|
if len(spl) == 2:
|
||||||
|
try:
|
||||||
|
linenum = int(spl[1])
|
||||||
|
except ValueError:
|
||||||
|
# eh. presumably just a colon in filename
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
path = spl[0]
|
||||||
|
return (path, linenum)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -227,6 +240,8 @@ def launch_in_terminal(cmd: List[str]):
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
|
# TODO could use that for column number? maybe an overkill though.. and most extractors won'tsupport it anyway
|
||||||
|
# https://www.gnu.org/software/emacs/manual/html_node/emacs/emacsclient-Options.html
|
||||||
def main():
|
def main():
|
||||||
p = argparse.ArgumentParser()
|
p = argparse.ArgumentParser()
|
||||||
# TODO allow passing a binary?
|
# TODO allow passing a binary?
|
||||||
|
|
Loading…
Add table
Reference in a new issue