Skip to content

Commit 8fda905

Browse files
authored
Fix symbols for non-existing (unsaved) files (#302)
1 parent 2e98dfa commit 8fda905

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

pylsp/plugins/symbols.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright 2021- Python Language Server Contributors.
33

44
import logging
5-
import os
5+
from pathlib import Path
66

77
from pylsp import hookimpl
88
from pylsp.lsp import SymbolKind
@@ -91,14 +91,7 @@ def pylsp_document_symbols(config, document):
9191
else:
9292
continue
9393

94-
try:
95-
docismodule = os.path.samefile(document.path, d.module_path)
96-
except (TypeError, FileNotFoundError):
97-
# Python 2 on Windows has no .samefile, but then these are
98-
# strings for sure
99-
docismodule = document.path == d.module_path
100-
101-
if _include_def(d) and docismodule:
94+
if _include_def(d) and Path(document.path) == d.module_path:
10295
tuple_range = _tuple_range(d)
10396
if tuple_range in exclude:
10497
continue

test/plugins/test_symbols.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ def test_symbols_all_scopes(config, workspace):
8080
helper_check_symbols_all_scope(symbols)
8181

8282

83+
def test_symbols_non_existing_file(config, workspace, tmpdir):
84+
path = tmpdir.join("foo.py")
85+
# Check pre-condition: file must not exist
86+
assert not path.check(exists=1)
87+
88+
doc = Document(uris.from_fs_path(str(path)), workspace, DOC)
89+
symbols = pylsp_document_symbols(config, doc)
90+
helper_check_symbols_all_scope(symbols)
91+
92+
8393
@pytest.mark.skipif(PY2 or not LINUX or not CI, reason="tested on linux and python 3 only")
8494
def test_symbols_all_scopes_with_jedi_environment(workspace):
8595
doc = Document(DOC_URI, workspace, DOC)

0 commit comments

Comments
 (0)