|
21 | 21 | FRAGMENT_PATTERN = re.compile(r'^L?(\d+)(?:,(\d+))?(?:-L?(\d+)(?:,(\d+))?)?')
|
22 | 22 |
|
23 | 23 |
|
| 24 | +def lsp_range_from_uri_fragment(fragment: str) -> Optional[Range]: |
| 25 | + match = FRAGMENT_PATTERN.match(fragment) |
| 26 | + if match: |
| 27 | + selection = {'start': {'line': 0, 'character': 0}, 'end': {'line': 0, 'character': 0}} # type: Range |
| 28 | + # Line and column numbers in the fragment are assumed to be 1-based and need to be converted to 0-based |
| 29 | + # numbers for the LSP Position structure. |
| 30 | + start_line, start_column, end_line, end_column = [max(0, int(g) - 1) if g else None for g in match.groups()] |
| 31 | + if start_line: |
| 32 | + selection['start']['line'] = start_line |
| 33 | + selection['end']['line'] = start_line |
| 34 | + if start_column: |
| 35 | + selection['start']['character'] = start_column |
| 36 | + selection['end']['character'] = start_column |
| 37 | + if end_line: |
| 38 | + selection['end']['line'] = end_line |
| 39 | + selection['end']['character'] = UINT_MAX |
| 40 | + if end_column is not None: |
| 41 | + selection['end']['character'] = end_column |
| 42 | + return selection |
| 43 | + return None |
| 44 | + |
| 45 | + |
24 | 46 | def open_file_uri(
|
25 | 47 | window: sublime.Window, uri: DocumentUri, flags: int = 0, group: int = -1
|
26 | 48 | ) -> Promise[Optional[sublime.View]]:
|
27 | 49 |
|
28 |
| - def parse_fragment(fragment: str) -> Optional[Range]: |
29 |
| - match = FRAGMENT_PATTERN.match(fragment) |
30 |
| - if match: |
31 |
| - selection = {'start': {'line': 0, 'character': 0}, 'end': {'line': 0, 'character': 0}} # type: Range |
32 |
| - # Line and column numbers in the fragment are assumed to be 1-based and need to be converted to 0-based |
33 |
| - # numbers for the LSP Position structure. |
34 |
| - start_line, start_column, end_line, end_column = [max(0, int(g) - 1) if g else None for g in match.groups()] |
35 |
| - if start_line: |
36 |
| - selection['start']['line'] = start_line |
37 |
| - selection['end']['line'] = start_line |
38 |
| - if start_column: |
39 |
| - selection['start']['character'] = start_column |
40 |
| - selection['end']['character'] = start_column |
41 |
| - if end_line: |
42 |
| - selection['end']['line'] = end_line |
43 |
| - selection['end']['character'] = UINT_MAX |
44 |
| - if end_column is not None: |
45 |
| - selection['end']['character'] = end_column |
46 |
| - return selection |
47 |
| - return None |
48 |
| - |
49 | 50 | decoded_uri = unquote(uri) # decode percent-encoded characters
|
50 | 51 | parsed = urlparse(decoded_uri)
|
51 | 52 | open_promise = open_file(window, decoded_uri, flags, group)
|
52 | 53 | if parsed.fragment:
|
53 |
| - selection = parse_fragment(parsed.fragment) |
| 54 | + selection = lsp_range_from_uri_fragment(parsed.fragment) |
54 | 55 | if selection:
|
55 | 56 | return open_promise.then(lambda view: _select_and_center(view, cast(Range, selection)))
|
56 | 57 | return open_promise
|
|
0 commit comments