|
| 1 | +# Copyright 2017-2020 Palantir Technologies, Inc. |
| 2 | +# Copyright 2021- Python Language Server Contributors. |
| 3 | + |
| 4 | +import logging |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from pylsp import hookimpl |
| 8 | +from pylsp.lsp import SymbolKind |
| 9 | + |
| 10 | +log = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +@hookimpl |
| 14 | +def pylsp_workspace_symbol(config, workspace, document, query): |
| 15 | + if not query or not workspace: |
| 16 | + return [] |
| 17 | + |
| 18 | + return [_jedi_name_to_symbol(jedi_name) for jedi_name in workspace.search(query)] |
| 19 | + |
| 20 | + |
| 21 | +def _jedi_name_to_symbol(jedi_name): |
| 22 | + return { |
| 23 | + "name": jedi_name.name, |
| 24 | + "kind": _jedi_type_to_symbol_kind(jedi_name.type), |
| 25 | + "location": { |
| 26 | + "uri": "file://" + str(jedi_name.module_path), |
| 27 | + "range": { |
| 28 | + "start": {"line": jedi_name.line - 1, "character": jedi_name.column}, |
| 29 | + "end": {"line": jedi_name.line - 1, "character": jedi_name.column}, |
| 30 | + }, |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | +def _jedi_type_to_symbol_kind(jedi_type): |
| 36 | + if jedi_type == "module": |
| 37 | + return SymbolKind.Module |
| 38 | + if jedi_type == "class": |
| 39 | + return SymbolKind.Class |
| 40 | + if jedi_type == "function": |
| 41 | + return SymbolKind.Function |
| 42 | + if jedi_type == "instance": |
| 43 | + return SymbolKind.Variable |
| 44 | + if jedi_type == "statement": |
| 45 | + return SymbolKind.Variable |
| 46 | + if jedi_type == "param": |
| 47 | + return SymbolKind.Variable |
| 48 | + if jedi_type == "path": |
| 49 | + return SymbolKind.File |
| 50 | + return SymbolKind.Variable |
0 commit comments