Skip to content

Commit 82a666e

Browse files
committed
Replace ActionContext by iextensionrunnerinfoprovider. Add file classifier: distinguish source files and tests. Services for getting venv dir, venv site packages.
1 parent dad46cf commit 82a666e

File tree

26 files changed

+308
-69
lines changed

26 files changed

+308
-69
lines changed

extensions/fine_python_mypy/fine_python_mypy/action.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
icommandrunner,
1616
ifilemanager,
1717
ilogger,
18+
iextensionrunnerinfoprovider,
19+
iprojectinfoprovider
1820
)
1921

2022

@@ -44,14 +46,16 @@ class MypyLintHandler(
4446

4547
def __init__(
4648
self,
47-
context: code_action.ActionContext,
49+
extension_runner_info_provider: iextensionrunnerinfoprovider.IExtensionRunnerInfoProvider,
50+
project_info_provider: iprojectinfoprovider.IProjectInfoProvider,
4851
cache: icache.ICache,
4952
logger: ilogger.ILogger,
5053
file_manager: ifilemanager.IFileManager,
5154
lifecycle: code_action.ActionHandlerLifecycle,
5255
command_runner: icommandrunner.ICommandRunner,
5356
) -> None:
54-
self.context = context
57+
self.extension_runner_info_provider = extension_runner_info_provider
58+
self.project_info_provider = project_info_provider
5559
self.cache = cache
5660
self.logger = logger
5761
self.file_manager = file_manager
@@ -189,7 +193,7 @@ async def run(
189193
file_paths = [file_path async for file_path in payload]
190194

191195
files_by_projects: dict[Path, list[Path]] = self.group_files_by_projects(
192-
file_paths, self.context.project_dir
196+
file_paths, self.project_info_provider.get_current_project_dir_path()
193197
)
194198

195199
for project_path, project_files in files_by_projects.items():
@@ -225,7 +229,7 @@ def exit(self) -> None:
225229
self.logger.error(str(error))
226230

227231
def _get_status_file_path(self, dmypy_cwd: Path) -> Path:
228-
file_dir_path = self.context.cache_dir
232+
file_dir_path = self.extension_runner_info_provider.get_cache_dir_path()
229233
# use hash to avoid name conflict if python packages have the same name
230234
file_dir_path_hash = hashlib.md5(str(dmypy_cwd).encode("utf-8")).hexdigest()
231235
file_path = (

extensions/fine_python_pyrefly/fine_python_pyrefly/lint_handler.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from finecode_extension_api import code_action
99
from finecode_extension_api.actions import lint as lint_action
10-
from finecode_extension_api.interfaces import icache, icommandrunner, ilogger, ifilemanager
10+
from finecode_extension_api.interfaces import icache, icommandrunner, ilogger, ifilemanager, iprojectfileclassifier, iextensionrunnerinfoprovider
1111

1212

1313
@dataclasses.dataclass
@@ -32,12 +32,16 @@ def __init__(
3232
logger: ilogger.ILogger,
3333
file_manager: ifilemanager.IFileManager,
3434
command_runner: icommandrunner.ICommandRunner,
35+
project_file_classifier: iprojectfileclassifier.IProjectFileClassifier,
36+
extension_runner_info_provider: iextensionrunnerinfoprovider.IExtensionRunnerInfoProvider
3537
) -> None:
3638
self.config = config
3739
self.cache = cache
3840
self.logger = logger
3941
self.file_manager = file_manager
4042
self.command_runner = command_runner
43+
self.project_file_classifier = project_file_classifier
44+
self.extension_runner_info_provider = extension_runner_info_provider
4145

4246
self.pyrefly_bin_path = Path(sys.executable).parent / "pyrefly"
4347

@@ -82,15 +86,31 @@ async def run_pyrefly_lint_on_single_file(
8286
) -> list[lint_action.LintMessage]:
8387
"""Run pyrefly type checking on a single file"""
8488
lint_messages: list[lint_action.LintMessage] = []
89+
90+
try:
91+
# project file classifier caches result, we can just get it each time again
92+
file_type = self.project_file_classifier.get_project_file_type(file_path=file_path)
93+
file_env = self.project_file_classifier.get_env_for_file_type(file_type=file_type)
94+
except NotImplementedError:
95+
self.logger.warning(f"Skip {file_path} because file type or env for it could be determined")
96+
return lint_messages
97+
98+
venv_dir_path = self.extension_runner_info_provider.get_venv_dir_path_of_env(env_name=file_env)
99+
site_package_pathes = self.extension_runner_info_provider.get_venv_site_packages(venv_dir_path=venv_dir_path)
85100

86101
cmd = [
87102
str(self.pyrefly_bin_path),
88103
"check",
89-
"--output-format",
90-
"json",
91-
str(file_path),
104+
"--output-format=json",
105+
"--disable-search-path-heuristics=true",
106+
"--skip-interpreter-query",
107+
"--python-version='3.11'" # TODO
92108
]
93109

110+
for path in site_package_pathes:
111+
cmd.append(f'--site-package-path={str(path)}')
112+
cmd.append(str(file_path))
113+
94114
cmd_str = " ".join(cmd)
95115
pyrefly_process = await self.command_runner.run(cmd_str)
96116

extensions/fine_python_ruff/fine_python_ruff/format_handler.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from finecode_extension_api import code_action
1313
from finecode_extension_api.actions import format as format_action
14-
from finecode_extension_api.interfaces import icache, icommandrunner, ilogger
14+
from finecode_extension_api.interfaces import icache, icommandrunner, ilogger, iextensionrunnerinfoprovider
1515

1616

1717
@dataclasses.dataclass
@@ -31,7 +31,7 @@ class RuffFormatHandler(
3131
def __init__(
3232
self,
3333
config: RuffFormatHandlerConfig,
34-
context: code_action.ActionContext,
34+
extension_runner_info_provider: iextensionrunnerinfoprovider.IExtensionRunnerInfoProvider,
3535
logger: ilogger.ILogger,
3636
cache: icache.ICache,
3737
command_runner: icommandrunner.ICommandRunner,
@@ -41,6 +41,7 @@ def __init__(
4141
self.logger = logger
4242
self.cache = cache
4343
self.command_runner = command_runner
44+
self.extension_runner_info_provider = extension_runner_info_provider
4445

4546
self.ruff_bin_path = Path(sys.executable).parent / "ruff"
4647

@@ -89,7 +90,7 @@ async def format_one(self, file_path: Path, file_content: str) -> tuple[str, boo
8990
str(self.ruff_bin_path),
9091
"format",
9192
"--cache-dir",
92-
str(self.context.cache_dir / ".ruff_cache"),
93+
str(self.extension_runner_info_provider.get_cache_dir_path() / ".ruff_cache"),
9394
"--line-length",
9495
str(self.config.line_length),
9596
f'--config="indent-width={str(self.config.indent_width)}"',

finecode_builtin_handlers/src/finecode_builtin_handlers/prepare_envs_install_deps.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import asyncio
22
import dataclasses
33
import itertools
4-
import shutil
54

65
from finecode_extension_api import code_action
76
from finecode_extension_api.actions import prepare_envs as prepare_envs_action
87
from finecode_extension_api.interfaces import (
98
iactionrunner,
109
ilogger,
11-
iprojectinfoprovider,
1210
)
1311
from finecode_builtin_handlers import dependency_config_utils
1412

finecode_builtin_handlers/src/finecode_builtin_handlers/prepare_runners_install_runner_and_presets.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import asyncio
22
import dataclasses
33
import itertools
4-
import shutil
54
import typing
65

76
from finecode_extension_api import code_action
87
from finecode_extension_api.actions import prepare_runners as prepare_runners_action
98
from finecode_extension_api.interfaces import (
109
iactionrunner,
1110
ilogger,
12-
iprojectinfoprovider,
1311
)
1412
from finecode_builtin_handlers import dependency_config_utils
1513

finecode_builtin_handlers/src/finecode_builtin_handlers/prepare_runners_read_configs.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import asyncio
22
import dataclasses
33
import pathlib
4-
import shutil
54
import typing
65

76
from finecode_extension_api import code_action

finecode_extension_api/src/finecode_extension_api/code_action.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import dataclasses
66
import enum
77
import typing
8-
from pathlib import Path
98
from typing import Generic, Protocol, TypeVar
109

1110
from finecode_extension_api import partialresultscheduler, textstyler
@@ -73,13 +72,6 @@ def __init__(self, run_id: int) -> None:
7372
self.partial_result_scheduler = partialresultscheduler.PartialResultScheduler()
7473

7574

76-
class ActionContext:
77-
def __init__(self, project_dir: Path, cache_dir: Path) -> None:
78-
self.project_dir = project_dir
79-
# runner-specific cache dir
80-
self.cache_dir = cache_dir
81-
82-
8375
@dataclasses.dataclass
8476
class ActionConfig:
8577
run_handlers_concurrently: bool = False
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pathlib
2+
from typing import Protocol
3+
4+
5+
class IExtensionRunnerInfoProvider(Protocol):
6+
def get_cache_dir_path(self) -> pathlib.Path: ...
7+
8+
def get_venv_dir_path_of_env(self, env_name: str) -> pathlib.Path:
9+
...
10+
11+
def get_venv_site_packages(self, venv_dir_path: pathlib.Path) -> list[pathlib.Path]:
12+
...
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import typing
2-
from asyncio import BaseProtocol
32

43
T = typing.TypeVar("T")
54
P = typing.ParamSpec("P")
65

76

8-
class IProcessExecutor(BaseProtocol):
7+
class IProcessExecutor(typing.Protocol):
98
async def submit(
109
self, func: typing.Callable[P, T], *args: P.args, **kwargs: P.kwargs
1110
): ...
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import enum
2+
import typing
3+
import pathlib
4+
5+
6+
class ProjectFileType(enum.Enum):
7+
SOURCE = enum.auto()
8+
TEST = enum.auto()
9+
UNKNOWN = enum.auto()
10+
11+
12+
class IProjectFileClassifier(typing.Protocol):
13+
def get_project_file_type(self, file_path: pathlib.Path) -> ProjectFileType:
14+
...
15+
16+
def get_env_for_file_type(self, file_type: ProjectFileType) -> str:
17+
...

0 commit comments

Comments
 (0)