Skip to content

PR: Fix Inline comments including text with import cause functions/classes to disappear when under cells in the Outline explorer #639

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 4 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
8 changes: 7 additions & 1 deletion pylsp/plugins/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2021- Python Language Server Contributors.

import logging
import re
from pathlib import Path

from pylsp import hookimpl
Expand All @@ -19,6 +20,10 @@ def pylsp_document_symbols(config, document):
symbols = []
exclude = set({})
redefinitions = {}
pattern_import = (
re.compile
(r'^\s*(?!#)\s*(from\s+[.\w]+(\.[\w]+)*\s+import\s+[\w\s,()*]+|import\s+[\w\s,.*]+)')
)

while definitions != []:
d = definitions.pop(0)
Expand All @@ -27,7 +32,8 @@ def pylsp_document_symbols(config, document):
if not add_import_symbols:
# Skip if there's an import in the code the symbol is defined.
code = d.get_line_code()
if " import " in code or "import " in code:

if pattern_import.match(code):
continue

# Skip imported symbols comparing module names.
Expand Down
27 changes: 27 additions & 0 deletions test/plugins/test_symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ def main(x):

"""

DOC_IMPORTS = """from . import something
from ..module import something
from module import (a, b)

def main():
# import ignored
print("from module import x") # string with import
return something

"""


def helper_check_symbols_all_scope(symbols):
# All eight symbols (import sys, a, B, __init__, x, y, main, y)
Expand Down Expand Up @@ -73,6 +84,22 @@ def sym(name):
assert sym("main")["location"]["range"]["end"] == {"line": 12, "character": 0}


def test_symbols_complex_imports(config, workspace):
doc = Document(DOC_URI, workspace, DOC_IMPORTS)
config.update({"plugins": {"jedi_symbols": {"all_scopes": False}}})
symbols = pylsp_document_symbols(config, doc)

import_symbols = [s for s in symbols if s["kind"] == SymbolKind.Module]

assert len(import_symbols) == 4

names = [s["name"] for s in import_symbols]
assert "something" in names
assert "a" in names or "b" in names

assert any(s["name"] == "main" and s["kind"] == SymbolKind.Function for s in symbols)


def test_symbols_all_scopes(config, workspace) -> None:
doc = Document(DOC_URI, workspace, DOC)
symbols = pylsp_document_symbols(config, doc)
Expand Down
Loading