-
Notifications
You must be signed in to change notification settings - Fork 217
Implement support for workspace/symbol #564
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
Open
niilohlin
wants to merge
6
commits into
python-lsp:develop
Choose a base branch
from
niilohlin:workspace-symbols
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
# Copyright 2017-2020 Palantir Technologies, Inc. | ||
# Copyright 2021- Python Language Server Contributors. | ||
|
||
import logging | ||
|
||
from pylsp import hookimpl | ||
from pylsp.lsp import SymbolKind | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
@hookimpl | ||
def pylsp_workspace_symbol(workspace, query): | ||
if not query or not workspace: | ||
return [] | ||
|
||
return [ | ||
_jedi_name_to_symbol(jedi_name) | ||
for jedi_name in workspace.complete_search(query) | ||
] | ||
|
||
|
||
def _jedi_name_to_symbol(jedi_name): | ||
return { | ||
"name": jedi_name.name, | ||
"kind": _jedi_type_to_symbol_kind(jedi_name.type), | ||
"location": { | ||
"uri": "file://" + str(jedi_name.module_path), | ||
"range": { | ||
"start": {"line": jedi_name.line - 1, "character": jedi_name.column}, | ||
"end": {"line": jedi_name.line - 1, "character": jedi_name.column}, | ||
}, | ||
}, | ||
} | ||
|
||
|
||
def _jedi_type_to_symbol_kind(jedi_type): | ||
return { | ||
"module": SymbolKind.Module, | ||
"class": SymbolKind.Class, | ||
"function": SymbolKind.Function, | ||
}.get(jedi_type, SymbolKind.Variable) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
# Copyright 2017-2020 Palantir Technologies, Inc. | ||
# Copyright 2021- Python Language Server Contributors. | ||
|
||
import os | ||
import sys | ||
|
||
import pytest | ||
|
||
from pylsp import uris | ||
from pylsp.plugins.workspace_symbol import pylsp_workspace_symbol | ||
|
||
PY2 = sys.version[0] == "2" | ||
LINUX = sys.platform.startswith("linux") | ||
CI = os.environ.get("CI") | ||
DOC_URI = uris.from_fs_path(__file__) | ||
|
||
|
||
DOC1_NAME = "file1.py" | ||
DOC2_NAME = "file2.py" | ||
|
||
DOC1 = """class Test1(): | ||
pass | ||
""" | ||
|
||
DOC2 = """from file1 import Test1 | ||
|
||
try: | ||
Test1() | ||
except UnicodeError: | ||
pass | ||
""" | ||
|
||
|
||
@pytest.fixture | ||
def tmp_workspace(temp_workspace_factory): | ||
return temp_workspace_factory( | ||
{ | ||
DOC1_NAME: DOC1, | ||
DOC2_NAME: DOC2, | ||
} | ||
) | ||
|
||
|
||
def test_symbols_empty_query(tmp_workspace): | ||
symbols = pylsp_workspace_symbol(tmp_workspace, "") | ||
|
||
assert len(symbols) == 0 | ||
|
||
|
||
def test_symbols_nonempty_query(tmp_workspace): | ||
symbols = pylsp_workspace_symbol(tmp_workspace, "Test") | ||
|
||
assert len(symbols) == 1 | ||
assert symbols[0]["name"] == "Test1" | ||
assert symbols[0]["kind"] == 5 # Class |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.