Skip to content

Commit 4ad1e99

Browse files
lsp: handle when the optimized function is not found
1 parent 7d3de7c commit 4ad1e99

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

codeflash/code_utils/git_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
import re
45
import shutil
56
import subprocess
67
import sys
@@ -221,6 +222,14 @@ def create_detached_worktree(module_root: Path) -> Optional[Path]:
221222
repository.git.add("-N", ".") # add the index for untracked files to be included in the diff
222223
uni_diff_text = repository.git.diff(None, "HEAD", ignore_blank_lines=True, ignore_space_at_eol=True)
223224

225+
# HACK: remove binary files from the diff, find a better way # noqa: FIX004
226+
uni_diff_text = re.sub(
227+
r"^diff --git a\/.*?\.(?:pyc|class|jar|exe|dll|so|dylib|o|obj|bin|pdf|jpg|jpeg|png|gif|zip|tar|gz) b\/.*?\.\w+.*?\n(?:.*?\n)*?(?=diff --git|\Z)",
228+
"",
229+
uni_diff_text,
230+
flags=re.MULTILINE,
231+
)
232+
224233
if not uni_diff_text.strip():
225234
logger.info("No uncommitted changes to copy to worktree.")
226235
return worktree_dir

codeflash/discovery/functions_to_optimize.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def get_functions_to_optimize(
169169
)
170170
functions: dict[str, list[FunctionToOptimize]]
171171
trace_file_path: Path | None = None
172+
is_lsp = is_LSP_enabled()
172173
with warnings.catch_warnings():
173174
warnings.simplefilter(action="ignore", category=SyntaxWarning)
174175
if optimize_all:
@@ -186,6 +187,8 @@ def get_functions_to_optimize(
186187
if only_get_this_function is not None:
187188
split_function = only_get_this_function.split(".")
188189
if len(split_function) > 2:
190+
if is_lsp:
191+
return functions, 0, None
189192
exit_with_message(
190193
"Function name should be in the format 'function_name' or 'class_name.function_name'"
191194
)
@@ -201,6 +204,8 @@ def get_functions_to_optimize(
201204
):
202205
found_function = fn
203206
if found_function is None:
207+
if is_lsp:
208+
return functions, 0, None
204209
exit_with_message(
205210
f"Function {only_function_name} not found in file {file}\nor the function does not have a 'return' statement or is a property"
206211
)

codeflash/lsp/beta.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,12 @@ def initialize_function_optimization(
106106
f"Args set - function: {server.optimizer.args.function}, file: {server.optimizer.args.file}", "Info"
107107
)
108108

109-
optimizable_funcs, _, _ = server.optimizer.get_optimizable_functions()
110-
if not optimizable_funcs:
109+
optimizable_funcs, count, _ = server.optimizer.get_optimizable_functions()
110+
111+
if count == 0:
111112
server.show_message_log(f"No optimizable functions found for {params.functionName}", "Warning")
112-
return {
113-
"functionName": params.functionName,
114-
"status": "error",
115-
"message": "function is no found or not optimizable",
116-
"args": None,
117-
}
113+
cleanup_the_optimizer(server)
114+
return {"functionName": params.functionName, "status": "error", "message": "not found", "args": None}
118115

119116
fto = optimizable_funcs.popitem()[1][0]
120117
server.optimizer.current_function_being_optimized = fto

codeflash/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# These version placeholders will be replaced by uv-dynamic-versioning during build.
2-
__version__ = "0.16.5"
2+
__version__ = "0.16.3.post94.dev0+7d3de7c5"

0 commit comments

Comments
 (0)