From a1e74f2dfb98948d50efcdd4ac25c7a9282884fd Mon Sep 17 00:00:00 2001 From: "LAPTOP-G43MA5MO\\ekarni" Date: Sat, 6 Jan 2024 01:54:57 +0200 Subject: [PATCH 1/2] Support workspace symbols --- pylsp/hookspecs.py | 4 ++++ pylsp/plugins/symbols.py | 38 ++++++++++++++++++++++++++++++++------ pylsp/python_lsp.py | 8 ++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) 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..14275fcc 100644 --- a/pylsp/plugins/symbols.py +++ b/pylsp/plugins/symbols.py @@ -9,9 +9,35 @@ 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 @@ -19,8 +45,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 +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 @@ -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], diff --git a/pylsp/python_lsp.py b/pylsp/python_lsp.py index a31e7612..0c52b52b 100644 --- a/pylsp/python_lsp.py +++ b/pylsp/python_lsp.py @@ -278,6 +278,7 @@ def capabilities(self): "documentHighlightProvider": True, "documentRangeFormattingProvider": True, "documentSymbolProvider": True, + "workspaceSymbolProvider": True, "definitionProvider": True, "executeCommandProvider": { "commands": flatten(self._hook("pylsp_commands")) @@ -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)) @@ -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] From 0ccf39f94a07c03b35143517441eb94a6bbbe6e8 Mon Sep 17 00:00:00 2001 From: "LAPTOP-G43MA5MO\\ekarni" Date: Mon, 8 Jan 2024 01:04:12 +0200 Subject: [PATCH 2/2] improved log --- pylsp/plugins/symbols.py | 34 +++++++++++++++++++++++----------- pylsp/python_lsp.py | 7 +++---- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/pylsp/plugins/symbols.py b/pylsp/plugins/symbols.py index 14275fcc..cbb6838f 100644 --- a/pylsp/plugins/symbols.py +++ b/pylsp/plugins/symbols.py @@ -1,8 +1,10 @@ # 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 @@ -11,24 +13,34 @@ from pylsp.uris import to_fs_path @hookimpl -def pylsp_document_symbols(config,document,workspace): - return pylsp_document_symbols_int(config,document,workspace) +def pylsp_document_symbols(config,document): + return pylsp_document_symbols_int(config,document) @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)) + 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: + except OSError: log.warn('Fail to process file '+f) - return [] + 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) diff --git a/pylsp/python_lsp.py b/pylsp/python_lsp.py index 0c52b52b..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 @@ -892,10 +894,7 @@ 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])) + return flatten(self._hook("pylsp_workspace_symbols")) def flatten(list_of_lists):