-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a custom "find callers" command (#90)
typescript-language-server implements a custom "textDocument/calls" request which is a lot like normal "find references" but it skips all references that are not calling the symbol. So it filters a lot of noise like references to function being imported or just passed around. The "textDocument/calls" request supports finding both callers (implemented here) and a sort of an opposite mode of operation where it finds all functions called inside the selected symbol (function). I find that kinda useless so didn't implement it but it can be easily added by creating another `lsp_typescript_calls` command and passing "direction": "outgoing" argument to it.
- Loading branch information
Showing
7 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
.dependabot export-ignore | ||
.github/ export-ignore | ||
codecov.yml export-ignore | ||
mypy.ini export-ignore | ||
tests/ export-ignore | ||
tox.ini export-ignore | ||
unittesting.json export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from .protocol import Call, CallsDirection, CallsRequestParams, CallsResponse | ||
from LSP.plugin import Request | ||
from LSP.plugin import Session | ||
from LSP.plugin.core.protocol import LocationLink | ||
from LSP.plugin.core.registry import LspTextCommand | ||
from LSP.plugin.core.typing import Optional | ||
from LSP.plugin.core.views import text_document_position_params | ||
from LSP.plugin.locationpicker import LocationPicker | ||
import functools | ||
import sublime | ||
|
||
|
||
SESSION_NAME = "LSP-typescript" | ||
|
||
|
||
class LspTypescriptCallsCommand(LspTextCommand): | ||
|
||
session_name = SESSION_NAME | ||
|
||
def is_enabled(self) -> bool: | ||
selection = self.view.sel() | ||
return len(selection) > 0 and super().is_enabled() | ||
|
||
def run(self, edit: sublime.Edit, direction: CallsDirection) -> None: | ||
session = self.session_by_name(self.session_name) | ||
if session is None: | ||
return | ||
position_params = text_document_position_params(self.view, self.view.sel()[0].b) | ||
params = { | ||
'textDocument': position_params['textDocument'], | ||
'position': position_params['position'], | ||
'direction': direction | ||
} # type: CallsRequestParams | ||
session.send_request(Request("textDocument/calls", params), functools.partial(self.on_result_async, session)) | ||
|
||
def on_result_async(self, session: Session, result: Optional[CallsResponse]) -> None: | ||
if not result: | ||
return | ||
|
||
def to_location_link(call: Call) -> LocationLink: | ||
return { | ||
'targetUri': call['location']['uri'], | ||
'targetSelectionRange': call['location']['range'], | ||
} | ||
|
||
locations = list(map(to_location_link, result['calls'])) | ||
self.view.run_command("add_jump_record", {"selection": [(r.a, r.b) for r in self.view.sel()]}) | ||
LocationPicker(self.view, session, locations, side_by_side=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[mypy] | ||
# ignore_missing_imports = True | ||
# check_untyped_defs = True | ||
disallow_untyped_defs = True | ||
strict_optional = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from LSP.plugin.core.protocol import Location, Position, RangeLsp, TextDocumentIdentifier | ||
from LSP.plugin.core.typing import List, Literal, Optional, TypedDict, Union | ||
|
||
|
||
CallsDirection = Union[Literal['incoming'], Literal['outgoing']] | ||
|
||
CallsRequestParams = TypedDict('CallsRequestParams', { | ||
'textDocument': TextDocumentIdentifier, | ||
'position': Position, | ||
'direction': CallsDirection | ||
}, total=True) | ||
|
||
DefinitionSymbol = TypedDict('DefinitionSymbol', { | ||
'name': str, | ||
'detail': Optional[str], | ||
'kind': int, | ||
'location': Location, | ||
'selectionRange': RangeLsp, | ||
}, total=True) | ||
|
||
Call = TypedDict('Call', { | ||
'location': Location, | ||
'symbol': DefinitionSymbol, | ||
}, total=True) | ||
|
||
CallsResponse = TypedDict('CallsResponse', { | ||
'symbol': Optional[DefinitionSymbol], | ||
'calls': List[Call], | ||
}, total=True) |