Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally fallback to goto_definition in lsp_symbol_definition #1986

Merged
merged 5 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@
// {
// "command": "lsp_symbol_definition",
// "args": {
// "side_by_side": false
// "side_by_side": false,
// "fallback": false
// },
// "keys": [
// "f12"
Expand Down
3 changes: 3 additions & 0 deletions docs/src/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ In addition to the basic "Goto Definition", the protocol also provides further r
- Goto Declaration
- Goto Implementation

Additionally, the LSP's "Goto Definition" command can fall back to the built-in Sublime's "Goto Definition" if the `fallback` argument is set to `true`.
This way, when there are no results found the built-in "Goto Definition" command will be triggered.

## Find References

[Example GIF 1](https://user-images.githubusercontent.com/6579999/128551752-b37fe407-148c-41cf-b1e4-6fe96ed0f77c.gif)
Expand Down
41 changes: 31 additions & 10 deletions plugin/goto.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,74 @@
class LspGotoCommand(LspTextCommand):

method = ''
fallback_command = ''

def is_enabled(self, event: Optional[dict] = None, point: Optional[int] = None, side_by_side: bool = False) -> bool:
return super().is_enabled(event, point)
def is_enabled(
self,
event: Optional[dict] = None,
point: Optional[int] = None,
side_by_side: bool = False,
fallback: bool = False
) -> bool:
return fallback or super().is_enabled(event, point)

def run(
self,
_: sublime.Edit,
event: Optional[dict] = None,
point: Optional[int] = None,
side_by_side: bool = False
side_by_side: bool = False,
fallback: bool = False
) -> None:
session = self.best_session(self.capability)
position = get_position(self.view, event, point)
if session and position is not None:
rchl marked this conversation as resolved.
Show resolved Hide resolved
params = text_document_position_params(self.view, position)
request = Request(self.method, params, self.view, progress=True)
session.send_request(request, functools.partial(self._handle_response_async, session, side_by_side))
session.send_request(
request, functools.partial(self._handle_response_async, session, side_by_side, fallback)
)
else:
self._handle_no_results(fallback, side_by_side)

def _handle_response_async(
self,
session: Session,
side_by_side: bool,
fallback: bool,
response: Union[None, Location, List[Location], List[LocationLink]]
) -> None:
if isinstance(response, dict):
self.view.run_command("add_jump_record", {"selection": [(r.a, r.b) for r in self.view.sel()]})
open_location_async(session, response, side_by_side)
elif isinstance(response, list):
if len(response) == 0:
window = self.view.window()
if window:
window.status_message("No results found")
self._handle_no_results(fallback, side_by_side)
elif len(response) == 1:
self.view.run_command("add_jump_record", {"selection": [(r.a, r.b) for r in self.view.sel()]})
open_location_async(session, response[0], side_by_side)
else:
self.view.run_command("add_jump_record", {"selection": [(r.a, r.b) for r in self.view.sel()]})
sublime.set_timeout(functools.partial(LocationPicker, self.view, session, response, side_by_side))
else:
window = self.view.window()
if window:
window.status_message("No results found")
self._handle_no_results(fallback, side_by_side)

def _handle_no_results(self, fallback: bool = False, side_by_side: bool = False) -> None:
window = self.view.window()

if not window:
return

if fallback and self.fallback_command:
window.run_command(self.fallback_command, {"side_by_side": side_by_side})
else:
window.status_message("No results found")


class LspSymbolDefinitionCommand(LspGotoCommand):
method = "textDocument/definition"
capability = method_to_capability(method)[0]
fallback_command = "goto_definition"
rwols marked this conversation as resolved.
Show resolved Hide resolved


class LspSymbolTypeDefinitionCommand(LspGotoCommand):
Expand Down