Skip to content

Commit b9b4664

Browse files
more enhancements
1 parent 0f2a8d1 commit b9b4664

File tree

6 files changed

+48
-39
lines changed

6 files changed

+48
-39
lines changed

codeflash/cli_cmds/console.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
from contextlib import contextmanager
55
from itertools import cycle
6-
from typing import TYPE_CHECKING, Any, Optional
6+
from typing import TYPE_CHECKING, Optional
77

88
from rich.console import Console
99
from rich.logging import RichHandler
@@ -19,7 +19,8 @@
1919

2020
from codeflash.cli_cmds.console_constants import SPINNER_TYPES
2121
from codeflash.cli_cmds.logging_config import BARE_LOGGING_FORMAT
22-
from codeflash.lsp.helpers import enhanced_log, is_LSP_enabled
22+
from codeflash.lsp.helpers import is_LSP_enabled
23+
from codeflash.lsp.lsp_logger import enhanced_log
2324
from codeflash.lsp.lsp_message import LspCodeMessage
2425

2526
if TYPE_CHECKING:
@@ -51,11 +52,11 @@
5152
setattr(logger, level, lambda msg, *args, _real_fn=real_fn, **kwargs: enhanced_log(msg, _real_fn, *args, **kwargs))
5253

5354

54-
def lsp_log(message: LspMessage, *args: Any, **kwargs: Any) -> None: # noqa: ANN401
55+
def lsp_log(message: LspMessage) -> None:
5556
if not is_LSP_enabled():
5657
return
5758
json_msg = message.serialize()
58-
logger.info(json_msg, *args, **kwargs)
59+
logger.info(json_msg)
5960

6061

6162
def paneled_text(
@@ -81,9 +82,6 @@ def code_print(code_str: str, file_name: Optional[str] = None, function_name: Op
8182
from rich.syntax import Syntax
8283

8384
formatted_code = Syntax(code_str, "python", line_numbers=True, theme="github-dark")
84-
# if is_LSP_enabled():
85-
# logger.debug(formatted_code.__str__())
86-
# return
8785

8886
console.rule()
8987
console.print(formatted_code)

codeflash/lsp/beta.py

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

1212
from codeflash.api.cfapi import get_codeflash_api_key, get_user_id
1313
from codeflash.cli_cmds.cli import process_pyproject_config
14+
from codeflash.cli_cmds.console import code_print
1415
from codeflash.code_utils.git_worktree_utils import (
1516
create_diff_patch_from_worktree,
1617
get_patches_metadata,
@@ -321,6 +322,12 @@ def perform_function_optimization( # noqa: PLR0911
321322

322323
should_run_experiment, code_context, original_helper_code = initialization_result.unwrap()
323324

325+
code_print(
326+
code_context.read_writable_code.flat,
327+
file_name=current_function.file_path,
328+
function_name=current_function.function_name,
329+
)
330+
324331
test_setup_result = function_optimizer.generate_and_instrument_tests(
325332
code_context, should_run_experiment=should_run_experiment
326333
)

codeflash/lsp/helpers.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,7 @@
11
import os
22
from functools import lru_cache
3-
from typing import Any, Callable
4-
5-
skip_lsp_log_prefix = "!lsp:"
63

74

85
@lru_cache(maxsize=1)
96
def is_LSP_enabled() -> bool:
107
return os.getenv("CODEFLASH_LSP", default="false").lower() == "true"
11-
12-
13-
def enhanced_log(msg: str, actual_log_fn: Callable[[str, Any, Any], None], *args: Any, **kwargs: Any) -> None: # noqa: ANN401
14-
lsp_enabled = is_LSP_enabled()
15-
str_msg = isinstance(msg, str)
16-
skip_lsp_log = str_msg and msg.strip().startswith(skip_lsp_log_prefix)
17-
18-
if skip_lsp_log:
19-
msg = msg[len(skip_lsp_log_prefix) :]
20-
21-
# normal cli mode
22-
if not lsp_enabled:
23-
actual_log_fn(msg, *args, **kwargs)
24-
return
25-
26-
#### LSP mode ####
27-
if skip_lsp_log or not str_msg:
28-
return
29-
30-
actual_log_fn(f"::::{msg}", *args, **kwargs)

codeflash/lsp/lsp_logger.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import Any, Callable
2+
3+
from codeflash.lsp.helpers import is_LSP_enabled
4+
from codeflash.lsp.lsp_message import LspTextMessage
5+
6+
skip_lsp_log_prefix = "!lsp:"
7+
8+
9+
def enhanced_log(msg: str, actual_log_fn: Callable[[str, Any, Any], None], *args: Any, **kwargs: Any) -> None: # noqa: ANN401
10+
lsp_enabled = is_LSP_enabled()
11+
str_msg = isinstance(msg, str)
12+
# if the message starts with !lsp:, it won't be sent to the client
13+
skip_lsp_log = str_msg and msg.strip().startswith(skip_lsp_log_prefix)
14+
15+
if skip_lsp_log:
16+
# get the message without the prefix
17+
msg = msg[len(skip_lsp_log_prefix) :]
18+
19+
# normal cli mode
20+
if not lsp_enabled:
21+
actual_log_fn(msg, *args, **kwargs)
22+
return
23+
24+
#### LSP mode ####
25+
if skip_lsp_log or not str_msg:
26+
return
27+
28+
if not msg.startswith("{"):
29+
# it is not a json message, use a text message
30+
msg = LspTextMessage(text=msg).serialize()
31+
32+
actual_log_fn(msg, *args, **kwargs)

codeflash/lsp/lsp_message.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
@dataclass
1212
class LspMessage:
13-
takes_time: bool = field(
14-
default=False, kw_only=True
15-
) # to show a loading indicator if the operation is taking time like generating candidates or tests
13+
# to show a loading indicator if the operation is taking time like generating candidates or tests
14+
takes_time: bool = field(default=False, kw_only=True)
1615

1716
def _loop_through(self, obj: Any) -> Any: # noqa: ANN401
1817
if isinstance(obj, list):
@@ -29,7 +28,7 @@ def type(self) -> str:
2928
raise NotImplementedError
3029

3130
def serialize(self) -> str:
32-
data = asdict(self)
31+
data = self._loop_through(asdict(self))
3332
# Important: keep type as the first key, for making it easy and fast for the client to know if this is a lsp message before parsing it
3433
ordered = {"type": self.type(), **data}
3534
return json.dumps(ordered)
@@ -67,8 +66,3 @@ class LspStatsMessage(LspMessage):
6766

6867
def type(self) -> str:
6968
return "stats"
70-
71-
72-
if __name__ == "__main__":
73-
msg = LspTextMessage(text="Hello World")
74-
print(msg.serialize())

codeflash/optimization/function_optimizer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ def generate_and_instrument_tests(
365365
)
366366
)
367367

368+
# note: this isn't called by the lsp, only called by cli
368369
def optimize_function(self) -> Result[BestOptimization, str]:
369370
initialization_result = self.can_be_optimized()
370371
if not is_successful(initialization_result):

0 commit comments

Comments
 (0)