Skip to content
Merged
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
26 changes: 24 additions & 2 deletions bundled/tool/lsp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,33 @@ def is_stdlib_file(file_path: str) -> bool:
return any(normalized_path.startswith(path) for path in _stdlib_paths)


def is_match(patterns: List[str], file_path: str) -> bool:
def _get_relative_path(file_path: str, workspace_root: str) -> str:
"""Returns the file path relative to the workspace root.

Falls back to the original path if the workspace root is empty or
the paths are on different drives (Windows).
"""
if not workspace_root:
return pathlib.Path(file_path).as_posix()
try:
return pathlib.Path(file_path).relative_to(workspace_root).as_posix()
except ValueError:
return pathlib.Path(file_path).as_posix()


def is_match(patterns: List[str], file_path: str, workspace_root: str = None) -> bool:
"""Returns true if the file matches one of the fnmatch patterns."""
if not patterns:
return False
return any(fnmatch.fnmatch(file_path, pattern) for pattern in patterns)
relative_path = (
_get_relative_path(file_path, workspace_root) if workspace_root else file_path
)
file_name = pathlib.Path(file_path).name
return any(
fnmatch.fnmatch(relative_path, pattern)
or (not pattern.startswith("/") and fnmatch.fnmatch(file_name, pattern))
for pattern in patterns
)


class RunResult:
Expand Down
Loading