diff --git a/pylsp/hookspecs.py b/pylsp/hookspecs.py index 9c9cf387..184a82b1 100644 --- a/pylsp/hookspecs.py +++ b/pylsp/hookspecs.py @@ -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): diff --git a/pylsp/plugins/symbols.py b/pylsp/plugins/symbols.py index e3c961c7..cbb6838f 100644 --- a/pylsp/plugins/symbols.py +++ b/pylsp/plugins/symbols.py @@ -1,17 +1,55 @@ # Copyright 2017-2020 Palantir Technologies, Inc. +import re # Copyright 2021- Python Language Server Contributors. import logging from pathlib import Path +from jedi.file_io import FolderIO from pylsp import hookimpl from pylsp.lsp import SymbolKind log = logging.getLogger(__name__) - +from pylsp.uris import to_fs_path @hookimpl -def pylsp_document_symbols(config, document): +def pylsp_document_symbols(config,document): + return pylsp_document_symbols_int(config,document) + +@hookimpl +def pylsp_workspace_symbols(config,document,workspace): + def do(f): + for p in ignore_paths: + if re.search(p, f, re.IGNORECASE): + log.debug("Ignoring path %s", f) + return [] + 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 [] + except Exception as e : + log.exception('Failed , exception in file '+f + ' ' +str(e)) + return [] + symbols_settings = config.plugin_settings("jedi_symbols") + ignore_paths= symbols_settings.get("ignore_paths", []) + log.debug("ignore_paths: %s", ignore_paths) + from jedi.inference.references import recurse_find_python_files + fil = recurse_find_python_files(FolderIO(workspace._root_path), []) + fil=map(lambda f:str(f.path), fil) + symb=[] + + + 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 @@ -19,8 +57,8 @@ def pylsp_document_symbols(config, document): # 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({}) @@ -91,7 +129,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 @@ -108,7 +146,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], diff --git a/pylsp/python_lsp.py b/pylsp/python_lsp.py index a31e7612..fbbaec7f 100644 --- a/pylsp/python_lsp.py +++ b/pylsp/python_lsp.py @@ -1,6 +1,7 @@ # Copyright 2017-2020 Palantir Technologies, Inc. # Copyright 2021- Python Language Server Contributors. + from functools import partial import logging import os @@ -10,6 +11,7 @@ from typing import List, Dict, Any import ujson as json + from pylsp_jsonrpc.dispatchers import MethodDispatcher from pylsp_jsonrpc.endpoint import Endpoint from pylsp_jsonrpc.streams import JsonRpcStreamReader, JsonRpcStreamWriter @@ -278,6 +280,7 @@ def capabilities(self): "documentHighlightProvider": True, "documentRangeFormattingProvider": True, "documentSymbolProvider": True, + "workspaceSymbolProvider": True, "definitionProvider": True, "executeCommandProvider": { "commands": flatten(self._hook("pylsp_commands")) @@ -415,6 +418,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)) @@ -889,6 +893,9 @@ 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): + return flatten(self._hook("pylsp_workspace_symbols")) + def flatten(list_of_lists): return [item for lst in list_of_lists for item in lst]