Skip to content
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

Support workspace symbols #511

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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
50 changes: 44 additions & 6 deletions pylsp/plugins/symbols.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,64 @@
# 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
# 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 +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
Expand All @@ -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],
Expand Down
7 changes: 7 additions & 0 deletions pylsp/python_lsp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2017-2020 Palantir Technologies, Inc.
# Copyright 2021- Python Language Server Contributors.


from functools import partial
import logging
import os
Expand All @@ -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
Expand Down Expand Up @@ -278,6 +280,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 +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))

Expand Down Expand Up @@ -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]
Expand Down