Skip to content

Support workspace symbols #511

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
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Support workspace symbols
  • Loading branch information
eyalk11 committed Jan 6, 2024
commit a1e74f2dfb98948d50efcdd4ac25c7a9282884fd
4 changes: 4 additions & 0 deletions pylsp/hookspecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def pylsp_document_highlight(config, workspace, document, position):
def pylsp_document_symbols(config, workspace, document):
pass

@hookspec
def pylsp_workspace_symbols(config, workspace, document):
pass


@hookspec(firstresult=True)
def pylsp_execute_command(config, workspace, command, arguments):
Expand Down
38 changes: 32 additions & 6 deletions pylsp/plugins/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,44 @@

log = logging.getLogger(__name__)


from pylsp.uris import to_fs_path
@hookimpl
def pylsp_document_symbols(config, document):
def pylsp_document_symbols(config,document,workspace):
return pylsp_document_symbols_int(config,document,workspace)

@hookimpl
def pylsp_workspace_symbols(config,document,workspace):
from jedi.inference.references import _find_project_modules
workspace._root_path = to_fs_path(workspace._root_path)

fil=_find_project_modules(document.jedi_script()._inference_state,[])
fil=map(lambda f:str(f.path), fil)
symb=[]
def do(f):
doc=workspace.get_document(to_fs_path(f))
try:
return pylsp_document_symbols_int(config,doc,disable_all_scopes=True)
except OSError:
log.warn('Fail to process file '+f)
return []
for f in fil:
print(f)
symb+=do(f)
return symb


# ios = recurse_find_python_folders_and_files(FolderIO(str(self._path)))

def pylsp_document_symbols_int(config, document,disable_all_scopes=False):
# pylint: disable=broad-except
# pylint: disable=too-many-nested-blocks
# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
# pylint: disable=too-many-statements

symbols_settings = config.plugin_settings("jedi_symbols")
all_scopes = symbols_settings.get("all_scopes", True)
add_import_symbols = symbols_settings.get("include_import_symbols", True)
all_scopes = not disable_all_scopes and symbols_settings.get("all_scopes", True)
add_import_symbols = not disable_all_scopes and symbols_settings.get("include_import_symbols", True)
definitions = document.jedi_names(all_scopes=all_scopes)
symbols = []
exclude = set({})
Expand Down Expand Up @@ -91,7 +117,7 @@ def pylsp_document_symbols(config, document):
else:
continue

if _include_def(d) and Path(document.path) == Path(d.module_path):
if _include_def(d) and Path(to_fs_path(str(document.path))) == Path(to_fs_path(str(d.module_path))):
tuple_range = _tuple_range(d)
if tuple_range in exclude:
continue
Expand All @@ -108,7 +134,7 @@ def pylsp_document_symbols(config, document):
"name": d.name,
"containerName": _container(d),
"location": {
"uri": document.uri,
"uri": str(d.module_path),
"range": _range(d),
},
"kind": _kind(d) if kind is None else _SYMBOL_KIND_MAP[kind],
Expand Down
8 changes: 8 additions & 0 deletions pylsp/python_lsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ def capabilities(self):
"documentHighlightProvider": True,
"documentRangeFormattingProvider": True,
"documentSymbolProvider": True,
"workspaceSymbolProvider": True,
"definitionProvider": True,
"executeCommandProvider": {
"commands": flatten(self._hook("pylsp_commands"))
Expand Down Expand Up @@ -415,6 +416,7 @@ def completion_item_resolve(self, completion_item):
def definitions(self, doc_uri, position):
return flatten(self._hook("pylsp_definitions", doc_uri, position=position))


def document_symbols(self, doc_uri):
return flatten(self._hook("pylsp_document_symbols", doc_uri))

Expand Down Expand Up @@ -889,6 +891,12 @@ def m_workspace__did_change_watched_files(self, changes=None, **_kwargs):
def m_workspace__execute_command(self, command=None, arguments=None):
return self.execute_command(command, arguments)

def m_workspace__symbol(self, query=None):
l=list(self.workspace.documents.keys())
if len(l)==0:
return []
return flatten(self._hook("pylsp_workspace_symbols", l[0]))


def flatten(list_of_lists):
return [item for lst in list_of_lists for item in lst]
Expand Down