Skip to content

Commit 4bca82e

Browse files
committed
feat: improve find_project_root
1 parent bbe5072 commit 4bca82e

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

src/lsp_client/client/lang.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,19 @@ class LanguageConfig:
2323
exclude_files: list[str] = Factory(list)
2424
"""Files that indicate a directory should not be considered a project root for this language."""
2525

26-
def find_project_root(self, file_path: Path) -> Path | None:
27-
"""Find the project root directory for the given file path.
28-
29-
Args:
30-
file_path (Path): The file path to check.
31-
Returns:
32-
Path | None: The project root directory if found, otherwise None.
33-
"""
34-
35-
if not file_path.is_file():
36-
raise ValueError(f"Expected a file path, got: {file_path}")
26+
def _find_project_root(self, dir_path: Path) -> Path | None:
27+
for project_path in (dir_path, *dir_path.parents):
28+
if any((project_path / excl).exists() for excl in self.exclude_files):
29+
return
30+
if any((project_path / proj).exists() for proj in self.project_files):
31+
return project_path
3732

38-
if not any(file_path.name.endswith(suffix) for suffix in self.suffixes):
39-
return
33+
return dir_path
4034

41-
for parent in file_path.parents:
42-
if any((parent / excl).exists() for excl in self.exclude_files):
35+
def find_project_root(self, path: Path) -> Path | None:
36+
if path.is_file():
37+
if not any(path.name.endswith(suffix) for suffix in self.suffixes):
4338
return
44-
if any((parent / proj).exists() for proj in self.project_files):
45-
return parent
39+
path = path.parent
4640

47-
return file_path.parent
41+
return self._find_project_root(path)

0 commit comments

Comments
 (0)