Skip to content

Commit 3260e4a

Browse files
more enhancements
1 parent b9b4664 commit 3260e4a

File tree

6 files changed

+53
-6
lines changed

6 files changed

+53
-6
lines changed

codeflash/api/aiservice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def optimize_python_code( # noqa: D417
144144

145145
if response.status_code == 200:
146146
optimizations_json = response.json()["optimizations"]
147-
logger.info(f"Generated {len(optimizations_json)} candidate optimizations.")
147+
logger.info(f"Generated `{len(optimizations_json)}` candidate optimizations.")
148148
console.rule()
149149
end_time = time.perf_counter()
150150
logger.debug(f"Generating optimizations took {end_time - start_time:.2f} seconds.")

codeflash/cli_cmds/console.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from codeflash.cli_cmds.logging_config import BARE_LOGGING_FORMAT
2222
from codeflash.lsp.helpers import is_LSP_enabled
2323
from codeflash.lsp.lsp_logger import enhanced_log
24-
from codeflash.lsp.lsp_message import LspCodeMessage
24+
from codeflash.lsp.lsp_message import LspCodeMessage, LspTextMessage
2525

2626
if TYPE_CHECKING:
2727
from collections.abc import Generator
@@ -100,6 +100,11 @@ def progress_bar(
100100
If revert_to_print is True, falls back to printing a single logger.info message
101101
instead of showing a progress bar.
102102
"""
103+
if is_LSP_enabled():
104+
lsp_log(LspTextMessage(text=message, takes_time=True))
105+
yield
106+
return
107+
103108
if revert_to_print:
104109
logger.info(message)
105110

codeflash/discovery/functions_to_optimize.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,15 @@ def get_functions_to_optimize(
173173
with warnings.catch_warnings():
174174
warnings.simplefilter(action="ignore", category=SyntaxWarning)
175175
if optimize_all:
176-
logger.info("Finding all functions in the module '%s'…", optimize_all)
176+
logger.info("!lsp:Finding all functions in the module '%s'…", optimize_all)
177177
console.rule()
178178
functions = get_all_files_and_functions(Path(optimize_all))
179179
elif replay_test:
180180
functions, trace_file_path = get_all_replay_test_functions(
181181
replay_test=replay_test, test_cfg=test_cfg, project_root_path=project_root
182182
)
183183
elif file is not None:
184-
logger.info("Finding all functions in the file '%s'…", file)
184+
logger.info("!lsp:Finding all functions in the file '%s'…", file)
185185
console.rule()
186186
functions = find_all_functions_in_file(file)
187187
if only_get_this_function is not None:
@@ -219,7 +219,7 @@ def get_functions_to_optimize(
219219
functions, test_cfg.tests_root, ignore_paths, project_root, module_root, previous_checkpoint_functions
220220
)
221221

222-
logger.info(f"Found {functions_count} function{'s' if functions_count > 1 else ''} to optimize")
222+
logger.info(f"!lsp:Found {functions_count} function{'s' if functions_count > 1 else ''} to optimize")
223223
if optimize_all:
224224
three_min_in_ns = int(1.8e11)
225225
console.rule()

codeflash/lsp/helpers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
import os
2+
import re
23
from functools import lru_cache
34

45

56
@lru_cache(maxsize=1)
67
def is_LSP_enabled() -> bool:
78
return os.getenv("CODEFLASH_LSP", default="false").lower() == "true"
9+
10+
11+
worktree_path_regex = re.compile(r'\/[^"]*worktrees\/[^"]\S*')
12+
13+
14+
def simplify_worktree_paths(msg: str, highlight: bool = True) -> str: # noqa: FBT001, FBT002
15+
path_in_msg = worktree_path_regex.search(msg)
16+
if path_in_msg:
17+
last_part_of_path = path_in_msg.group(0).split("/")[-1]
18+
if highlight:
19+
last_part_of_path = f"`{last_part_of_path}`"
20+
return msg.replace(path_in_msg.group(0), last_part_of_path)
21+
return msg
22+
23+
24+
def replace_quotes_with_backticks(text: str) -> str:
25+
# double-quoted strings
26+
text = re.sub(r'"(.*?)"', r"`\1`", text)
27+
# single-quoted strings
28+
return re.sub(r"'(.*?)'", r"`\1`", text)

codeflash/lsp/lsp_message.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
from pathlib import Path
66
from typing import Any, Optional
77

8+
from codeflash.lsp.helpers import replace_quotes_with_backticks, simplify_worktree_paths
9+
810
json_primitive_types = (str, float, int, bool)
11+
max_code_lines_before_collapse = 45
912

1013

1114
@dataclass
@@ -28,6 +31,15 @@ def type(self) -> str:
2831
raise NotImplementedError
2932

3033
def serialize(self) -> str:
34+
if isinstance(self, LspTextMessage):
35+
self.text = simplify_worktree_paths(self.text)
36+
self.text = replace_quotes_with_backticks(self.text)
37+
if isinstance(self, LspCodeMessage):
38+
self.file_name = simplify_worktree_paths(str(self.file_name), highlight=False)
39+
if isinstance(self, LspMarkdownMessage):
40+
self.markdown = simplify_worktree_paths(self.markdown)
41+
self.markdown = replace_quotes_with_backticks(self.markdown)
42+
3143
data = self._loop_through(asdict(self))
3244
# 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
3345
ordered = {"type": self.type(), **data}
@@ -47,10 +59,19 @@ class LspCodeMessage(LspMessage):
4759
code: str
4860
file_name: Optional[Path] = None
4961
function_name: Optional[str] = None
62+
collapsed: bool = False
63+
lines_count: Optional[int] = None
5064

5165
def type(self) -> str:
5266
return "code"
5367

68+
def serialize(self) -> str:
69+
code_lines_length = len(self.code.split("\n"))
70+
self.lines_count = code_lines_length
71+
if code_lines_length > max_code_lines_before_collapse:
72+
self.collapsed = True
73+
return super().serialize()
74+
5475

5576
@dataclass
5677
class LspMarkdownMessage(LspMessage):

codeflash/optimization/optimizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def prepare_module_for_optimization(
192192
from codeflash.code_utils.code_replacer import normalize_code, normalize_node
193193
from codeflash.code_utils.static_analysis import analyze_imported_modules
194194

195-
logger.info(f"Examining file {original_module_path!s}…")
195+
logger.info(f"Examining file {original_module_path!s} …")
196196
console.rule()
197197

198198
original_module_code: str = original_module_path.read_text(encoding="utf8")

0 commit comments

Comments
 (0)