Strip the longest matching import path, not the first match
This commit is contained in:
parent
bdf31076a7
commit
2f7295ba9e
1 changed files with 11 additions and 6 deletions
|
@ -26,16 +26,21 @@ def _store_path(path, ctx, imports):
|
|||
|
||||
else:
|
||||
# Main workspace, for example 'mypackage/main.py'
|
||||
# stored_path = ctx.workspace_name + "/" + path
|
||||
stored_path = path
|
||||
stored_path = ctx.workspace_name + "/" + path
|
||||
|
||||
matching_prefix = None
|
||||
matching_prefixes = []
|
||||
for i in imports:
|
||||
if stored_path.startswith(i):
|
||||
stored_path = stored_path[len(i):]
|
||||
matching_prefix = i
|
||||
break
|
||||
matching_prefixes.append(i)
|
||||
|
||||
# Find the longest prefix match
|
||||
matching_prefixes = sorted(matching_prefixes, key=len, reverse=True)
|
||||
|
||||
if matching_prefixes:
|
||||
# Strip the longest matching prefix
|
||||
stored_path = stored_path[len(matching_prefixes[0]):]
|
||||
|
||||
# Strip any trailing /
|
||||
stored_path = stored_path.lstrip("/")
|
||||
|
||||
return stored_path
|
||||
|
|
Loading…
Reference in a new issue