Skip to content

Commit 001c7fc

Browse files
committed
Check-in missing function
1 parent 4dd2096 commit 001c7fc

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

accelerant/chat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22
from rich.markup import escape as rescape
33
from typing import Any, List, Optional
4-
from llm_utils import number_group_of_lines
54
import openai
65
from openai import NotGiven
76
from openai.types.chat import (
@@ -30,6 +29,7 @@
3029
GetSurroundingCodeTool,
3130
LLMToolRunner,
3231
)
32+
from accelerant.util import custom_number_group_of_lines
3333
from perfparser import LineLoc
3434

3535

@@ -64,7 +64,7 @@ def _build_hotspot_prompt(
6464
# FIXME: avoid crashing
6565
assert parent_sym is not None
6666
sline = parent_sym["range"]["start"]["line"]
67-
prettyline = number_group_of_lines(
67+
prettyline = custom_number_group_of_lines(
6868
project.get_range(filename, parent_sym["range"]),
6969
max(sline + 1, 1),
7070
with_note=lambda n: " <--- HOTSPOT" if n == lineno else "",

accelerant/util.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Optional, TypedDict
1+
from typing import Callable, List, Optional, TypedDict
22

33

44
class SymbolLoc(TypedDict):
@@ -47,3 +47,43 @@ def truncate_for_llm(text: str, char_limit: int):
4747
if len(text) > char_limit:
4848
return text[:char_limit] + "[...too long...]"
4949
return text
50+
51+
52+
def custom_number_group_of_lines(
53+
group: List[str],
54+
first: int,
55+
strip: bool = True,
56+
with_note: Optional[Callable[[int], str]] = None,
57+
) -> str:
58+
"""
59+
Add line numbers for each line in the input list.
60+
61+
Args:
62+
group (List[str]): The lines to number.
63+
first (int): The number for the first line.
64+
strip (bool): Whether to strip leading and trailing blank lines.
65+
66+
Returns:
67+
A string concatenation of the numbered lines.
68+
"""
69+
if strip:
70+
while group and not group[0].strip():
71+
group = group[1:]
72+
first += 1
73+
while group and not group[-1].strip():
74+
group = group[:-1]
75+
76+
last = first + len(group) - 1
77+
max_line_number_length = len(str(last))
78+
result = "\n".join(
79+
[
80+
"{0:>{1}} {2}{3}".format(
81+
first + i,
82+
max_line_number_length,
83+
line,
84+
with_note(first + i) if with_note else "",
85+
)
86+
for i, line in enumerate(group)
87+
]
88+
)
89+
return result

0 commit comments

Comments
 (0)