Skip to content

Commit

Permalink
Make a trivial handler for 'LocationLink' (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
rwols committed Nov 29, 2019
1 parent f41eca0 commit 790d5f4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
8 changes: 4 additions & 4 deletions plugin/core/sessions.py
Expand Up @@ -110,10 +110,10 @@ def get_initialize_params(project_path: str, config: ClientConfig) -> dict:
},
"formatting": {},
"rangeFormatting": {},
"declaration": {},
"definition": {},
"typeDefinition": {},
"implementation": {},
"declaration": {"linkSupport": True},
"definition": {"linkSupport": True},
"typeDefinition": {"linkSupport": True},
"implementation": {"linkSupport": True},
"codeAction": {
"codeActionLiteralSupport": {
"codeActionKind": {
Expand Down
9 changes: 7 additions & 2 deletions plugin/goto.py
Expand Up @@ -46,8 +46,13 @@ def handle_response(self, response: 'Optional[Any]', position: int) -> None:
get_jump_history_for_view(self.view).push_selection(self.view)
# TODO: DocumentLink support.
location = response if isinstance(response, dict) else response[0]
file_path = uri_to_filename(location.get("uri"))
start = Point.from_lsp(location['range']['start'])
if "targetUri" in location:
# TODO: Do something clever with originSelectionRange and targetRange.
file_path = uri_to_filename(location["targetUri"])
start = Point.from_lsp(location["targetSelectionRange"]["start"])
else:
file_path = uri_to_filename(location["uri"])
start = Point.from_lsp(location["range"]["start"])
file_location = "{}:{}:{}".format(file_path, start.row + 1, start.col + 1)
debug("opening location", location)
window.open_file(file_location, sublime.ENCODED_POSITION)
Expand Down
46 changes: 40 additions & 6 deletions tests/test_goto.py
Expand Up @@ -21,6 +21,16 @@
}
]

RESPONSE_LOCATION_LINK = [
{
'originSelectionRange': {'start': {'line': 0, 'character': 0}},
'targetUri': RESPONSE[0]['uri'],
'targetRange': RESPONSE[0]['range'],
'targetSelectionRange': RESPONSE[0]['range']
}
]


CONTENT = r'''abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
Expand All @@ -36,12 +46,12 @@ def after_cursor(view) -> str:

class GotoTestCase(TextDocumentTestCase):

def do_run(self, text_document_request: str, subl_command_suffix: str) -> None:
def do_run(self, response: dict, text_document_request: str, subl_command_suffix: str) -> None:
self.view.run_command('insert', {'characters': CONTENT})
# Put the cursor back at the start of the buffer, otherwise is_at_word fails in goto.py.
self.view.sel().clear()
self.view.sel().add(sublime.Region(0, 0))
self.client.responses['textDocument/{}'.format(text_document_request)] = RESPONSE
self.client.responses['textDocument/{}'.format(text_document_request)] = response
self.view.run_command('lsp_symbol_{}'.format(subl_command_suffix))

def do_common_checks(self) -> None:
Expand All @@ -57,24 +67,48 @@ def do_common_checks(self) -> None:

def test_definition(self):
yield 100
self.do_run('definition', 'definition')
self.do_run(RESPONSE, 'definition', 'definition')
yield 100
self.do_common_checks()

def test_definition_location_link(self):
yield 100
self.do_run(RESPONSE_LOCATION_LINK, 'definition', 'definition')
yield 100
self.do_common_checks()

def test_type_definition(self):
yield 100
self.do_run('typeDefinition', 'type_definition')
self.do_run(RESPONSE, 'typeDefinition', 'type_definition')
yield 100
self.do_common_checks()

def test_type_definition_location_link(self):
yield 100
self.do_run(RESPONSE_LOCATION_LINK, 'typeDefinition', 'type_definition')
yield 100
self.do_common_checks()

def test_declaration(self):
yield 100
self.do_run('declaration', 'declaration')
self.do_run(RESPONSE, 'declaration', 'declaration')
yield 100
self.do_common_checks()

def test_declaration_location_link(self):
yield 100
self.do_run(RESPONSE_LOCATION_LINK, 'declaration', 'declaration')
yield 100
self.do_common_checks()

def test_implementation(self):
yield 100
self.do_run('implementation', 'implementation')
self.do_run(RESPONSE, 'implementation', 'implementation')
yield 100
self.do_common_checks()

def test_implementation_location_link(self):
yield 100
self.do_run(RESPONSE_LOCATION_LINK, 'implementation', 'implementation')
yield 100
self.do_common_checks()

0 comments on commit 790d5f4

Please sign in to comment.