From edce2fc183992ea3aa7d34ad6449548305eabb51 Mon Sep 17 00:00:00 2001 From: Mark Nauwelaerts Date: Sat, 25 Oct 2025 21:35:23 +0200 Subject: [PATCH 1/2] utils: prefer parent uri when matching uri --- pylsp/_utils.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pylsp/_utils.py b/pylsp/_utils.py index c9eb6fb1..b4893f61 100644 --- a/pylsp/_utils.py +++ b/pylsp/_utils.py @@ -16,6 +16,8 @@ import docstring_to_markdown import jedi +from pylsp import IS_WIN + JEDI_VERSION = jedi.__version__ # Eol chars accepted by the LSP protocol @@ -134,9 +136,23 @@ def match_uri_to_workspace(uri, workspaces): if len(workspace_parts) > len(path): continue match_len = 0 + is_parent = True for workspace_part, path_part in zip(workspace_parts, path): + # filename match is case insensitive on windows + # also, uris._normalize_win_path() lowercases the drive letter + if IS_WIN: + workspace_part = workspace_part.lower() + path_part = path_part.lower() if workspace_part == path_part: match_len += 1 + else: + # give up, any subsequent match is no longer relevant + is_parent = False + break + # prefer a match that is actually a parent of uri + # otherwise fall back to longest matching non-parent + if is_parent and match_len > 0: + match_len += 1000 if match_len > 0: if match_len > max_len: max_len = match_len From 6398294ae0f941034f07918149ecf034febf26f4 Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Sat, 18 Jul 2026 18:50:10 -0500 Subject: [PATCH 2/2] Improve readability --- pylsp/_utils.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pylsp/_utils.py b/pylsp/_utils.py index e92138e4..0ce0e8cb 100644 --- a/pylsp/_utils.py +++ b/pylsp/_utils.py @@ -141,6 +141,7 @@ def match_uri_to_workspace(uri, workspaces): workspace_parts = pathlib.Path(workspace).parts if len(workspace_parts) > len(path): continue + match_len = 0 is_parent = True for workspace_part, path_part in zip(workspace_parts, path): @@ -149,20 +150,24 @@ def match_uri_to_workspace(uri, workspaces): if IS_WIN: workspace_part = workspace_part.lower() path_part = path_part.lower() + if workspace_part == path_part: match_len += 1 else: # give up, any subsequent match is no longer relevant is_parent = False break + # prefer a match that is actually a parent of uri # otherwise fall back to longest matching non-parent if is_parent and match_len > 0: match_len += 1000 + if match_len > 0: if match_len > max_len: max_len = match_len chosen_workspace = workspace + return chosen_workspace