Skip to content

Commit 0f283d4

Browse files
authored
Prefer parent uri when matching uri (#682)
1 parent 19fc07a commit 0f283d4

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

pylsp/_utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import docstring_to_markdown
1717
import jedi
1818

19+
from pylsp import IS_WIN
20+
1921
JEDI_VERSION = jedi.__version__
2022

2123
# Eol chars accepted by the LSP protocol
@@ -139,14 +141,33 @@ def match_uri_to_workspace(uri, workspaces):
139141
workspace_parts = pathlib.Path(workspace).parts
140142
if len(workspace_parts) > len(path):
141143
continue
144+
142145
match_len = 0
146+
is_parent = True
143147
for workspace_part, path_part in zip(workspace_parts, path):
148+
# filename match is case insensitive on windows
149+
# also, uris._normalize_win_path() lowercases the drive letter
150+
if IS_WIN:
151+
workspace_part = workspace_part.lower()
152+
path_part = path_part.lower()
153+
144154
if workspace_part == path_part:
145155
match_len += 1
156+
else:
157+
# give up, any subsequent match is no longer relevant
158+
is_parent = False
159+
break
160+
161+
# prefer a match that is actually a parent of uri
162+
# otherwise fall back to longest matching non-parent
163+
if is_parent and match_len > 0:
164+
match_len += 1000
165+
146166
if match_len > 0:
147167
if match_len > max_len:
148168
max_len = match_len
149169
chosen_workspace = workspace
170+
150171
return chosen_workspace
151172

152173

0 commit comments

Comments
 (0)