Skip to content

Commit 429f56e

Browse files
remove singleton class
1 parent b6e4804 commit 429f56e

File tree

4 files changed

+9
-25
lines changed

4 files changed

+9
-25
lines changed

codeflash/lsp/beta.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
)
3131
from codeflash.either import is_successful
3232
from codeflash.lsp.features.perform_optimization import get_cancelled_reponse, sync_perform_optimization
33-
from codeflash.lsp.server import CodeflashServerSingleton
33+
from codeflash.lsp.server import CodeflashLanguageServer, CodeflashLanguageServerProtocol
3434

3535
if TYPE_CHECKING:
3636
from argparse import Namespace
@@ -87,7 +87,7 @@ class WriteConfigParams:
8787
config: any
8888

8989

90-
server = CodeflashServerSingleton.get()
90+
server = CodeflashLanguageServer("codeflash-language-server", "v1.0", protocol_cls=CodeflashLanguageServerProtocol)
9191

9292

9393
@server.feature("getOptimizableFunctionsInCurrentDiff")
@@ -421,7 +421,7 @@ async def perform_function_optimization(params: FunctionOptimizationParams) -> d
421421

422422
try:
423423
ctx = contextvars.copy_context()
424-
return await loop.run_in_executor(None, ctx.run, sync_perform_optimization, params)
424+
return await loop.run_in_executor(None, ctx.run, sync_perform_optimization, server, params)
425425
except asyncio.CancelledError:
426426
server.cancel_event.set()
427427
return get_cancelled_reponse()

codeflash/lsp/features/perform_optimization.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
from codeflash.cli_cmds.console import code_print
88
from codeflash.code_utils.git_worktree_utils import create_diff_patch_from_worktree
99
from codeflash.either import is_successful
10-
from codeflash.lsp.server import CodeflashServerSingleton
1110

1211
if TYPE_CHECKING:
1312
import threading
1413

14+
from codeflash.lsp.server import CodeflashLanguageServer
15+
1516

1617
def get_cancelled_reponse() -> dict[str, str]:
1718
return {"status": "canceled", "message": "Task was canceled"}
@@ -22,8 +23,7 @@ def abort_if_cancelled(cancel_event: threading.Event) -> None:
2223
raise RuntimeError("cancelled")
2324

2425

25-
def sync_perform_optimization(params) -> dict[str, str]: # noqa
26-
server = CodeflashServerSingleton.get()
26+
def sync_perform_optimization(server: CodeflashLanguageServer, params) -> dict[str, str]: # noqa
2727
cancel_event = server.cancel_event
2828
server.show_message_log(f"Starting optimization for function: {params.functionName}", "Info")
2929
should_run_experiment, code_context, original_helper_code = server.current_optimization_init_result

codeflash/lsp/lsp_message.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from typing import Any, Optional
77

88
from codeflash.lsp.helpers import replace_quotes_with_backticks, simplify_worktree_paths
9-
from codeflash.lsp.server import CodeflashServerSingleton
109

1110
json_primitive_types = (str, float, int, bool)
1211
max_code_lines_before_collapse = 45
@@ -35,8 +34,9 @@ def type(self) -> str:
3534
raise NotImplementedError
3635

3736
def serialize(self) -> str:
38-
lsp_server_instance = CodeflashServerSingleton.get()
39-
current_task_id = lsp_server_instance.execution_context_vars.get().get("task_id", None)
37+
from codeflash.lsp.beta import server
38+
39+
current_task_id = server.execution_context_vars.get().get("task_id", None)
4040
data = self._loop_through(asdict(self))
4141
ordered = {"type": self.type(), "task_id": current_task_id, **data}
4242
return message_delimiter + json.dumps(ordered) + message_delimiter

codeflash/lsp/server.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,6 @@ class CodeflashLanguageServerProtocol(LanguageServerProtocol):
1818
_server: CodeflashLanguageServer
1919

2020

21-
class CodeflashServerSingleton:
22-
_instance: CodeflashLanguageServer | None = None
23-
24-
@classmethod
25-
def get(cls) -> CodeflashLanguageServer:
26-
if cls._instance is None:
27-
cls._instance = CodeflashLanguageServer(
28-
"codeflash-language-server", "v1.0", protocol_cls=CodeflashLanguageServerProtocol
29-
)
30-
return cls._instance
31-
32-
def __init__(self) -> None:
33-
# This is a singleton class, so we don't want to initialize.
34-
...
35-
36-
3721
class CodeflashLanguageServer(LanguageServer):
3822
def __init__(self, name: str, version: str, protocol_cls: type[LanguageServerProtocol]) -> None:
3923
super().__init__(name, version, protocol_cls=protocol_cls)

0 commit comments

Comments
 (0)