1212 icommandrunner ,
1313 ilogger ,
1414 ifileeditor ,
15+ iprojectinfoprovider ,
1516 isrcartifactfileclassifier ,
1617 iextensionrunnerinfoprovider ,
1718)
19+ from fine_python_pyrefly .pyrefly_lsp_service import PyreflyLspService
1820
1921
2022@dataclasses .dataclass
2123class PyreflyLintFilesHandlerConfig (code_action .ActionHandlerConfig ):
2224 python_version : str | None = None
25+ use_cli : bool = False
2326
2427
2528class PyreflyLintFilesHandler (
@@ -47,6 +50,8 @@ def __init__(
4750 command_runner : icommandrunner .ICommandRunner ,
4851 src_artifact_file_classifier : isrcartifactfileclassifier .ISrcArtifactFileClassifier ,
4952 extension_runner_info_provider : iextensionrunnerinfoprovider .IExtensionRunnerInfoProvider ,
53+ project_info_provider : iprojectinfoprovider .IProjectInfoProvider ,
54+ lsp_service : PyreflyLspService ,
5055 ) -> None :
5156 self .config = config
5257 self .cache = cache
@@ -55,9 +60,27 @@ def __init__(
5560 self .command_runner = command_runner
5661 self .src_artifact_file_classifier = src_artifact_file_classifier
5762 self .extension_runner_info_provider = extension_runner_info_provider
63+ self .project_info_provider : iprojectinfoprovider .IProjectInfoProvider = project_info_provider
64+ self .lsp_service : PyreflyLspService = lsp_service
5865
5966 self .pyrefly_bin_path = Path (sys .executable ).parent / "pyrefly"
6067
68+ if not self .config .use_cli :
69+ # Pyrefly uses pull-based config: the LSP server sends
70+ # workspace/configuration requests with section="python",
71+ # expecting responses like [{"pyrefly": {"displayTypeErrors": ...}}].
72+ # The same format is used for initializationOptions.
73+ venv_dir = self .extension_runner_info_provider .get_venv_dir_path_of_env ("runtime" )
74+ interpreter_path = self .extension_runner_info_provider .get_venv_python_interpreter (venv_dir )
75+ site_packages = self .extension_runner_info_provider .get_venv_site_packages (venv_dir )
76+ self .lsp_service .update_settings ({
77+ "pythonPath" : str (interpreter_path ),
78+ "pyrefly" : {
79+ "displayTypeErrors" : "force-on" ,
80+ "extraPaths" : [str (p ) for p in site_packages ],
81+ },
82+ })
83+
6184 async def run_on_single_file (
6285 self , file_path : Path
6386 ) -> lint_files_action .LintFilesRunResult :
@@ -76,7 +99,14 @@ async def run_on_single_file(
7699 ) as session :
77100 file_version = await session .read_file_version (file_path )
78101
79- lint_messages = await self .run_pyrefly_lint_on_single_file (file_path )
102+ if self .config .use_cli :
103+ lint_messages = await self .run_pyrefly_lint_on_single_file (file_path )
104+ else :
105+ root_uri = self .project_info_provider .get_current_project_dir_path ().as_uri ()
106+ await self .lsp_service .ensure_started (root_uri )
107+
108+ lint_messages = await self .lsp_service .check_file (file_path )
109+
80110 messages [str (file_path )] = lint_messages
81111 await self .cache .save_file_cache (
82112 file_path , file_version , self .CACHE_KEY , lint_messages
0 commit comments