Skip to content

Fix symbols for non-existing (unsaved) files #302

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

Merged
merged 2 commits into from
Dec 11, 2022
Merged
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
11 changes: 2 additions & 9 deletions pylsp/plugins/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright 2021- Python Language Server Contributors.

import logging
import os
from pathlib import Path

from pylsp import hookimpl
from pylsp.lsp import SymbolKind
Expand Down Expand Up @@ -91,14 +91,7 @@ def pylsp_document_symbols(config, document):
else:
continue

try:
docismodule = os.path.samefile(document.path, d.module_path)
except (TypeError, FileNotFoundError):
# Python 2 on Windows has no .samefile, but then these are
# strings for sure
docismodule = document.path == d.module_path

if _include_def(d) and docismodule:
if _include_def(d) and Path(document.path) == d.module_path:
tuple_range = _tuple_range(d)
if tuple_range in exclude:
continue
Expand Down
10 changes: 10 additions & 0 deletions test/plugins/test_symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ def test_symbols_all_scopes(config, workspace):
helper_check_symbols_all_scope(symbols)


def test_symbols_non_existing_file(config, workspace, tmpdir):
path = tmpdir.join("foo.py")
# Check pre-condition: file must not exist
assert not path.check(exists=1)

doc = Document(uris.from_fs_path(str(path)), workspace, DOC)
symbols = pylsp_document_symbols(config, doc)
helper_check_symbols_all_scope(symbols)


@pytest.mark.skipif(PY2 or not LINUX or not CI, reason="tested on linux and python 3 only")
def test_symbols_all_scopes_with_jedi_environment(workspace):
doc = Document(DOC_URI, workspace, DOC)
Expand Down